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 { createHash } from "node:crypto"; |
| 2 | import { isResource } from "alchemy/Resource"; |
| 3 | import { CurrentRuntimeContext } from "alchemy/RuntimeContext"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schema from "effect/Schema"; |
| 6 | import { bindBackendEnvironment } from "./BackendConnection.ts"; |
| 7 | import type { Bucket } from "./Bucket.ts"; |
| 8 | import { |
| 9 | ObjectDecodeError, |
| 10 | serializeObjectValue, |
| 11 | storageBodyBytes, |
| 12 | type Object, |
| 13 | } from "./Object.ts"; |
| 14 | import type { ReadObjectClient, ObjectValue } from "./ReadObject.ts"; |
| 15 | import { |
| 16 | makeStorageBinding, |
| 17 | StorageBindingError, |
| 18 | type StorageBindingOptions, |
| 19 | } from "./StorageBinding.ts"; |
| 20 | import type { WriteObjectClient } from "./WriteObject.ts"; |
| 21 | |
| 22 | const isBucket = (value: unknown): value is Bucket => |
| 23 | isResource(value) && value.Type === "Neon.Bucket"; |
| 24 | |
| 25 | export const makeReadObjectHttp = () => |
| 26 | Effect.gen(function* () { |
| 27 | const bind = yield* makeStorageBinding("storage:read"); |
| 28 | const runtime = yield* CurrentRuntimeContext; |
| 29 | if (!runtime) |
| 30 | return yield* Effect.die( |
| 31 | new StorageBindingError({ |
| 32 | message: "Object bindings require a Platform host", |
| 33 | }), |
| 34 | ); |
| 35 | return Effect.fn(function* <T>(object: Object<T>, options?: StorageBindingOptions) { |
| 36 | const target = object.Props.bucket; |
| 37 | if (!isBucket(target)) |
| 38 | return yield* Effect.die( |
| 39 | new StorageBindingError({ |
| 40 | message: "Object bindings require a Neon.Bucket resource reference", |
| 41 | }), |
| 42 | ); |
| 43 | const bucket = yield* bind(target, options); |
| 44 | const name = yield* Effect.sync( |
| 45 | () => |
| 46 | `NEON_OBJECT_${createHash("sha256").update(object.FQN).digest("hex").slice(0, 24).toUpperCase()}_KEY`, |
| 47 | ); |
| 48 | yield* bindBackendEnvironment(`${object.FQN}:ReadObject`, { |
| 49 | [name]: object.key, |
| 50 | }); |
| 51 | const key = runtime.get<string>(name).pipe( |
| 52 | Effect.flatMap((key) => |
| 53 | key === undefined |
| 54 | ? Effect.fail( |
| 55 | new StorageBindingError({ |
| 56 | message: "Object key was not bound", |
| 57 | }), |
| 58 | ) |
| 59 | : Effect.succeed(key), |
| 60 | ), |
| 61 | ); |
| 62 | const bytes = Effect.gen(function* () { |
| 63 | const object = yield* bucket.get(yield* key); |
| 64 | return object ? yield* storageBodyBytes(object.Body) : undefined; |
| 65 | }); |
| 66 | const get = Effect.gen(function* () { |
| 67 | const body = yield* bytes; |
| 68 | if (body === undefined) return undefined; |
| 69 | if (!("value" in object.Props)) return body as ObjectValue<T>; |
| 70 | const parsed: unknown = yield* Effect.try({ |
| 71 | try: () => JSON.parse(new TextDecoder().decode(body)), |
| 72 | catch: () => |
| 73 | new ObjectDecodeError({ |
| 74 | message: "Stored object is not valid JSON", |
| 75 | }), |
| 76 | }); |
| 77 | if (!object.Props.schema) return parsed as ObjectValue<T>; |
| 78 | return (yield* Schema.decodeUnknownEffect(object.Props.schema)(parsed).pipe( |
| 79 | Effect.mapError( |
| 80 | () => |
| 81 | new ObjectDecodeError({ |
| 82 | message: "Stored JSON does not satisfy the object's schema", |
| 83 | }), |
| 84 | ), |
| 85 | )) as ObjectValue<T>; |
| 86 | }); |
| 87 | return { |
| 88 | get: () => get, |
| 89 | bytes: () => bytes, |
| 90 | } satisfies ReadObjectClient<T>; |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | export const makeWriteObjectHttp = () => |
| 95 | Effect.gen(function* () { |
| 96 | const bind = yield* makeStorageBinding("storage:write"); |
| 97 | const runtime = yield* CurrentRuntimeContext; |
| 98 | if (!runtime) |
| 99 | return yield* Effect.die( |
| 100 | new StorageBindingError({ |
| 101 | message: "Object bindings require a Platform host", |
| 102 | }), |
| 103 | ); |
| 104 | return Effect.fn(function* <T>(object: Object<T>, options?: StorageBindingOptions) { |
| 105 | const target = object.Props.bucket; |
| 106 | if (!isBucket(target)) |
| 107 | return yield* Effect.die( |
| 108 | new StorageBindingError({ |
| 109 | message: "Object bindings require a Neon.Bucket resource reference", |
| 110 | }), |
| 111 | ); |
| 112 | const bucket = yield* bind(target, options); |
| 113 | const name = yield* Effect.sync( |
| 114 | () => |
| 115 | `NEON_OBJECT_${createHash("sha256").update(object.FQN).digest("hex").slice(0, 24).toUpperCase()}_KEY`, |
| 116 | ); |
| 117 | yield* bindBackendEnvironment(`${object.FQN}:WriteObject`, { |
| 118 | [name]: object.key, |
| 119 | }); |
| 120 | const put = Effect.fn(function* (value: [T] extends [never] ? string | Uint8Array : T) { |
| 121 | const key = yield* runtime.get<string>(name); |
| 122 | if (key === undefined) |
| 123 | return yield* new StorageBindingError({ |
| 124 | message: "Object key was not bound", |
| 125 | }); |
| 126 | const observed = yield* bucket.head(key); |
| 127 | const metadata = { |
| 128 | ContentType: observed?.ContentType, |
| 129 | CacheControl: observed?.CacheControl, |
| 130 | ContentDisposition: observed?.ContentDisposition, |
| 131 | ContentEncoding: observed?.ContentEncoding, |
| 132 | Metadata: observed?.Metadata, |
| 133 | }; |
| 134 | if (!("value" in object.Props)) { |
| 135 | return yield* bucket.put(key, value as string | Uint8Array, metadata); |
| 136 | } |
| 137 | const checked = object.Props.schema |
| 138 | ? yield* Schema.decodeUnknownEffect(object.Props.schema)(value).pipe( |
| 139 | Effect.mapError( |
| 140 | () => |
| 141 | new ObjectDecodeError({ |
| 142 | message: "Written JSON does not satisfy the object's schema", |
| 143 | }), |
| 144 | ), |
| 145 | ) |
| 146 | : value; |
| 147 | return yield* bucket.put(key, yield* serializeObjectValue(checked), { |
| 148 | ...metadata, |
| 149 | ContentType: metadata.ContentType ?? "application/json", |
| 150 | }); |
| 151 | }); |
| 152 | return { put } satisfies WriteObjectClient<T>; |
| 153 | }); |
| 154 | }); |