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
| 1 | import type { ReadWriteBucketClient } from "@alchemy.run/cloudflare/R2/ReadWriteBucket"; |
| 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 | import * as Stream from "effect/Stream"; |
| 6 | |
| 7 | /** Real native R2 bodies cross the public read/write binding in these routes. */ |
| 8 | export const streamRoutes = ( |
| 9 | r2: ReadWriteBucketClient, |
| 10 | request: HttpServerRequest.HttpServerRequest, |
| 11 | url: URL, |
| 12 | ) => |
| 13 | Effect.gen(function* () { |
| 14 | const key = url.searchParams.get("key") ?? ""; |
| 15 | if (request.method === "PUT" && url.pathname === "/copy-source") { |
| 16 | const body = yield* request.text; |
| 17 | const stored = yield* r2.put(key, body, { |
| 18 | httpMetadata: { contentType: "application/octet-stream", cacheControl: "max-age=60" }, |
| 19 | customMetadata: { purpose: "stream-copy", label: key }, |
| 20 | }); |
| 21 | return yield* HttpServerResponse.json({ size: stored.size }); |
| 22 | } |
| 23 | if (request.method === "GET" && url.pathname === "/copy-readback") { |
| 24 | const object = yield* r2.get(key); |
| 25 | if (object === null) return yield* HttpServerResponse.json({ exists: false }); |
| 26 | const bytes = yield* object.bytes(); |
| 27 | return yield* HttpServerResponse.json({ |
| 28 | exists: true, |
| 29 | size: object.size, |
| 30 | bytes: Array.from(bytes), |
| 31 | httpMetadata: object.httpMetadata, |
| 32 | customMetadata: object.customMetadata, |
| 33 | }); |
| 34 | } |
| 35 | if (request.method !== "POST" || url.pathname !== "/copy-stream") return undefined; |
| 36 | |
| 37 | const source = url.searchParams.get("source") ?? ""; |
| 38 | const offset = url.searchParams.get("offset"); |
| 39 | const rangeLength = url.searchParams.get("rangeLength"); |
| 40 | const object = yield* r2.get( |
| 41 | source, |
| 42 | offset === null || rangeLength === null |
| 43 | ? undefined |
| 44 | : { range: { offset: Number(offset), length: Number(rangeLength) } }, |
| 45 | ); |
| 46 | if (object === null) return HttpServerResponse.text("Source missing", { status: 404 }); |
| 47 | const transformed = url.searchParams.get("transform") === "invert"; |
| 48 | const body = transformed |
| 49 | ? object.body.pipe(Stream.map((chunk) => chunk.map((byte) => byte ^ 0xff))) |
| 50 | : object.body; |
| 51 | const contentLength = url.searchParams.get("contentLength"); |
| 52 | const sha256 = url.searchParams.get("sha256"); |
| 53 | return yield* r2 |
| 54 | .put(key, body, { |
| 55 | // This public conditional overload accepts a native body without a |
| 56 | // contentLength hint. Existing destinations must not be overwritten. |
| 57 | onlyIf: { etagDoesNotMatch: "*" }, |
| 58 | ...(contentLength === null ? {} : { contentLength: Number(contentLength) }), |
| 59 | ...(sha256 === null ? {} : { sha256 }), |
| 60 | httpMetadata: object.httpMetadata, |
| 61 | customMetadata: object.customMetadata, |
| 62 | }) |
| 63 | .pipe( |
| 64 | Effect.flatMap((stored) => |
| 65 | HttpServerResponse.json({ stored: stored !== null, size: stored?.size ?? null }), |
| 66 | ), |
| 67 | ); |
| 68 | }).pipe( |
| 69 | Effect.catchTag("R2Error", (error) => |
| 70 | HttpServerResponse.json({ stored: false, error: error.message }, { status: 400 }), |
| 71 | ), |
| 72 | ); |