@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 343 lines 12.5 KB
Raw
1 import { createHash } from "node:crypto";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import type { PropsInput } from "alchemy/Input";
5 import * as Provider from "alchemy/Provider";
6 import { Resource, type ResourceClass } from "alchemy/Resource";
7 import { createInternalTags } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as FileSystem from "effect/FileSystem";
11 import * as Schema from "effect/Schema";
12 import * as Stream from "effect/Stream";
13 import { bucketStorageClient, type BucketAttributes } from "./Bucket.ts";
14 import type { Providers } from "./Providers.ts";
15 import type { StoragePutOptions } from "./Storage.ts";
16
17 export type ObjectProps<T = never> = {
18 /** Target bucket; its branch and credentials are inferred. */
19 bucket: BucketAttributes;
20 /** Object key. Moving keys or buckets replaces the object create-first. */
21 key: string;
22 /** Content type. JSON values default to application/json. */
23 contentType?: string;
24 /** HTTP cache policy. */
25 cacheControl?: string;
26 /** HTTP download filename/disposition. */
27 contentDisposition?: string;
28 /** HTTP content encoding. */
29 contentEncoding?: string;
30 /** Application metadata; keys are normalized to lowercase and alchemy-* names are reserved. */
31 metadata?: Record<string, string>;
32 } & (
33 | {
34 /** Typed JSON desired state, serialized deterministically. */
35 value: T;
36 /** Optional runtime validation of externally written JSON. */
37 schema?: Schema.Codec<T>;
38 body?: never;
39 source?: never;
40 }
41 | {
42 /** Raw text or bytes; not JSON encoded. */
43 body: string | Uint8Array;
44 value?: never;
45 source?: never;
46 schema?: never;
47 }
48 | {
49 /** File read through the Effect filesystem during reconciliation. */
50 source: string;
51 value?: never;
52 body?: never;
53 schema?: never;
54 }
55 );
56
57 export interface ObjectAttributes {
58 /** Target bucket and branch identity. */
59 bucket: BucketAttributes;
60 /** Object key. */
61 key: string;
62 /** Opaque S3 entity tag; never assumed to be an MD5. */
63 etag: string | undefined;
64 /** Observed byte length. */
65 size: number;
66 /** SHA-256 of observed bytes. */
67 contentHash: string;
68 /** Whether the declared payload is JSON. */
69 json: boolean;
70 /** Observed content type. */
71 contentType: string | undefined;
72 /** Observed cache policy. */
73 cacheControl: string | undefined;
74 /** Observed content disposition. */
75 contentDisposition: string | undefined;
76 /** Observed encoding. */
77 contentEncoding: string | undefined;
78 /** Observed application and ownership metadata. */
79 metadata: Record<string, string>;
80 }
81
82 export interface Object<T = never> extends Resource<
83 "Neon.Object",
84 ObjectProps<T>,
85 ObjectAttributes,
86 never,
87 Providers
88 > {}
89 const ObjectResource = Resource<Object<unknown>>("Neon.Object");
90 type ObjectConstructor = Pick<
91 ResourceClass<Object<unknown>>,
92 "Type" | "Provider" | "Props" | "Self" | "Aliases"
93 > & {
94 <T = never>(
95 id: string,
96 props: PropsInput<ObjectProps<T>>,
97 ): Effect.Effect<Object<T>, never, Providers>;
98 };
99
100 /**
101 * A typed declarative object. JSON values are serialized automatically and their
102 * type is retained by ReadObject and WriteObject. A generic alone is not runtime
103 * validation; add a schema when external writers may violate the contract.
104 * Declared content is infrastructure desired state, not mutable application data.
105 *
106 * ### JSON Values
107 * **Example:** Inferred settings
108 * ```typescript
109 * const settings = yield* Neon.Object("Settings", {
110 * bucket: uploads, key: "settings.json", value: { theme: "system", pageSize: 25 },
111 * });
112 * ```
113 *
114 * ### File Content
115 * **Example:** An SVG without JSON serialization
116 * ```typescript
117 * const logo = yield* Neon.Object("Logo", {
118 * bucket: assets, key: "logo.svg", source: "./assets/logo.svg", contentType: "image/svg+xml",
119 * });
120 * ```
121 *
122 * @resource
123 * @product Bucket
124 */
125 export const Object = ObjectResource as ObjectConstructor;
126
127 export class ObjectDecodeError extends Data.TaggedError("ObjectDecodeError")<{
128 message: string;
129 }> {}
130
131 /** Stable JSON with explicit rejection of lossy values and cycles. */
132 export const serializeObjectValue = (value: unknown) =>
133 Effect.try({
134 try: () => {
135 const ancestors = new Set<object>();
136 const encode = (value: unknown): string => {
137 if (value === null || typeof value === "string" || typeof value === "boolean")
138 return JSON.stringify(value);
139 if (typeof value === "number" && Number.isFinite(value)) return JSON.stringify(value);
140 if (typeof value !== "object" || value === null) throw new Error("Unsupported JSON value");
141 if (ancestors.has(value)) throw new Error("Cyclic JSON value");
142 if (Reflect.ownKeys(value).some((key) => typeof key === "symbol"))
143 throw new Error("Symbol JSON keys are unsupported");
144 ancestors.add(value);
145 let encoded: string;
146 if (Array.isArray(value)) {
147 if (globalThis.Object.getOwnPropertyNames(value).length !== value.length + 1)
148 throw new Error("Sparse arrays and array properties are unsupported");
149 encoded = `[${Array.from({ length: value.length }, (_, index) => {
150 const descriptor = globalThis.Object.getOwnPropertyDescriptor(value, String(index));
151 if (!descriptor || !descriptor.enumerable || !("value" in descriptor))
152 throw new Error("Sparse arrays, non-enumerable fields and accessors are unsupported");
153 return encode(descriptor.value);
154 }).join(",")}]`;
155 } else {
156 if (
157 globalThis.Object.getPrototypeOf(value) !== globalThis.Object.prototype &&
158 globalThis.Object.getPrototypeOf(value) !== null
159 )
160 throw new Error("JSON values must be plain records");
161 if (
162 globalThis.Object.getOwnPropertyNames(value).length !==
163 globalThis.Object.keys(value).length
164 )
165 throw new Error("Non-enumerable JSON fields are unsupported");
166 encoded = `{${globalThis.Object.keys(value)
167 .sort()
168 .map((key) => {
169 const descriptor = globalThis.Object.getOwnPropertyDescriptor(value, key)!;
170 if (!("value" in descriptor)) throw new Error("JSON accessors are unsupported");
171 return `${JSON.stringify(key)}:${encode(descriptor.value)}`;
172 })
173 .join(",")}}`;
174 }
175 ancestors.delete(value);
176 return encoded;
177 };
178 return encode(value);
179 },
180 catch: () =>
181 new ObjectDecodeError({
182 message: "Value is not losslessly serializable JSON",
183 }),
184 });
185
186 export const storageBodyBytes = (body: Stream.Stream<Uint8Array, Error> | undefined) =>
187 body === undefined
188 ? Effect.succeed(new Uint8Array())
189 : Stream.runCollect(body).pipe(
190 Effect.map((chunks) => {
191 const bytes = new Uint8Array(chunks.reduce((size, chunk) => size + chunk.length, 0));
192 let offset = 0;
193 for (const chunk of chunks) {
194 bytes.set(chunk, offset);
195 offset += chunk.length;
196 }
197 return bytes;
198 }),
199 );
200
201 const hashBytes = (bytes: Uint8Array) =>
202 Effect.sync(() => createHash("sha256").update(bytes).digest("hex"));
203 const isJson = (props: ObjectProps<unknown>) => "value" in props;
204 const desiredBytes = Effect.fn(function* (props: ObjectProps<unknown>) {
205 if (isJson(props)) {
206 const value = props.schema
207 ? yield* Schema.decodeUnknownEffect(props.schema)(props.value).pipe(
208 Effect.mapError(
209 () =>
210 new ObjectDecodeError({
211 message: "Declared JSON does not satisfy its schema",
212 }),
213 ),
214 )
215 : props.value;
216 const json = yield* serializeObjectValue(value);
217 return yield* Effect.sync(() => new TextEncoder().encode(json));
218 }
219 if (props.source !== undefined)
220 return yield* (yield* FileSystem.FileSystem).readFile(props.source);
221 const body = props.body!;
222 return typeof body === "string" ? yield* Effect.sync(() => new TextEncoder().encode(body)) : body;
223 });
224
225 const observeObject = Effect.fn(function* (bucket: BucketAttributes, key: string, json: boolean) {
226 const client = yield* bucketStorageClient(bucket);
227 const object = yield* client.get(key);
228 if (!object) return undefined;
229 const body = yield* storageBodyBytes(object.Body);
230 return {
231 bucket,
232 key,
233 json,
234 etag: object.ETag,
235 size: body.length,
236 contentHash: yield* hashBytes(body),
237 contentType: object.ContentType,
238 cacheControl: object.CacheControl,
239 contentDisposition: object.ContentDisposition,
240 contentEncoding: object.ContentEncoding,
241 metadata: globalThis.Object.fromEntries(
242 globalThis.Object.entries(object.Metadata ?? {}).filter(
243 (entry): entry is [string, string] => entry[1] !== undefined,
244 ),
245 ),
246 } satisfies ObjectAttributes;
247 });
248
249 const ownership = Effect.fn(function* (id: string, bucket: BucketAttributes) {
250 const tags = yield* createInternalTags(id);
251 return {
252 "alchemy-stack": tags["alchemy::stack"],
253 "alchemy-stage": tags["alchemy::stage"],
254 "alchemy-id": id,
255 "alchemy-branch": bucket.branchId,
256 };
257 });
258
259 export const ObjectProvider = () =>
260 Provider.succeed(ObjectResource, {
261 stables: ["key"],
262 diff: Effect.fn(function* ({ news, output }) {
263 if (!isResolved(news) || !output) return;
264 if (
265 news.key !== output.key ||
266 news.bucket.bucketName !== output.bucket.bucketName ||
267 news.bucket.projectId !== output.bucket.projectId ||
268 news.bucket.branchId !== output.bucket.branchId
269 )
270 return { action: "replace" };
271 if (news.source !== undefined) return { action: "update" };
272 }),
273 read: Effect.fn(function* ({ fqn, olds, output }) {
274 const bucket = output?.bucket ?? olds?.bucket;
275 if (!bucket?.bucketName) return undefined;
276 const attrs = yield* observeObject(bucket, output?.key ?? olds.key, isJson(olds));
277 if (!attrs) return undefined;
278 const expected = yield* ownership(fqn, attrs.bucket);
279 return globalThis.Object.entries(expected).every(
280 ([key, value]) => attrs.metadata[key] === value,
281 )
282 ? attrs
283 : Unowned(attrs);
284 }),
285 reconcile: Effect.fn(function* ({ fqn, news }) {
286 const bucket = news.bucket;
287 const client = yield* bucketStorageClient(bucket);
288 const body = yield* desiredBytes(news);
289 const hash = yield* hashBytes(body);
290 const current = yield* observeObject(bucket, news.key, isJson(news));
291 if (
292 globalThis.Object.keys(news.metadata ?? {}).some((key) =>
293 key.toLowerCase().startsWith("alchemy-"),
294 )
295 ) {
296 return yield* new ObjectDecodeError({
297 message: "alchemy-* object metadata is reserved for ownership",
298 });
299 }
300 const entries = globalThis.Object.entries(news.metadata ?? {});
301 const applicationMetadata = globalThis.Object.fromEntries(
302 entries.map(([key, value]) => [key.toLowerCase(), value]),
303 );
304 if (globalThis.Object.keys(applicationMetadata).length !== entries.length) {
305 return yield* new ObjectDecodeError({
306 message: "Object metadata keys must be unique ignoring case",
307 });
308 }
309 const metadata = {
310 ...applicationMetadata,
311 ...(yield* ownership(fqn, bucket)),
312 };
313 const options: StoragePutOptions = {
314 ContentType:
315 news.contentType ?? (isJson(news) ? "application/json" : "application/octet-stream"),
316 CacheControl: news.cacheControl,
317 ContentDisposition: news.contentDisposition,
318 ContentEncoding: news.contentEncoding,
319 Metadata: metadata,
320 };
321 if (
322 !current ||
323 current.contentHash !== hash ||
324 current.contentType !== options.ContentType ||
325 current.cacheControl !== options.CacheControl ||
326 current.contentDisposition !== options.ContentDisposition ||
327 current.contentEncoding !== options.ContentEncoding ||
328 (yield* serializeObjectValue(current.metadata)) !== (yield* serializeObjectValue(metadata))
329 ) {
330 yield* client.put(news.key, body, options);
331 }
332 const result = yield* observeObject(bucket, news.key, isJson(news));
333 if (!result)
334 return yield* new ObjectDecodeError({
335 message: "Object was not readable after upload",
336 });
337 return result;
338 }),
339 delete: Effect.fn(function* ({ output }) {
340 const client = yield* bucketStorageClient(output.bucket);
341 yield* client.delete(output.key).pipe(Effect.catchTag("NoSuchBucket", () => Effect.void));
342 }),
343 });