The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as kv from "@distilled.cloud/cloudflare/kv"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import { |
| 5 | makeHttpKVNamespaceBinding, |
| 6 | makeKVAuth, |
| 7 | makeKVHttpScope, |
| 8 | toKVNamespaceError, |
| 9 | type KVAuth, |
| 10 | } from "./NamespaceHttp.ts"; |
| 11 | import { NamespaceError } from "./NamespaceTypes.ts"; |
| 12 | import { WriteNamespace, type WriteNamespaceClient } from "./WriteNamespace.ts"; |
| 13 | |
| 14 | /** |
| 15 | * HTTP-backed implementation of the {@link WriteNamespace} binding. |
| 16 | * |
| 17 | * It creates a scoped {@link AccountApiToken} with the `Workers KV Storage |
| 18 | * Write` permission and writes values via the Cloudflare KV HTTP API. |
| 19 | */ |
| 20 | export const WriteNamespaceHttp = Layer.effect( |
| 21 | WriteNamespace, |
| 22 | Effect.suspend(() => |
| 23 | makeHttpKVNamespaceBinding({ |
| 24 | permissionGroups: ["Workers KV Storage Write"], |
| 25 | makeClient: (token, namespaceId) => makeWriteKVHttpClient(makeKVAuth(token), namespaceId), |
| 26 | }), |
| 27 | ), |
| 28 | ); |
| 29 | |
| 30 | export const makeWriteKVHttpClient = ( |
| 31 | auth: KVAuth, |
| 32 | namespaceId: Effect.Effect<string>, |
| 33 | ): WriteNamespaceClient => { |
| 34 | const { authorize } = auth; |
| 35 | const scope = makeKVHttpScope(auth, namespaceId); |
| 36 | |
| 37 | return { |
| 38 | put: (( |
| 39 | key: string, |
| 40 | value: string | ArrayBuffer | ArrayBufferView | ReadableStream, |
| 41 | options?: KVNamespacePutOptions, |
| 42 | ) => |
| 43 | scope.pipe( |
| 44 | Effect.flatMap(({ accountId, namespaceId }) => |
| 45 | toKVBody(value).pipe( |
| 46 | Effect.flatMap((body) => |
| 47 | authorize( |
| 48 | kv.putNamespaceValue({ |
| 49 | accountId, |
| 50 | namespaceId, |
| 51 | keyName: key, |
| 52 | value: body, |
| 53 | expiration: options?.expiration, |
| 54 | expirationTtl: options?.expirationTtl, |
| 55 | metadata: options?.metadata, |
| 56 | }), |
| 57 | ), |
| 58 | ), |
| 59 | ), |
| 60 | ), |
| 61 | Effect.mapError(toKVNamespaceError), |
| 62 | Effect.asVoid, |
| 63 | )) as any, |
| 64 | delete: ((key: string) => |
| 65 | scope.pipe( |
| 66 | Effect.flatMap(({ accountId, namespaceId }) => |
| 67 | authorize(kv.deleteNamespaceValue({ accountId, namespaceId, keyName: key })), |
| 68 | ), |
| 69 | Effect.mapError(toKVNamespaceError), |
| 70 | Effect.asVoid, |
| 71 | )) as any, |
| 72 | }; |
| 73 | }; |
| 74 | |
| 75 | /** Collect a put value into a body accepted by the KV HTTP API. */ |
| 76 | const toKVBody = ( |
| 77 | value: string | ArrayBuffer | ArrayBufferView | ReadableStream, |
| 78 | ): Effect.Effect<string | Blob, NamespaceError> => |
| 79 | Effect.gen(function* () { |
| 80 | if (typeof value === "string") return value; |
| 81 | if (value instanceof Blob) return value; |
| 82 | if (value instanceof ArrayBuffer) return new Blob([value]); |
| 83 | if (value instanceof ReadableStream) { |
| 84 | const buffer = yield* Effect.tryPromise({ |
| 85 | try: () => new Response(value as any).arrayBuffer(), |
| 86 | catch: toKVNamespaceError, |
| 87 | }); |
| 88 | return new Blob([buffer]); |
| 89 | } |
| 90 | const view = value as ArrayBufferView; |
| 91 | const bytes = new Uint8Array(view.byteLength); |
| 92 | bytes.set(new Uint8Array(view.buffer as ArrayBuffer, view.byteOffset, view.byteLength)); |
| 93 | return new Blob([bytes]); |
| 94 | }); |