@objectstack/driver-sqlite-wasm 14.7.0 → 14.8.0

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.d.mts CHANGED
@@ -38,6 +38,12 @@ interface WasmConnectionOptions {
38
38
  * configurable persistence strategy so the on-disk file stays in sync.
39
39
  */
40
40
  declare class WasmSqliteConnection {
41
+ /**
42
+ * Process-wide counter making each atomic-write temp filename unique, so
43
+ * concurrent connections (or overlapping flushes) never target the same
44
+ * temp path. Combined with `process.pid` for cross-process uniqueness.
45
+ */
46
+ private static tmpSeq;
41
47
  readonly filename: string;
42
48
  readonly persist: PersistMode;
43
49
  readonly isEphemeral: boolean;
@@ -68,6 +74,20 @@ declare class WasmSqliteConnection {
68
74
  constructor(opts: WasmConnectionOptions);
69
75
  /** Open the underlying sql.js database, loading bytes from disk if any. */
70
76
  open(sqlJs?: SqlJsStatic, locateFile?: (file: string) => string): Promise<void>;
77
+ /**
78
+ * Force sql.js to actually read a page so a malformed image surfaces now
79
+ * rather than on the first business query. `PRAGMA quick_check` walks the
80
+ * b-tree structure without the full-scan cost of `integrity_check`; a healthy
81
+ * database returns a single `ok` row. Any thrown error (raw string or Error)
82
+ * or a non-`ok` result is treated as corruption.
83
+ */
84
+ private assertReadable;
85
+ /**
86
+ * Move a corrupt database file aside so its bytes are preserved for
87
+ * post-mortem while a fresh, empty database takes its place. Best-effort:
88
+ * failures here must not prevent the server from booting on a clean DB.
89
+ */
90
+ private quarantineCorruptFile;
71
91
  /**
72
92
  * Update transaction state from a transaction-control statement and, when a
73
93
  * transaction has just fully closed, run any flush that was deferred while
@@ -94,6 +114,20 @@ declare class WasmSqliteConnection {
94
114
  * write that arrived mid-flush is captured by the next queued step.
95
115
  */
96
116
  flush(): Promise<void>;
117
+ /**
118
+ * Write the database bytes to disk atomically: write to a sibling temp file,
119
+ * fsync it, then `rename()` it over the target.
120
+ *
121
+ * A plain `writeFile(this.filename, …)` truncates the target and streams the
122
+ * new bytes in place, so a process killed mid-write (a dev-server restart,
123
+ * Ctrl-C, or crash — likely under `on-write`, where every dispatcher tick
124
+ * flushes) leaves a half-written file. sql.js then rejects that file on the
125
+ * next boot with "database disk image is malformed". `rename(2)` is atomic
126
+ * within a filesystem, so a reader always sees either the complete old file
127
+ * or the complete new one — never a torn image. The temp file lives in the
128
+ * same directory as the target so the rename stays intra-filesystem.
129
+ */
130
+ private atomicWriteFile;
97
131
  /** Close the database, flushing any pending writes first. */
98
132
  close(): Promise<void>;
99
133
  /** Access the raw sql.js database (for the Knex dialect). */
package/dist/index.d.ts CHANGED
@@ -38,6 +38,12 @@ interface WasmConnectionOptions {
38
38
  * configurable persistence strategy so the on-disk file stays in sync.
39
39
  */
40
40
  declare class WasmSqliteConnection {
41
+ /**
42
+ * Process-wide counter making each atomic-write temp filename unique, so
43
+ * concurrent connections (or overlapping flushes) never target the same
44
+ * temp path. Combined with `process.pid` for cross-process uniqueness.
45
+ */
46
+ private static tmpSeq;
41
47
  readonly filename: string;
42
48
  readonly persist: PersistMode;
43
49
  readonly isEphemeral: boolean;
@@ -68,6 +74,20 @@ declare class WasmSqliteConnection {
68
74
  constructor(opts: WasmConnectionOptions);
69
75
  /** Open the underlying sql.js database, loading bytes from disk if any. */
70
76
  open(sqlJs?: SqlJsStatic, locateFile?: (file: string) => string): Promise<void>;
77
+ /**
78
+ * Force sql.js to actually read a page so a malformed image surfaces now
79
+ * rather than on the first business query. `PRAGMA quick_check` walks the
80
+ * b-tree structure without the full-scan cost of `integrity_check`; a healthy
81
+ * database returns a single `ok` row. Any thrown error (raw string or Error)
82
+ * or a non-`ok` result is treated as corruption.
83
+ */
84
+ private assertReadable;
85
+ /**
86
+ * Move a corrupt database file aside so its bytes are preserved for
87
+ * post-mortem while a fresh, empty database takes its place. Best-effort:
88
+ * failures here must not prevent the server from booting on a clean DB.
89
+ */
90
+ private quarantineCorruptFile;
71
91
  /**
72
92
  * Update transaction state from a transaction-control statement and, when a
73
93
  * transaction has just fully closed, run any flush that was deferred while
@@ -94,6 +114,20 @@ declare class WasmSqliteConnection {
94
114
  * write that arrived mid-flush is captured by the next queued step.
95
115
  */
96
116
  flush(): Promise<void>;
117
+ /**
118
+ * Write the database bytes to disk atomically: write to a sibling temp file,
119
+ * fsync it, then `rename()` it over the target.
120
+ *
121
+ * A plain `writeFile(this.filename, …)` truncates the target and streams the
122
+ * new bytes in place, so a process killed mid-write (a dev-server restart,
123
+ * Ctrl-C, or crash — likely under `on-write`, where every dispatcher tick
124
+ * flushes) leaves a half-written file. sql.js then rejects that file on the
125
+ * next boot with "database disk image is malformed". `rename(2)` is atomic
126
+ * within a filesystem, so a reader always sees either the complete old file
127
+ * or the complete new one — never a torn image. The temp file lives in the
128
+ * same directory as the target so the rename stays intra-filesystem.
129
+ */
130
+ private atomicWriteFile;
97
131
  /** Close the database, flushing any pending writes first. */
98
132
  close(): Promise<void>;
99
133
  /** Access the raw sql.js database (for the Knex dialect). */
package/dist/index.js CHANGED
@@ -83,7 +83,7 @@ async function loadSqlJs(locateFile) {
83
83
  })();
84
84
  return cachedSqlJs;
85
85
  }
86
- var WasmSqliteConnection = class {
86
+ var _WasmSqliteConnection = class _WasmSqliteConnection {
87
87
  constructor(opts) {
88
88
  this.fs = null;
89
89
  this.dirty = false;
@@ -145,7 +145,52 @@ var WasmSqliteConnection = class {
145
145
  } catch (e) {
146
146
  if (e?.code !== "ENOENT") throw e;
147
147
  }
148
- this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();
148
+ if (!bytes) {
149
+ this.db = new SQL.Database();
150
+ return;
151
+ }
152
+ try {
153
+ const candidate = new SQL.Database(bytes);
154
+ this.assertReadable(candidate);
155
+ this.db = candidate;
156
+ } catch (err) {
157
+ await this.quarantineCorruptFile(err);
158
+ this.db = new SQL.Database();
159
+ }
160
+ }
161
+ /**
162
+ * Force sql.js to actually read a page so a malformed image surfaces now
163
+ * rather than on the first business query. `PRAGMA quick_check` walks the
164
+ * b-tree structure without the full-scan cost of `integrity_check`; a healthy
165
+ * database returns a single `ok` row. Any thrown error (raw string or Error)
166
+ * or a non-`ok` result is treated as corruption.
167
+ */
168
+ assertReadable(db) {
169
+ const res = db.exec("PRAGMA quick_check(1)");
170
+ const first = res?.[0]?.values?.[0]?.[0];
171
+ if (typeof first === "string" && first.toLowerCase() !== "ok") {
172
+ throw new Error(`sqlite quick_check failed: ${first}`);
173
+ }
174
+ }
175
+ /**
176
+ * Move a corrupt database file aside so its bytes are preserved for
177
+ * post-mortem while a fresh, empty database takes its place. Best-effort:
178
+ * failures here must not prevent the server from booting on a clean DB.
179
+ */
180
+ async quarantineCorruptFile(cause) {
181
+ if (!this.fs) return;
182
+ const reason = typeof cause === "string" ? cause : cause?.message ?? String(cause);
183
+ const backup = `${this.filename}.corrupt-${Date.now()}`;
184
+ try {
185
+ await this.fs.rename(this.filename, backup);
186
+ this.logger.warn(
187
+ `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt (${reason}). Quarantined to ${backup} and starting from an empty database so the server can boot.`
188
+ );
189
+ } catch (renameErr) {
190
+ this.logger.warn(
191
+ `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt (${reason}) and could not be quarantined (${String(renameErr)}). Starting from an empty database; the corrupt file will be overwritten on the next flush.`
192
+ );
193
+ }
149
194
  }
150
195
  /**
151
196
  * Update transaction state from a transaction-control statement and, when a
@@ -219,7 +264,7 @@ var WasmSqliteConnection = class {
219
264
  this.dirty = false;
220
265
  try {
221
266
  const exported = this.db.export();
222
- await this.fs.writeFile(this.filename, Buffer.from(exported));
267
+ await this.atomicWriteFile(Buffer.from(exported));
223
268
  } catch (err) {
224
269
  this.dirty = true;
225
270
  throw err;
@@ -229,6 +274,44 @@ var WasmSqliteConnection = class {
229
274
  });
230
275
  await step;
231
276
  }
277
+ /**
278
+ * Write the database bytes to disk atomically: write to a sibling temp file,
279
+ * fsync it, then `rename()` it over the target.
280
+ *
281
+ * A plain `writeFile(this.filename, …)` truncates the target and streams the
282
+ * new bytes in place, so a process killed mid-write (a dev-server restart,
283
+ * Ctrl-C, or crash — likely under `on-write`, where every dispatcher tick
284
+ * flushes) leaves a half-written file. sql.js then rejects that file on the
285
+ * next boot with "database disk image is malformed". `rename(2)` is atomic
286
+ * within a filesystem, so a reader always sees either the complete old file
287
+ * or the complete new one — never a torn image. The temp file lives in the
288
+ * same directory as the target so the rename stays intra-filesystem.
289
+ */
290
+ async atomicWriteFile(data) {
291
+ if (!this.fs) return;
292
+ const tmp = `${this.filename}.tmp-${process.pid}-${_WasmSqliteConnection.tmpSeq += 1}`;
293
+ let handle;
294
+ try {
295
+ handle = await this.fs.open(tmp, "w");
296
+ await handle.writeFile(data);
297
+ await handle.sync();
298
+ await handle.close();
299
+ handle = void 0;
300
+ await this.fs.rename(tmp, this.filename);
301
+ } catch (err) {
302
+ if (handle) {
303
+ try {
304
+ await handle.close();
305
+ } catch {
306
+ }
307
+ }
308
+ try {
309
+ await this.fs.unlink(tmp);
310
+ } catch {
311
+ }
312
+ throw err;
313
+ }
314
+ }
232
315
  /** Close the database, flushing any pending writes first. */
233
316
  async close() {
234
317
  if (this.destroyed) return;
@@ -253,6 +336,13 @@ var WasmSqliteConnection = class {
253
336
  return this.db;
254
337
  }
255
338
  };
339
+ /**
340
+ * Process-wide counter making each atomic-write temp filename unique, so
341
+ * concurrent connections (or overlapping flushes) never target the same
342
+ * temp path. Combined with `process.pid` for cross-process uniqueness.
343
+ */
344
+ _WasmSqliteConnection.tmpSeq = 0;
345
+ var WasmSqliteConnection = _WasmSqliteConnection;
256
346
 
257
347
  // src/knex-wasm-dialect.ts
258
348
  var import_meta2 = {};
@@ -266,6 +356,7 @@ function getEsmRequire() {
266
356
  function formatBindings(bindings) {
267
357
  if (!bindings) return [];
268
358
  return bindings.map((b) => {
359
+ if (b === void 0) return null;
269
360
  if (b instanceof Date) return b.valueOf();
270
361
  if (typeof b === "boolean") return Number(b);
271
362
  return b;
package/dist/index.js.map CHANGED
@@ -1 +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 // Transaction-control statements are routed through\n // `noteTransactionControl`, which owns flushing for the transaction\n // lifecycle: it suppresses flushes while a transaction is open (sql.js\n // `export()` closes+reopens the db, which would abort the txn) and\n // performs a single flush once the transaction fully closes. Routing\n // them away from `markDirty` avoids a second, racing flush on COMMIT.\n if (/^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE)\\b/i.test(obj.sql)) {\n connection.noteTransactionControl(obj.sql);\n } else {\n connection.markDirty('run');\n }\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 flushChain: Promise<void> | null = null;\n private destroyed = false;\n private logger: { warn: (msg: string, meta?: unknown) => void };\n\n /**\n * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked\n * because sql.js's {@link Database.export} closes and reopens the database\n * (it has no in-place serialize), and closing a connection rolls back any\n * open transaction. Flushing mid-transaction would therefore silently\n * abort it, leaving the eventual `COMMIT` to fail with\n * \"cannot commit - no transaction is active\". We defer the flush until the\n * transaction fully closes. See {@link noteTransactionControl}.\n */\n private rootTxActive = false;\n /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */\n private savepointDepth = 0;\n /** A flush was requested while a transaction was open; run it on close. */\n private flushDeferred = false;\n\n /** True while any transaction (root or savepoint) is in flight. */\n private get inTransaction(): boolean {\n return this.rootTxActive || this.savepointDepth > 0;\n }\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 /**\n * Update transaction state from a transaction-control statement and, when a\n * transaction has just fully closed, run any flush that was deferred while\n * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /\n * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.\n *\n * We bias toward \"in transaction\": an unrecognised form leaves the flag set,\n * which at worst delays a flush (safe) rather than exporting mid-transaction\n * (which would abort it).\n */\n noteTransactionControl(sql: string): void {\n const s = sql.trim().toUpperCase();\n if (/^BEGIN\\b/.test(s)) {\n this.rootTxActive = true;\n } else if (/^(COMMIT|END)\\b/.test(s)) {\n // A COMMIT/END ends the whole transaction regardless of savepoint nesting.\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^ROLLBACK\\s+TO\\b/.test(s)) {\n // Rolls back to a savepoint but keeps the (outer) transaction open.\n } else if (/^ROLLBACK\\b/.test(s)) {\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^SAVEPOINT\\b/.test(s)) {\n this.savepointDepth += 1;\n } else if (/^RELEASE\\b/.test(s)) {\n this.savepointDepth = Math.max(0, this.savepointDepth - 1);\n }\n // If the transaction just fully closed and a flush was deferred while it\n // was open, run it now. We key off `flushDeferred` (set only when\n // `markDirty` actually wanted to flush) rather than `dirty`, so persist\n // modes that don't flush per-write — e.g. `on-disconnect` — still defer to\n // close() instead of flushing on every COMMIT.\n if (!this.inTransaction && this.flushDeferred) {\n this.flushDeferred = false;\n void this.flush();\n }\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 /**\n * Force a write of the current database state to disk.\n *\n * Flushes are strictly serialized through a single promise chain: every call\n * appends an export+write step that runs after all previously-queued steps.\n * This matters because sql.js `export()` mutates the live connection (it\n * closes and reopens the database), so two exports must never overlap — and\n * because the returned promise must not resolve until the caller's own write\n * has hit disk (deterministic for tests and for `close()`). Each step\n * re-checks `dirty` at run time, so a no-op write collapses cheaply and a\n * write that arrived mid-flush is captured by the next queued step.\n */\n async flush(): Promise<void> {\n if (this.isEphemeral || !this.fs || this.destroyed) return;\n // Never export while a transaction is open: sql.js's `export()` closes and\n // reopens the database, which rolls back the in-flight transaction and\n // makes the subsequent COMMIT fail. Defer until the transaction closes\n // (handled in `noteTransactionControl`).\n if (this.inTransaction) {\n this.flushDeferred = true;\n return;\n }\n\n const prev = this.flushChain;\n const step = (prev ?? Promise.resolve()).then(async () => {\n if (!this.dirty || this.destroyed || this.inTransaction) return;\n // Snapshot dirty=false before export so a concurrent write re-marks us\n // and is picked up by the next queued step.\n this.dirty = false;\n try {\n const exported = this.db.export();\n // sql.js returns a Uint8Array; Buffer.from on it shares memory but\n // works fine for the synchronous writeFile enqueue below.\n await this.fs!.writeFile(this.filename, Buffer.from(exported));\n } catch (err) {\n this.dirty = true; // let a later flush retry\n throw err;\n }\n });\n // Keep the chain tail alive but swallow its rejection there so one failed\n // flush doesn't poison every future flush; the awaited `step` still throws.\n this.flushChain = step.catch(() => {});\n await step;\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 // Any transaction still open at close is abandoned and will be rolled back\n // by `db.close()`; clear the flag so the final flush is not deferred and\n // already-committed data is persisted.\n this.rootTxActive = false;\n this.savepointDepth = 0;\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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AACA,WAAK,oBAAoB;AAAA,IAC3B;AACA,UAAM,MAAM,WAAW;AAAA,EACzB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,QAAuB;AAE3B,UAAM,OAAQ,KAAa;AAC3B,UAAM,SAAS,MAAM;AACrB,UAAM,OAAO,QAAQ;AACrB,QAAI,CAAC,QAAQ,OAAO,KAAK,YAAY,WAAY;AAEjD,UAAM,UAAU,OAAO,mBAAmB,KAAK,MAAM;AACrD,UAAM,UAAU,OAAO,mBAAmB,KAAK,MAAM;AACrD,QAAI,CAAC,WAAW,CAAC,QAAS;AAE1B,UAAM,OAAO,MAAM,QAAQ;AAC3B,QAAI;AACF,UAAI,QAAQ,OAAO,KAAK,UAAU,YAAY;AAC5C,cAAM,KAAK,MAAM;AAAA,MACnB;AAAA,IACF,UAAE;AACA,YAAM,QAAQ,IAAI;AAAA,IACpB;AAAA,EACF;AACF;;;AD5JA,IAAO,gBAAQ;AAAA,EACb,IAAI;AAAA,EACJ,SAAS;AAAA,EAET,UAAU,OAAO,YAAiB;AAChC,UAAM,EAAE,QAAQ,QAAQ,QAAQ,IAAI;AACpC,YAAQ,OAAO,sCAAsC;AAErD,QAAI,SAAS;AACX,YAAM,SAAS,IAAI,iBAAiB,MAAM;AAC1C,cAAQ,SAAS,MAAM;AACvB,cAAQ,OAAO,2CAA2C,OAAO,IAAI,EAAE;AAAA,IACzE,OAAO;AACL,cAAQ,OAAO,2DAA2D;AAAA,IAC5E;AAAA,EACF;AACF;","names":["createRequire","require","import_meta","Client_WasmSqlite"]}
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 *\n * `undefined` is mapped to `null`: sql.js's binder only accepts\n * string/number/bigint/boolean/null (and array/blob) and `throw`s a *raw\n * string* — `\"Wrong API use : tried to bind a value of an unknown type\n * (undefined).\"` — for anything else. Because it throws a string rather than\n * an `Error`, it logs as a garbled char-indexed object and aborts the whole\n * write. Mapping to `null` matches the `useNullAsDefault` semantics the\n * dialect is configured with, so a missing/undefined value persists as SQL\n * `NULL` exactly as it would through better-sqlite3.\n */\nfunction formatBindings(bindings: unknown[] | undefined): unknown[] {\n if (!bindings) return [];\n return bindings.map((b) => {\n if (b === undefined) return null;\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 // Transaction-control statements are routed through\n // `noteTransactionControl`, which owns flushing for the transaction\n // lifecycle: it suppresses flushes while a transaction is open (sql.js\n // `export()` closes+reopens the db, which would abort the txn) and\n // performs a single flush once the transaction fully closes. Routing\n // them away from `markDirty` avoids a second, racing flush on COMMIT.\n if (/^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE)\\b/i.test(obj.sql)) {\n connection.noteTransactionControl(obj.sql);\n } else {\n connection.markDirty('run');\n }\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 /**\n * Process-wide counter making each atomic-write temp filename unique, so\n * concurrent connections (or overlapping flushes) never target the same\n * temp path. Combined with `process.pid` for cross-process uniqueness.\n */\n private static tmpSeq = 0;\n\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 flushChain: Promise<void> | null = null;\n private destroyed = false;\n private logger: { warn: (msg: string, meta?: unknown) => void };\n\n /**\n * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked\n * because sql.js's {@link Database.export} closes and reopens the database\n * (it has no in-place serialize), and closing a connection rolls back any\n * open transaction. Flushing mid-transaction would therefore silently\n * abort it, leaving the eventual `COMMIT` to fail with\n * \"cannot commit - no transaction is active\". We defer the flush until the\n * transaction fully closes. See {@link noteTransactionControl}.\n */\n private rootTxActive = false;\n /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */\n private savepointDepth = 0;\n /** A flush was requested while a transaction was open; run it on close. */\n private flushDeferred = false;\n\n /** True while any transaction (root or savepoint) is in flight. */\n private get inTransaction(): boolean {\n return this.rootTxActive || this.savepointDepth > 0;\n }\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 if (!bytes) {\n this.db = new SQL.Database();\n return;\n }\n\n // Open the on-disk bytes, but guard against a corrupt image. A torn write\n // (process killed mid-flush before atomic writes existed) or otherwise\n // damaged file makes `new SQL.Database(bytes)` either throw (\"file is not a\n // database\") or open a handle whose every query fails with \"database disk\n // image is malformed\" — which, for a background dispatcher on a tick loop,\n // means the same error spammed forever with no path to recovery. Detect it\n // once at open, quarantine the bad file, and start fresh so the dev server\n // becomes usable again instead of wedging.\n try {\n const candidate = new SQL.Database(bytes);\n this.assertReadable(candidate);\n this.db = candidate;\n } catch (err) {\n await this.quarantineCorruptFile(err);\n this.db = new SQL.Database();\n }\n }\n\n /**\n * Force sql.js to actually read a page so a malformed image surfaces now\n * rather than on the first business query. `PRAGMA quick_check` walks the\n * b-tree structure without the full-scan cost of `integrity_check`; a healthy\n * database returns a single `ok` row. Any thrown error (raw string or Error)\n * or a non-`ok` result is treated as corruption.\n */\n private assertReadable(db: Database): void {\n const res = db.exec('PRAGMA quick_check(1)');\n const first = res?.[0]?.values?.[0]?.[0];\n if (typeof first === 'string' && first.toLowerCase() !== 'ok') {\n throw new Error(`sqlite quick_check failed: ${first}`);\n }\n }\n\n /**\n * Move a corrupt database file aside so its bytes are preserved for\n * post-mortem while a fresh, empty database takes its place. Best-effort:\n * failures here must not prevent the server from booting on a clean DB.\n */\n private async quarantineCorruptFile(cause: unknown): Promise<void> {\n if (!this.fs) return;\n const reason =\n typeof cause === 'string' ? cause : (cause as Error)?.message ?? String(cause);\n const backup = `${this.filename}.corrupt-${Date.now()}`;\n try {\n await this.fs.rename(this.filename, backup);\n this.logger.warn(\n `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt ` +\n `(${reason}). Quarantined to ${backup} and starting from an empty ` +\n `database so the server can boot.`,\n );\n } catch (renameErr) {\n // Could not move it aside (e.g. permissions) — overwrite is still better\n // than looping forever on a malformed image. Warn loudly and continue.\n this.logger.warn(\n `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt ` +\n `(${reason}) and could not be quarantined (${String(renameErr)}). ` +\n `Starting from an empty database; the corrupt file will be overwritten ` +\n `on the next flush.`,\n );\n }\n }\n\n /**\n * Update transaction state from a transaction-control statement and, when a\n * transaction has just fully closed, run any flush that was deferred while\n * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /\n * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.\n *\n * We bias toward \"in transaction\": an unrecognised form leaves the flag set,\n * which at worst delays a flush (safe) rather than exporting mid-transaction\n * (which would abort it).\n */\n noteTransactionControl(sql: string): void {\n const s = sql.trim().toUpperCase();\n if (/^BEGIN\\b/.test(s)) {\n this.rootTxActive = true;\n } else if (/^(COMMIT|END)\\b/.test(s)) {\n // A COMMIT/END ends the whole transaction regardless of savepoint nesting.\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^ROLLBACK\\s+TO\\b/.test(s)) {\n // Rolls back to a savepoint but keeps the (outer) transaction open.\n } else if (/^ROLLBACK\\b/.test(s)) {\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^SAVEPOINT\\b/.test(s)) {\n this.savepointDepth += 1;\n } else if (/^RELEASE\\b/.test(s)) {\n this.savepointDepth = Math.max(0, this.savepointDepth - 1);\n }\n // If the transaction just fully closed and a flush was deferred while it\n // was open, run it now. We key off `flushDeferred` (set only when\n // `markDirty` actually wanted to flush) rather than `dirty`, so persist\n // modes that don't flush per-write — e.g. `on-disconnect` — still defer to\n // close() instead of flushing on every COMMIT.\n if (!this.inTransaction && this.flushDeferred) {\n this.flushDeferred = false;\n void this.flush();\n }\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 /**\n * Force a write of the current database state to disk.\n *\n * Flushes are strictly serialized through a single promise chain: every call\n * appends an export+write step that runs after all previously-queued steps.\n * This matters because sql.js `export()` mutates the live connection (it\n * closes and reopens the database), so two exports must never overlap — and\n * because the returned promise must not resolve until the caller's own write\n * has hit disk (deterministic for tests and for `close()`). Each step\n * re-checks `dirty` at run time, so a no-op write collapses cheaply and a\n * write that arrived mid-flush is captured by the next queued step.\n */\n async flush(): Promise<void> {\n if (this.isEphemeral || !this.fs || this.destroyed) return;\n // Never export while a transaction is open: sql.js's `export()` closes and\n // reopens the database, which rolls back the in-flight transaction and\n // makes the subsequent COMMIT fail. Defer until the transaction closes\n // (handled in `noteTransactionControl`).\n if (this.inTransaction) {\n this.flushDeferred = true;\n return;\n }\n\n const prev = this.flushChain;\n const step = (prev ?? Promise.resolve()).then(async () => {\n if (!this.dirty || this.destroyed || this.inTransaction) return;\n // Snapshot dirty=false before export so a concurrent write re-marks us\n // and is picked up by the next queued step.\n this.dirty = false;\n try {\n const exported = this.db.export();\n // sql.js returns a Uint8Array; Buffer.from on it shares memory but\n // works fine for the atomic write below.\n await this.atomicWriteFile(Buffer.from(exported));\n } catch (err) {\n this.dirty = true; // let a later flush retry\n throw err;\n }\n });\n // Keep the chain tail alive but swallow its rejection there so one failed\n // flush doesn't poison every future flush; the awaited `step` still throws.\n this.flushChain = step.catch(() => {});\n await step;\n }\n\n /**\n * Write the database bytes to disk atomically: write to a sibling temp file,\n * fsync it, then `rename()` it over the target.\n *\n * A plain `writeFile(this.filename, …)` truncates the target and streams the\n * new bytes in place, so a process killed mid-write (a dev-server restart,\n * Ctrl-C, or crash — likely under `on-write`, where every dispatcher tick\n * flushes) leaves a half-written file. sql.js then rejects that file on the\n * next boot with \"database disk image is malformed\". `rename(2)` is atomic\n * within a filesystem, so a reader always sees either the complete old file\n * or the complete new one — never a torn image. The temp file lives in the\n * same directory as the target so the rename stays intra-filesystem.\n */\n private async atomicWriteFile(data: Buffer): Promise<void> {\n if (!this.fs) return;\n const tmp = `${this.filename}.tmp-${process.pid}-${(WasmSqliteConnection.tmpSeq += 1)}`;\n let handle: import('node:fs/promises').FileHandle | undefined;\n try {\n handle = await this.fs.open(tmp, 'w');\n await handle.writeFile(data);\n // Flush the bytes to the platter before the rename so a crash can't leave\n // a renamed-but-empty file behind on filesystems that reorder the two.\n await handle.sync();\n await handle.close();\n handle = undefined;\n await this.fs.rename(tmp, this.filename);\n } catch (err) {\n if (handle) {\n try {\n await handle.close();\n } catch {\n /* ignore */\n }\n }\n // Clean up the temp file so a failed flush doesn't litter the data dir.\n try {\n await this.fs.unlink(tmp);\n } catch {\n /* ignore */\n }\n throw err;\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 // Any transaction still open at close is abandoned and will be rolled back\n // by `db.close()`; clear the flag so the final flush is not deferred and\n // already-committed data is persisted.\n this.rootTxActive = false;\n this.savepointDepth = 0;\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;AAA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client_WasmSqlite"]}
package/dist/index.mjs CHANGED
@@ -43,7 +43,7 @@ async function loadSqlJs(locateFile) {
43
43
  })();
44
44
  return cachedSqlJs;
45
45
  }
46
- var WasmSqliteConnection = class {
46
+ var _WasmSqliteConnection = class _WasmSqliteConnection {
47
47
  constructor(opts) {
48
48
  this.fs = null;
49
49
  this.dirty = false;
@@ -105,7 +105,52 @@ var WasmSqliteConnection = class {
105
105
  } catch (e) {
106
106
  if (e?.code !== "ENOENT") throw e;
107
107
  }
108
- this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();
108
+ if (!bytes) {
109
+ this.db = new SQL.Database();
110
+ return;
111
+ }
112
+ try {
113
+ const candidate = new SQL.Database(bytes);
114
+ this.assertReadable(candidate);
115
+ this.db = candidate;
116
+ } catch (err) {
117
+ await this.quarantineCorruptFile(err);
118
+ this.db = new SQL.Database();
119
+ }
120
+ }
121
+ /**
122
+ * Force sql.js to actually read a page so a malformed image surfaces now
123
+ * rather than on the first business query. `PRAGMA quick_check` walks the
124
+ * b-tree structure without the full-scan cost of `integrity_check`; a healthy
125
+ * database returns a single `ok` row. Any thrown error (raw string or Error)
126
+ * or a non-`ok` result is treated as corruption.
127
+ */
128
+ assertReadable(db) {
129
+ const res = db.exec("PRAGMA quick_check(1)");
130
+ const first = res?.[0]?.values?.[0]?.[0];
131
+ if (typeof first === "string" && first.toLowerCase() !== "ok") {
132
+ throw new Error(`sqlite quick_check failed: ${first}`);
133
+ }
134
+ }
135
+ /**
136
+ * Move a corrupt database file aside so its bytes are preserved for
137
+ * post-mortem while a fresh, empty database takes its place. Best-effort:
138
+ * failures here must not prevent the server from booting on a clean DB.
139
+ */
140
+ async quarantineCorruptFile(cause) {
141
+ if (!this.fs) return;
142
+ const reason = typeof cause === "string" ? cause : cause?.message ?? String(cause);
143
+ const backup = `${this.filename}.corrupt-${Date.now()}`;
144
+ try {
145
+ await this.fs.rename(this.filename, backup);
146
+ this.logger.warn(
147
+ `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt (${reason}). Quarantined to ${backup} and starting from an empty database so the server can boot.`
148
+ );
149
+ } catch (renameErr) {
150
+ this.logger.warn(
151
+ `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt (${reason}) and could not be quarantined (${String(renameErr)}). Starting from an empty database; the corrupt file will be overwritten on the next flush.`
152
+ );
153
+ }
109
154
  }
110
155
  /**
111
156
  * Update transaction state from a transaction-control statement and, when a
@@ -179,7 +224,7 @@ var WasmSqliteConnection = class {
179
224
  this.dirty = false;
180
225
  try {
181
226
  const exported = this.db.export();
182
- await this.fs.writeFile(this.filename, Buffer.from(exported));
227
+ await this.atomicWriteFile(Buffer.from(exported));
183
228
  } catch (err) {
184
229
  this.dirty = true;
185
230
  throw err;
@@ -189,6 +234,44 @@ var WasmSqliteConnection = class {
189
234
  });
190
235
  await step;
191
236
  }
237
+ /**
238
+ * Write the database bytes to disk atomically: write to a sibling temp file,
239
+ * fsync it, then `rename()` it over the target.
240
+ *
241
+ * A plain `writeFile(this.filename, …)` truncates the target and streams the
242
+ * new bytes in place, so a process killed mid-write (a dev-server restart,
243
+ * Ctrl-C, or crash — likely under `on-write`, where every dispatcher tick
244
+ * flushes) leaves a half-written file. sql.js then rejects that file on the
245
+ * next boot with "database disk image is malformed". `rename(2)` is atomic
246
+ * within a filesystem, so a reader always sees either the complete old file
247
+ * or the complete new one — never a torn image. The temp file lives in the
248
+ * same directory as the target so the rename stays intra-filesystem.
249
+ */
250
+ async atomicWriteFile(data) {
251
+ if (!this.fs) return;
252
+ const tmp = `${this.filename}.tmp-${process.pid}-${_WasmSqliteConnection.tmpSeq += 1}`;
253
+ let handle;
254
+ try {
255
+ handle = await this.fs.open(tmp, "w");
256
+ await handle.writeFile(data);
257
+ await handle.sync();
258
+ await handle.close();
259
+ handle = void 0;
260
+ await this.fs.rename(tmp, this.filename);
261
+ } catch (err) {
262
+ if (handle) {
263
+ try {
264
+ await handle.close();
265
+ } catch {
266
+ }
267
+ }
268
+ try {
269
+ await this.fs.unlink(tmp);
270
+ } catch {
271
+ }
272
+ throw err;
273
+ }
274
+ }
192
275
  /** Close the database, flushing any pending writes first. */
193
276
  async close() {
194
277
  if (this.destroyed) return;
@@ -213,6 +296,13 @@ var WasmSqliteConnection = class {
213
296
  return this.db;
214
297
  }
215
298
  };
299
+ /**
300
+ * Process-wide counter making each atomic-write temp filename unique, so
301
+ * concurrent connections (or overlapping flushes) never target the same
302
+ * temp path. Combined with `process.pid` for cross-process uniqueness.
303
+ */
304
+ _WasmSqliteConnection.tmpSeq = 0;
305
+ var WasmSqliteConnection = _WasmSqliteConnection;
216
306
 
217
307
  // src/knex-wasm-dialect.ts
218
308
  var cachedEsmRequire = null;
@@ -225,6 +315,7 @@ function getEsmRequire() {
225
315
  function formatBindings(bindings) {
226
316
  if (!bindings) return [];
227
317
  return bindings.map((b) => {
318
+ if (b === void 0) return null;
228
319
  if (b instanceof Date) return b.valueOf();
229
320
  if (typeof b === "boolean") return Number(b);
230
321
  return b;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/sqlite-wasm-driver.ts","../src/knex-wasm-dialect.ts","../src/wasm-connection.ts","../src/index.ts"],"sourcesContent":["// 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 // Transaction-control statements are routed through\n // `noteTransactionControl`, which owns flushing for the transaction\n // lifecycle: it suppresses flushes while a transaction is open (sql.js\n // `export()` closes+reopens the db, which would abort the txn) and\n // performs a single flush once the transaction fully closes. Routing\n // them away from `markDirty` avoids a second, racing flush on COMMIT.\n if (/^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE)\\b/i.test(obj.sql)) {\n connection.noteTransactionControl(obj.sql);\n } else {\n connection.markDirty('run');\n }\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 flushChain: Promise<void> | null = null;\n private destroyed = false;\n private logger: { warn: (msg: string, meta?: unknown) => void };\n\n /**\n * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked\n * because sql.js's {@link Database.export} closes and reopens the database\n * (it has no in-place serialize), and closing a connection rolls back any\n * open transaction. Flushing mid-transaction would therefore silently\n * abort it, leaving the eventual `COMMIT` to fail with\n * \"cannot commit - no transaction is active\". We defer the flush until the\n * transaction fully closes. See {@link noteTransactionControl}.\n */\n private rootTxActive = false;\n /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */\n private savepointDepth = 0;\n /** A flush was requested while a transaction was open; run it on close. */\n private flushDeferred = false;\n\n /** True while any transaction (root or savepoint) is in flight. */\n private get inTransaction(): boolean {\n return this.rootTxActive || this.savepointDepth > 0;\n }\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 /**\n * Update transaction state from a transaction-control statement and, when a\n * transaction has just fully closed, run any flush that was deferred while\n * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /\n * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.\n *\n * We bias toward \"in transaction\": an unrecognised form leaves the flag set,\n * which at worst delays a flush (safe) rather than exporting mid-transaction\n * (which would abort it).\n */\n noteTransactionControl(sql: string): void {\n const s = sql.trim().toUpperCase();\n if (/^BEGIN\\b/.test(s)) {\n this.rootTxActive = true;\n } else if (/^(COMMIT|END)\\b/.test(s)) {\n // A COMMIT/END ends the whole transaction regardless of savepoint nesting.\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^ROLLBACK\\s+TO\\b/.test(s)) {\n // Rolls back to a savepoint but keeps the (outer) transaction open.\n } else if (/^ROLLBACK\\b/.test(s)) {\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^SAVEPOINT\\b/.test(s)) {\n this.savepointDepth += 1;\n } else if (/^RELEASE\\b/.test(s)) {\n this.savepointDepth = Math.max(0, this.savepointDepth - 1);\n }\n // If the transaction just fully closed and a flush was deferred while it\n // was open, run it now. We key off `flushDeferred` (set only when\n // `markDirty` actually wanted to flush) rather than `dirty`, so persist\n // modes that don't flush per-write — e.g. `on-disconnect` — still defer to\n // close() instead of flushing on every COMMIT.\n if (!this.inTransaction && this.flushDeferred) {\n this.flushDeferred = false;\n void this.flush();\n }\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 /**\n * Force a write of the current database state to disk.\n *\n * Flushes are strictly serialized through a single promise chain: every call\n * appends an export+write step that runs after all previously-queued steps.\n * This matters because sql.js `export()` mutates the live connection (it\n * closes and reopens the database), so two exports must never overlap — and\n * because the returned promise must not resolve until the caller's own write\n * has hit disk (deterministic for tests and for `close()`). Each step\n * re-checks `dirty` at run time, so a no-op write collapses cheaply and a\n * write that arrived mid-flush is captured by the next queued step.\n */\n async flush(): Promise<void> {\n if (this.isEphemeral || !this.fs || this.destroyed) return;\n // Never export while a transaction is open: sql.js's `export()` closes and\n // reopens the database, which rolls back the in-flight transaction and\n // makes the subsequent COMMIT fail. Defer until the transaction closes\n // (handled in `noteTransactionControl`).\n if (this.inTransaction) {\n this.flushDeferred = true;\n return;\n }\n\n const prev = this.flushChain;\n const step = (prev ?? Promise.resolve()).then(async () => {\n if (!this.dirty || this.destroyed || this.inTransaction) return;\n // Snapshot dirty=false before export so a concurrent write re-marks us\n // and is picked up by the next queued step.\n this.dirty = false;\n try {\n const exported = this.db.export();\n // sql.js returns a Uint8Array; Buffer.from on it shares memory but\n // works fine for the synchronous writeFile enqueue below.\n await this.fs!.writeFile(this.filename, Buffer.from(exported));\n } catch (err) {\n this.dirty = true; // let a later flush retry\n throw err;\n }\n });\n // Keep the chain tail alive but swallow its rejection there so one failed\n // flush doesn't poison every future flush; the awaited `step` still throws.\n this.flushChain = step.catch(() => {});\n await step;\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 // Any transaction still open at close is abandoned and will be rolled back\n // by `db.close()`; clear the flag so the final flush is not deferred and\n // already-committed data is persisted.\n this.rootTxActive = false;\n this.savepointDepth = 0;\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","// 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"],"mappings":";AAaA,SAAS,iBAAuC;;;ACWhD,SAAS,qBAAqB;;;ACe9B,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;AAOO,IAAM,uBAAN,MAA2B;AAAA,EAkChC,YAAY,MAA6B;AA5BzC,SAAQ,KAA+C;AACvD,SAAQ,QAAQ;AACh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YAAQ,OAAO,sCAAsC;AAErD,QAAI,SAAS;AACX,YAAM,SAAS,IAAI,iBAAiB,MAAM;AAC1C,cAAQ,SAAS,MAAM;AACvB,cAAQ,OAAO,2CAA2C,OAAO,IAAI,EAAE;AAAA,IACzE,OAAO;AACL,cAAQ,OAAO,2DAA2D;AAAA,IAC5E;AAAA,EACF;AACF;","names":["createRequire","require","Client_WasmSqlite"]}
1
+ {"version":3,"sources":["../src/sqlite-wasm-driver.ts","../src/knex-wasm-dialect.ts","../src/wasm-connection.ts","../src/index.ts"],"sourcesContent":["// 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 *\n * `undefined` is mapped to `null`: sql.js's binder only accepts\n * string/number/bigint/boolean/null (and array/blob) and `throw`s a *raw\n * string* — `\"Wrong API use : tried to bind a value of an unknown type\n * (undefined).\"` — for anything else. Because it throws a string rather than\n * an `Error`, it logs as a garbled char-indexed object and aborts the whole\n * write. Mapping to `null` matches the `useNullAsDefault` semantics the\n * dialect is configured with, so a missing/undefined value persists as SQL\n * `NULL` exactly as it would through better-sqlite3.\n */\nfunction formatBindings(bindings: unknown[] | undefined): unknown[] {\n if (!bindings) return [];\n return bindings.map((b) => {\n if (b === undefined) return null;\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 // Transaction-control statements are routed through\n // `noteTransactionControl`, which owns flushing for the transaction\n // lifecycle: it suppresses flushes while a transaction is open (sql.js\n // `export()` closes+reopens the db, which would abort the txn) and\n // performs a single flush once the transaction fully closes. Routing\n // them away from `markDirty` avoids a second, racing flush on COMMIT.\n if (/^\\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE)\\b/i.test(obj.sql)) {\n connection.noteTransactionControl(obj.sql);\n } else {\n connection.markDirty('run');\n }\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 /**\n * Process-wide counter making each atomic-write temp filename unique, so\n * concurrent connections (or overlapping flushes) never target the same\n * temp path. Combined with `process.pid` for cross-process uniqueness.\n */\n private static tmpSeq = 0;\n\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 flushChain: Promise<void> | null = null;\n private destroyed = false;\n private logger: { warn: (msg: string, meta?: unknown) => void };\n\n /**\n * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked\n * because sql.js's {@link Database.export} closes and reopens the database\n * (it has no in-place serialize), and closing a connection rolls back any\n * open transaction. Flushing mid-transaction would therefore silently\n * abort it, leaving the eventual `COMMIT` to fail with\n * \"cannot commit - no transaction is active\". We defer the flush until the\n * transaction fully closes. See {@link noteTransactionControl}.\n */\n private rootTxActive = false;\n /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */\n private savepointDepth = 0;\n /** A flush was requested while a transaction was open; run it on close. */\n private flushDeferred = false;\n\n /** True while any transaction (root or savepoint) is in flight. */\n private get inTransaction(): boolean {\n return this.rootTxActive || this.savepointDepth > 0;\n }\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 if (!bytes) {\n this.db = new SQL.Database();\n return;\n }\n\n // Open the on-disk bytes, but guard against a corrupt image. A torn write\n // (process killed mid-flush before atomic writes existed) or otherwise\n // damaged file makes `new SQL.Database(bytes)` either throw (\"file is not a\n // database\") or open a handle whose every query fails with \"database disk\n // image is malformed\" — which, for a background dispatcher on a tick loop,\n // means the same error spammed forever with no path to recovery. Detect it\n // once at open, quarantine the bad file, and start fresh so the dev server\n // becomes usable again instead of wedging.\n try {\n const candidate = new SQL.Database(bytes);\n this.assertReadable(candidate);\n this.db = candidate;\n } catch (err) {\n await this.quarantineCorruptFile(err);\n this.db = new SQL.Database();\n }\n }\n\n /**\n * Force sql.js to actually read a page so a malformed image surfaces now\n * rather than on the first business query. `PRAGMA quick_check` walks the\n * b-tree structure without the full-scan cost of `integrity_check`; a healthy\n * database returns a single `ok` row. Any thrown error (raw string or Error)\n * or a non-`ok` result is treated as corruption.\n */\n private assertReadable(db: Database): void {\n const res = db.exec('PRAGMA quick_check(1)');\n const first = res?.[0]?.values?.[0]?.[0];\n if (typeof first === 'string' && first.toLowerCase() !== 'ok') {\n throw new Error(`sqlite quick_check failed: ${first}`);\n }\n }\n\n /**\n * Move a corrupt database file aside so its bytes are preserved for\n * post-mortem while a fresh, empty database takes its place. Best-effort:\n * failures here must not prevent the server from booting on a clean DB.\n */\n private async quarantineCorruptFile(cause: unknown): Promise<void> {\n if (!this.fs) return;\n const reason =\n typeof cause === 'string' ? cause : (cause as Error)?.message ?? String(cause);\n const backup = `${this.filename}.corrupt-${Date.now()}`;\n try {\n await this.fs.rename(this.filename, backup);\n this.logger.warn(\n `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt ` +\n `(${reason}). Quarantined to ${backup} and starting from an empty ` +\n `database so the server can boot.`,\n );\n } catch (renameErr) {\n // Could not move it aside (e.g. permissions) — overwrite is still better\n // than looping forever on a malformed image. Warn loudly and continue.\n this.logger.warn(\n `[driver-sqlite-wasm] Database image at ${this.filename} is corrupt ` +\n `(${reason}) and could not be quarantined (${String(renameErr)}). ` +\n `Starting from an empty database; the corrupt file will be overwritten ` +\n `on the next flush.`,\n );\n }\n }\n\n /**\n * Update transaction state from a transaction-control statement and, when a\n * transaction has just fully closed, run any flush that was deferred while\n * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /\n * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.\n *\n * We bias toward \"in transaction\": an unrecognised form leaves the flag set,\n * which at worst delays a flush (safe) rather than exporting mid-transaction\n * (which would abort it).\n */\n noteTransactionControl(sql: string): void {\n const s = sql.trim().toUpperCase();\n if (/^BEGIN\\b/.test(s)) {\n this.rootTxActive = true;\n } else if (/^(COMMIT|END)\\b/.test(s)) {\n // A COMMIT/END ends the whole transaction regardless of savepoint nesting.\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^ROLLBACK\\s+TO\\b/.test(s)) {\n // Rolls back to a savepoint but keeps the (outer) transaction open.\n } else if (/^ROLLBACK\\b/.test(s)) {\n this.rootTxActive = false;\n this.savepointDepth = 0;\n } else if (/^SAVEPOINT\\b/.test(s)) {\n this.savepointDepth += 1;\n } else if (/^RELEASE\\b/.test(s)) {\n this.savepointDepth = Math.max(0, this.savepointDepth - 1);\n }\n // If the transaction just fully closed and a flush was deferred while it\n // was open, run it now. We key off `flushDeferred` (set only when\n // `markDirty` actually wanted to flush) rather than `dirty`, so persist\n // modes that don't flush per-write — e.g. `on-disconnect` — still defer to\n // close() instead of flushing on every COMMIT.\n if (!this.inTransaction && this.flushDeferred) {\n this.flushDeferred = false;\n void this.flush();\n }\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 /**\n * Force a write of the current database state to disk.\n *\n * Flushes are strictly serialized through a single promise chain: every call\n * appends an export+write step that runs after all previously-queued steps.\n * This matters because sql.js `export()` mutates the live connection (it\n * closes and reopens the database), so two exports must never overlap — and\n * because the returned promise must not resolve until the caller's own write\n * has hit disk (deterministic for tests and for `close()`). Each step\n * re-checks `dirty` at run time, so a no-op write collapses cheaply and a\n * write that arrived mid-flush is captured by the next queued step.\n */\n async flush(): Promise<void> {\n if (this.isEphemeral || !this.fs || this.destroyed) return;\n // Never export while a transaction is open: sql.js's `export()` closes and\n // reopens the database, which rolls back the in-flight transaction and\n // makes the subsequent COMMIT fail. Defer until the transaction closes\n // (handled in `noteTransactionControl`).\n if (this.inTransaction) {\n this.flushDeferred = true;\n return;\n }\n\n const prev = this.flushChain;\n const step = (prev ?? Promise.resolve()).then(async () => {\n if (!this.dirty || this.destroyed || this.inTransaction) return;\n // Snapshot dirty=false before export so a concurrent write re-marks us\n // and is picked up by the next queued step.\n this.dirty = false;\n try {\n const exported = this.db.export();\n // sql.js returns a Uint8Array; Buffer.from on it shares memory but\n // works fine for the atomic write below.\n await this.atomicWriteFile(Buffer.from(exported));\n } catch (err) {\n this.dirty = true; // let a later flush retry\n throw err;\n }\n });\n // Keep the chain tail alive but swallow its rejection there so one failed\n // flush doesn't poison every future flush; the awaited `step` still throws.\n this.flushChain = step.catch(() => {});\n await step;\n }\n\n /**\n * Write the database bytes to disk atomically: write to a sibling temp file,\n * fsync it, then `rename()` it over the target.\n *\n * A plain `writeFile(this.filename, …)` truncates the target and streams the\n * new bytes in place, so a process killed mid-write (a dev-server restart,\n * Ctrl-C, or crash — likely under `on-write`, where every dispatcher tick\n * flushes) leaves a half-written file. sql.js then rejects that file on the\n * next boot with \"database disk image is malformed\". `rename(2)` is atomic\n * within a filesystem, so a reader always sees either the complete old file\n * or the complete new one — never a torn image. The temp file lives in the\n * same directory as the target so the rename stays intra-filesystem.\n */\n private async atomicWriteFile(data: Buffer): Promise<void> {\n if (!this.fs) return;\n const tmp = `${this.filename}.tmp-${process.pid}-${(WasmSqliteConnection.tmpSeq += 1)}`;\n let handle: import('node:fs/promises').FileHandle | undefined;\n try {\n handle = await this.fs.open(tmp, 'w');\n await handle.writeFile(data);\n // Flush the bytes to the platter before the rename so a crash can't leave\n // a renamed-but-empty file behind on filesystems that reorder the two.\n await handle.sync();\n await handle.close();\n handle = undefined;\n await this.fs.rename(tmp, this.filename);\n } catch (err) {\n if (handle) {\n try {\n await handle.close();\n } catch {\n /* ignore */\n }\n }\n // Clean up the temp file so a failed flush doesn't litter the data dir.\n try {\n await this.fs.unlink(tmp);\n } catch {\n /* ignore */\n }\n throw err;\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 // Any transaction still open at close is abandoned and will be rolled back\n // by `db.close()`; clear the flag so the final flush is not deferred and\n // already-committed data is persisted.\n this.rootTxActive = false;\n this.savepointDepth = 0;\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","// 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"],"mappings":";AAaA,SAAS,iBAAuC;;;ACWhD,SAAS,qBAAqB;;;ACe9B,IAAM,gBAAgB,oBAAI,IAAI;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAMD,e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I;AAAA,IACpB;AAAA,EACF;AACF;;;AG5JA,IAAO,gBAAQ;AAAA,EACb,IAAI;AAAA,EACJ,SAAS;AAAA,EAET,UAAU,OAAO,YAAiB;AAChC,UAAM,EAAE,QAAQ,QAAQ,QAAQ,IAAI;AACpC,YAAQ,OAAO,sCAAsC;AAErD,QAAI,SAAS;AACX,YAAM,SAAS,IAAI,iBAAiB,MAAM;AAC1C,cAAQ,SAAS,MAAM;AACvB,cAAQ,OAAO,2CAA2C,OAAO,IAAI,EAAE;AAAA,IACzE,OAAO;AACL,cAAQ,OAAO,2DAA2D;AAAA,IAC5E;AAAA,EACF;AACF;","names":["createRequire","require","Client_WasmSqlite"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@objectstack/driver-sqlite-wasm",
3
- "version": "14.7.0",
3
+ "version": "14.8.0",
4
4
  "license": "Apache-2.0",
5
5
  "description": "WASM SQLite Driver for ObjectStack — runs in browser/WebContainer (StackBlitz) without native bindings",
6
6
  "keywords": [
@@ -26,9 +26,9 @@
26
26
  "knex": "^3.3.0",
27
27
  "nanoid": "^6.0.0",
28
28
  "sql.js": "^1.14.1",
29
- "@objectstack/core": "14.7.0",
30
- "@objectstack/driver-sql": "14.7.0",
31
- "@objectstack/spec": "14.7.0"
29
+ "@objectstack/core": "14.8.0",
30
+ "@objectstack/driver-sql": "14.8.0",
31
+ "@objectstack/spec": "14.8.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^26.1.1",