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, 04:45 UTC
| 1 | import * as kv from "@distilled.cloud/cloudflare/kv"; |
| 2 | import type * as Cause from "effect/Cause"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as Stream from "effect/Stream"; |
| 6 | import { |
| 7 | makeHttpKVNamespaceBinding, |
| 8 | makeKVAuth, |
| 9 | makeKVHttpScope, |
| 10 | toKVNamespaceError, |
| 11 | type KVAuth, |
| 12 | } from "./NamespaceHttp.ts"; |
| 13 | import { ReadNamespace, type ReadNamespaceClient } from "./ReadNamespace.ts"; |
| 14 | |
| 15 | /** |
| 16 | * HTTP-backed implementation of the {@link ReadNamespace} binding. |
| 17 | * |
| 18 | * It creates a scoped {@link AccountApiToken} with the `Workers KV Storage |
| 19 | * Read` permission and reads values via the Cloudflare KV HTTP API. |
| 20 | */ |
| 21 | export const ReadNamespaceHttp = Layer.effect( |
| 22 | ReadNamespace, |
| 23 | Effect.suspend(() => |
| 24 | makeHttpKVNamespaceBinding({ |
| 25 | permissionGroups: ["Workers KV Storage Read"], |
| 26 | makeClient: (token, namespaceId) => makeReadKVHttpClient(makeKVAuth(token), namespaceId), |
| 27 | }), |
| 28 | ), |
| 29 | ); |
| 30 | |
| 31 | export const makeReadKVHttpClient = ( |
| 32 | auth: KVAuth, |
| 33 | namespaceId: Effect.Effect<string>, |
| 34 | ): ReadNamespaceClient => { |
| 35 | const { authorize } = auth; |
| 36 | const scope = makeKVHttpScope(auth, namespaceId); |
| 37 | |
| 38 | const getOne = (key: string, type: string) => |
| 39 | scope.pipe( |
| 40 | Effect.flatMap(({ accountId, namespaceId }) => |
| 41 | authorize(kv.getNamespaceValue({ accountId, namespaceId, keyName: key })).pipe( |
| 42 | Effect.flatMap((res) => materializeBody(res.body, type)), |
| 43 | Effect.catchTag("KeyNotFound", () => Effect.succeed(null)), |
| 44 | ), |
| 45 | ), |
| 46 | Effect.mapError(toKVNamespaceError), |
| 47 | ); |
| 48 | |
| 49 | const getMany = (keys: string[], type: string) => |
| 50 | scope.pipe( |
| 51 | Effect.flatMap(({ accountId, namespaceId }) => |
| 52 | authorize( |
| 53 | kv.bulkGetNamespaceKeys({ |
| 54 | accountId, |
| 55 | namespaceId, |
| 56 | keys, |
| 57 | type: type === "json" ? "json" : "text", |
| 58 | }), |
| 59 | ), |
| 60 | ), |
| 61 | Effect.mapError(toKVNamespaceError), |
| 62 | Effect.map((res) => { |
| 63 | const values = (res.values ?? {}) as Record<string, unknown>; |
| 64 | const map = new Map<string, unknown>(); |
| 65 | for (const key of keys) { |
| 66 | map.set(key, key in values ? decodeValue(values[key], type) : null); |
| 67 | } |
| 68 | return map; |
| 69 | }), |
| 70 | ); |
| 71 | |
| 72 | const getWithMetadataOne = (key: string, type: string) => |
| 73 | scope.pipe( |
| 74 | Effect.flatMap(({ accountId, namespaceId }) => |
| 75 | Effect.all({ |
| 76 | value: authorize(kv.getNamespaceValue({ accountId, namespaceId, keyName: key })).pipe( |
| 77 | Effect.flatMap((res) => materializeBody(res.body, type)), |
| 78 | Effect.catchTag("KeyNotFound", () => Effect.succeed(null)), |
| 79 | ), |
| 80 | metadata: authorize( |
| 81 | kv.getNamespaceMetadata({ accountId, namespaceId, keyName: key }), |
| 82 | ).pipe(Effect.catchTag(["KeyNotFound", "NamespaceNotFound"], () => Effect.succeed(null))), |
| 83 | }), |
| 84 | ), |
| 85 | Effect.mapError(toKVNamespaceError), |
| 86 | Effect.map(({ value, metadata }) => ({ |
| 87 | value, |
| 88 | metadata: metadata ?? null, |
| 89 | cacheStatus: null, |
| 90 | })), |
| 91 | ); |
| 92 | |
| 93 | return { |
| 94 | raw: Effect.die( |
| 95 | new (class extends Error {})( |
| 96 | "KV HTTP client does not expose a native Namespace binding; use get/list/getWithMetadata.", |
| 97 | ), |
| 98 | ) as any, |
| 99 | get: ((key: string | string[], typeOrOptions?: unknown) => |
| 100 | Array.isArray(key) |
| 101 | ? getMany(key, readType(typeOrOptions)) |
| 102 | : getOne(key, readType(typeOrOptions))) as any, |
| 103 | getWithMetadata: ((key: string | string[], typeOrOptions?: unknown) => { |
| 104 | const type = readType(typeOrOptions); |
| 105 | if (Array.isArray(key)) { |
| 106 | return getMany(key, type).pipe( |
| 107 | Effect.map((values) => { |
| 108 | const map = new Map<string, unknown>(); |
| 109 | for (const [k, value] of values) { |
| 110 | map.set(k, { value, metadata: null, cacheStatus: null }); |
| 111 | } |
| 112 | return map; |
| 113 | }), |
| 114 | ); |
| 115 | } |
| 116 | return getWithMetadataOne(key, type); |
| 117 | }) as any, |
| 118 | list: ((options?: KVNamespaceListOptions) => |
| 119 | scope.pipe( |
| 120 | Effect.flatMap(({ accountId, namespaceId }) => |
| 121 | authorize( |
| 122 | kv.listNamespaceKeys({ |
| 123 | accountId, |
| 124 | namespaceId, |
| 125 | prefix: options?.prefix ?? undefined, |
| 126 | limit: options?.limit ?? undefined, |
| 127 | cursor: options?.cursor ?? undefined, |
| 128 | }), |
| 129 | ), |
| 130 | ), |
| 131 | Effect.mapError(toKVNamespaceError), |
| 132 | Effect.map((res) => { |
| 133 | const keys = res.result.map((k) => ({ |
| 134 | name: k.name, |
| 135 | expiration: k.expiration ?? undefined, |
| 136 | metadata: k.metadata ?? undefined, |
| 137 | })); |
| 138 | const cursor = res.resultInfo?.cursor ?? undefined; |
| 139 | return ( |
| 140 | cursor |
| 141 | ? { keys, list_complete: false, cursor, cacheStatus: null } |
| 142 | : { keys, list_complete: true, cacheStatus: null } |
| 143 | ) as unknown; |
| 144 | }), |
| 145 | )) as any, |
| 146 | }; |
| 147 | }; |
| 148 | |
| 149 | /** |
| 150 | * Materialize the raw value byte stream according to the requested type |
| 151 | * ("text" | "json" | "arrayBuffer" | "stream"). |
| 152 | */ |
| 153 | const materializeBody = ( |
| 154 | body: kv.GetNamespaceValueResponse["body"], |
| 155 | type: string, |
| 156 | ): Effect.Effect<unknown, Cause.UnknownError> => |
| 157 | type === "stream" |
| 158 | ? Effect.sync(() => Stream.toReadableStream(body)) |
| 159 | : Effect.tryPromise(() => { |
| 160 | const response = new Response(Stream.toReadableStream(body) as BodyInit); |
| 161 | return type === "arrayBuffer" |
| 162 | ? response.arrayBuffer() |
| 163 | : type === "json" |
| 164 | ? response.json() |
| 165 | : response.text(); |
| 166 | }); |
| 167 | |
| 168 | /** Resolve the requested decode type from the overloaded second argument. */ |
| 169 | const readType = (typeOrOptions: unknown): string => |
| 170 | typeof typeOrOptions === "string" |
| 171 | ? typeOrOptions |
| 172 | : ((typeOrOptions as { type?: string } | undefined)?.type ?? "text"); |
| 173 | |
| 174 | /** Decode a raw KV value according to the requested type. */ |
| 175 | const decodeValue = (value: unknown, type: string): unknown => { |
| 176 | if (value === null || value === undefined) return null; |
| 177 | if (type === "json") { |
| 178 | return typeof value === "string" ? JSON.parse(value) : value; |
| 179 | } |
| 180 | return typeof value === "string" ? value : JSON.stringify(value); |
| 181 | }; |