@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, 06:00 UTC

main
ts 85 lines 3.07 KB
Raw
1 import * as SigV4 from "@distilled.cloud/aws4signer";
2 import * as Effect from "effect/Effect";
3 import * as Redacted from "effect/Redacted";
4 import { Worker, WorkerEnvironment } from "../Workers/Worker.ts";
5 import type { Bucket } from "./Bucket.ts";
6 import type { PresignError } from "./BucketTypes.ts";
7 import {
8 makeS3Credentials,
9 type S3CredentialsAccess,
10 type S3CredentialsValue,
11 } from "./S3Credentials.ts";
12
13 /**
14 * Shared scaffolding for the R2 presign bindings (`PresignGetObjectToken`,
15 * `PresignPutObjectToken`): resolve the bucket's `S3Credentials` and
16 * sign each request locally with SigV4. `makeS3Credentials` handles the
17 * per-mode wiring (a scoped API token when deployed, the Worker's local S3
18 * endpoint under `alchemy dev`).
19 *
20 * NOT exported from `index.ts`.
21 */
22 export const makePresignBinding = <Request>(options: {
23 name: string;
24 access: S3CredentialsAccess;
25 presign: (
26 credentials: S3CredentialsValue,
27 request: Request,
28 ) => Effect.Effect<string, PresignError>;
29 }) =>
30 Effect.gen(function* () {
31 // Resolved once at layer construction and provided to each bind, so the
32 // binding callable carries no Worker requirements.
33 const host = yield* Worker;
34 const env = yield* WorkerEnvironment;
35
36 return Effect.fn(function* (bucket: Bucket) {
37 const credentials = yield* makeS3Credentials(host, env, bucket, options.access);
38 return Effect.fn(`${options.name}(${bucket.LogicalId})`)(function* (request: Request) {
39 return yield* options.presign(yield* credentials, request);
40 });
41 });
42 });
43
44 export interface PresignR2UrlOptions {
45 method: "GET" | "PUT";
46 key: string;
47 /** @default 900 */
48 expiresIn?: number;
49 /** Headers the consumer must send verbatim (signed into the URL). */
50 headers?: Record<string, string>;
51 /** Extra query parameters signed into the URL (e.g. `response-content-type`). */
52 query?: Record<string, string>;
53 }
54
55 /**
56 * Presign a path-style R2 object URL (`{endpoint}/{bucket}/{key}`) with the
57 * given {@link S3CredentialsValue}. Pure SigV4 — no request is made.
58 */
59 export const presignR2Url = (
60 credentials: S3CredentialsValue,
61 options: PresignR2UrlOptions,
62 ): Effect.Effect<string, PresignError> =>
63 Effect.gen(function* () {
64 const key = options.key.split("/").map(encodeURIComponent).join("/");
65 const url = new URL(
66 `${credentials.endpoint.replace(/\/+$/, "")}/${encodeURIComponent(credentials.bucketName)}/${key}`,
67 );
68 for (const [name, value] of Object.entries(options.query ?? {})) {
69 url.searchParams.set(name, value);
70 }
71 url.searchParams.set("X-Amz-Expires", String(options.expiresIn ?? 900));
72 const signed = yield* SigV4.sign({
73 method: options.method,
74 url: url.toString(),
75 headers: options.headers,
76 accessKeyId: credentials.accessKeyId,
77 secretAccessKey: Redacted.make(credentials.secretAccessKey),
78 service: "s3",
79 region: credentials.region,
80 signQuery: true,
81 // Pin explicitly passed headers (e.g. content-type) into the signature
82 allHeaders: options.headers !== undefined,
83 });
84 return signed.url;
85 });