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

main
ts 112 lines 3.73 KB
Raw
1 import type * as runtime from "@cloudflare/workers-types";
2 import { getRawStream } from "alchemy/Util/Stream";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Stream from "effect/Stream";
6 import { makeBucketBinding, makeHelpers } from "./BucketBinding.ts";
7 import type {
8 Conditional,
9 MultipartOptions,
10 MultipartUpload,
11 PutOptions,
12 UploadPartOptions,
13 UploadedPart,
14 } from "./BucketTypes.ts";
15 import { WriteBucket, type WriteBucketClient } from "./WriteBucket.ts";
16
17 /**
18 * Implementation of the {@link WriteBucket} binding that uses a Worker binding.
19 */
20 export const WriteBucketBinding = Layer.effect(
21 WriteBucket,
22 Effect.suspend(() => makeBucketBinding({ makeClient: makeWrite })),
23 );
24
25 /** Build the write half of the binding client. */
26 export const makeWrite = ({
27 raw,
28 use,
29 tryPromise,
30 wrapR2Object,
31 wrapR2ObjectOrBody,
32 }: ReturnType<typeof makeHelpers>): WriteBucketClient => {
33 const wrapR2MultipartUpload = (upload: runtime.R2MultipartUpload): MultipartUpload => ({
34 ...upload,
35 raw: upload,
36 uploadId: upload.uploadId,
37 abort: () => tryPromise(() => upload.abort()),
38 complete: (uploadedParts: UploadedPart[]) =>
39 tryPromise(() => upload.complete(uploadedParts)).pipe(Effect.map(wrapR2Object)),
40 uploadPart: (
41 partNumber: number,
42 value:
43 | ReadableStream
44 | ArrayBuffer
45 | ArrayBufferView
46 | string
47 | Blob
48 | Stream.Stream<Uint8Array>,
49 options?: UploadPartOptions,
50 ) =>
51 tryPromise(() =>
52 upload.uploadPart(
53 partNumber,
54 Stream.isStream(value) ? value.pipe(Stream.toReadableStream()) : (value as any),
55 options,
56 ),
57 ),
58 });
59
60 return {
61 // @ts-expect-error
62 put: (
63 key: string,
64 value:
65 | ReadableStream
66 | ArrayBuffer
67 | ArrayBufferView
68 | string
69 | null
70 | Blob
71 | Stream.Stream<Uint8Array>,
72 options?: PutOptions & {
73 onlyIf: Conditional | Headers;
74 contentLength?: number;
75 },
76 ) =>
77 use((raw) => {
78 if (Stream.isStream(value)) {
79 // `contentLength` is ours; everything else (checksums, metadata,
80 // conditions) is R2's and must reach it on every path.
81 const { contentLength, ...r2Options } = options ?? {};
82 const rawStream = getRawStream(value);
83 if (rawStream) {
84 // Keep explicit length validation without routing native chunks
85 // through Effect. Without a hint, R2 uses the native stream's length.
86 const readable =
87 contentLength === undefined
88 ? rawStream
89 : rawStream.pipeThrough(new FixedLengthStream(contentLength));
90 return raw.put(key, readable as any, r2Options);
91 } else if (!contentLength) {
92 throw new Error("Content length is required");
93 }
94 // R2 needs a known length, so the stream is piped through a fixed
95 // length stream.
96 const readable = Stream.toReadableStream(value).pipeThrough(
97 new FixedLengthStream(contentLength),
98 );
99 return raw.put(key, readable as any, r2Options);
100 }
101 return raw.put(key, value as any, options);
102 }).pipe(Effect.map(wrapR2ObjectOrBody)) as any,
103 delete: (keys: string | string[]) => use((raw) => raw.delete(keys)),
104 createMultipartUpload: (key: string, options?: MultipartOptions) =>
105 use((raw) => raw.createMultipartUpload(key, options)).pipe(Effect.map(wrapR2MultipartUpload)),
106 resumeMultipartUpload: (key: string, uploadId: string) =>
107 raw.pipe(
108 Effect.map((raw) => raw.resumeMultipartUpload(key, uploadId)),
109 Effect.map(wrapR2MultipartUpload),
110 ),
111 };
112 };