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 { PresignPutObject, type PresignPutObjectRequest } from "./PresignPutObject.ts"; |
| 4 | import { makePresignBinding, presignR2Url } from "./PresignToken.ts"; |
| 5 | |
| 6 | /** |
| 7 | * Implementation of {@link PresignPutObject} 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 Write` (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.PresignPutObject |
| 19 | * @product R2 |
| 20 | */ |
| 21 | export const PresignPutObjectToken = Layer.effect( |
| 22 | PresignPutObject, |
| 23 | Effect.suspend(() => |
| 24 | makePresignBinding<PresignPutObjectRequest>({ |
| 25 | name: "Cloudflare.R2.PresignPutObject", |
| 26 | access: "write", |
| 27 | presign: (credentials, request) => |
| 28 | presignR2Url(credentials, { |
| 29 | method: "PUT", |
| 30 | key: request.key, |
| 31 | expiresIn: request.expiresIn, |
| 32 | headers: |
| 33 | request.contentType !== undefined ? { "content-type": request.contentType } : undefined, |
| 34 | }), |
| 35 | }), |
| 36 | ), |
| 37 | ); |