- MCP
- code on this site's /mcp
- CLI
- none: the JSON below is the answer
- JSON
- /christopher/abso-core/files/server/test/helpers.ts?format=json
82 linesspeed 98% · codex-capacity-chair 2%Last change codex-capacity-chair in version 157, 2 versions changed it
1// Stand-ins for the Workers runtime in Node tests: a Durable Object's SQLite storage over node:sqlite, and the
2// DigestStream the blob store hashes uploads with.
4import { createHash } from "node:crypto";
5import { DatabaseSync } from "node:sqlite";
6import type { SqliteStorage } from "../src/engine-project.ts";
8export function memoryStorage(filename = ":memory:"): SqliteStorage & { db: DatabaseSync } {
9 const db = new DatabaseSync(filename);
10 let depth = 0;
11 return {
12 db,
13 sql: {
14 exec(query: string, ...bindings: unknown[]) {
15 const statement = db.prepare(query);
16 const args = bindings as never[];
17 const rows = statement.columns().length > 0 ? statement.all(...args) : (statement.run(...args), []);
18 // Workers hands BLOB columns back as ArrayBuffers.
19 return {
20 toArray: () =>
21 rows.map((r) =>
22 Object.fromEntries(
23 Object.entries(r as Record<string, unknown>).map(([k, v]) => [
24 k,
25 v instanceof Uint8Array ? v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength) : v,
26 ]),
27 ),
28 ),
29 };
30 },
31 },
32 transactionSync<T>(fn: () => T): T {
33 const name = `t${depth++}`;
34 db.exec(`SAVEPOINT ${name}`);
35 try {
36 const value = fn();
37 db.exec(`RELEASE ${name}`);
38 return value;
39 } catch (e) {
40 db.exec(`ROLLBACK TO ${name}; RELEASE ${name}`);
41 throw e;
42 } finally {
43 depth--;
44 }
45 },
46 };
47}
49/** Workers' crypto.DigestStream, for the blob store's streaming hash. */
50export function installDigestStream(): void {
51 const c = (globalThis as unknown as { crypto: { DigestStream?: unknown } }).crypto;
52 if (c.DigestStream) return;
53 class DigestStream extends WritableStream<Uint8Array> {
54 readonly digest: Promise<ArrayBuffer>;
55 constructor(algorithm: string) {
56 const hash = createHash(algorithm.replace("-", "").toLowerCase());
57 let done!: (b: ArrayBuffer) => void;
58 const digest = new Promise<ArrayBuffer>((ok) => {
59 done = ok;
60 });
61 super({
62 write: (chunk) => void hash.update(chunk),
63 close: () => {
64 const b = hash.digest();
65 done(b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength));
66 },
67 });
68 this.digest = digest;
69 }
70 }
71 c.DigestStream = DigestStream;
72}
74export function streamOf(text: string): ReadableStream<Uint8Array> {
75 const bytes = new TextEncoder().encode(text);
76 return new ReadableStream({
77 start(c) {
78 c.enqueue(bytes);
79 c.close();
80 },
81 });
82}