@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 07:15 UTC

main
ts 78 lines 3.85 KB
Raw
1 import type { WriteBucketClient } from "@alchemy.run/cloudflare/R2/WriteBucket";
2 import * as Effect from "effect/Effect";
3 import type * as HttpServerRequest from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5
6 /**
7 * Shared write-side routes exercised by both the binding and HTTP fixtures so
8 * every (HTTP-supported) method of {@link WriteBucketClient} is driven over
9 * `fetch`:
10 *
11 * - `PUT /put?key=` — `put(key, body)` (returns the object key to prove the
12 * call resolved an `R2Object`).
13 * - `DELETE /del?key=` — `delete(key)` (single).
14 * - `DELETE /del-many?keys=a,b,c` — `delete(keys)` (batch). Keys travel as a
15 * comma-separated query param rather than a request body because DELETE
16 * bodies are unreliable across `fetch`/the CF edge.
17 *
18 * Returns `undefined` when the path is not a write route so the caller can
19 * fall through (used by the read-write fixtures).
20 */
21 export const writeRoutes = (
22 r2: WriteBucketClient,
23 request: HttpServerRequest.HttpServerRequest,
24 url: URL,
25 ) =>
26 Effect.gen(function* () {
27 if (request.method === "PUT" && url.pathname === "/put") {
28 const key = url.searchParams.get("key") ?? "";
29 const body = yield* request.text;
30 const object = yield* r2.put(key, body).pipe(Effect.orDie);
31 return yield* HttpServerResponse.json({
32 ok: true,
33 key: object?.key ?? key,
34 });
35 }
36 if (request.method === "DELETE" && url.pathname === "/del") {
37 const key = url.searchParams.get("key") ?? "";
38 yield* r2.delete(key).pipe(Effect.orDie);
39 return yield* HttpServerResponse.json({ ok: true });
40 }
41 if (request.method === "DELETE" && url.pathname === "/del-many") {
42 const keys = (url.searchParams.get("keys") ?? "").split(",").filter((k) => k.length > 0);
43 yield* r2.delete(keys).pipe(Effect.orDie);
44 return yield* HttpServerResponse.json({ ok: true });
45 }
46 // Multipart upload (native binding only — the HTTP client `Effect.die`s
47 // these). R2 requires every part except the last to be ≥ 5 MiB, so the
48 // first part is exactly 5 MiB and the tail is a tiny final part. The
49 // worker generates the bytes internally so the test payload stays small.
50 if (request.method === "POST" && url.pathname === "/mpu") {
51 const key = url.searchParams.get("key") ?? "";
52 const part = new Uint8Array(5 * 1024 * 1024).fill(65);
53 const upload = yield* r2.createMultipartUpload(key).pipe(Effect.orDie);
54 const p1 = yield* upload.uploadPart(1, part).pipe(Effect.orDie);
55 const p2 = yield* upload.uploadPart(2, "tail").pipe(Effect.orDie);
56 const object = yield* upload.complete([p1, p2]).pipe(Effect.orDie);
57 return yield* HttpServerResponse.json({ ok: true, size: object.size });
58 }
59 if (request.method === "POST" && url.pathname === "/mpu-abort") {
60 const key = url.searchParams.get("key") ?? "";
61 const part = new Uint8Array(5 * 1024 * 1024).fill(66);
62 const upload = yield* r2.createMultipartUpload(key).pipe(Effect.orDie);
63 yield* upload.uploadPart(1, part).pipe(Effect.orDie);
64 yield* upload.abort().pipe(Effect.orDie);
65 return yield* HttpServerResponse.json({ ok: true });
66 }
67 if (request.method === "POST" && url.pathname === "/mpu-resume") {
68 const key = url.searchParams.get("key") ?? "";
69 const part = new Uint8Array(5 * 1024 * 1024).fill(67);
70 const created = yield* r2.createMultipartUpload(key).pipe(Effect.orDie);
71 const upload = yield* r2.resumeMultipartUpload(key, created.uploadId).pipe(Effect.orDie);
72 const p1 = yield* upload.uploadPart(1, part).pipe(Effect.orDie);
73 const p2 = yield* upload.uploadPart(2, "tail").pipe(Effect.orDie);
74 const object = yield* upload.complete([p1, p2]).pipe(Effect.orDie);
75 return yield* HttpServerResponse.json({ ok: true, size: object.size });
76 }
77 return undefined;
78 });