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, 06:00 UTC
| 1 | import * as GCP from "@alchemy.run/gcp"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 4 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 5 | import { documentFor, Files, isValidName, objectFor, Uploads } from "./resources.ts"; |
| 6 | |
| 7 | /** |
| 8 | * The front door of an upload pipeline. |
| 9 | * |
| 10 | * Uploads go straight to Cloud Storage; this service never computes |
| 11 | * anything about them. The {@link ../Indexer.ts Indexer} picks each one |
| 12 | * up from the bucket's notification and writes the metadata that |
| 13 | * `GET /files` serves. |
| 14 | * |
| 15 | * - `PUT /files/:name` — store the request body at `uploads/:name`. |
| 16 | * - `GET /files/:name` — return the stored bytes. |
| 17 | * - `GET /files` — list the indexed metadata from Firestore. |
| 18 | * - `DELETE /files/:name` — delete the object and its metadata. |
| 19 | * |
| 20 | * `invokerIamDisabled: true` makes the service public. Put real |
| 21 | * authentication in front of it before storing anything that matters. |
| 22 | */ |
| 23 | export default class Api extends GCP.Function<Api>()( |
| 24 | "Api", |
| 25 | { |
| 26 | main: import.meta.url, |
| 27 | invokerIamDisabled: true, |
| 28 | }, |
| 29 | Effect.gen(function* () { |
| 30 | const bucket = yield* Uploads; |
| 31 | const database = yield* Files; |
| 32 | |
| 33 | // Least privilege per verb: objectViewer to read, objectUser to write |
| 34 | // (overwriting an object needs delete, which objectCreator lacks). |
| 35 | const reader = yield* GCP.Storage.ReadBucket(bucket); |
| 36 | const writer = yield* GCP.Storage.WriteBucket(bucket); |
| 37 | // Listing metadata and removing it on delete. |
| 38 | const files = yield* GCP.Firestore.ReadWriteDatabase(database); |
| 39 | |
| 40 | return { |
| 41 | fetch: Effect.gen(function* () { |
| 42 | const request = yield* HttpServerRequest; |
| 43 | const url = new URL(request.originalUrl); |
| 44 | const segments = url.pathname.split("/").filter(Boolean); |
| 45 | |
| 46 | if (request.method === "GET" && segments.length === 0) { |
| 47 | return HttpServerResponse.text("ok"); |
| 48 | } |
| 49 | |
| 50 | if (request.method === "GET" && segments.length === 1 && segments[0] === "files") { |
| 51 | const { documents } = yield* files.list("files", { pageSize: 100 }).pipe(Effect.orDie); |
| 52 | return yield* HttpServerResponse.json({ |
| 53 | files: documents.map((document) => document.fields), |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | if (segments.length !== 2 || segments[0] !== "files") { |
| 58 | return yield* HttpServerResponse.json({ error: "not found" }, { status: 404 }); |
| 59 | } |
| 60 | |
| 61 | const name = decodeURIComponent(segments[1]!); |
| 62 | if (!isValidName(name)) { |
| 63 | return yield* HttpServerResponse.json( |
| 64 | { error: "names are 1-128 characters of [A-Za-z0-9._-]" }, |
| 65 | { status: 400 }, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | if (request.method === "PUT") { |
| 70 | const body = new Uint8Array(yield* request.arrayBuffer); |
| 71 | const object = yield* writer |
| 72 | .put(objectFor(name), body, { |
| 73 | contentType: request.headers["content-type"] ?? "application/octet-stream", |
| 74 | }) |
| 75 | .pipe(Effect.orDie); |
| 76 | // 202: the bytes are stored, the metadata follows asynchronously. |
| 77 | return yield* HttpServerResponse.json( |
| 78 | { |
| 79 | name, |
| 80 | object: object.name, |
| 81 | generation: object.generation, |
| 82 | size: Number(object.size ?? body.byteLength), |
| 83 | }, |
| 84 | { status: 202 }, |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | if (request.method === "GET") { |
| 89 | const object = yield* reader.get(objectFor(name)).pipe(Effect.orDie); |
| 90 | if (object === undefined) { |
| 91 | return yield* HttpServerResponse.json({ error: "no such file" }, { status: 404 }); |
| 92 | } |
| 93 | return HttpServerResponse.uint8Array(object.body, { |
| 94 | contentType: object.contentType ?? "application/octet-stream", |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | if (request.method === "DELETE") { |
| 99 | yield* writer.delete(objectFor(name)).pipe(Effect.orDie); |
| 100 | yield* files.delete(documentFor(name)).pipe(Effect.orDie); |
| 101 | return HttpServerResponse.empty({ status: 204 }); |
| 102 | } |
| 103 | |
| 104 | return yield* HttpServerResponse.json({ error: "method not allowed" }, { status: 405 }); |
| 105 | }), |
| 106 | }; |
| 107 | }).pipe( |
| 108 | Effect.provide([ |
| 109 | GCP.Storage.ReadBucketHttp, |
| 110 | GCP.Storage.WriteBucketHttp, |
| 111 | GCP.Firestore.ReadWriteDatabaseHttp, |
| 112 | ]), |
| 113 | ), |
| 114 | ) {} |