@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 59 lines 2.54 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import type { Bucket } from "@alchemy.run/cloudflare/R2/Bucket";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6
7 /**
8 * Shared Worker implementation for the presign fixtures: mints presigned
9 * URLs for `bucket` and exposes the native binding so the test can verify
10 * that uploads through a presigned URL land in the same bucket.
11 *
12 * - `GET /presign-put?key=&contentType=` → `{ url }`
13 * - `GET /presign-get?key=&contentType=` → `{ url }`
14 * - `GET /read?key=` → `{ value, contentType }` via the native binding
15 * - `PUT /write?key=` → writes the body via the native binding
16 */
17 export const presignWorker = (bucket: Effect.Effect<Bucket, never, any>) =>
18 Effect.gen(function* () {
19 const b = yield* bucket;
20 const presignPut = yield* Cloudflare.R2.PresignPutObject(b);
21 const presignGet = yield* Cloudflare.R2.PresignGetObject(b);
22 const r2 = yield* Cloudflare.R2.ReadWriteBucket(b);
23 return {
24 fetch: Effect.gen(function* () {
25 const request = yield* HttpServerRequest;
26 const url = new URL(request.url, "http://x");
27 const key = url.searchParams.get("key") ?? "";
28 const contentType = url.searchParams.get("contentType") ?? undefined;
29 switch (url.pathname) {
30 case "/presign-put":
31 return yield* HttpServerResponse.json({
32 url: yield* presignPut({ key, contentType }).pipe(Effect.orDie),
33 });
34 case "/presign-get":
35 return yield* HttpServerResponse.json({
36 url: yield* presignGet({ key, contentType }).pipe(Effect.orDie),
37 });
38 case "/read": {
39 const object = yield* r2.get(key).pipe(Effect.orDie);
40 return yield* HttpServerResponse.json({
41 value: object === null ? null : yield* object.text().pipe(Effect.orDie),
42 contentType: object?.httpMetadata?.contentType ?? null,
43 });
44 }
45 case "/write": {
46 const body = yield* request.text.pipe(Effect.orDie);
47 yield* r2.put(key, body).pipe(Effect.orDie);
48 return HttpServerResponse.text("ok");
49 }
50 default:
51 return HttpServerResponse.text("ok");
52 }
53 }),
54 };
55 }).pipe(
56 Effect.provide(Cloudflare.R2.PresignPutObjectToken),
57 Effect.provide(Cloudflare.R2.PresignGetObjectToken),
58 Effect.provide(Cloudflare.R2.ReadWriteBucketBinding),
59 );