{"draft":null,"from":7,"to":318,"files":[{"path":"server/test/helpers.ts","added":82,"removed":0,"diff":"--- /dev/null\n+++ b/server/test/helpers.ts\n@@ -0,0 +1,82 @@\n+// Stand-ins for the Workers runtime in Node tests: a Durable Object's SQLite storage over node:sqlite, and the\n+// DigestStream the blob store hashes uploads with.\n+\n+import { createHash } from \"node:crypto\";\n+import { DatabaseSync } from \"node:sqlite\";\n+import type { SqliteStorage } from \"../src/engine-project.ts\";\n+\n+export function memoryStorage(filename = \":memory:\"): SqliteStorage & { db: DatabaseSync } {\n+\tconst db = new DatabaseSync(filename);\n+\tlet depth = 0;\n+\treturn {\n+\t\tdb,\n+\t\tsql: {\n+\t\t\texec(query: string, ...bindings: unknown[]) {\n+\t\t\t\tconst statement = db.prepare(query);\n+\t\t\t\tconst args = bindings as never[];\n+\t\t\t\tconst rows = statement.columns().length > 0 ? statement.all(...args) : (statement.run(...args), []);\n+\t\t\t\t// Workers hands BLOB columns back as ArrayBuffers.\n+\t\t\t\treturn {\n+\t\t\t\t\ttoArray: () =>\n+\t\t\t\t\t\trows.map((r) =>\n+\t\t\t\t\t\t\tObject.fromEntries(\n+\t\t\t\t\t\t\t\tObject.entries(r as Record<string, unknown>).map(([k, v]) => [\n+\t\t\t\t\t\t\t\t\tk,\n+\t\t\t\t\t\t\t\t\tv instanceof Uint8Array ? v.buffer.slice(v.byteOffset, v.byteOffset + v.byteLength) : v,\n+\t\t\t\t\t\t\t\t]),\n+\t\t\t\t\t\t\t),\n+\t\t\t\t\t\t),\n+\t\t\t\t};\n+\t\t\t},\n+\t\t},\n+\t\ttransactionSync<T>(fn: () => T): T {\n+\t\t\tconst name = `t${depth++}`;\n+\t\t\tdb.exec(`SAVEPOINT ${name}`);\n+\t\t\ttry {\n+\t\t\t\tconst value = fn();\n+\t\t\t\tdb.exec(`RELEASE ${name}`);\n+\t\t\t\treturn value;\n+\t\t\t} catch (e) {\n+\t\t\t\tdb.exec(`ROLLBACK TO ${name}; RELEASE ${name}`);\n+\t\t\t\tthrow e;\n+\t\t\t} finally {\n+\t\t\t\tdepth--;\n+\t\t\t}\n+\t\t},\n+\t};\n+}\n+\n+/** Workers' crypto.DigestStream, for the blob store's streaming hash. */\n+export function installDigestStream(): void {\n+\tconst c = (globalThis as unknown as { crypto: { DigestStream?: unknown } }).crypto;\n+\tif (c.DigestStream) return;\n+\tclass DigestStream extends WritableStream<Uint8Array> {\n+\t\treadonly digest: Promise<ArrayBuffer>;\n+\t\tconstructor(algorithm: string) {\n+\t\t\tconst hash = createHash(algorithm.replace(\"-\", \"\").toLowerCase());\n+\t\t\tlet done!: (b: ArrayBuffer) => void;\n+\t\t\tconst digest = new Promise<ArrayBuffer>((ok) => {\n+\t\t\t\tdone = ok;\n+\t\t\t});\n+\t\t\tsuper({\n+\t\t\t\twrite: (chunk) => void hash.update(chunk),\n+\t\t\t\tclose: () => {\n+\t\t\t\t\tconst b = hash.digest();\n+\t\t\t\t\tdone(b.buffer.slice(b.byteOffset, b.byteOffset + b.byteLength));\n+\t\t\t\t},\n+\t\t\t});\n+\t\t\tthis.digest = digest;\n+\t\t}\n+\t}\n+\tc.DigestStream = DigestStream;\n+}\n+\n+export function streamOf(text: string): ReadableStream<Uint8Array> {\n+\tconst bytes = new TextEncoder().encode(text);\n+\treturn new ReadableStream({\n+\t\tstart(c) {\n+\t\t\tc.enqueue(bytes);\n+\t\t\tc.close();\n+\t\t},\n+\t});\n+}"}],"unmatched":[]}