@objectstack/driver-sqlite-wasm 5.2.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/index.js ADDED
@@ -0,0 +1,422 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
+ Client_WasmSqlite: () => Client_WasmSqlite,
34
+ SqliteWasmDriver: () => SqliteWasmDriver,
35
+ WasmSqliteConnection: () => WasmSqliteConnection,
36
+ default: () => index_default
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+
40
+ // src/sqlite-wasm-driver.ts
41
+ var import_driver_sql = require("@objectstack/driver-sql");
42
+
43
+ // src/knex-wasm-dialect.ts
44
+ var import_node_module = require("module");
45
+
46
+ // src/wasm-connection.ts
47
+ var import_meta = {};
48
+ var WRITE_METHODS = /* @__PURE__ */ new Set([
49
+ "run",
50
+ "insert",
51
+ "update",
52
+ "del",
53
+ "counter"
54
+ ]);
55
+ async function tryLoadFs() {
56
+ try {
57
+ return await import("fs/promises");
58
+ } catch {
59
+ return null;
60
+ }
61
+ }
62
+ async function defaultLocateFile() {
63
+ try {
64
+ const { createRequire: createRequire2 } = await import("module");
65
+ const require2 = createRequire2(import_meta.url);
66
+ const pkgJsonPath = require2.resolve("sql.js/package.json");
67
+ const { dirname, join } = await import("path");
68
+ const dir = dirname(pkgJsonPath);
69
+ return (file) => join(dir, "dist", file);
70
+ } catch {
71
+ return void 0;
72
+ }
73
+ }
74
+ var cachedSqlJs = null;
75
+ async function loadSqlJs(locateFile) {
76
+ if (cachedSqlJs) return cachedSqlJs;
77
+ cachedSqlJs = (async () => {
78
+ const mod = await import("sql.js");
79
+ const initSqlJs = mod.default ?? mod;
80
+ const locator = locateFile ?? await defaultLocateFile();
81
+ const SQL = await initSqlJs(locator ? { locateFile: locator } : void 0);
82
+ return SQL;
83
+ })();
84
+ return cachedSqlJs;
85
+ }
86
+ var WasmSqliteConnection = class {
87
+ constructor(opts) {
88
+ this.fs = null;
89
+ this.dirty = false;
90
+ this.debounceMs = 0;
91
+ this.debounceTimer = null;
92
+ this.pendingFlush = null;
93
+ this.destroyed = false;
94
+ this.filename = opts.filename;
95
+ this.persist = opts.persist ?? "on-disconnect";
96
+ this.isEphemeral = this.filename === ":memory:" || this.filename.startsWith(":");
97
+ this.logger = opts.logger ?? console;
98
+ if (typeof this.persist === "string" && this.persist.startsWith("debounced:")) {
99
+ const ms = Number(this.persist.slice("debounced:".length));
100
+ this.debounceMs = Number.isFinite(ms) && ms > 0 ? ms : 250;
101
+ }
102
+ }
103
+ /** Open the underlying sql.js database, loading bytes from disk if any. */
104
+ async open(sqlJs, locateFile) {
105
+ const SQL = sqlJs ?? await loadSqlJs(locateFile);
106
+ if (this.isEphemeral) {
107
+ this.db = new SQL.Database();
108
+ return;
109
+ }
110
+ this.fs = await tryLoadFs();
111
+ if (!this.fs) {
112
+ this.logger.warn(
113
+ "[driver-sqlite-wasm] No node:fs available \u2014 falling back to in-memory database. Data will not be persisted across reloads."
114
+ );
115
+ this.db = new SQL.Database();
116
+ return;
117
+ }
118
+ const { dirname } = await import("path");
119
+ const dir = dirname(this.filename);
120
+ if (dir && dir !== ".") {
121
+ await this.fs.mkdir(dir, { recursive: true });
122
+ }
123
+ let bytes;
124
+ try {
125
+ const buf = await this.fs.readFile(this.filename);
126
+ bytes = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
127
+ } catch (e) {
128
+ if (e?.code !== "ENOENT") throw e;
129
+ }
130
+ this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();
131
+ }
132
+ /** Hint that a mutation just executed; schedule a flush if needed. */
133
+ markDirty(method) {
134
+ if (this.isEphemeral || !this.fs) return;
135
+ if (method && !WRITE_METHODS.has(method)) return;
136
+ this.dirty = true;
137
+ if (this.persist === "on-write") {
138
+ void this.flush();
139
+ return;
140
+ }
141
+ if (this.debounceMs > 0) {
142
+ if (this.debounceTimer) clearTimeout(this.debounceTimer);
143
+ this.debounceTimer = setTimeout(() => {
144
+ this.debounceTimer = null;
145
+ void this.flush();
146
+ }, this.debounceMs);
147
+ }
148
+ }
149
+ /** Force a write of the current database state to disk. */
150
+ async flush() {
151
+ if (this.isEphemeral || !this.fs || this.destroyed) return;
152
+ if (this.pendingFlush) {
153
+ await this.pendingFlush;
154
+ if (!this.dirty || this.destroyed) return;
155
+ }
156
+ if (!this.dirty) return;
157
+ this.pendingFlush = (async () => {
158
+ try {
159
+ this.dirty = false;
160
+ const exported = this.db.export();
161
+ await this.fs.writeFile(this.filename, Buffer.from(exported));
162
+ } catch (err) {
163
+ this.dirty = true;
164
+ throw err;
165
+ } finally {
166
+ this.pendingFlush = null;
167
+ }
168
+ })();
169
+ await this.pendingFlush;
170
+ if (this.dirty && !this.destroyed) {
171
+ await this.flush();
172
+ }
173
+ }
174
+ /** Close the database, flushing any pending writes first. */
175
+ async close() {
176
+ if (this.destroyed) return;
177
+ if (this.debounceTimer) {
178
+ clearTimeout(this.debounceTimer);
179
+ this.debounceTimer = null;
180
+ }
181
+ try {
182
+ await this.flush();
183
+ } finally {
184
+ this.destroyed = true;
185
+ try {
186
+ this.db.close();
187
+ } catch {
188
+ }
189
+ }
190
+ }
191
+ /** Access the raw sql.js database (for the Knex dialect). */
192
+ get raw() {
193
+ return this.db;
194
+ }
195
+ };
196
+
197
+ // src/knex-wasm-dialect.ts
198
+ var import_meta2 = {};
199
+ var cachedEsmRequire = null;
200
+ function getEsmRequire() {
201
+ if (cachedEsmRequire) return cachedEsmRequire;
202
+ const anchor = typeof import_meta2 !== "undefined" && import_meta2.url ? import_meta2.url : typeof __filename !== "undefined" ? __filename : process.cwd() + "/";
203
+ cachedEsmRequire = (0, import_node_module.createRequire)(anchor);
204
+ return cachedEsmRequire;
205
+ }
206
+ function formatBindings(bindings) {
207
+ if (!bindings) return [];
208
+ return bindings.map((b) => {
209
+ if (b instanceof Date) return b.valueOf();
210
+ if (typeof b === "boolean") return Number(b);
211
+ return b;
212
+ });
213
+ }
214
+ function isReadMethod(method, returning) {
215
+ if (method === "insert" || method === "update") return !!returning ? true : false;
216
+ if (method === "counter" || method === "del") return false;
217
+ return true;
218
+ }
219
+ function resolveKnexSqlite3Dialect() {
220
+ const g = globalThis;
221
+ if (typeof g.require === "function") {
222
+ try {
223
+ return g.require("knex/lib/dialects/sqlite3");
224
+ } catch {
225
+ }
226
+ }
227
+ return getEsmRequire()("knex/lib/dialects/sqlite3");
228
+ }
229
+ var cachedDialect = null;
230
+ function getClient_WasmSqlite() {
231
+ if (cachedDialect) return cachedDialect;
232
+ const Client_SQLite3 = resolveKnexSqlite3Dialect();
233
+ class Client_WasmSqlite2 extends Client_SQLite3 {
234
+ // sql.js has no shared "driver module" the way better-sqlite3 does. Knex
235
+ // only uses `this.driver` to construct connections, and we override
236
+ // `acquireRawConnection`, so a sentinel object is enough.
237
+ _driver() {
238
+ return { name: "sql.js" };
239
+ }
240
+ async acquireRawConnection() {
241
+ const settings = this.connectionSettings;
242
+ const conn = new WasmSqliteConnection({
243
+ filename: settings.filename,
244
+ persist: settings.persist,
245
+ sqlJs: settings.sqlJs,
246
+ locateFile: settings.locateFile,
247
+ logger: settings.logger
248
+ });
249
+ await conn.open(settings.sqlJs, settings.locateFile);
250
+ return conn;
251
+ }
252
+ async destroyRawConnection(connection) {
253
+ await connection.close();
254
+ }
255
+ async _query(connection, obj) {
256
+ if (!obj.sql) throw new Error("The query is empty");
257
+ if (!connection) throw new Error("No connection provided");
258
+ const db = connection.raw;
259
+ const bindings = formatBindings(obj.bindings);
260
+ const isDdl = /^\s*(CREATE|ALTER|DROP|BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|REINDEX|VACUUM|ATTACH|DETACH|TRUNCATE)\b/i.test(
261
+ obj.sql
262
+ );
263
+ if (isDdl) {
264
+ db.run(obj.sql, bindings);
265
+ obj.response = [];
266
+ connection.markDirty("run");
267
+ return obj;
268
+ }
269
+ if (isReadMethod(obj.method, obj.returning) || /^\s*PRAGMA\b/i.test(obj.sql)) {
270
+ const stmt = db.prepare(obj.sql);
271
+ try {
272
+ if (bindings.length) stmt.bind(bindings);
273
+ const rows = [];
274
+ while (stmt.step()) {
275
+ rows.push(stmt.getAsObject());
276
+ }
277
+ obj.response = rows;
278
+ } finally {
279
+ stmt.free();
280
+ }
281
+ return obj;
282
+ }
283
+ db.run(obj.sql, bindings);
284
+ const changes = db.getRowsModified();
285
+ let lastID = 0;
286
+ if (obj.method === "insert") {
287
+ const r = db.exec("SELECT last_insert_rowid() AS id");
288
+ lastID = r?.[0]?.values?.[0]?.[0] ?? 0;
289
+ }
290
+ obj.response = [];
291
+ obj.context = { lastID, changes };
292
+ connection.markDirty(obj.method);
293
+ return obj;
294
+ }
295
+ }
296
+ Object.assign(Client_WasmSqlite2.prototype, {
297
+ dialect: "sqlite3",
298
+ driverName: "wasm-sqlite"
299
+ });
300
+ cachedDialect = Client_WasmSqlite2;
301
+ return Client_WasmSqlite2;
302
+ }
303
+ var Client_WasmSqlite = new Proxy(function() {
304
+ }, {
305
+ get(_t, prop) {
306
+ return getClient_WasmSqlite()[prop];
307
+ },
308
+ construct(_t, args) {
309
+ const Klass = getClient_WasmSqlite();
310
+ return new Klass(...args);
311
+ },
312
+ apply(_t, thisArg, args) {
313
+ const Klass = getClient_WasmSqlite();
314
+ return Reflect.apply(Klass, thisArg, args);
315
+ }
316
+ });
317
+
318
+ // src/sqlite-wasm-driver.ts
319
+ var SqliteWasmDriver = class _SqliteWasmDriver extends import_driver_sql.SqlDriver {
320
+ constructor(config) {
321
+ const knexConfig = _SqliteWasmDriver.toKnexConfig(config);
322
+ super(knexConfig);
323
+ this.name = "com.objectstack.driver.sqlite-wasm";
324
+ this.version = "1.0.0";
325
+ this.beforeExitHandler = null;
326
+ this.wasmConfig = config;
327
+ if (config.logger) this.logger = config.logger;
328
+ }
329
+ /**
330
+ * Force the SQLite branch in {@link SqlDriver}. The base class detects
331
+ * SQLite by string-matching `config.client`, but we pass the dialect class
332
+ * directly so the string check would miss.
333
+ */
334
+ get isSqlite() {
335
+ return true;
336
+ }
337
+ /** Translate the public config into a Knex config that uses our dialect. */
338
+ static toKnexConfig(config) {
339
+ return {
340
+ // Knex accepts a Client class as `client`. The dialect's `driverName`
341
+ // is `'wasm-sqlite'` and its `dialect` is `'sqlite3'` so the SQLite
342
+ // query compiler is reused.
343
+ client: getClient_WasmSqlite(),
344
+ connection: {
345
+ filename: config.filename,
346
+ persist: config.persist,
347
+ sqlJs: config.sqlJs,
348
+ locateFile: config.locateFile,
349
+ logger: config.logger
350
+ },
351
+ // sql.js is single-threaded WASM — a single connection per pool keeps
352
+ // semantics consistent with the upstream SQLite dialect.
353
+ pool: config.pool ?? { min: 1, max: 1 },
354
+ useNullAsDefault: true
355
+ };
356
+ }
357
+ async connect() {
358
+ await super.connect();
359
+ if (this.wasmConfig.filename !== ":memory:" && !this.wasmConfig.filename.startsWith(":") && typeof process !== "undefined" && typeof process.once === "function") {
360
+ this.beforeExitHandler = () => {
361
+ void this.flush().catch(() => {
362
+ });
363
+ };
364
+ process.once("beforeExit", this.beforeExitHandler);
365
+ }
366
+ }
367
+ async disconnect() {
368
+ if (this.beforeExitHandler && typeof process !== "undefined") {
369
+ try {
370
+ process.removeListener("beforeExit", this.beforeExitHandler);
371
+ } catch {
372
+ }
373
+ this.beforeExitHandler = null;
374
+ }
375
+ await super.disconnect();
376
+ }
377
+ /**
378
+ * Force a flush of the in-memory database to disk. No-op for ephemeral
379
+ * databases or when no fs is available.
380
+ */
381
+ async flush() {
382
+ const knex = this.knex;
383
+ const client = knex?.client;
384
+ const pool = client?.pool;
385
+ if (!pool || typeof pool.numUsed !== "function") return;
386
+ const acquire = client.acquireConnection?.bind(client);
387
+ const release = client.releaseConnection?.bind(client);
388
+ if (!acquire || !release) return;
389
+ const conn = await acquire();
390
+ try {
391
+ if (conn && typeof conn.flush === "function") {
392
+ await conn.flush();
393
+ }
394
+ } finally {
395
+ await release(conn);
396
+ }
397
+ }
398
+ };
399
+
400
+ // src/index.ts
401
+ var index_default = {
402
+ id: "com.objectstack.driver.sqlite-wasm",
403
+ version: "1.0.0",
404
+ onEnable: async (context) => {
405
+ const { logger, config, drivers } = context;
406
+ logger?.info?.("[SQLite-WASM Driver] Initializing...");
407
+ if (drivers) {
408
+ const driver = new SqliteWasmDriver(config);
409
+ drivers.register(driver);
410
+ logger?.info?.(`[SQLite-WASM Driver] Registered driver: ${driver.name}`);
411
+ } else {
412
+ logger?.warn?.("[SQLite-WASM Driver] No driver registry found in context.");
413
+ }
414
+ }
415
+ };
416
+ // Annotate the CommonJS export names for ESM import in node:
417
+ 0 && (module.exports = {
418
+ Client_WasmSqlite,
419
+ SqliteWasmDriver,
420
+ WasmSqliteConnection
421
+ });
422
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/index.ts","../src/sqlite-wasm-driver.ts","../src/knex-wasm-dialect.ts","../src/wasm-connection.ts"],"sourcesContent":["// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\nimport { SqliteWasmDriver } from './sqlite-wasm-driver.js';\n\nexport { SqliteWasmDriver };\nexport type { SqliteWasmDriverConfig } from './sqlite-wasm-driver.js';\nexport { Client_WasmSqlite } from './knex-wasm-dialect.js';\nexport type { WasmSqliteConnectionSettings } from './knex-wasm-dialect.js';\nexport { WasmSqliteConnection } from './wasm-connection.js';\nexport type { PersistMode, WasmConnectionOptions } from './wasm-connection.js';\n\nexport default {\n id: 'com.objectstack.driver.sqlite-wasm',\n version: '1.0.0',\n\n onEnable: async (context: any) => {\n const { logger, config, drivers } = context;\n logger?.info?.('[SQLite-WASM Driver] Initializing...');\n\n if (drivers) {\n const driver = new SqliteWasmDriver(config);\n drivers.register(driver);\n logger?.info?.(`[SQLite-WASM Driver] Registered driver: ${driver.name}`);\n } else {\n logger?.warn?.('[SQLite-WASM Driver] No driver registry found in context.');\n }\n },\n};\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\n/**\n * SQLite-on-WASM driver for ObjectStack.\n *\n * Extends {@link SqlDriver} so all CRUD / schema / introspection / multi-tenant\n * logic is inherited as-is. Only the Knex transport is swapped to a custom\n * dialect ({@link Client_WasmSqlite}) backed by sql.js + Node `fs` persistence,\n * which lets the same `SqlDriver` codepath run inside StackBlitz WebContainer\n * (Node-in-browser) without the native `better-sqlite3` N-API binding.\n */\n\nimport type { SqlJsStatic } from 'sql.js';\nimport { SqlDriver, type SqlDriverConfig } from '@objectstack/driver-sql';\n\nimport { getClient_WasmSqlite } from './knex-wasm-dialect.js';\nimport type {\n PersistMode,\n WasmConnectionOptions,\n} from './wasm-connection.js';\n\n/** Public configuration for {@link SqliteWasmDriver}. */\nexport interface SqliteWasmDriverConfig {\n /**\n * SQLite filename. Use `:memory:` for an ephemeral database that is never\n * persisted. Any other value is treated as a Node `fs` path and the\n * sql.js database bytes are flushed back to disk according to {@link persist}.\n */\n filename: string;\n\n /**\n * Persistence strategy. Default: `'on-disconnect'`.\n *\n * - `'on-disconnect'` — flush once when the driver disconnects (and on\n * `process.beforeExit`).\n * - `'on-write'` — flush after every mutation. Safest, slowest.\n * - `` `debounced:${ms}` `` — debounce flushes by N milliseconds. Good\n * balance under bursty writes.\n */\n persist?: PersistMode;\n\n /** Pre-loaded sql.js module — skips lazy import. */\n sqlJs?: SqlJsStatic;\n\n /**\n * Override for sql.js's `locateFile`. Defaults to resolving the `.wasm`\n * file inside the installed `sql.js` package, which works in Node and\n * WebContainer.\n */\n locateFile?: (file: string) => string;\n\n /** Knex pool overrides. The dialect already defaults to `{ min: 1, max: 1 }`. */\n pool?: SqlDriverConfig['pool'];\n\n /** Optional logger. Defaults to `console`. */\n logger?: WasmConnectionOptions['logger'];\n}\n\n/**\n * SqlDriver subclass that runs Knex against sql.js (WASM SQLite).\n *\n * Behaves identically to the standard SQLite path — the dialect's\n * {@link Client_WasmSqlite._query} reports `lastID`/`changes` exactly the\n * way better-sqlite3 does, so {@link SqlDriver}'s SQL generation, returning\n * clauses, and schema introspection all keep working.\n */\nexport class SqliteWasmDriver extends SqlDriver {\n public override readonly name: string = 'com.objectstack.driver.sqlite-wasm';\n public override readonly version: string = '1.0.0';\n\n /**\n * Force the SQLite branch in {@link SqlDriver}. The base class detects\n * SQLite by string-matching `config.client`, but we pass the dialect class\n * directly so the string check would miss.\n */\n protected override get isSqlite(): boolean {\n return true;\n }\n\n private wasmConfig: SqliteWasmDriverConfig;\n private beforeExitHandler: (() => void) | null = null;\n\n constructor(config: SqliteWasmDriverConfig) {\n const knexConfig = SqliteWasmDriver.toKnexConfig(config);\n super(knexConfig);\n this.wasmConfig = config;\n if (config.logger) this.logger = config.logger as any;\n }\n\n /** Translate the public config into a Knex config that uses our dialect. */\n static toKnexConfig(config: SqliteWasmDriverConfig): SqlDriverConfig {\n return {\n // Knex accepts a Client class as `client`. The dialect's `driverName`\n // is `'wasm-sqlite'` and its `dialect` is `'sqlite3'` so the SQLite\n // query compiler is reused.\n client: getClient_WasmSqlite() as any,\n connection: {\n filename: config.filename,\n persist: config.persist,\n sqlJs: config.sqlJs,\n locateFile: config.locateFile,\n logger: config.logger,\n } as any,\n // sql.js is single-threaded WASM — a single connection per pool keeps\n // semantics consistent with the upstream SQLite dialect.\n pool: config.pool ?? { min: 1, max: 1 },\n useNullAsDefault: true,\n } as SqlDriverConfig;\n }\n\n override async connect(): Promise<void> {\n await super.connect();\n\n // Best-effort flush on process exit so `on-disconnect` mode still saves\n // user data if the host process is shut down without explicit cleanup.\n if (\n this.wasmConfig.filename !== ':memory:' &&\n !this.wasmConfig.filename.startsWith(':') &&\n typeof process !== 'undefined' &&\n typeof process.once === 'function'\n ) {\n this.beforeExitHandler = () => {\n // Fire-and-forget — beforeExit cannot await.\n void this.flush().catch(() => {\n /* ignore */\n });\n };\n process.once('beforeExit', this.beforeExitHandler);\n }\n }\n\n override async disconnect(): Promise<void> {\n if (this.beforeExitHandler && typeof process !== 'undefined') {\n try {\n process.removeListener('beforeExit', this.beforeExitHandler);\n } catch {\n /* ignore */\n }\n this.beforeExitHandler = null;\n }\n await super.disconnect();\n }\n\n /**\n * Force a flush of the in-memory database to disk. No-op for ephemeral\n * databases or when no fs is available.\n */\n async flush(): Promise<void> {\n // Reach into the Knex pool and ask every live connection to flush.\n const knex = (this as any).knex;\n const client = knex?.client;\n const pool = client?.pool;\n if (!pool || typeof pool.numUsed !== 'function') return;\n\n const acquire = client.acquireConnection?.bind(client);\n const release = client.releaseConnection?.bind(client);\n if (!acquire || !release) return;\n\n const conn = await acquire();\n try {\n if (conn && typeof conn.flush === 'function') {\n await conn.flush();\n }\n } finally {\n await release(conn);\n }\n }\n}\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\n/**\n * Custom Knex SQLite dialect backed by sql.js (WASM SQLite).\n *\n * Mimics the surface that `Client_BetterSQLite3` presents to Knex so the\n * upstream SQLite3 dialect's query compiler, schema builder, and column\n * compiler all keep working unchanged. Only the transport layer —\n * `_driver` / `acquireRawConnection` / `_query` — is swapped out.\n *\n * ## Why the dialect class is built lazily\n *\n * The class `Client_WasmSqlite extends Client_SQLite3` needs the upstream\n * SQLite3 dialect at class-definition time. Resolving it at module\n * top-level breaks when this file is re-bundled by another tsup/esbuild\n * pass (e.g. `packages/runtime`), because that pass rewrites our runtime\n * `createRequire(import.meta.url)` chain back into a static `__require2`\n * Proxy stub that throws `Dynamic require of \"X\" is not supported`.\n *\n * Building the class inside a lazy factory (`getClient_WasmSqlite()`)\n * keeps the `require` call out of module-init code, so the re-bundler\n * cannot intercept it.\n */\n\nimport { createRequire } from 'node:module';\n\nimport type { SqlJsStatic } from 'sql.js';\n\nimport {\n WasmSqliteConnection,\n type PersistMode,\n type WasmConnectionOptions,\n} from './wasm-connection.js';\n\n// Built lazily — `node:module` is a Node builtin and is left untouched\n// by esbuild/tsup, so the `createRequire` import survives downstream\n// re-bundling. We defer the actual `createRequire(...)` call so that the\n// CJS build (where `import.meta.url` is empty) doesn't blow up at module\n// init; the CJS path uses `globalThis.require` directly anyway.\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nlet cachedEsmRequire: any = null;\nfunction getEsmRequire(): any {\n if (cachedEsmRequire) return cachedEsmRequire;\n // `import.meta.url` is replaced with an empty string in CJS output;\n // fall back to the current file/cwd in that case.\n const anchor =\n typeof import.meta !== 'undefined' && (import.meta as any).url\n ? (import.meta as any).url\n : typeof __filename !== 'undefined'\n ? __filename\n : process.cwd() + '/';\n cachedEsmRequire = createRequire(anchor);\n return cachedEsmRequire;\n}\n\n/** Connection settings recognised by the WASM SQLite dialect. */\nexport interface WasmSqliteConnectionSettings {\n filename: string;\n persist?: PersistMode;\n sqlJs?: SqlJsStatic;\n locateFile?: (file: string) => string;\n logger?: WasmConnectionOptions['logger'];\n}\n\n/**\n * Coerce JS values that sql.js cannot bind directly. Mirrors\n * `Client_BetterSQLite3._formatBindings`.\n */\nfunction formatBindings(bindings: unknown[] | undefined): unknown[] {\n if (!bindings) return [];\n return bindings.map((b) => {\n if (b instanceof Date) return b.valueOf();\n if (typeof b === 'boolean') return Number(b);\n return b;\n });\n}\n\n/**\n * Mirrors the dispatch in upstream `Client_SQLite3._query`: only\n * `insert/update/counter/del` go through the row-less write path (and even\n * those switch to the read path when a `RETURNING` clause is requested).\n * Everything else — `select`, `first`, `pluck`, `columnInfo`, raw PRAGMA,\n * DDL with no `method` — is read with `all`/row iteration so Knex sees the\n * same response shape it would from better-sqlite3.\n */\nfunction isReadMethod(method?: string, returning?: unknown): boolean {\n if (method === 'insert' || method === 'update') return !!returning ? true : false;\n if (method === 'counter' || method === 'del') return false;\n return true;\n}\n\n/**\n * Resolve the upstream `knex/lib/dialects/sqlite3` class at runtime.\n *\n * Tries every escape hatch we have so that this works in:\n * - Plain Node ESM (use `createRequire(import.meta.url)`).\n * - Plain Node CJS (use the ambient `require` on `globalThis`).\n * - Re-bundled ESM where esbuild/tsup has stubbed `__require` — we\n * fall back to `new Function('return require')()` which evades static\n * analysis and grabs the real Node `require` at runtime.\n *\n * Wrapped in a function so the bundler cannot execute it at module init.\n */\nfunction resolveKnexSqlite3Dialect(): any {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const g = globalThis as any;\n if (typeof g.require === 'function') {\n try {\n return g.require('knex/lib/dialects/sqlite3');\n } catch {\n /* fall through */\n }\n }\n // ESM-safe path: `createRequire` was imported statically at the top of\n // this module from `node:module`. In a pure-ESM process there is no\n // ambient `require`, so this is the only reliable way to load a CJS\n // package like `knex/lib/dialects/sqlite3`.\n return getEsmRequire()('knex/lib/dialects/sqlite3');\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nlet cachedDialect: any = null;\n\n/**\n * Build (and cache) the `Client_WasmSqlite` class. Building lazily keeps\n * the `require('knex/lib/dialects/sqlite3')` call out of module-init\n * code so downstream re-bundlers (e.g. `packages/runtime`) cannot collapse\n * it into a Dynamic-require stub.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function getClient_WasmSqlite(): any {\n if (cachedDialect) return cachedDialect;\n const Client_SQLite3 = resolveKnexSqlite3Dialect();\n\n class Client_WasmSqlite extends Client_SQLite3 {\n // sql.js has no shared \"driver module\" the way better-sqlite3 does. Knex\n // only uses `this.driver` to construct connections, and we override\n // `acquireRawConnection`, so a sentinel object is enough.\n _driver(): { name: 'sql.js' } {\n return { name: 'sql.js' };\n }\n\n async acquireRawConnection(): Promise<WasmSqliteConnection> {\n const settings = (this as any)\n .connectionSettings as WasmSqliteConnectionSettings;\n\n const conn = new WasmSqliteConnection({\n filename: settings.filename,\n persist: settings.persist,\n sqlJs: settings.sqlJs,\n locateFile: settings.locateFile,\n logger: settings.logger,\n });\n await conn.open(settings.sqlJs, settings.locateFile);\n return conn;\n }\n\n async destroyRawConnection(connection: WasmSqliteConnection): Promise<void> {\n await connection.close();\n }\n\n async _query(\n connection: WasmSqliteConnection,\n obj: any,\n ): Promise<any> {\n if (!obj.sql) throw new Error('The query is empty');\n if (!connection) throw new Error('No connection provided');\n\n const db = connection.raw;\n const bindings = formatBindings(obj.bindings);\n\n // DDL / transactional control statements have no Knex `method`. sql.js's\n // `prepare`+`step` silently no-ops on many of these (e.g. CREATE TABLE),\n // so route them through `run` which is implemented via `exec` and\n // actually mutates the database. PRAGMA is intentionally excluded — many\n // PRAGMA forms (e.g. `PRAGMA table_info(...)`, `foreign_key_list(...)`)\n // return rows used by Knex's schema introspection/columnInfo, and\n // `db.run` discards those rows.\n const isDdl =\n /^\\s*(CREATE|ALTER|DROP|BEGIN|COMMIT|ROLLBACK|SAVEPOINT|RELEASE|REINDEX|VACUUM|ATTACH|DETACH|TRUNCATE)\\b/i.test(\n obj.sql,\n );\n if (isDdl) {\n db.run(obj.sql, bindings as any);\n obj.response = [];\n connection.markDirty('run');\n return obj;\n }\n\n if (isReadMethod(obj.method, obj.returning) || /^\\s*PRAGMA\\b/i.test(obj.sql)) {\n const stmt = db.prepare(obj.sql);\n try {\n if (bindings.length) stmt.bind(bindings as any);\n const rows: Record<string, unknown>[] = [];\n while (stmt.step()) {\n rows.push(stmt.getAsObject());\n }\n obj.response = rows;\n } finally {\n stmt.free();\n }\n return obj;\n }\n\n // Write path: execute via `run` (no row iteration needed) and capture\n // SQLite's per-connection lastID / changes counters.\n db.run(obj.sql, bindings as any);\n const changes = db.getRowsModified();\n let lastID: number | bigint = 0;\n if (obj.method === 'insert') {\n const r = db.exec('SELECT last_insert_rowid() AS id');\n lastID = (r?.[0]?.values?.[0]?.[0] as number) ?? 0;\n }\n obj.response = [];\n obj.context = { lastID, changes };\n connection.markDirty(obj.method);\n return obj;\n }\n }\n\n Object.assign(Client_WasmSqlite.prototype, {\n dialect: 'sqlite3',\n driverName: 'wasm-sqlite',\n });\n\n cachedDialect = Client_WasmSqlite;\n return Client_WasmSqlite;\n}\n\n/**\n * Back-compat re-export. Prefer `getClient_WasmSqlite()` so the dialect\n * is resolved lazily; the named export triggers the factory on first\n * access of any static property.\n *\n * Note: importing this binding will execute the factory at import time\n * in some bundlers, which defeats the lazy pattern. New code should call\n * `getClient_WasmSqlite()` directly.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const Client_WasmSqlite: any = new Proxy(function () {} as any, {\n get(_t, prop) {\n return (getClient_WasmSqlite() as any)[prop];\n },\n construct(_t, args) {\n const Klass = getClient_WasmSqlite();\n return new Klass(...args);\n },\n apply(_t, thisArg, args) {\n const Klass = getClient_WasmSqlite();\n return Reflect.apply(Klass, thisArg, args);\n },\n});\n\nexport default Client_WasmSqlite;\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\n/**\n * Thin wrapper over sql.js {@link Database} that mimics the surface of\n * `better-sqlite3`'s `Database` (only the methods the Knex dialect uses).\n *\n * Persistence is handled here, not in the Knex dialect, so it can be\n * orchestrated per-connection without polluting the SQL execution path.\n */\n\nimport type { Database, SqlJsStatic } from 'sql.js';\n\n/** When to flush the in-memory WASM database to disk. */\nexport type PersistMode =\n | 'on-disconnect'\n | 'on-write'\n | `debounced:${number}`;\n\nexport interface WasmConnectionOptions {\n /**\n * On-disk file path. `:memory:` (or any value starting with `:`) skips\n * persistence entirely and the database lives only for the process.\n */\n filename: string;\n /** When to persist. Default: `on-disconnect`. */\n persist?: PersistMode;\n /** Pre-loaded sql.js module. If omitted, loaded lazily on first connect. */\n sqlJs?: SqlJsStatic;\n /**\n * Optional override for the `.wasm` locator passed to `initSqlJs()`.\n * Defaults to resolving the file from the `sql.js` package on disk\n * (works in Node and WebContainer).\n */\n locateFile?: (file: string) => string;\n /** Optional logger; defaults to `console`. */\n logger?: { warn: (msg: string, meta?: unknown) => void };\n}\n\n/** Mutation method names that should trigger a persistence cycle. */\nconst WRITE_METHODS = new Set([\n 'run',\n 'insert',\n 'update',\n 'del',\n 'counter',\n]);\n\n/**\n * Detect whether a Node-style `fs` module is available. WebContainer\n * (StackBlitz) provides Node `fs`; pure-browser environments do not.\n */\nasync function tryLoadFs(): Promise<typeof import('node:fs/promises') | null> {\n try {\n return await import('node:fs/promises');\n } catch {\n return null;\n }\n}\n\n/**\n * Resolve a default sql.js WASM locator. We point sql.js at the `.wasm`\n * file shipped inside `sql.js`'s own `dist/` folder. This avoids requiring\n * the caller to host the WASM separately.\n */\nasync function defaultLocateFile(): Promise<((file: string) => string) | undefined> {\n try {\n const { createRequire } = await import('node:module');\n const require = createRequire(import.meta.url);\n const pkgJsonPath = require.resolve('sql.js/package.json');\n const { dirname, join } = await import('node:path');\n const dir = dirname(pkgJsonPath);\n return (file: string) => join(dir, 'dist', file);\n } catch {\n return undefined;\n }\n}\n\nlet cachedSqlJs: Promise<SqlJsStatic> | null = null;\n\nasync function loadSqlJs(\n locateFile?: (file: string) => string,\n): Promise<SqlJsStatic> {\n if (cachedSqlJs) return cachedSqlJs;\n cachedSqlJs = (async () => {\n const mod = await import('sql.js');\n const initSqlJs = (mod as any).default ?? (mod as any);\n const locator = locateFile ?? (await defaultLocateFile());\n const SQL = await initSqlJs(locator ? { locateFile: locator } : undefined);\n return SQL as SqlJsStatic;\n })();\n return cachedSqlJs;\n}\n\n/**\n * A sql.js-backed connection that exposes the `prepare`/`exec`/`close`\n * subset used by Knex's SQLite dialect. Mutations are queued through a\n * configurable persistence strategy so the on-disk file stays in sync.\n */\nexport class WasmSqliteConnection {\n readonly filename: string;\n readonly persist: PersistMode;\n readonly isEphemeral: boolean;\n\n private db!: Database;\n private fs: typeof import('node:fs/promises') | null = null;\n private dirty = false;\n private debounceMs = 0;\n private debounceTimer: ReturnType<typeof setTimeout> | null = null;\n private pendingFlush: Promise<void> | null = null;\n private destroyed = false;\n private logger: { warn: (msg: string, meta?: unknown) => void };\n\n constructor(opts: WasmConnectionOptions) {\n this.filename = opts.filename;\n this.persist = opts.persist ?? 'on-disconnect';\n this.isEphemeral =\n this.filename === ':memory:' || this.filename.startsWith(':');\n this.logger = opts.logger ?? console;\n\n if (typeof this.persist === 'string' && this.persist.startsWith('debounced:')) {\n const ms = Number(this.persist.slice('debounced:'.length));\n this.debounceMs = Number.isFinite(ms) && ms > 0 ? ms : 250;\n }\n }\n\n /** Open the underlying sql.js database, loading bytes from disk if any. */\n async open(sqlJs?: SqlJsStatic, locateFile?: (file: string) => string): Promise<void> {\n const SQL = sqlJs ?? (await loadSqlJs(locateFile));\n\n if (this.isEphemeral) {\n this.db = new SQL.Database();\n return;\n }\n\n this.fs = await tryLoadFs();\n if (!this.fs) {\n this.logger.warn(\n '[driver-sqlite-wasm] No node:fs available — falling back to in-memory database. ' +\n 'Data will not be persisted across reloads.',\n );\n this.db = new SQL.Database();\n return;\n }\n\n // Ensure parent directory exists, then load bytes if the file exists.\n const { dirname } = await import('node:path');\n const dir = dirname(this.filename);\n if (dir && dir !== '.') {\n await this.fs.mkdir(dir, { recursive: true });\n }\n\n let bytes: Uint8Array | undefined;\n try {\n const buf = await this.fs.readFile(this.filename);\n bytes = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);\n } catch (e: any) {\n if (e?.code !== 'ENOENT') throw e;\n }\n\n this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();\n }\n\n /** Hint that a mutation just executed; schedule a flush if needed. */\n markDirty(method?: string): void {\n if (this.isEphemeral || !this.fs) return;\n if (method && !WRITE_METHODS.has(method)) return;\n this.dirty = true;\n\n if (this.persist === 'on-write') {\n void this.flush();\n return;\n }\n if (this.debounceMs > 0) {\n if (this.debounceTimer) clearTimeout(this.debounceTimer);\n this.debounceTimer = setTimeout(() => {\n this.debounceTimer = null;\n void this.flush();\n }, this.debounceMs);\n }\n // 'on-disconnect' → flush only at close()\n }\n\n /** Force a write of the current database state to disk. */\n async flush(): Promise<void> {\n if (this.isEphemeral || !this.fs || this.destroyed) return;\n // If a flush is already in flight, wait for it and then re-flush so any\n // writes that arrived after the in-flight flush's `db.export()` call get\n // persisted too. Without this, on-write mode loses writes that happen\n // between a flush's synchronous export and its async file write.\n if (this.pendingFlush) {\n await this.pendingFlush;\n if (!this.dirty || this.destroyed) return;\n }\n if (!this.dirty) return;\n\n this.pendingFlush = (async () => {\n try {\n // Snapshot dirty=false BEFORE export so concurrent writes that occur\n // during the async writeFile mark us dirty again and trigger another\n // flush via markDirty.\n this.dirty = false;\n const exported = this.db.export();\n // sql.js returns a Uint8Array; Buffer.from on it shares memory but\n // works fine for writeFile.\n await this.fs!.writeFile(this.filename, Buffer.from(exported));\n } catch (err) {\n // Restore dirty state so a subsequent flush retries.\n this.dirty = true;\n throw err;\n } finally {\n this.pendingFlush = null;\n }\n })();\n\n await this.pendingFlush;\n\n // A write that arrived after `db.export()` (synchronous) but before the\n // file write completed will have set dirty=true again. Re-flush to\n // persist it.\n if (this.dirty && !this.destroyed) {\n await this.flush();\n }\n }\n\n /** Close the database, flushing any pending writes first. */\n async close(): Promise<void> {\n if (this.destroyed) return;\n if (this.debounceTimer) {\n clearTimeout(this.debounceTimer);\n this.debounceTimer = null;\n }\n try {\n await this.flush();\n } finally {\n this.destroyed = true;\n try {\n this.db.close();\n } catch {\n /* ignore */\n }\n }\n }\n\n /** Access the raw sql.js database (for the Knex dialect). */\n get raw(): Database {\n return this.db;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaA,wBAAgD;;;ACWhD,yBAA8B;;;ACxB9B;AAuCA,IAAM,gBAAgB,oBAAI,IAAI;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAMD,eAAe,YAA+D;AAC5E,MAAI;AACF,WAAO,MAAM,OAAO,aAAkB;AAAA,EACxC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAOA,eAAe,oBAAqE;AAClF,MAAI;AACF,UAAM,EAAE,eAAAA,eAAc,IAAI,MAAM,OAAO,QAAa;AACpD,UAAMC,WAAUD,eAAc,YAAY,GAAG;AAC7C,UAAM,cAAcC,SAAQ,QAAQ,qBAAqB;AACzD,UAAM,EAAE,SAAS,KAAK,IAAI,MAAM,OAAO,MAAW;AAClD,UAAM,MAAM,QAAQ,WAAW;AAC/B,WAAO,CAAC,SAAiB,KAAK,KAAK,QAAQ,IAAI;AAAA,EACjD,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,IAAI,cAA2C;AAE/C,eAAe,UACb,YACsB;AACtB,MAAI,YAAa,QAAO;AACxB,iBAAe,YAAY;AACzB,UAAM,MAAM,MAAM,OAAO,QAAQ;AACjC,UAAM,YAAa,IAAY,WAAY;AAC3C,UAAM,UAAU,cAAe,MAAM,kBAAkB;AACvD,UAAM,MAAM,MAAM,UAAU,UAAU,EAAE,YAAY,QAAQ,IAAI,MAAS;AACzE,WAAO;AAAA,EACT,GAAG;AACH,SAAO;AACT;AA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