@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, 06:00 UTC

main
ts 269 lines 9.36 KB
Raw
1 /**
2 * In-process harness for the repo object store: a `SqlClient` over
3 * `bun:sqlite` (the same DDL the Durable Object runs) and an in-memory
4 * `BlobStoreShape` with ranged reads. Lets the emitter, compaction, and
5 * closure code be unit-tested without a Worker.
6 */
7 import { Database } from "bun:sqlite";
8 import type { BlobBody, BlobMeta, BlobStoreShape } from "@alchemy.run/git/BlobStore";
9 import { BlobStoreError } from "@alchemy.run/git/BlobStore";
10 import { StoreError } from "@alchemy.run/git/Protocol/Store";
11 import {
12 chunk,
13 MAX_IN_PARAMS,
14 placeholders,
15 REPO_DDL,
16 type SqlClient,
17 } from "@alchemy.run/git/Store/Sql";
18 import * as Effect from "effect/Effect";
19 import * as Stream from "effect/Stream";
20
21 type Value = string | number | ArrayBuffer | Uint8Array | null;
22
23 const bind = (v: Value): string | number | Uint8Array | null =>
24 v instanceof ArrayBuffer ? new Uint8Array(v) : v;
25
26 /** Rows come back with BLOB columns as ArrayBuffer, like the DO. */
27 const normalize = (row: Record<string, unknown>) => {
28 for (const key of Object.keys(row)) {
29 const v = row[key];
30 if (v instanceof Uint8Array) {
31 row[key] = v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength);
32 }
33 }
34 return row;
35 };
36
37 class RollbackSignal {
38 constructor(readonly error: unknown) {}
39 }
40
41 export const makeTestSqlClient = (): SqlClient & { readonly db: Database } => {
42 const db = new Database(":memory:");
43 for (const ddl of REPO_DDL) db.run(ddl);
44
45 const exec = (query: string, bindings: ReadonlyArray<Value>) => {
46 // Durable Object SQLite: at most 100 bound parameters per statement.
47 if (bindings.length > MAX_IN_PARAMS) {
48 throw new Error(
49 `too many SQL variables (${bindings.length} > ${MAX_IN_PARAMS}): SQLITE_ERROR`,
50 );
51 }
52 const stmt = db.query(query);
53 const isRead = /^\s*(SELECT|WITH|PRAGMA)/i.test(query);
54 let rowsWritten = 0;
55 let rows: Array<Record<string, unknown>> = [];
56 if (isRead || /RETURNING/i.test(query)) {
57 rows = stmt.all(...bindings.map(bind)) as Array<Record<string, unknown>>;
58 } else {
59 // `run` reports `changes`, which is what the DO cursor's rowsWritten is.
60 rowsWritten = (stmt.run(...bindings.map(bind)) as { changes: number }).changes;
61 }
62 const out = rows.map(normalize);
63 return {
64 toArray: () => out,
65 one: () => {
66 if (out.length !== 1) throw new Error(`expected 1 row, got ${out.length}`);
67 return out[0];
68 },
69 raw: () => out.map((r) => Object.values(r)),
70 rowsWritten,
71 rowsRead: out.length,
72 columnNames: out.length > 0 ? Object.keys(out[0]!) : [],
73 [Symbol.iterator]: () => out[Symbol.iterator](),
74 };
75 };
76 const raw = {
77 exec: (query: string, ...bindings: Array<Value>) => exec(query, bindings),
78 } as unknown as SqlClient["raw"];
79
80 const allWith = <Row extends Record<string, unknown>>(
81 query: string,
82 bindings: ReadonlyArray<Value>,
83 ) =>
84 Effect.try({
85 try: () => exec(query, bindings).toArray() as Array<Row>,
86 catch: (cause) =>
87 new StoreError({
88 reason: `sql failed (${query.slice(0, 120)}): ${cause instanceof Error ? cause.message : String(cause)}`,
89 }),
90 });
91
92 return {
93 db,
94 raw,
95 all: (query, ...bindings) => allWith(query, bindings as Array<Value>),
96 first: (query, ...bindings) =>
97 allWith(query, bindings as Array<Value>).pipe(
98 Effect.map((rows) => (rows.length > 0 ? rows[0] : undefined)),
99 ) as never,
100 run: (query, ...bindings) => allWith(query, bindings as Array<Value>).pipe(Effect.asVoid),
101 inChunks: (makeQuery, items, options) =>
102 Effect.gen(function* () {
103 const prefix = options?.prefix ?? [];
104 const suffix = options?.suffix ?? [];
105 const size = Math.max(
106 1,
107 Math.min(
108 options?.chunkSize ?? MAX_IN_PARAMS,
109 MAX_IN_PARAMS - prefix.length - suffix.length,
110 ),
111 );
112 const rows: Array<never> = [];
113 for (const part of chunk(items, size)) {
114 const batch = yield* allWith(makeQuery(placeholders(part.length)), [
115 ...prefix,
116 ...part,
117 ...suffix,
118 ] as Array<Value>);
119 for (const row of batch) rows.push(row as never);
120 }
121 return rows;
122 }) as never,
123 transactionSync: (body) =>
124 Effect.try({
125 try: () => {
126 db.run("BEGIN");
127 try {
128 const result = body(raw, (error) => {
129 throw new RollbackSignal(error);
130 });
131 db.run("COMMIT");
132 return result;
133 } catch (error) {
134 db.run("ROLLBACK");
135 throw error;
136 }
137 },
138 catch: (cause) =>
139 cause instanceof RollbackSignal
140 ? (cause.error as never)
141 : new StoreError({
142 reason: `transactionSync failed: ${cause instanceof Error ? cause.message : String(cause)}`,
143 }),
144 }) as never,
145 };
146 };
147
148 export interface MemoryBlobStore extends BlobStoreShape {
149 readonly objects: Map<string, Uint8Array>;
150 /** Every `get`, with its range — for asserting window fetch counts. */
151 readonly gets: Array<{ key: string; offset?: number; length?: number }>;
152 }
153
154 export const makeMemoryBlobStore = (): MemoryBlobStore => {
155 const objects = new Map<string, Uint8Array>();
156 const uploads = new Map<string, Map<number, Uint8Array>>();
157 const putPart = (uploadId: string, partNumber: number, part: Uint8Array) =>
158 Effect.suspend(() => {
159 const stored = uploads.get(uploadId);
160 if (stored === undefined) {
161 return Effect.fail(new BlobStoreError({ reason: `no upload ${uploadId}` }));
162 }
163 stored.set(partNumber, Uint8Array.from(part));
164 return Effect.succeed({ partNumber, etag: `etag-${partNumber}` });
165 });
166
167 const gets: MemoryBlobStore["gets"] = [];
168 const body = (bytes: Uint8Array): BlobBody => ({
169 size: bytes.length,
170 bytes: Effect.succeed(bytes),
171 stream: Stream.succeed(bytes),
172 });
173 return {
174 objects,
175 gets,
176 get: (key, range) =>
177 Effect.sync(() => {
178 gets.push({ key, offset: range?.offset, length: range?.length });
179 const whole = objects.get(key);
180 if (whole === undefined) return null;
181 if (range === undefined) return body(whole);
182 return body(
183 whole.subarray(range.offset, Math.min(range.offset + range.length, whole.length)),
184 );
185 }),
186 put: (key, data) =>
187 Effect.gen(function* () {
188 if (data instanceof Uint8Array) {
189 objects.set(key, Uint8Array.from(data));
190 return;
191 }
192 const parts = Array.from(
193 yield* Stream.runCollect(data as Stream.Stream<Uint8Array, BlobStoreError>),
194 );
195 const total = parts.reduce((n, p) => n + p.length, 0);
196 const out = new Uint8Array(total);
197 let at = 0;
198 for (const p of parts) {
199 out.set(p, at);
200 at += p.length;
201 }
202 objects.set(key, out);
203 }) as never,
204 head: (key) =>
205 Effect.sync((): BlobMeta | null => {
206 const o = objects.get(key);
207 return o === undefined ? null : { key, size: o.length };
208 }),
209 multipart: (key) =>
210 Effect.sync(() => {
211 const uploadId = `${key}#${uploads.size + 1}`;
212 uploads.set(uploadId, new Map());
213 return {
214 uploadId,
215 uploadPart: (partNumber, part) => putPart(uploadId, partNumber, part),
216 complete: (parts) =>
217 Effect.suspend(() => {
218 const stored = uploads.get(uploadId);
219 if (stored === undefined) {
220 return Effect.fail(new BlobStoreError({ reason: `no upload ${uploadId}` }));
221 }
222 // R2's rule, checked as R2 checks it: every part but the
223 // last (in the list as given) must be the same size.
224 const listed = parts.map((p) => stored.get(p.partNumber));
225 if (listed.some((p) => p === undefined)) {
226 return Effect.fail(new BlobStoreError({ reason: "complete: unknown part" }));
227 }
228 const sizes = listed.map((p) => p!.length);
229 for (let i = 0; i < sizes.length - 1; i++) {
230 if (sizes[i] !== sizes[0]) {
231 return Effect.fail(
232 new BlobStoreError({
233 reason:
234 "Your proposed upload is smaller than the minimum allowed object size",
235 }),
236 );
237 }
238 }
239 const total = sizes.reduce((n, x) => n + x, 0);
240 const out = new Uint8Array(total);
241 let at = 0;
242 for (const p of listed) {
243 out.set(p!, at);
244 at += p!.length;
245 }
246 objects.set(key, out);
247 uploads.delete(uploadId);
248 return Effect.void;
249 }),
250 abort: Effect.sync(() => {
251 uploads.delete(uploadId);
252 }),
253 };
254 }),
255 uploadPart: (_key, uploadId, partNumber, part) => putPart(uploadId, partNumber, part),
256 delete: (keys) =>
257 Effect.sync(() => {
258 for (const key of typeof keys === "string" ? [keys] : keys) {
259 objects.delete(key);
260 }
261 }),
262 list: (prefix) =>
263 Stream.fromIterable(
264 Array.from(objects.entries())
265 .filter(([k]) => k.startsWith(prefix ?? ""))
266 .map(([key, o]): BlobMeta => ({ key, size: o.length })),
267 ),
268 } as MemoryBlobStore;
269 };