The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import { PresignGetObject, type PresignGetObjectRequest } from "./PresignGetObject.ts"; |
| 4 | import { makePresignBinding, presignR2Url } from "./PresignToken.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Implementation of {@link PresignGetObject} that signs URLs with S3 |
| 8 | * credentials derived from an API token. Signing is local SigV4 — no request |
| 9 | * is made to R2. |
| 10 | * |
| 11 | * Deployed, it mints a scoped account API token with |
| 12 | * `Workers R2 Storage Read` (access key id = token id, secret = SHA-256 of |
| 13 | * the token value) and signs URLs for `{accountId}.r2.cloudflarestorage.com`. |
| 14 | * Under `alchemy dev`, a locally-emulated bucket is signed with fixed local |
| 15 | * credentials for the Worker's local S3 endpoint, and no token is created. |
| 16 | * |
| 17 | * @layer |
| 18 | * @provides Cloudflare.R2.PresignGetObject |
| 19 | * @product R2 |
| 20 | */ |
| 21 | export const PresignGetObjectToken = Layer.effect( |
| 22 | PresignGetObject, |
| 23 | Effect.suspend(() => |
| 24 | makePresignBinding<PresignGetObjectRequest>({ |
| 25 | name: "Cloudflare.R2.PresignGetObject", |
| 26 | access: "read", |
| 27 | presign: (credentials, request) => |
| 28 | presignR2Url(credentials, { |
| 29 | method: "GET", |
| 30 | key: request.key, |
| 31 | expiresIn: request.expiresIn, |
| 32 | query: { |
| 33 | ...(request.contentType !== undefined |
| 34 | ? { "response-content-type": request.contentType } |
| 35 | : {}), |
| 36 | ...(request.contentDisposition !== undefined |
| 37 | ? { "response-content-disposition": request.contentDisposition } |
| 38 | : {}), |
| 39 | }, |
| 40 | }), |
| 41 | }), |
| 42 | ), |
| 43 | ); |