@statewalker/db-sqlite-do 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/fake.d.mts.map +1 -1
- package/dist/fake.mjs +6 -1
- package/dist/fake.mjs.map +1 -1
- package/package.json +4 -4
- package/src/do-sqlite-db.test.ts +19 -0
- package/src/fake-do-storage.ts +7 -1
package/dist/fake.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake.d.mts","names":[],"sources":["../src/fake-do-storage.ts"],"mappings":";;KAGY,gBAAgB;EAAiB;;;;;;;;
|
|
1
|
+
{"version":3,"file":"fake.d.mts","names":[],"sources":["../src/fake-do-storage.ts"],"mappings":";;KAGY,gBAAgB;EAAiB;;;;;;;;wBAmB7B,oBAAoB"}
|
package/dist/fake.mjs
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { DatabaseSync } from "node:sqlite";
|
|
2
2
|
//#region src/fake-do-storage.ts
|
|
3
|
+
/**
|
|
4
|
+
* An empty view over a real buffer. node:sqlite (Node 22) binds a view whose buffer has no backing
|
|
5
|
+
* store, such as `new Uint8Array(new ArrayBuffer(0))`, as NULL; the platform stores an empty BLOB.
|
|
6
|
+
*/
|
|
7
|
+
const EMPTY_BLOB = (/* @__PURE__ */ new Uint8Array(1)).subarray(0, 0);
|
|
3
8
|
/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */
|
|
4
9
|
const MAX_BOUND_PARAMETERS = 100;
|
|
5
10
|
const MAX_VALUE_BYTES = 2e6;
|
|
@@ -18,7 +23,7 @@ function newFakeDoStorage() {
|
|
|
18
23
|
if (typeof value === "string" && Buffer.byteLength(value) > MAX_VALUE_BYTES) throw tooBig();
|
|
19
24
|
if (value instanceof ArrayBuffer) {
|
|
20
25
|
if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();
|
|
21
|
-
return new Uint8Array(value);
|
|
26
|
+
return value.byteLength === 0 ? EMPTY_BLOB : new Uint8Array(value);
|
|
22
27
|
}
|
|
23
28
|
return value;
|
|
24
29
|
};
|
package/dist/fake.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fake.mjs","names":[],"sources":["../src/fake-do-storage.ts"],"sourcesContent":["import { DatabaseSync, type SQLInputValue } from \"node:sqlite\";\nimport type { DoSqlStorage } from \"./do-sqlite-db.js\";\n\nexport type FakeDoStorage = DoSqlStorage & { close(): void };\n\n/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */\nconst MAX_BOUND_PARAMETERS = 100;\nconst MAX_VALUE_BYTES = 2_000_000;\nconst NOT_AUTHORIZED = /^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|PRAGMA)\\b/i;\n\n/**\n * An in-memory stand-in for a Durable Object's `ctx.storage` on Node.js's `node:sqlite`, with the\n * DO limits: no SQL-level transactions or PRAGMA, at most 100 bound parameters, BLOBs bound and\n * read as ArrayBuffer, and bound strings (UTF-8 bytes) and BLOBs at most ~2 MB. `transaction` runs the closure between BEGIN and\n * COMMIT/ROLLBACK and, like the platform, does not queue concurrent calls.\n */\nexport function newFakeDoStorage(): FakeDoStorage {\n const db = new DatabaseSync(\":memory:\");\n const bind = (value: unknown): SQLInputValue => {\n if (ArrayBuffer.isView(value)) {\n throw new TypeError(\"fake DO storage: bind a BLOB as an ArrayBuffer, not a typed array\");\n }\n const tooBig = () => new Error(\"SQL error: bind sync KV cursor: string or blob too big\");\n if (typeof value === \"string\" && Buffer.byteLength(value) > MAX_VALUE_BYTES) throw tooBig();\n if (value instanceof ArrayBuffer) {\n if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();\n return new Uint8Array(value);\n }\n return value as SQLInputValue;\n };\n return {\n sql: {\n exec(query: string, ...bindings: unknown[]) {\n if (NOT_AUTHORIZED.test(query))\n throw new Error(\"SQL error: prepare SQL cursor: not authorized\");\n if (bindings.length > MAX_BOUND_PARAMETERS) {\n throw new Error(\"SQL error: prepare SQL cursor: too many SQL variables\");\n }\n const statement = db.prepare(query);\n let rows: Record<string, unknown>[] = [];\n // prepare() compiles only the first statement: run row-less, parameter-less text with exec().\n if (bindings.length === 0 && statement.columns().length === 0) db.exec(query);\n else rows = statement.all(...bindings.map(bind)) as Record<string, unknown>[];\n const out = rows.map((row) =>\n Object.fromEntries(\n Object.entries(row).map(([key, value]) => [\n key,\n value instanceof Uint8Array ? value.slice().buffer : value,\n ]),\n ),\n );\n return { toArray: () => out };\n },\n },\n async transaction<T>(closure: () => Promise<T>): Promise<T> {\n db.exec(\"BEGIN\");\n try {\n const result = await closure();\n db.exec(\"COMMIT\");\n return result;\n } catch (error) {\n // Like the platform: when SQLite already ended the transaction (ON CONFLICT ROLLBACK), the\n // rollback fails and its error replaces the callback's.\n if (!db.isTransaction) {\n throw new Error(\n \"a transaction rollback failed: cannot rollback - no transaction is active\",\n );\n }\n db.exec(\"ROLLBACK\");\n throw error;\n }\n },\n close: () => db.close(),\n };\n}\n"],"mappings":"
|
|
1
|
+
{"version":3,"file":"fake.mjs","names":[],"sources":["../src/fake-do-storage.ts"],"sourcesContent":["import { DatabaseSync, type SQLInputValue } from \"node:sqlite\";\nimport type { DoSqlStorage } from \"./do-sqlite-db.js\";\n\nexport type FakeDoStorage = DoSqlStorage & { close(): void };\n\n/**\n * An empty view over a real buffer. node:sqlite (Node 22) binds a view whose buffer has no backing\n * store, such as `new Uint8Array(new ArrayBuffer(0))`, as NULL; the platform stores an empty BLOB.\n */\nconst EMPTY_BLOB = new Uint8Array(1).subarray(0, 0);\n\n/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */\nconst MAX_BOUND_PARAMETERS = 100;\nconst MAX_VALUE_BYTES = 2_000_000;\nconst NOT_AUTHORIZED = /^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE|PRAGMA)\\b/i;\n\n/**\n * An in-memory stand-in for a Durable Object's `ctx.storage` on Node.js's `node:sqlite`, with the\n * DO limits: no SQL-level transactions or PRAGMA, at most 100 bound parameters, BLOBs bound and\n * read as ArrayBuffer, and bound strings (UTF-8 bytes) and BLOBs at most ~2 MB. `transaction` runs the closure between BEGIN and\n * COMMIT/ROLLBACK and, like the platform, does not queue concurrent calls.\n */\nexport function newFakeDoStorage(): FakeDoStorage {\n const db = new DatabaseSync(\":memory:\");\n const bind = (value: unknown): SQLInputValue => {\n if (ArrayBuffer.isView(value)) {\n throw new TypeError(\"fake DO storage: bind a BLOB as an ArrayBuffer, not a typed array\");\n }\n const tooBig = () => new Error(\"SQL error: bind sync KV cursor: string or blob too big\");\n if (typeof value === \"string\" && Buffer.byteLength(value) > MAX_VALUE_BYTES) throw tooBig();\n if (value instanceof ArrayBuffer) {\n if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();\n return value.byteLength === 0 ? EMPTY_BLOB : new Uint8Array(value);\n }\n return value as SQLInputValue;\n };\n return {\n sql: {\n exec(query: string, ...bindings: unknown[]) {\n if (NOT_AUTHORIZED.test(query))\n throw new Error(\"SQL error: prepare SQL cursor: not authorized\");\n if (bindings.length > MAX_BOUND_PARAMETERS) {\n throw new Error(\"SQL error: prepare SQL cursor: too many SQL variables\");\n }\n const statement = db.prepare(query);\n let rows: Record<string, unknown>[] = [];\n // prepare() compiles only the first statement: run row-less, parameter-less text with exec().\n if (bindings.length === 0 && statement.columns().length === 0) db.exec(query);\n else rows = statement.all(...bindings.map(bind)) as Record<string, unknown>[];\n const out = rows.map((row) =>\n Object.fromEntries(\n Object.entries(row).map(([key, value]) => [\n key,\n value instanceof Uint8Array ? value.slice().buffer : value,\n ]),\n ),\n );\n return { toArray: () => out };\n },\n },\n async transaction<T>(closure: () => Promise<T>): Promise<T> {\n db.exec(\"BEGIN\");\n try {\n const result = await closure();\n db.exec(\"COMMIT\");\n return result;\n } catch (error) {\n // Like the platform: when SQLite already ended the transaction (ON CONFLICT ROLLBACK), the\n // rollback fails and its error replaces the callback's.\n if (!db.isTransaction) {\n throw new Error(\n \"a transaction rollback failed: cannot rollback - no transaction is active\",\n );\n }\n db.exec(\"ROLLBACK\");\n throw error;\n }\n },\n close: () => db.close(),\n };\n}\n"],"mappings":";;;;;;AASA,MAAM,8BAAa,IAAI,WAAW,CAAC,EAAA,CAAE,SAAS,GAAG,CAAC;;AAGlD,MAAM,uBAAuB;AAC7B,MAAM,kBAAkB;AACxB,MAAM,iBAAiB;;;;;;;AAQvB,SAAgB,mBAAkC;CAChD,MAAM,KAAK,IAAI,aAAa,UAAU;CACtC,MAAM,QAAQ,UAAkC;EAC9C,IAAI,YAAY,OAAO,KAAK,GAC1B,MAAM,IAAI,UAAU,mEAAmE;EAEzF,MAAM,+BAAe,IAAI,MAAM,wDAAwD;EACvF,IAAI,OAAO,UAAU,YAAY,OAAO,WAAW,KAAK,IAAI,iBAAiB,MAAM,OAAO;EAC1F,IAAI,iBAAiB,aAAa;GAChC,IAAI,MAAM,aAAa,iBAAiB,MAAM,OAAO;GACrD,OAAO,MAAM,eAAe,IAAI,aAAa,IAAI,WAAW,KAAK;EACnE;EACA,OAAO;CACT;CACA,OAAO;EACL,KAAK,EACH,KAAK,OAAe,GAAG,UAAqB;GAC1C,IAAI,eAAe,KAAK,KAAK,GAC3B,MAAM,IAAI,MAAM,+CAA+C;GACjE,IAAI,SAAS,SAAS,sBACpB,MAAM,IAAI,MAAM,uDAAuD;GAEzE,MAAM,YAAY,GAAG,QAAQ,KAAK;GAClC,IAAI,OAAkC,CAAC;GAEvC,IAAI,SAAS,WAAW,KAAK,UAAU,QAAQ,CAAC,CAAC,WAAW,GAAG,GAAG,KAAK,KAAK;QACvE,OAAO,UAAU,IAAI,GAAG,SAAS,IAAI,IAAI,CAAC;GAC/C,MAAM,MAAM,KAAK,KAAK,QACpB,OAAO,YACL,OAAO,QAAQ,GAAG,CAAC,CAAC,KAAK,CAAC,KAAK,WAAW,CACxC,KACA,iBAAiB,aAAa,MAAM,MAAM,CAAC,CAAC,SAAS,KACvD,CAAC,CACH,CACF;GACA,OAAO,EAAE,eAAe,IAAI;EAC9B,EACF;EACA,MAAM,YAAe,SAAuC;GAC1D,GAAG,KAAK,OAAO;GACf,IAAI;IACF,MAAM,SAAS,MAAM,QAAQ;IAC7B,GAAG,KAAK,QAAQ;IAChB,OAAO;GACT,SAAS,OAAO;IAGd,IAAI,CAAC,GAAG,eACN,MAAM,IAAI,MACR,2EACF;IAEF,GAAG,KAAK,UAAU;IAClB,MAAM;GACR;EACF;EACA,aAAa,GAAG,MAAM;CACxB;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@statewalker/db-sqlite-do",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Durable Object SQLite (ctx.storage) driver for @statewalker/db-api, with transactions through @statewalker/db-sqlite and a node:sqlite fake for tests.",
|
|
@@ -35,15 +35,15 @@
|
|
|
35
35
|
"src"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@statewalker/db-api": "^0.1.
|
|
39
|
-
"@statewalker/db-sqlite": "^0.1.
|
|
38
|
+
"@statewalker/db-api": "^0.1.5",
|
|
39
|
+
"@statewalker/db-sqlite": "^0.1.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/node": "^26.6.4",
|
|
43
43
|
"rimraf": "^6.1.3",
|
|
44
44
|
"tsdown": "^0.23.0",
|
|
45
45
|
"typescript": "^7.0.2",
|
|
46
|
-
"vitest": "^5.0.3",
|
|
46
|
+
"vitest": "^4.1.10 || ^5.0.3",
|
|
47
47
|
"@statewalker/db-tests": "^0.1.1"
|
|
48
48
|
},
|
|
49
49
|
"engines": {
|
package/src/do-sqlite-db.test.ts
CHANGED
|
@@ -46,6 +46,25 @@ describe("newFakeDoStorage", () => {
|
|
|
46
46
|
}
|
|
47
47
|
});
|
|
48
48
|
|
|
49
|
+
it("stores an empty BLOB as a BLOB through the driver (not NULL, on any Node)", async () => {
|
|
50
|
+
const db = newDoSqliteDb(newFakeDoStorage());
|
|
51
|
+
await db.exec("CREATE TABLE e (k INTEGER PRIMARY KEY, d BLOB)");
|
|
52
|
+
await db.query("INSERT INTO e VALUES (1, ?), (2, ?)", [
|
|
53
|
+
new Uint8Array(0),
|
|
54
|
+
new Uint8Array(new ArrayBuffer(0)),
|
|
55
|
+
]);
|
|
56
|
+
expect(await db.query("SELECT typeof(d) AS t, length(d) AS n FROM e ORDER BY k")).toEqual([
|
|
57
|
+
{ t: "blob", n: 0 },
|
|
58
|
+
{ t: "blob", n: 0 },
|
|
59
|
+
]);
|
|
60
|
+
const back = await db.query<{ d: unknown }>("SELECT d FROM e ORDER BY k");
|
|
61
|
+
for (const { d } of back) {
|
|
62
|
+
expect(d).toBeInstanceOf(Uint8Array);
|
|
63
|
+
expect((d as Uint8Array).byteLength).toBe(0);
|
|
64
|
+
}
|
|
65
|
+
await db.close();
|
|
66
|
+
});
|
|
67
|
+
|
|
49
68
|
it("rejects a typed array bound as a BLOB, as the driver must pass an ArrayBuffer", () => {
|
|
50
69
|
const storage = newFakeDoStorage();
|
|
51
70
|
try {
|
package/src/fake-do-storage.ts
CHANGED
|
@@ -3,6 +3,12 @@ import type { DoSqlStorage } from "./do-sqlite-db.js";
|
|
|
3
3
|
|
|
4
4
|
export type FakeDoStorage = DoSqlStorage & { close(): void };
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* An empty view over a real buffer. node:sqlite (Node 22) binds a view whose buffer has no backing
|
|
8
|
+
* store, such as `new Uint8Array(new ArrayBuffer(0))`, as NULL; the platform stores an empty BLOB.
|
|
9
|
+
*/
|
|
10
|
+
const EMPTY_BLOB = new Uint8Array(1).subarray(0, 0);
|
|
11
|
+
|
|
6
12
|
/** Limits probed on celld's DO SQLite (see test-do/platform-probe.ts). */
|
|
7
13
|
const MAX_BOUND_PARAMETERS = 100;
|
|
8
14
|
const MAX_VALUE_BYTES = 2_000_000;
|
|
@@ -24,7 +30,7 @@ export function newFakeDoStorage(): FakeDoStorage {
|
|
|
24
30
|
if (typeof value === "string" && Buffer.byteLength(value) > MAX_VALUE_BYTES) throw tooBig();
|
|
25
31
|
if (value instanceof ArrayBuffer) {
|
|
26
32
|
if (value.byteLength > MAX_VALUE_BYTES) throw tooBig();
|
|
27
|
-
return new Uint8Array(value);
|
|
33
|
+
return value.byteLength === 0 ? EMPTY_BLOB : new Uint8Array(value);
|
|
28
34
|
}
|
|
29
35
|
return value as SQLInputValue;
|
|
30
36
|
};
|