@objectstack/driver-sqlite-wasm 7.5.0 → 7.6.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
@@ -46,15 +46,53 @@ declare class WasmSqliteConnection {
46
46
  private dirty;
47
47
  private debounceMs;
48
48
  private debounceTimer;
49
- private pendingFlush;
49
+ private flushChain;
50
50
  private destroyed;
51
51
  private logger;
52
+ /**
53
+ * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked
54
+ * because sql.js's {@link Database.export} closes and reopens the database
55
+ * (it has no in-place serialize), and closing a connection rolls back any
56
+ * open transaction. Flushing mid-transaction would therefore silently
57
+ * abort it, leaving the eventual `COMMIT` to fail with
58
+ * "cannot commit - no transaction is active". We defer the flush until the
59
+ * transaction fully closes. See {@link noteTransactionControl}.
60
+ */
61
+ private rootTxActive;
62
+ /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */
63
+ private savepointDepth;
64
+ /** A flush was requested while a transaction was open; run it on close. */
65
+ private flushDeferred;
66
+ /** True while any transaction (root or savepoint) is in flight. */
67
+ private get inTransaction();
52
68
  constructor(opts: WasmConnectionOptions);
53
69
  /** Open the underlying sql.js database, loading bytes from disk if any. */
54
70
  open(sqlJs?: SqlJsStatic, locateFile?: (file: string) => string): Promise<void>;
71
+ /**
72
+ * Update transaction state from a transaction-control statement and, when a
73
+ * transaction has just fully closed, run any flush that was deferred while
74
+ * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /
75
+ * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.
76
+ *
77
+ * We bias toward "in transaction": an unrecognised form leaves the flag set,
78
+ * which at worst delays a flush (safe) rather than exporting mid-transaction
79
+ * (which would abort it).
80
+ */
81
+ noteTransactionControl(sql: string): void;
55
82
  /** Hint that a mutation just executed; schedule a flush if needed. */
56
83
  markDirty(method?: string): void;
57
- /** Force a write of the current database state to disk. */
84
+ /**
85
+ * Force a write of the current database state to disk.
86
+ *
87
+ * Flushes are strictly serialized through a single promise chain: every call
88
+ * appends an export+write step that runs after all previously-queued steps.
89
+ * This matters because sql.js `export()` mutates the live connection (it
90
+ * closes and reopens the database), so two exports must never overlap — and
91
+ * because the returned promise must not resolve until the caller's own write
92
+ * has hit disk (deterministic for tests and for `close()`). Each step
93
+ * re-checks `dirty` at run time, so a no-op write collapses cheaply and a
94
+ * write that arrived mid-flush is captured by the next queued step.
95
+ */
58
96
  flush(): Promise<void>;
59
97
  /** Close the database, flushing any pending writes first. */
60
98
  close(): Promise<void>;
package/dist/index.d.ts CHANGED
@@ -46,15 +46,53 @@ declare class WasmSqliteConnection {
46
46
  private dirty;
47
47
  private debounceMs;
48
48
  private debounceTimer;
49
- private pendingFlush;
49
+ private flushChain;
50
50
  private destroyed;
51
51
  private logger;
52
+ /**
53
+ * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked
54
+ * because sql.js's {@link Database.export} closes and reopens the database
55
+ * (it has no in-place serialize), and closing a connection rolls back any
56
+ * open transaction. Flushing mid-transaction would therefore silently
57
+ * abort it, leaving the eventual `COMMIT` to fail with
58
+ * "cannot commit - no transaction is active". We defer the flush until the
59
+ * transaction fully closes. See {@link noteTransactionControl}.
60
+ */
61
+ private rootTxActive;
62
+ /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */
63
+ private savepointDepth;
64
+ /** A flush was requested while a transaction was open; run it on close. */
65
+ private flushDeferred;
66
+ /** True while any transaction (root or savepoint) is in flight. */
67
+ private get inTransaction();
52
68
  constructor(opts: WasmConnectionOptions);
53
69
  /** Open the underlying sql.js database, loading bytes from disk if any. */
54
70
  open(sqlJs?: SqlJsStatic, locateFile?: (file: string) => string): Promise<void>;
71
+ /**
72
+ * Update transaction state from a transaction-control statement and, when a
73
+ * transaction has just fully closed, run any flush that was deferred while
74
+ * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /
75
+ * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.
76
+ *
77
+ * We bias toward "in transaction": an unrecognised form leaves the flag set,
78
+ * which at worst delays a flush (safe) rather than exporting mid-transaction
79
+ * (which would abort it).
80
+ */
81
+ noteTransactionControl(sql: string): void;
55
82
  /** Hint that a mutation just executed; schedule a flush if needed. */
56
83
  markDirty(method?: string): void;
57
- /** Force a write of the current database state to disk. */
84
+ /**
85
+ * Force a write of the current database state to disk.
86
+ *
87
+ * Flushes are strictly serialized through a single promise chain: every call
88
+ * appends an export+write step that runs after all previously-queued steps.
89
+ * This matters because sql.js `export()` mutates the live connection (it
90
+ * closes and reopens the database), so two exports must never overlap — and
91
+ * because the returned promise must not resolve until the caller's own write
92
+ * has hit disk (deterministic for tests and for `close()`). Each step
93
+ * re-checks `dirty` at run time, so a no-op write collapses cheaply and a
94
+ * write that arrived mid-flush is captured by the next queued step.
95
+ */
58
96
  flush(): Promise<void>;
59
97
  /** Close the database, flushing any pending writes first. */
60
98
  close(): Promise<void>;
package/dist/index.js CHANGED
@@ -89,8 +89,22 @@ var WasmSqliteConnection = class {
89
89
  this.dirty = false;
90
90
  this.debounceMs = 0;
91
91
  this.debounceTimer = null;
92
- this.pendingFlush = null;
92
+ this.flushChain = null;
93
93
  this.destroyed = false;
94
+ /**
95
+ * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked
96
+ * because sql.js's {@link Database.export} closes and reopens the database
97
+ * (it has no in-place serialize), and closing a connection rolls back any
98
+ * open transaction. Flushing mid-transaction would therefore silently
99
+ * abort it, leaving the eventual `COMMIT` to fail with
100
+ * "cannot commit - no transaction is active". We defer the flush until the
101
+ * transaction fully closes. See {@link noteTransactionControl}.
102
+ */
103
+ this.rootTxActive = false;
104
+ /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */
105
+ this.savepointDepth = 0;
106
+ /** A flush was requested while a transaction was open; run it on close. */
107
+ this.flushDeferred = false;
94
108
  this.filename = opts.filename;
95
109
  this.persist = opts.persist ?? "on-disconnect";
96
110
  this.isEphemeral = this.filename === ":memory:" || this.filename.startsWith(":");
@@ -100,6 +114,10 @@ var WasmSqliteConnection = class {
100
114
  this.debounceMs = Number.isFinite(ms) && ms > 0 ? ms : 250;
101
115
  }
102
116
  }
117
+ /** True while any transaction (root or savepoint) is in flight. */
118
+ get inTransaction() {
119
+ return this.rootTxActive || this.savepointDepth > 0;
120
+ }
103
121
  /** Open the underlying sql.js database, loading bytes from disk if any. */
104
122
  async open(sqlJs, locateFile) {
105
123
  const SQL = sqlJs ?? await loadSqlJs(locateFile);
@@ -129,6 +147,37 @@ var WasmSqliteConnection = class {
129
147
  }
130
148
  this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();
131
149
  }
150
+ /**
151
+ * Update transaction state from a transaction-control statement and, when a
152
+ * transaction has just fully closed, run any flush that was deferred while
153
+ * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /
154
+ * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.
155
+ *
156
+ * We bias toward "in transaction": an unrecognised form leaves the flag set,
157
+ * which at worst delays a flush (safe) rather than exporting mid-transaction
158
+ * (which would abort it).
159
+ */
160
+ noteTransactionControl(sql) {
161
+ const s = sql.trim().toUpperCase();
162
+ if (/^BEGIN\b/.test(s)) {
163
+ this.rootTxActive = true;
164
+ } else if (/^(COMMIT|END)\b/.test(s)) {
165
+ this.rootTxActive = false;
166
+ this.savepointDepth = 0;
167
+ } else if (/^ROLLBACK\s+TO\b/.test(s)) {
168
+ } else if (/^ROLLBACK\b/.test(s)) {
169
+ this.rootTxActive = false;
170
+ this.savepointDepth = 0;
171
+ } else if (/^SAVEPOINT\b/.test(s)) {
172
+ this.savepointDepth += 1;
173
+ } else if (/^RELEASE\b/.test(s)) {
174
+ this.savepointDepth = Math.max(0, this.savepointDepth - 1);
175
+ }
176
+ if (!this.inTransaction && this.flushDeferred) {
177
+ this.flushDeferred = false;
178
+ void this.flush();
179
+ }
180
+ }
132
181
  /** Hint that a mutation just executed; schedule a flush if needed. */
133
182
  markDirty(method) {
134
183
  if (this.isEphemeral || !this.fs) return;
@@ -146,30 +195,39 @@ var WasmSqliteConnection = class {
146
195
  }, this.debounceMs);
147
196
  }
148
197
  }
149
- /** Force a write of the current database state to disk. */
198
+ /**
199
+ * Force a write of the current database state to disk.
200
+ *
201
+ * Flushes are strictly serialized through a single promise chain: every call
202
+ * appends an export+write step that runs after all previously-queued steps.
203
+ * This matters because sql.js `export()` mutates the live connection (it
204
+ * closes and reopens the database), so two exports must never overlap — and
205
+ * because the returned promise must not resolve until the caller's own write
206
+ * has hit disk (deterministic for tests and for `close()`). Each step
207
+ * re-checks `dirty` at run time, so a no-op write collapses cheaply and a
208
+ * write that arrived mid-flush is captured by the next queued step.
209
+ */
150
210
  async flush() {
151
211
  if (this.isEphemeral || !this.fs || this.destroyed) return;
152
- if (this.pendingFlush) {
153
- await this.pendingFlush;
154
- if (!this.dirty || this.destroyed) return;
212
+ if (this.inTransaction) {
213
+ this.flushDeferred = true;
214
+ return;
155
215
  }
156
- if (!this.dirty) return;
157
- this.pendingFlush = (async () => {
216
+ const prev = this.flushChain;
217
+ const step = (prev ?? Promise.resolve()).then(async () => {
218
+ if (!this.dirty || this.destroyed || this.inTransaction) return;
219
+ this.dirty = false;
158
220
  try {
159
- this.dirty = false;
160
221
  const exported = this.db.export();
161
222
  await this.fs.writeFile(this.filename, Buffer.from(exported));
162
223
  } catch (err) {
163
224
  this.dirty = true;
164
225
  throw err;
165
- } finally {
166
- this.pendingFlush = null;
167
226
  }
168
- })();
169
- await this.pendingFlush;
170
- if (this.dirty && !this.destroyed) {
171
- await this.flush();
172
- }
227
+ });
228
+ this.flushChain = step.catch(() => {
229
+ });
230
+ await step;
173
231
  }
174
232
  /** Close the database, flushing any pending writes first. */
175
233
  async close() {
@@ -178,6 +236,8 @@ var WasmSqliteConnection = class {
178
236
  clearTimeout(this.debounceTimer);
179
237
  this.debounceTimer = null;
180
238
  }
239
+ this.rootTxActive = false;
240
+ this.savepointDepth = 0;
181
241
  try {
182
242
  await this.flush();
183
243
  } finally {
@@ -263,7 +323,11 @@ function getClient_WasmSqlite() {
263
323
  if (isDdl) {
264
324
  db.run(obj.sql, bindings);
265
325
  obj.response = [];
266
- connection.markDirty("run");
326
+ if (/^\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE)\b/i.test(obj.sql)) {
327
+ connection.noteTransactionControl(obj.sql);
328
+ } else {
329
+ connection.markDirty("run");
330
+ }
267
331
  return obj;
268
332
  }
269
333
  if (isReadMethod(obj.method, obj.returning) || /^\s*PRAGMA\b/i.test(obj.sql)) {
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 connection.markDirty('run');\n return obj;\n }\n\n if (isReadMethod(obj.method, obj.returning) || /^\\s*PRAGMA\\b/i.test(obj.sql)) {\n const stmt = db.prepare(obj.sql);\n try {\n if (bindings.length) stmt.bind(bindings as any);\n const rows: Record<string, unknown>[] = [];\n while (stmt.step()) {\n rows.push(stmt.getAsObject());\n }\n obj.response = rows;\n } finally {\n stmt.free();\n }\n return obj;\n }\n\n // Write path: execute via `run` (no row iteration needed) and capture\n // SQLite's per-connection lastID / changes counters.\n db.run(obj.sql, bindings as any);\n const changes = db.getRowsModified();\n let lastID: number | bigint = 0;\n if (obj.method === 'insert') {\n const r = db.exec('SELECT last_insert_rowid() AS id');\n lastID = (r?.[0]?.values?.[0]?.[0] as number) ?? 0;\n }\n obj.response = [];\n obj.context = { lastID, changes };\n connection.markDirty(obj.method);\n return obj;\n }\n }\n\n Object.assign(Client_WasmSqlite.prototype, {\n dialect: 'sqlite3',\n driverName: 'wasm-sqlite',\n });\n\n cachedDialect = Client_WasmSqlite;\n return Client_WasmSqlite;\n}\n\n/**\n * Back-compat re-export. Prefer `getClient_WasmSqlite()` so the dialect\n * is resolved lazily; the named export triggers the factory on first\n * access of any static property.\n *\n * Note: importing this binding will execute the factory at import time\n * in some bundlers, which defeats the lazy pattern. New code should call\n * `getClient_WasmSqlite()` directly.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const Client_WasmSqlite: any = new Proxy(function () {} as any, {\n get(_t, prop) {\n return (getClient_WasmSqlite() as any)[prop];\n },\n construct(_t, args) {\n const Klass = getClient_WasmSqlite();\n return new Klass(...args);\n },\n apply(_t, thisArg, args) {\n const Klass = getClient_WasmSqlite();\n return Reflect.apply(Klass, thisArg, args);\n },\n});\n\nexport default Client_WasmSqlite;\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\n/**\n * Thin wrapper over sql.js {@link Database} that mimics the surface of\n * `better-sqlite3`'s `Database` (only the methods the Knex dialect uses).\n *\n * Persistence is handled here, not in the Knex dialect, so it can be\n * orchestrated per-connection without polluting the SQL execution path.\n */\n\nimport type { Database, SqlJsStatic } from 'sql.js';\n\n/** When to flush the in-memory WASM database to disk. */\nexport type PersistMode =\n | 'on-disconnect'\n | 'on-write'\n | `debounced:${number}`;\n\nexport interface WasmConnectionOptions {\n /**\n * On-disk file path. `:memory:` (or any value starting with `:`) skips\n * persistence entirely and the database lives only for the process.\n */\n filename: string;\n /** When to persist. Default: `on-disconnect`. */\n persist?: PersistMode;\n /** Pre-loaded sql.js module. If omitted, loaded lazily on first connect. */\n sqlJs?: SqlJsStatic;\n /**\n * Optional override for the `.wasm` locator passed to `initSqlJs()`.\n * Defaults to resolving the file from the `sql.js` package on disk\n * (works in Node and WebContainer).\n */\n locateFile?: (file: string) => string;\n /** Optional logger; defaults to `console`. */\n logger?: { warn: (msg: string, meta?: unknown) => void };\n}\n\n/** Mutation method names that should trigger a persistence cycle. */\nconst WRITE_METHODS = new Set([\n 'run',\n 'insert',\n 'update',\n 'del',\n 'counter',\n]);\n\n/**\n * Detect whether a Node-style `fs` module is available. WebContainer\n * (StackBlitz) provides Node `fs`; pure-browser environments do not.\n */\nasync function tryLoadFs(): Promise<typeof import('node:fs/promises') | null> {\n try {\n return await import('node:fs/promises');\n } catch {\n return null;\n }\n}\n\n/**\n * Resolve a default sql.js WASM locator. We point sql.js at the `.wasm`\n * file shipped inside `sql.js`'s own `dist/` folder. This avoids requiring\n * the caller to host the WASM separately.\n */\nasync function defaultLocateFile(): Promise<((file: string) => string) | undefined> {\n try {\n const { createRequire } = await import('node:module');\n const require = createRequire(import.meta.url);\n const pkgJsonPath = require.resolve('sql.js/package.json');\n const { dirname, join } = await import('node:path');\n const dir = dirname(pkgJsonPath);\n return (file: string) => join(dir, 'dist', file);\n } catch {\n return undefined;\n }\n}\n\nlet cachedSqlJs: Promise<SqlJsStatic> | null = null;\n\nasync function loadSqlJs(\n locateFile?: (file: string) => string,\n): Promise<SqlJsStatic> {\n if (cachedSqlJs) return cachedSqlJs;\n cachedSqlJs = (async () => {\n const mod = await import('sql.js');\n const initSqlJs = (mod as any).default ?? (mod as any);\n const locator = locateFile ?? (await defaultLocateFile());\n const SQL = await initSqlJs(locator ? { locateFile: locator } : undefined);\n return SQL as SqlJsStatic;\n })();\n return cachedSqlJs;\n}\n\n/**\n * A sql.js-backed connection that exposes the `prepare`/`exec`/`close`\n * subset used by Knex's SQLite dialect. Mutations are queued through a\n * configurable persistence strategy so the on-disk file stays in sync.\n */\nexport class WasmSqliteConnection {\n readonly filename: string;\n readonly persist: PersistMode;\n readonly isEphemeral: boolean;\n\n private db!: Database;\n private fs: typeof import('node:fs/promises') | null = null;\n private dirty = false;\n private debounceMs = 0;\n private debounceTimer: ReturnType<typeof setTimeout> | null = null;\n private pendingFlush: Promise<void> | null = null;\n private destroyed = false;\n private logger: { warn: (msg: string, meta?: unknown) => void };\n\n constructor(opts: WasmConnectionOptions) {\n this.filename = opts.filename;\n this.persist = opts.persist ?? 'on-disconnect';\n this.isEphemeral =\n this.filename === ':memory:' || this.filename.startsWith(':');\n this.logger = opts.logger ?? console;\n\n if (typeof this.persist === 'string' && this.persist.startsWith('debounced:')) {\n const ms = Number(this.persist.slice('debounced:'.length));\n this.debounceMs = Number.isFinite(ms) && ms > 0 ? ms : 250;\n }\n }\n\n /** Open the underlying sql.js database, loading bytes from disk if any. */\n async open(sqlJs?: SqlJsStatic, locateFile?: (file: string) => string): Promise<void> {\n const SQL = sqlJs ?? (await loadSqlJs(locateFile));\n\n if (this.isEphemeral) {\n this.db = new SQL.Database();\n return;\n }\n\n this.fs = await tryLoadFs();\n if (!this.fs) {\n this.logger.warn(\n '[driver-sqlite-wasm] No node:fs available — falling back to in-memory database. ' +\n 'Data will not be persisted across reloads.',\n );\n this.db = new SQL.Database();\n return;\n }\n\n // Ensure parent directory exists, then load bytes if the file exists.\n const { dirname } = await import('node:path');\n const dir = dirname(this.filename);\n if (dir && dir !== '.') {\n await this.fs.mkdir(dir, { recursive: true });\n }\n\n let bytes: Uint8Array | undefined;\n try {\n const buf = await this.fs.readFile(this.filename);\n bytes = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);\n } catch (e: any) {\n if (e?.code !== 'ENOENT') throw e;\n }\n\n this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();\n }\n\n /** Hint that a mutation just executed; schedule a flush if needed. */\n markDirty(method?: string): void {\n if (this.isEphemeral || !this.fs) return;\n if (method && !WRITE_METHODS.has(method)) return;\n this.dirty = true;\n\n if (this.persist === 'on-write') {\n void this.flush();\n return;\n }\n if (this.debounceMs > 0) {\n if (this.debounceTimer) clearTimeout(this.debounceTimer);\n this.debounceTimer = setTimeout(() => {\n this.debounceTimer = null;\n void this.flush();\n }, this.debounceMs);\n }\n // 'on-disconnect' → flush only at close()\n }\n\n /** Force a write of the current database state to disk. */\n async flush(): Promise<void> {\n if (this.isEphemeral || !this.fs || this.destroyed) return;\n // If a flush is already in flight, wait for it and then re-flush so any\n // writes that arrived after the in-flight flush's `db.export()` call get\n // persisted too. Without this, on-write mode loses writes that happen\n // between a flush's synchronous export and its async file write.\n if (this.pendingFlush) {\n await this.pendingFlush;\n if (!this.dirty || this.destroyed) return;\n }\n if (!this.dirty) return;\n\n this.pendingFlush = (async () => {\n try {\n // Snapshot dirty=false BEFORE export so concurrent writes that occur\n // during the async writeFile mark us dirty again and trigger another\n // flush via markDirty.\n this.dirty = false;\n const exported = this.db.export();\n // sql.js returns a Uint8Array; Buffer.from on it shares memory but\n // works fine for writeFile.\n await this.fs!.writeFile(this.filename, Buffer.from(exported));\n } catch (err) {\n // Restore dirty state so a subsequent flush retries.\n this.dirty = true;\n throw err;\n } finally {\n this.pendingFlush = null;\n }\n })();\n\n await this.pendingFlush;\n\n // A write that arrived after `db.export()` (synchronous) but before the\n // file write completed will have set dirty=true again. Re-flush to\n // persist it.\n if (this.dirty && !this.destroyed) {\n await this.flush();\n }\n }\n\n /** Close the database, flushing any pending writes first. */\n async close(): Promise<void> {\n if (this.destroyed) return;\n if (this.debounceTimer) {\n clearTimeout(this.debounceTimer);\n this.debounceTimer = null;\n }\n try {\n await this.flush();\n } finally {\n this.destroyed = true;\n try {\n this.db.close();\n } catch {\n /* ignore */\n }\n }\n }\n\n /** Access the raw sql.js database (for the Knex dialect). */\n get raw(): Database {\n return this.db;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACaA,wBAAgD;;;ACWhD,yBAA8B;;;ACxB9B;AAuCA,IAAM,gBAAgB,oBAAI,IAAI;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AAMD,eAAe,YAA+D;AAC5E,MAAI;AACF,WAAO,MAAM,OAAO,aAAkB;AAAA,EACxC,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAOA,eAAe,oBAAqE;AAClF,MAAI;AACF,UAAM,EAAE,eAAAA,eAAc,IAAI,MAAM,OAAO,QAAa;AACpD,UAAMC,WAAUD,eAAc,YAAY,GAAG;AAC7C,UAAM,cAAcC,SAAQ,QAAQ,qBAAqB;AACzD,UAAM,EAAE,SAAS,KAAK,IAAI,MAAM,OAAO,MAAW;AAClD,UAAM,MAAM,QAAQ,WAAW;AAC/B,WAAO,CAAC,SAAiB,KAAK,KAAK,QAAQ,IAAI;AAAA,EACjD,QAAQ;AACN,WAAO;AAAA,EACT;A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AAa;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 */\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,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,QAA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M,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"]}
package/dist/index.mjs CHANGED
@@ -49,8 +49,22 @@ var WasmSqliteConnection = class {
49
49
  this.dirty = false;
50
50
  this.debounceMs = 0;
51
51
  this.debounceTimer = null;
52
- this.pendingFlush = null;
52
+ this.flushChain = null;
53
53
  this.destroyed = false;
54
+ /**
55
+ * Whether a `BEGIN…COMMIT/ROLLBACK` transaction is currently open. Tracked
56
+ * because sql.js's {@link Database.export} closes and reopens the database
57
+ * (it has no in-place serialize), and closing a connection rolls back any
58
+ * open transaction. Flushing mid-transaction would therefore silently
59
+ * abort it, leaving the eventual `COMMIT` to fail with
60
+ * "cannot commit - no transaction is active". We defer the flush until the
61
+ * transaction fully closes. See {@link noteTransactionControl}.
62
+ */
63
+ this.rootTxActive = false;
64
+ /** Open `SAVEPOINT` depth (nested transactions emitted by Knex). */
65
+ this.savepointDepth = 0;
66
+ /** A flush was requested while a transaction was open; run it on close. */
67
+ this.flushDeferred = false;
54
68
  this.filename = opts.filename;
55
69
  this.persist = opts.persist ?? "on-disconnect";
56
70
  this.isEphemeral = this.filename === ":memory:" || this.filename.startsWith(":");
@@ -60,6 +74,10 @@ var WasmSqliteConnection = class {
60
74
  this.debounceMs = Number.isFinite(ms) && ms > 0 ? ms : 250;
61
75
  }
62
76
  }
77
+ /** True while any transaction (root or savepoint) is in flight. */
78
+ get inTransaction() {
79
+ return this.rootTxActive || this.savepointDepth > 0;
80
+ }
63
81
  /** Open the underlying sql.js database, loading bytes from disk if any. */
64
82
  async open(sqlJs, locateFile) {
65
83
  const SQL = sqlJs ?? await loadSqlJs(locateFile);
@@ -89,6 +107,37 @@ var WasmSqliteConnection = class {
89
107
  }
90
108
  this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();
91
109
  }
110
+ /**
111
+ * Update transaction state from a transaction-control statement and, when a
112
+ * transaction has just fully closed, run any flush that was deferred while
113
+ * it was open. Called by the Knex dialect for every `BEGIN` / `COMMIT` /
114
+ * `ROLLBACK` / `SAVEPOINT` / `RELEASE` statement.
115
+ *
116
+ * We bias toward "in transaction": an unrecognised form leaves the flag set,
117
+ * which at worst delays a flush (safe) rather than exporting mid-transaction
118
+ * (which would abort it).
119
+ */
120
+ noteTransactionControl(sql) {
121
+ const s = sql.trim().toUpperCase();
122
+ if (/^BEGIN\b/.test(s)) {
123
+ this.rootTxActive = true;
124
+ } else if (/^(COMMIT|END)\b/.test(s)) {
125
+ this.rootTxActive = false;
126
+ this.savepointDepth = 0;
127
+ } else if (/^ROLLBACK\s+TO\b/.test(s)) {
128
+ } else if (/^ROLLBACK\b/.test(s)) {
129
+ this.rootTxActive = false;
130
+ this.savepointDepth = 0;
131
+ } else if (/^SAVEPOINT\b/.test(s)) {
132
+ this.savepointDepth += 1;
133
+ } else if (/^RELEASE\b/.test(s)) {
134
+ this.savepointDepth = Math.max(0, this.savepointDepth - 1);
135
+ }
136
+ if (!this.inTransaction && this.flushDeferred) {
137
+ this.flushDeferred = false;
138
+ void this.flush();
139
+ }
140
+ }
92
141
  /** Hint that a mutation just executed; schedule a flush if needed. */
93
142
  markDirty(method) {
94
143
  if (this.isEphemeral || !this.fs) return;
@@ -106,30 +155,39 @@ var WasmSqliteConnection = class {
106
155
  }, this.debounceMs);
107
156
  }
108
157
  }
109
- /** Force a write of the current database state to disk. */
158
+ /**
159
+ * Force a write of the current database state to disk.
160
+ *
161
+ * Flushes are strictly serialized through a single promise chain: every call
162
+ * appends an export+write step that runs after all previously-queued steps.
163
+ * This matters because sql.js `export()` mutates the live connection (it
164
+ * closes and reopens the database), so two exports must never overlap — and
165
+ * because the returned promise must not resolve until the caller's own write
166
+ * has hit disk (deterministic for tests and for `close()`). Each step
167
+ * re-checks `dirty` at run time, so a no-op write collapses cheaply and a
168
+ * write that arrived mid-flush is captured by the next queued step.
169
+ */
110
170
  async flush() {
111
171
  if (this.isEphemeral || !this.fs || this.destroyed) return;
112
- if (this.pendingFlush) {
113
- await this.pendingFlush;
114
- if (!this.dirty || this.destroyed) return;
172
+ if (this.inTransaction) {
173
+ this.flushDeferred = true;
174
+ return;
115
175
  }
116
- if (!this.dirty) return;
117
- this.pendingFlush = (async () => {
176
+ const prev = this.flushChain;
177
+ const step = (prev ?? Promise.resolve()).then(async () => {
178
+ if (!this.dirty || this.destroyed || this.inTransaction) return;
179
+ this.dirty = false;
118
180
  try {
119
- this.dirty = false;
120
181
  const exported = this.db.export();
121
182
  await this.fs.writeFile(this.filename, Buffer.from(exported));
122
183
  } catch (err) {
123
184
  this.dirty = true;
124
185
  throw err;
125
- } finally {
126
- this.pendingFlush = null;
127
186
  }
128
- })();
129
- await this.pendingFlush;
130
- if (this.dirty && !this.destroyed) {
131
- await this.flush();
132
- }
187
+ });
188
+ this.flushChain = step.catch(() => {
189
+ });
190
+ await step;
133
191
  }
134
192
  /** Close the database, flushing any pending writes first. */
135
193
  async close() {
@@ -138,6 +196,8 @@ var WasmSqliteConnection = class {
138
196
  clearTimeout(this.debounceTimer);
139
197
  this.debounceTimer = null;
140
198
  }
199
+ this.rootTxActive = false;
200
+ this.savepointDepth = 0;
141
201
  try {
142
202
  await this.flush();
143
203
  } finally {
@@ -222,7 +282,11 @@ function getClient_WasmSqlite() {
222
282
  if (isDdl) {
223
283
  db.run(obj.sql, bindings);
224
284
  obj.response = [];
225
- connection.markDirty("run");
285
+ if (/^\s*(BEGIN|COMMIT|END|ROLLBACK|SAVEPOINT|RELEASE)\b/i.test(obj.sql)) {
286
+ connection.noteTransactionControl(obj.sql);
287
+ } else {
288
+ connection.markDirty("run");
289
+ }
226
290
  return obj;
227
291
  }
228
292
  if (isReadMethod(obj.method, obj.returning) || /^\s*PRAGMA\b/i.test(obj.sql)) {
@@ -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 connection.markDirty('run');\n return obj;\n }\n\n if (isReadMethod(obj.method, obj.returning) || /^\\s*PRAGMA\\b/i.test(obj.sql)) {\n const stmt = db.prepare(obj.sql);\n try {\n if (bindings.length) stmt.bind(bindings as any);\n const rows: Record<string, unknown>[] = [];\n while (stmt.step()) {\n rows.push(stmt.getAsObject());\n }\n obj.response = rows;\n } finally {\n stmt.free();\n }\n return obj;\n }\n\n // Write path: execute via `run` (no row iteration needed) and capture\n // SQLite's per-connection lastID / changes counters.\n db.run(obj.sql, bindings as any);\n const changes = db.getRowsModified();\n let lastID: number | bigint = 0;\n if (obj.method === 'insert') {\n const r = db.exec('SELECT last_insert_rowid() AS id');\n lastID = (r?.[0]?.values?.[0]?.[0] as number) ?? 0;\n }\n obj.response = [];\n obj.context = { lastID, changes };\n connection.markDirty(obj.method);\n return obj;\n }\n }\n\n Object.assign(Client_WasmSqlite.prototype, {\n dialect: 'sqlite3',\n driverName: 'wasm-sqlite',\n });\n\n cachedDialect = Client_WasmSqlite;\n return Client_WasmSqlite;\n}\n\n/**\n * Back-compat re-export. Prefer `getClient_WasmSqlite()` so the dialect\n * is resolved lazily; the named export triggers the factory on first\n * access of any static property.\n *\n * Note: importing this binding will execute the factory at import time\n * in some bundlers, which defeats the lazy pattern. New code should call\n * `getClient_WasmSqlite()` directly.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport const Client_WasmSqlite: any = new Proxy(function () {} as any, {\n get(_t, prop) {\n return (getClient_WasmSqlite() as any)[prop];\n },\n construct(_t, args) {\n const Klass = getClient_WasmSqlite();\n return new Klass(...args);\n },\n apply(_t, thisArg, args) {\n const Klass = getClient_WasmSqlite();\n return Reflect.apply(Klass, thisArg, args);\n },\n});\n\nexport default Client_WasmSqlite;\n","// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.\n\n/**\n * Thin wrapper over sql.js {@link Database} that mimics the surface of\n * `better-sqlite3`'s `Database` (only the methods the Knex dialect uses).\n *\n * Persistence is handled here, not in the Knex dialect, so it can be\n * orchestrated per-connection without polluting the SQL execution path.\n */\n\nimport type { Database, SqlJsStatic } from 'sql.js';\n\n/** When to flush the in-memory WASM database to disk. */\nexport type PersistMode =\n | 'on-disconnect'\n | 'on-write'\n | `debounced:${number}`;\n\nexport interface WasmConnectionOptions {\n /**\n * On-disk file path. `:memory:` (or any value starting with `:`) skips\n * persistence entirely and the database lives only for the process.\n */\n filename: string;\n /** When to persist. Default: `on-disconnect`. */\n persist?: PersistMode;\n /** Pre-loaded sql.js module. If omitted, loaded lazily on first connect. */\n sqlJs?: SqlJsStatic;\n /**\n * Optional override for the `.wasm` locator passed to `initSqlJs()`.\n * Defaults to resolving the file from the `sql.js` package on disk\n * (works in Node and WebContainer).\n */\n locateFile?: (file: string) => string;\n /** Optional logger; defaults to `console`. */\n logger?: { warn: (msg: string, meta?: unknown) => void };\n}\n\n/** Mutation method names that should trigger a persistence cycle. */\nconst WRITE_METHODS = new Set([\n 'run',\n 'insert',\n 'update',\n 'del',\n 'counter',\n]);\n\n/**\n * Detect whether a Node-style `fs` module is available. WebContainer\n * (StackBlitz) provides Node `fs`; pure-browser environments do not.\n */\nasync function tryLoadFs(): Promise<typeof import('node:fs/promises') | null> {\n try {\n return await import('node:fs/promises');\n } catch {\n return null;\n }\n}\n\n/**\n * Resolve a default sql.js WASM locator. We point sql.js at the `.wasm`\n * file shipped inside `sql.js`'s own `dist/` folder. This avoids requiring\n * the caller to host the WASM separately.\n */\nasync function defaultLocateFile(): Promise<((file: string) => string) | undefined> {\n try {\n const { createRequire } = await import('node:module');\n const require = createRequire(import.meta.url);\n const pkgJsonPath = require.resolve('sql.js/package.json');\n const { dirname, join } = await import('node:path');\n const dir = dirname(pkgJsonPath);\n return (file: string) => join(dir, 'dist', file);\n } catch {\n return undefined;\n }\n}\n\nlet cachedSqlJs: Promise<SqlJsStatic> | null = null;\n\nasync function loadSqlJs(\n locateFile?: (file: string) => string,\n): Promise<SqlJsStatic> {\n if (cachedSqlJs) return cachedSqlJs;\n cachedSqlJs = (async () => {\n const mod = await import('sql.js');\n const initSqlJs = (mod as any).default ?? (mod as any);\n const locator = locateFile ?? (await defaultLocateFile());\n const SQL = await initSqlJs(locator ? { locateFile: locator } : undefined);\n return SQL as SqlJsStatic;\n })();\n return cachedSqlJs;\n}\n\n/**\n * A sql.js-backed connection that exposes the `prepare`/`exec`/`close`\n * subset used by Knex's SQLite dialect. Mutations are queued through a\n * configurable persistence strategy so the on-disk file stays in sync.\n */\nexport class WasmSqliteConnection {\n readonly filename: string;\n readonly persist: PersistMode;\n readonly isEphemeral: boolean;\n\n private db!: Database;\n private fs: typeof import('node:fs/promises') | null = null;\n private dirty = false;\n private debounceMs = 0;\n private debounceTimer: ReturnType<typeof setTimeout> | null = null;\n private pendingFlush: Promise<void> | null = null;\n private destroyed = false;\n private logger: { warn: (msg: string, meta?: unknown) => void };\n\n constructor(opts: WasmConnectionOptions) {\n this.filename = opts.filename;\n this.persist = opts.persist ?? 'on-disconnect';\n this.isEphemeral =\n this.filename === ':memory:' || this.filename.startsWith(':');\n this.logger = opts.logger ?? console;\n\n if (typeof this.persist === 'string' && this.persist.startsWith('debounced:')) {\n const ms = Number(this.persist.slice('debounced:'.length));\n this.debounceMs = Number.isFinite(ms) && ms > 0 ? ms : 250;\n }\n }\n\n /** Open the underlying sql.js database, loading bytes from disk if any. */\n async open(sqlJs?: SqlJsStatic, locateFile?: (file: string) => string): Promise<void> {\n const SQL = sqlJs ?? (await loadSqlJs(locateFile));\n\n if (this.isEphemeral) {\n this.db = new SQL.Database();\n return;\n }\n\n this.fs = await tryLoadFs();\n if (!this.fs) {\n this.logger.warn(\n '[driver-sqlite-wasm] No node:fs available — falling back to in-memory database. ' +\n 'Data will not be persisted across reloads.',\n );\n this.db = new SQL.Database();\n return;\n }\n\n // Ensure parent directory exists, then load bytes if the file exists.\n const { dirname } = await import('node:path');\n const dir = dirname(this.filename);\n if (dir && dir !== '.') {\n await this.fs.mkdir(dir, { recursive: true });\n }\n\n let bytes: Uint8Array | undefined;\n try {\n const buf = await this.fs.readFile(this.filename);\n bytes = new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);\n } catch (e: any) {\n if (e?.code !== 'ENOENT') throw e;\n }\n\n this.db = bytes ? new SQL.Database(bytes) : new SQL.Database();\n }\n\n /** Hint that a mutation just executed; schedule a flush if needed. */\n markDirty(method?: string): void {\n if (this.isEphemeral || !this.fs) return;\n if (method && !WRITE_METHODS.has(method)) return;\n this.dirty = true;\n\n if (this.persist === 'on-write') {\n void this.flush();\n return;\n }\n if (this.debounceMs > 0) {\n if (this.debounceTimer) clearTimeout(this.debounceTimer);\n this.debounceTimer = setTimeout(() => {\n this.debounceTimer = null;\n void this.flush();\n }, this.debounceMs);\n }\n // 'on-disconnect' → flush only at close()\n }\n\n /** Force a write of the current database state to disk. */\n async flush(): Promise<void> {\n if (this.isEphemeral || !this.fs || this.destroyed) return;\n // If a flush is already in flight, wait for it and then re-flush so any\n // writes that arrived after the in-flight flush's `db.export()` call get\n // persisted too. Without this, on-write mode loses writes that happen\n // between a flush's synchronous export and its async file write.\n if (this.pendingFlush) {\n await this.pendingFlush;\n if (!this.dirty || this.destroyed) return;\n }\n if (!this.dirty) return;\n\n this.pendingFlush = (async () => {\n try {\n // Snapshot dirty=false BEFORE export so concurrent writes that occur\n // during the async writeFile mark us dirty again and trigger another\n // flush via markDirty.\n this.dirty = false;\n const exported = this.db.export();\n // sql.js returns a Uint8Array; Buffer.from on it shares memory but\n // works fine for writeFile.\n await this.fs!.writeFile(this.filename, Buffer.from(exported));\n } catch (err) {\n // Restore dirty state so a subsequent flush retries.\n this.dirty = true;\n throw err;\n } finally {\n this.pendingFlush = null;\n }\n })();\n\n await this.pendingFlush;\n\n // A write that arrived after `db.export()` (synchronous) but before the\n // file write completed will have set dirty=true again. Re-flush to\n // persist it.\n if (this.dirty && !this.destroyed) {\n await this.flush();\n }\n }\n\n /** Close the database, flushing any pending writes first. */\n async close(): Promise<void> {\n if (this.destroyed) return;\n if (this.debounceTimer) {\n clearTimeout(this.debounceTimer);\n this.debounceTimer = null;\n }\n try {\n await this.flush();\n } finally {\n this.destroyed = true;\n try {\n this.db.close();\n } catch {\n /* ignore */\n }\n }\n }\n\n /** Access the raw sql.js database (for the Knex dialect). */\n get raw(): Database {\n return this.db;\n }\n}\n","// 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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rD,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;;;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"]}
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;AA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AAM,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": "7.5.0",
3
+ "version": "7.6.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.2.10",
27
27
  "nanoid": "^5.1.11",
28
28
  "sql.js": "^1.14.1",
29
- "@objectstack/core": "7.5.0",
30
- "@objectstack/driver-sql": "7.5.0",
31
- "@objectstack/spec": "7.5.0"
29
+ "@objectstack/core": "7.6.0",
30
+ "@objectstack/driver-sql": "7.6.0",
31
+ "@objectstack/spec": "7.6.0"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^25.9.1",