The best way to control Claude Code, Codex and more from any computer or phone

main
ts 147 lines 4.88 KB
Raw
1 import * as r2 from "@distilled.cloud/cloudflare/r2";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as Stream from "effect/Stream";
5 import { authorizeWith } from "../HttpClientUtils.ts";
6 import {
7 baseObject,
8 makeHttpBucketBinding,
9 makeR2HttpScope,
10 readHttpMetadata,
11 toBody,
12 toR2Error,
13 type R2Auth,
14 } from "./BucketHttp.ts";
15 import { R2Error, type PutOptions } from "./BucketTypes.ts";
16 import { WriteBucket, type WriteBucketClient } from "./WriteBucket.ts";
17
18 /**
19 * HTTP-backed implementation of the {@link WriteBucket} binding.
20 *
21 * It creates a scoped {@link AccountApiToken} with the `Workers R2 Storage Read` and `Workers R2 Storage Write` permissions.
22 */
23 export const WriteBucketHttp = Layer.effect(
24 WriteBucket,
25 Effect.suspend(() =>
26 makeHttpBucketBinding({
27 permissionGroups: ["Workers R2 Storage Write"],
28 makeClient: (token, bucketName, jurisdiction) =>
29 makeWriteR2HttpClient(
30 { authorize: authorizeWith(token), accountId: token.accountId },
31 bucketName,
32 jurisdiction,
33 ),
34 }),
35 ),
36 );
37
38 /** Build the write half of the HTTP-backed {@link ReadWrite} client. */
39 export const makeWriteR2HttpClient = (
40 auth: R2Auth,
41 bucketName: Effect.Effect<string>,
42 jurisdiction: Effect.Effect<string>,
43 ): WriteBucketClient => {
44 const authorize = auth.authorize;
45 const scope = makeR2HttpScope(auth.accountId, bucketName, jurisdiction);
46
47 return {
48 put: ((
49 key: string,
50 value:
51 | ReadableStream
52 | ArrayBuffer
53 | ArrayBufferView
54 | string
55 | null
56 | Blob
57 | Stream.Stream<Uint8Array, unknown>,
58 options?: PutOptions,
59 ) =>
60 scope.pipe(
61 Effect.flatMap(({ accountId, bucketName, cfR2Jurisdiction }) =>
62 toBody(value).pipe(
63 Effect.flatMap(({ body, contentLength }) => {
64 const meta = readHttpMetadata(options);
65 return authorize(
66 r2.putObject({
67 accountId,
68 bucketName,
69 objectName: key,
70 cfR2Jurisdiction,
71 body,
72 contentType: meta?.contentType,
73 contentEncoding: meta?.contentEncoding,
74 contentDisposition: meta?.contentDisposition,
75 contentLanguage: meta?.contentLanguage,
76 cacheControl: meta?.cacheControl,
77 contentLength:
78 options?.contentLength != null
79 ? String(options.contentLength)
80 : contentLength != null
81 ? String(contentLength)
82 : undefined,
83 cfR2StorageClass: (options as { storageClass?: string } | undefined)
84 ?.storageClass,
85 }),
86 ).pipe(
87 Effect.map(() =>
88 baseObject(key, meta ?? {}, {
89 size: contentLength,
90 customMetadata: (
91 options as { customMetadata?: Record<string, string> } | undefined
92 )?.customMetadata,
93 storageClass: (options as { storageClass?: string } | undefined)?.storageClass,
94 uploaded: new Date(),
95 }),
96 ),
97 );
98 }),
99 ),
100 ),
101 Effect.mapError(toR2Error),
102 )) as any,
103 delete: (keys: string | string[]) =>
104 scope.pipe(
105 Effect.flatMap(({ accountId, bucketName, cfR2Jurisdiction }) =>
106 Array.isArray(keys)
107 ? authorize(
108 r2.deleteObjects({
109 accountId,
110 bucketName,
111 cfR2Jurisdiction,
112 body: keys,
113 }),
114 ).pipe(Effect.asVoid, Effect.mapError(toR2Error))
115 : authorize(
116 r2.deleteObject({
117 accountId,
118 bucketName,
119 objectName: keys,
120 cfR2Jurisdiction,
121 }),
122 ).pipe(
123 Effect.asVoid,
124 // The native binding's `delete` is idempotent — deleting a
125 // key that isn't there resolves. Keep the HTTP client at
126 // parity instead of surfacing R2's `NoSuchKey`.
127 Effect.catchTag("NoSuchKey", () => Effect.void),
128 Effect.mapError(toR2Error),
129 ),
130 ),
131 ),
132 createMultipartUpload: () =>
133 Effect.die(
134 new R2Error({
135 message: "R2BucketBindingHttp does not support multipart uploads over the HTTP API.",
136 cause: new Error("unsupported"),
137 }),
138 ),
139 resumeMultipartUpload: () =>
140 Effect.die(
141 new R2Error({
142 message: "R2BucketBindingHttp does not support multipart uploads over the HTTP API.",
143 cause: new Error("unsupported"),
144 }),
145 ),
146 };
147 };