js-bao 0.12.0-alpha.11 → 0.12.0-alpha.12

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/README.md CHANGED
@@ -1310,6 +1310,9 @@ public server path).
1310
1310
 
1311
1311
  - **Vite / Create React App**: copy `node_modules/sql.js/dist/sql-wasm.wasm` into
1312
1312
  your project's `public/` directory so it is served at `/sql-wasm.wasm`.
1313
+ Copy it from the sql.js that js-bao resolved, and again whenever that version
1314
+ changes: sql.js only loads the wasm built with it, and a wasm from another
1315
+ version rejects `ensureReady()` with a `sql.js wasm mismatch` error.
1313
1316
  - **Custom path**: if the wasm lives elsewhere, point at it via `DatabaseConfig`:
1314
1317
 
1315
1318
  ```typescript
package/dist/browser.cjs CHANGED
@@ -12432,7 +12432,43 @@ async function preflightNodeWasm(wasmPath) {
12432
12432
  );
12433
12433
  }
12434
12434
  }
12435
- async function loadSqlJsModule(locateFile) {
12435
+ var SQLJS_RUNTIME_INIT_TIMEOUT_MS = 5e3;
12436
+ var sqlJsWasmMismatch = null;
12437
+ var sqlJsLoading = null;
12438
+ function wasmMismatchError(wasmPath, cause) {
12439
+ const detail = cause instanceof Error ? cause.message : cause == null ? "" : String(cause);
12440
+ return new Error(
12441
+ `[SqljsEngine] sql.js wasm mismatch: "${wasmPath}" was built for a different sql.js version than the sql.js JavaScript loading it` + (detail ? ` (${detail})` : "") + ". Serve the sql-wasm.wasm from the installed sql.js package (node_modules/sql.js/dist/sql-wasm.wasm)."
12442
+ );
12443
+ }
12444
+ function isLinkError(reason) {
12445
+ if (typeof WebAssembly !== "undefined" && reason instanceof WebAssembly.LinkError) {
12446
+ return true;
12447
+ }
12448
+ return /LinkError/.test(
12449
+ reason instanceof Error ? reason.message : String(reason)
12450
+ );
12451
+ }
12452
+ function loadSqlJsModule(locateFile) {
12453
+ if (sqlJsWasmMismatch) {
12454
+ return Promise.reject(sqlJsWasmMismatch);
12455
+ }
12456
+ sqlJsLoading ??= guardedLoadSqlJs(locateFile).finally(() => {
12457
+ sqlJsLoading = null;
12458
+ });
12459
+ return sqlJsLoading;
12460
+ }
12461
+ async function guardedLoadSqlJs(locateFile) {
12462
+ const wasmPath = locateFile(SQL_WASM_FILENAME);
12463
+ let runtimeStarted = false;
12464
+ let runtimeFinished = false;
12465
+ let runtimeFailureCaught = false;
12466
+ let runtimeWatchdog;
12467
+ const recordMismatch = (cause) => {
12468
+ sqlJsWasmMismatch ??= wasmMismatchError(wasmPath, cause);
12469
+ return sqlJsWasmMismatch;
12470
+ };
12471
+ const runtimeFailed = () => runtimeStarted && !runtimeFinished;
12436
12472
  let abortReject;
12437
12473
  const toError = (reason) => reason instanceof Error ? reason : new Error(
12438
12474
  `[SqljsEngine] sql.js aborted during initialization: ${String(
@@ -12441,6 +12477,11 @@ async function loadSqlJsModule(locateFile) {
12441
12477
  );
12442
12478
  const nodeProcess = isNode() && typeof process !== "undefined" ? process : void 0;
12443
12479
  const onUnhandled = (reason) => {
12480
+ if (runtimeFailed() && !runtimeFailureCaught) {
12481
+ runtimeFailureCaught = true;
12482
+ abortReject?.(recordMismatch(reason));
12483
+ return;
12484
+ }
12444
12485
  const err = toError(reason);
12445
12486
  if (/Aborted/.test(err.message) || /Aborted/.test(String(reason))) {
12446
12487
  abortReject?.(err);
@@ -12463,11 +12504,36 @@ async function loadSqlJsModule(locateFile) {
12463
12504
  const initPromise = (0, import_sql10.default)({
12464
12505
  locateFile,
12465
12506
  // Layer 1: emscripten onAbort.
12466
- onAbort: (reason) => abortReject?.(toError(reason))
12507
+ onAbort: (reason) => abortReject?.(
12508
+ isLinkError(reason) ? recordMismatch(reason) : toError(reason)
12509
+ ),
12510
+ preRun: [
12511
+ () => {
12512
+ runtimeStarted = true;
12513
+ runtimeWatchdog = setTimeout(() => {
12514
+ if (runtimeFailed()) {
12515
+ abortReject?.(recordMismatch(void 0));
12516
+ }
12517
+ }, SQLJS_RUNTIME_INIT_TIMEOUT_MS);
12518
+ }
12519
+ ],
12520
+ postRun: [
12521
+ () => {
12522
+ runtimeFinished = true;
12523
+ }
12524
+ ]
12467
12525
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
12468
12526
  });
12469
- return await Promise.race([initPromise, abortGuard]);
12527
+ try {
12528
+ return await Promise.race([initPromise, abortGuard]);
12529
+ } catch (error) {
12530
+ if (sqlJsWasmMismatch) throw sqlJsWasmMismatch;
12531
+ if (isLinkError(error) || runtimeFailed()) throw recordMismatch(error);
12532
+ throw error;
12533
+ }
12470
12534
  } finally {
12535
+ clearTimeout(runtimeWatchdog);
12536
+ runtimeFailureCaught = true;
12471
12537
  if (nodeProcess) {
12472
12538
  setTimeout(() => {
12473
12539
  nodeProcess.removeListener("unhandledRejection", onUnhandled);
@@ -18708,6 +18774,7 @@ var OpfsEngine = class {
18708
18774
  }
18709
18775
  this.requestPersistence();
18710
18776
  entry.docIds.add(docId);
18777
+ entry.served.add(docId);
18711
18778
  const routing = this.docs.get(docId);
18712
18779
  if (routing && routing.entry === entry) routing.sessions++;
18713
18780
  else this.docs.set(docId, { entry, sessions: 1 });
@@ -19060,7 +19127,7 @@ var OpfsEngine = class {
19060
19127
  if (!attached) {
19061
19128
  return this.purgeWithoutAttachment(docId, namespace, force);
19062
19129
  }
19063
- return this.purgeThroughAttachment(docId, namespace, attached, force);
19130
+ return this.purgeThroughAttachment(docId, namespace, attached, force, { running: true });
19064
19131
  }
19065
19132
  /**
19066
19133
  * No attachment for the namespace: what is on disk decides what happens.
@@ -19096,8 +19163,13 @@ var OpfsEngine = class {
19096
19163
  }
19097
19164
  return this.purgeThroughAttachment(docId, namespace, entry, force);
19098
19165
  }
19099
- /** An attachment for the namespace: the worker decides, then the leader acts. */
19100
- async purgeThroughAttachment(docId, namespace, entry, force) {
19166
+ /**
19167
+ * An attachment for the namespace: the worker decides, then the leader acts.
19168
+ *
19169
+ * `running`: the worker was already up for this tab before the purge, rather
19170
+ * than spawned to answer it.
19171
+ */
19172
+ async purgeThroughAttachment(docId, namespace, entry, force, options = {}) {
19101
19173
  let reply;
19102
19174
  try {
19103
19175
  reply = await entry.attachment.purge(docId, force);
@@ -19127,6 +19199,12 @@ var OpfsEngine = class {
19127
19199
  Logger.info(
19128
19200
  `[OpfsEngine] ${docId}: local store purged (${reply.pendingOps} unacknowledged write(s) dropped, ${reply.remainingDocuments} document(s) left in the pool).`
19129
19201
  );
19202
+ if (reply.nothingStored && options.running && !entry.served.has(docId)) {
19203
+ Logger.debug(
19204
+ `[OpfsEngine] ${docId}: nothing of it was stored, so the running pool stays.`
19205
+ );
19206
+ return { ...purgedReport(reply), poolRemoved: false };
19207
+ }
19130
19208
  if (reply.remainingDocuments > 0) {
19131
19209
  Logger.debug(
19132
19210
  `[OpfsEngine] ${docId}: its pool still holds ${reply.remainingDocuments} document(s), so its directory stays.`
@@ -19527,7 +19605,8 @@ var OpfsEngine = class {
19527
19605
  poolName: namespace.poolName,
19528
19606
  attachment,
19529
19607
  engine: new RemoteQueryEngine(attachment),
19530
- docIds: /* @__PURE__ */ new Set()
19608
+ docIds: /* @__PURE__ */ new Set(),
19609
+ served: /* @__PURE__ */ new Set()
19531
19610
  };
19532
19611
  this.workers.set(namespace.poolName, entry);
19533
19612
  return entry;
package/dist/browser.js CHANGED
@@ -12263,7 +12263,43 @@ async function preflightNodeWasm(wasmPath) {
12263
12263
  );
12264
12264
  }
12265
12265
  }
12266
- async function loadSqlJsModule(locateFile) {
12266
+ var SQLJS_RUNTIME_INIT_TIMEOUT_MS = 5e3;
12267
+ var sqlJsWasmMismatch = null;
12268
+ var sqlJsLoading = null;
12269
+ function wasmMismatchError(wasmPath, cause) {
12270
+ const detail = cause instanceof Error ? cause.message : cause == null ? "" : String(cause);
12271
+ return new Error(
12272
+ `[SqljsEngine] sql.js wasm mismatch: "${wasmPath}" was built for a different sql.js version than the sql.js JavaScript loading it` + (detail ? ` (${detail})` : "") + ". Serve the sql-wasm.wasm from the installed sql.js package (node_modules/sql.js/dist/sql-wasm.wasm)."
12273
+ );
12274
+ }
12275
+ function isLinkError(reason) {
12276
+ if (typeof WebAssembly !== "undefined" && reason instanceof WebAssembly.LinkError) {
12277
+ return true;
12278
+ }
12279
+ return /LinkError/.test(
12280
+ reason instanceof Error ? reason.message : String(reason)
12281
+ );
12282
+ }
12283
+ function loadSqlJsModule(locateFile) {
12284
+ if (sqlJsWasmMismatch) {
12285
+ return Promise.reject(sqlJsWasmMismatch);
12286
+ }
12287
+ sqlJsLoading ??= guardedLoadSqlJs(locateFile).finally(() => {
12288
+ sqlJsLoading = null;
12289
+ });
12290
+ return sqlJsLoading;
12291
+ }
12292
+ async function guardedLoadSqlJs(locateFile) {
12293
+ const wasmPath = locateFile(SQL_WASM_FILENAME);
12294
+ let runtimeStarted = false;
12295
+ let runtimeFinished = false;
12296
+ let runtimeFailureCaught = false;
12297
+ let runtimeWatchdog;
12298
+ const recordMismatch = (cause) => {
12299
+ sqlJsWasmMismatch ??= wasmMismatchError(wasmPath, cause);
12300
+ return sqlJsWasmMismatch;
12301
+ };
12302
+ const runtimeFailed = () => runtimeStarted && !runtimeFinished;
12267
12303
  let abortReject;
12268
12304
  const toError = (reason) => reason instanceof Error ? reason : new Error(
12269
12305
  `[SqljsEngine] sql.js aborted during initialization: ${String(
@@ -12272,6 +12308,11 @@ async function loadSqlJsModule(locateFile) {
12272
12308
  );
12273
12309
  const nodeProcess = isNode() && typeof process !== "undefined" ? process : void 0;
12274
12310
  const onUnhandled = (reason) => {
12311
+ if (runtimeFailed() && !runtimeFailureCaught) {
12312
+ runtimeFailureCaught = true;
12313
+ abortReject?.(recordMismatch(reason));
12314
+ return;
12315
+ }
12275
12316
  const err = toError(reason);
12276
12317
  if (/Aborted/.test(err.message) || /Aborted/.test(String(reason))) {
12277
12318
  abortReject?.(err);
@@ -12294,11 +12335,36 @@ async function loadSqlJsModule(locateFile) {
12294
12335
  const initPromise = initSqlJs({
12295
12336
  locateFile,
12296
12337
  // Layer 1: emscripten onAbort.
12297
- onAbort: (reason) => abortReject?.(toError(reason))
12338
+ onAbort: (reason) => abortReject?.(
12339
+ isLinkError(reason) ? recordMismatch(reason) : toError(reason)
12340
+ ),
12341
+ preRun: [
12342
+ () => {
12343
+ runtimeStarted = true;
12344
+ runtimeWatchdog = setTimeout(() => {
12345
+ if (runtimeFailed()) {
12346
+ abortReject?.(recordMismatch(void 0));
12347
+ }
12348
+ }, SQLJS_RUNTIME_INIT_TIMEOUT_MS);
12349
+ }
12350
+ ],
12351
+ postRun: [
12352
+ () => {
12353
+ runtimeFinished = true;
12354
+ }
12355
+ ]
12298
12356
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
12299
12357
  });
12300
- return await Promise.race([initPromise, abortGuard]);
12358
+ try {
12359
+ return await Promise.race([initPromise, abortGuard]);
12360
+ } catch (error) {
12361
+ if (sqlJsWasmMismatch) throw sqlJsWasmMismatch;
12362
+ if (isLinkError(error) || runtimeFailed()) throw recordMismatch(error);
12363
+ throw error;
12364
+ }
12301
12365
  } finally {
12366
+ clearTimeout(runtimeWatchdog);
12367
+ runtimeFailureCaught = true;
12302
12368
  if (nodeProcess) {
12303
12369
  setTimeout(() => {
12304
12370
  nodeProcess.removeListener("unhandledRejection", onUnhandled);
@@ -18538,6 +18604,7 @@ var OpfsEngine = class {
18538
18604
  }
18539
18605
  this.requestPersistence();
18540
18606
  entry.docIds.add(docId);
18607
+ entry.served.add(docId);
18541
18608
  const routing = this.docs.get(docId);
18542
18609
  if (routing && routing.entry === entry) routing.sessions++;
18543
18610
  else this.docs.set(docId, { entry, sessions: 1 });
@@ -18890,7 +18957,7 @@ var OpfsEngine = class {
18890
18957
  if (!attached) {
18891
18958
  return this.purgeWithoutAttachment(docId, namespace, force);
18892
18959
  }
18893
- return this.purgeThroughAttachment(docId, namespace, attached, force);
18960
+ return this.purgeThroughAttachment(docId, namespace, attached, force, { running: true });
18894
18961
  }
18895
18962
  /**
18896
18963
  * No attachment for the namespace: what is on disk decides what happens.
@@ -18926,8 +18993,13 @@ var OpfsEngine = class {
18926
18993
  }
18927
18994
  return this.purgeThroughAttachment(docId, namespace, entry, force);
18928
18995
  }
18929
- /** An attachment for the namespace: the worker decides, then the leader acts. */
18930
- async purgeThroughAttachment(docId, namespace, entry, force) {
18996
+ /**
18997
+ * An attachment for the namespace: the worker decides, then the leader acts.
18998
+ *
18999
+ * `running`: the worker was already up for this tab before the purge, rather
19000
+ * than spawned to answer it.
19001
+ */
19002
+ async purgeThroughAttachment(docId, namespace, entry, force, options = {}) {
18931
19003
  let reply;
18932
19004
  try {
18933
19005
  reply = await entry.attachment.purge(docId, force);
@@ -18957,6 +19029,12 @@ var OpfsEngine = class {
18957
19029
  Logger.info(
18958
19030
  `[OpfsEngine] ${docId}: local store purged (${reply.pendingOps} unacknowledged write(s) dropped, ${reply.remainingDocuments} document(s) left in the pool).`
18959
19031
  );
19032
+ if (reply.nothingStored && options.running && !entry.served.has(docId)) {
19033
+ Logger.debug(
19034
+ `[OpfsEngine] ${docId}: nothing of it was stored, so the running pool stays.`
19035
+ );
19036
+ return { ...purgedReport(reply), poolRemoved: false };
19037
+ }
18960
19038
  if (reply.remainingDocuments > 0) {
18961
19039
  Logger.debug(
18962
19040
  `[OpfsEngine] ${docId}: its pool still holds ${reply.remainingDocuments} document(s), so its directory stays.`
@@ -19357,7 +19435,8 @@ var OpfsEngine = class {
19357
19435
  poolName: namespace.poolName,
19358
19436
  attachment,
19359
19437
  engine: new RemoteQueryEngine(attachment),
19360
- docIds: /* @__PURE__ */ new Set()
19438
+ docIds: /* @__PURE__ */ new Set(),
19439
+ served: /* @__PURE__ */ new Set()
19361
19440
  };
19362
19441
  this.workers.set(namespace.poolName, entry);
19363
19442
  return entry;
@@ -17337,7 +17337,13 @@ function purgeRecordLog(sql, docId, force) {
17337
17337
  );
17338
17338
  return { purged: false, reason: "pending", pendingOps: reply.pendingOps };
17339
17339
  }
17340
- return { purged: true, pendingOps: reply.pendingOps, handedBack: reply.handedBack };
17340
+ return {
17341
+ purged: true,
17342
+ // The store held nothing of this document: the purge removed nothing.
17343
+ ...reply.nothingStored ? { nothingStored: true } : {},
17344
+ pendingOps: reply.pendingOps,
17345
+ handedBack: reply.handedBack
17346
+ };
17341
17347
  }
17342
17348
  function purgeDocumentQueryRows(sql, docId) {
17343
17349
  const tables = Array.from(
@@ -19644,6 +19650,7 @@ function serveDb3Worker(port, deps) {
19644
19650
  if (!recordLog.purged) return recordLog;
19645
19651
  const reply = {
19646
19652
  purged: true,
19653
+ ...recordLog.nothingStored ? { nothingStored: true } : {},
19647
19654
  pendingOps: recordLog.pendingOps,
19648
19655
  remainingDocuments: remainingDocuments(sql2),
19649
19656
  ...recordLog.handedBack.length ? { handedBack: recordLog.handedBack } : {}
package/dist/index.cjs CHANGED
@@ -14450,7 +14450,13 @@ function purgeRecordLog(sql, docId, force) {
14450
14450
  );
14451
14451
  return { purged: false, reason: "pending", pendingOps: reply.pendingOps };
14452
14452
  }
14453
- return { purged: true, pendingOps: reply.pendingOps, handedBack: reply.handedBack };
14453
+ return {
14454
+ purged: true,
14455
+ // The store held nothing of this document: the purge removed nothing.
14456
+ ...reply.nothingStored ? { nothingStored: true } : {},
14457
+ pendingOps: reply.pendingOps,
14458
+ handedBack: reply.handedBack
14459
+ };
14454
14460
  }
14455
14461
  function purgeDocumentQueryRows(sql, docId) {
14456
14462
  const tables = Array.from(
@@ -16828,7 +16834,40 @@ async function preflightNodeWasm(wasmPath) {
16828
16834
  );
16829
16835
  }
16830
16836
  }
16831
- async function loadSqlJsModule(locateFile) {
16837
+ function wasmMismatchError(wasmPath, cause) {
16838
+ const detail = cause instanceof Error ? cause.message : cause == null ? "" : String(cause);
16839
+ return new Error(
16840
+ `[SqljsEngine] sql.js wasm mismatch: "${wasmPath}" was built for a different sql.js version than the sql.js JavaScript loading it` + (detail ? ` (${detail})` : "") + ". Serve the sql-wasm.wasm from the installed sql.js package (node_modules/sql.js/dist/sql-wasm.wasm)."
16841
+ );
16842
+ }
16843
+ function isLinkError(reason) {
16844
+ if (typeof WebAssembly !== "undefined" && reason instanceof WebAssembly.LinkError) {
16845
+ return true;
16846
+ }
16847
+ return /LinkError/.test(
16848
+ reason instanceof Error ? reason.message : String(reason)
16849
+ );
16850
+ }
16851
+ function loadSqlJsModule(locateFile) {
16852
+ if (sqlJsWasmMismatch) {
16853
+ return Promise.reject(sqlJsWasmMismatch);
16854
+ }
16855
+ sqlJsLoading ??= guardedLoadSqlJs(locateFile).finally(() => {
16856
+ sqlJsLoading = null;
16857
+ });
16858
+ return sqlJsLoading;
16859
+ }
16860
+ async function guardedLoadSqlJs(locateFile) {
16861
+ const wasmPath = locateFile(SQL_WASM_FILENAME);
16862
+ let runtimeStarted = false;
16863
+ let runtimeFinished = false;
16864
+ let runtimeFailureCaught = false;
16865
+ let runtimeWatchdog;
16866
+ const recordMismatch = (cause) => {
16867
+ sqlJsWasmMismatch ??= wasmMismatchError(wasmPath, cause);
16868
+ return sqlJsWasmMismatch;
16869
+ };
16870
+ const runtimeFailed = () => runtimeStarted && !runtimeFinished;
16832
16871
  let abortReject;
16833
16872
  const toError = (reason) => reason instanceof Error ? reason : new Error(
16834
16873
  `[SqljsEngine] sql.js aborted during initialization: ${String(
@@ -16837,6 +16876,11 @@ async function loadSqlJsModule(locateFile) {
16837
16876
  );
16838
16877
  const nodeProcess = isNode() && typeof process !== "undefined" ? process : void 0;
16839
16878
  const onUnhandled = (reason) => {
16879
+ if (runtimeFailed() && !runtimeFailureCaught) {
16880
+ runtimeFailureCaught = true;
16881
+ abortReject?.(recordMismatch(reason));
16882
+ return;
16883
+ }
16840
16884
  const err = toError(reason);
16841
16885
  if (/Aborted/.test(err.message) || /Aborted/.test(String(reason))) {
16842
16886
  abortReject?.(err);
@@ -16859,11 +16903,36 @@ async function loadSqlJsModule(locateFile) {
16859
16903
  const initPromise = (0, import_sql14.default)({
16860
16904
  locateFile,
16861
16905
  // Layer 1: emscripten onAbort.
16862
- onAbort: (reason) => abortReject?.(toError(reason))
16906
+ onAbort: (reason) => abortReject?.(
16907
+ isLinkError(reason) ? recordMismatch(reason) : toError(reason)
16908
+ ),
16909
+ preRun: [
16910
+ () => {
16911
+ runtimeStarted = true;
16912
+ runtimeWatchdog = setTimeout(() => {
16913
+ if (runtimeFailed()) {
16914
+ abortReject?.(recordMismatch(void 0));
16915
+ }
16916
+ }, SQLJS_RUNTIME_INIT_TIMEOUT_MS);
16917
+ }
16918
+ ],
16919
+ postRun: [
16920
+ () => {
16921
+ runtimeFinished = true;
16922
+ }
16923
+ ]
16863
16924
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
16864
16925
  });
16865
- return await Promise.race([initPromise, abortGuard]);
16926
+ try {
16927
+ return await Promise.race([initPromise, abortGuard]);
16928
+ } catch (error) {
16929
+ if (sqlJsWasmMismatch) throw sqlJsWasmMismatch;
16930
+ if (isLinkError(error) || runtimeFailed()) throw recordMismatch(error);
16931
+ throw error;
16932
+ }
16866
16933
  } finally {
16934
+ clearTimeout(runtimeWatchdog);
16935
+ runtimeFailureCaught = true;
16867
16936
  if (nodeProcess) {
16868
16937
  setTimeout(() => {
16869
16938
  nodeProcess.removeListener("unhandledRejection", onUnhandled);
@@ -16893,7 +16962,7 @@ function syncViewOf(db) {
16893
16962
  }
16894
16963
  };
16895
16964
  }
16896
- var import_sql14, import_async_mutex, import_meta, SQL_WASM_URL, SQL_WASM_FILENAME, TransactionalSQLJSOperations, SqljsEngine;
16965
+ var import_sql14, import_async_mutex, import_meta, SQL_WASM_URL, SQL_WASM_FILENAME, SQLJS_RUNTIME_INIT_TIMEOUT_MS, sqlJsWasmMismatch, sqlJsLoading, TransactionalSQLJSOperations, SqljsEngine;
16897
16966
  var init_SqljsEngine = __esm({
16898
16967
  "src/engines/SqljsEngine.ts"() {
16899
16968
  "use strict";
@@ -16907,6 +16976,9 @@ var init_SqljsEngine = __esm({
16907
16976
  import_meta = {};
16908
16977
  SQL_WASM_URL = "/sql-wasm.wasm";
16909
16978
  SQL_WASM_FILENAME = "sql-wasm.wasm";
16979
+ SQLJS_RUNTIME_INIT_TIMEOUT_MS = 5e3;
16980
+ sqlJsWasmMismatch = null;
16981
+ sqlJsLoading = null;
16910
16982
  TransactionalSQLJSOperations = class {
16911
16983
  db;
16912
16984
  engine;
@@ -19760,6 +19832,7 @@ var init_OpfsEngine = __esm({
19760
19832
  }
19761
19833
  this.requestPersistence();
19762
19834
  entry.docIds.add(docId);
19835
+ entry.served.add(docId);
19763
19836
  const routing = this.docs.get(docId);
19764
19837
  if (routing && routing.entry === entry) routing.sessions++;
19765
19838
  else this.docs.set(docId, { entry, sessions: 1 });
@@ -20112,7 +20185,7 @@ var init_OpfsEngine = __esm({
20112
20185
  if (!attached) {
20113
20186
  return this.purgeWithoutAttachment(docId, namespace, force);
20114
20187
  }
20115
- return this.purgeThroughAttachment(docId, namespace, attached, force);
20188
+ return this.purgeThroughAttachment(docId, namespace, attached, force, { running: true });
20116
20189
  }
20117
20190
  /**
20118
20191
  * No attachment for the namespace: what is on disk decides what happens.
@@ -20148,8 +20221,13 @@ var init_OpfsEngine = __esm({
20148
20221
  }
20149
20222
  return this.purgeThroughAttachment(docId, namespace, entry, force);
20150
20223
  }
20151
- /** An attachment for the namespace: the worker decides, then the leader acts. */
20152
- async purgeThroughAttachment(docId, namespace, entry, force) {
20224
+ /**
20225
+ * An attachment for the namespace: the worker decides, then the leader acts.
20226
+ *
20227
+ * `running`: the worker was already up for this tab before the purge, rather
20228
+ * than spawned to answer it.
20229
+ */
20230
+ async purgeThroughAttachment(docId, namespace, entry, force, options = {}) {
20153
20231
  let reply;
20154
20232
  try {
20155
20233
  reply = await entry.attachment.purge(docId, force);
@@ -20179,6 +20257,12 @@ var init_OpfsEngine = __esm({
20179
20257
  Logger.info(
20180
20258
  `[OpfsEngine] ${docId}: local store purged (${reply.pendingOps} unacknowledged write(s) dropped, ${reply.remainingDocuments} document(s) left in the pool).`
20181
20259
  );
20260
+ if (reply.nothingStored && options.running && !entry.served.has(docId)) {
20261
+ Logger.debug(
20262
+ `[OpfsEngine] ${docId}: nothing of it was stored, so the running pool stays.`
20263
+ );
20264
+ return { ...purgedReport(reply), poolRemoved: false };
20265
+ }
20182
20266
  if (reply.remainingDocuments > 0) {
20183
20267
  Logger.debug(
20184
20268
  `[OpfsEngine] ${docId}: its pool still holds ${reply.remainingDocuments} document(s), so its directory stays.`
@@ -20579,7 +20663,8 @@ var init_OpfsEngine = __esm({
20579
20663
  poolName: namespace.poolName,
20580
20664
  attachment,
20581
20665
  engine: new RemoteQueryEngine(attachment),
20582
- docIds: /* @__PURE__ */ new Set()
20666
+ docIds: /* @__PURE__ */ new Set(),
20667
+ served: /* @__PURE__ */ new Set()
20583
20668
  };
20584
20669
  this.workers.set(namespace.poolName, entry);
20585
20670
  return entry;
package/dist/index.js CHANGED
@@ -14433,7 +14433,13 @@ function purgeRecordLog(sql, docId, force) {
14433
14433
  );
14434
14434
  return { purged: false, reason: "pending", pendingOps: reply.pendingOps };
14435
14435
  }
14436
- return { purged: true, pendingOps: reply.pendingOps, handedBack: reply.handedBack };
14436
+ return {
14437
+ purged: true,
14438
+ // The store held nothing of this document: the purge removed nothing.
14439
+ ...reply.nothingStored ? { nothingStored: true } : {},
14440
+ pendingOps: reply.pendingOps,
14441
+ handedBack: reply.handedBack
14442
+ };
14437
14443
  }
14438
14444
  function purgeDocumentQueryRows(sql, docId) {
14439
14445
  const tables = Array.from(
@@ -16813,7 +16819,40 @@ async function preflightNodeWasm(wasmPath) {
16813
16819
  );
16814
16820
  }
16815
16821
  }
16816
- async function loadSqlJsModule(locateFile) {
16822
+ function wasmMismatchError(wasmPath, cause) {
16823
+ const detail = cause instanceof Error ? cause.message : cause == null ? "" : String(cause);
16824
+ return new Error(
16825
+ `[SqljsEngine] sql.js wasm mismatch: "${wasmPath}" was built for a different sql.js version than the sql.js JavaScript loading it` + (detail ? ` (${detail})` : "") + ". Serve the sql-wasm.wasm from the installed sql.js package (node_modules/sql.js/dist/sql-wasm.wasm)."
16826
+ );
16827
+ }
16828
+ function isLinkError(reason) {
16829
+ if (typeof WebAssembly !== "undefined" && reason instanceof WebAssembly.LinkError) {
16830
+ return true;
16831
+ }
16832
+ return /LinkError/.test(
16833
+ reason instanceof Error ? reason.message : String(reason)
16834
+ );
16835
+ }
16836
+ function loadSqlJsModule(locateFile) {
16837
+ if (sqlJsWasmMismatch) {
16838
+ return Promise.reject(sqlJsWasmMismatch);
16839
+ }
16840
+ sqlJsLoading ??= guardedLoadSqlJs(locateFile).finally(() => {
16841
+ sqlJsLoading = null;
16842
+ });
16843
+ return sqlJsLoading;
16844
+ }
16845
+ async function guardedLoadSqlJs(locateFile) {
16846
+ const wasmPath = locateFile(SQL_WASM_FILENAME);
16847
+ let runtimeStarted = false;
16848
+ let runtimeFinished = false;
16849
+ let runtimeFailureCaught = false;
16850
+ let runtimeWatchdog;
16851
+ const recordMismatch = (cause) => {
16852
+ sqlJsWasmMismatch ??= wasmMismatchError(wasmPath, cause);
16853
+ return sqlJsWasmMismatch;
16854
+ };
16855
+ const runtimeFailed = () => runtimeStarted && !runtimeFinished;
16817
16856
  let abortReject;
16818
16857
  const toError = (reason) => reason instanceof Error ? reason : new Error(
16819
16858
  `[SqljsEngine] sql.js aborted during initialization: ${String(
@@ -16822,6 +16861,11 @@ async function loadSqlJsModule(locateFile) {
16822
16861
  );
16823
16862
  const nodeProcess = isNode() && typeof process !== "undefined" ? process : void 0;
16824
16863
  const onUnhandled = (reason) => {
16864
+ if (runtimeFailed() && !runtimeFailureCaught) {
16865
+ runtimeFailureCaught = true;
16866
+ abortReject?.(recordMismatch(reason));
16867
+ return;
16868
+ }
16825
16869
  const err = toError(reason);
16826
16870
  if (/Aborted/.test(err.message) || /Aborted/.test(String(reason))) {
16827
16871
  abortReject?.(err);
@@ -16844,11 +16888,36 @@ async function loadSqlJsModule(locateFile) {
16844
16888
  const initPromise = initSqlJs({
16845
16889
  locateFile,
16846
16890
  // Layer 1: emscripten onAbort.
16847
- onAbort: (reason) => abortReject?.(toError(reason))
16891
+ onAbort: (reason) => abortReject?.(
16892
+ isLinkError(reason) ? recordMismatch(reason) : toError(reason)
16893
+ ),
16894
+ preRun: [
16895
+ () => {
16896
+ runtimeStarted = true;
16897
+ runtimeWatchdog = setTimeout(() => {
16898
+ if (runtimeFailed()) {
16899
+ abortReject?.(recordMismatch(void 0));
16900
+ }
16901
+ }, SQLJS_RUNTIME_INIT_TIMEOUT_MS);
16902
+ }
16903
+ ],
16904
+ postRun: [
16905
+ () => {
16906
+ runtimeFinished = true;
16907
+ }
16908
+ ]
16848
16909
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
16849
16910
  });
16850
- return await Promise.race([initPromise, abortGuard]);
16911
+ try {
16912
+ return await Promise.race([initPromise, abortGuard]);
16913
+ } catch (error) {
16914
+ if (sqlJsWasmMismatch) throw sqlJsWasmMismatch;
16915
+ if (isLinkError(error) || runtimeFailed()) throw recordMismatch(error);
16916
+ throw error;
16917
+ }
16851
16918
  } finally {
16919
+ clearTimeout(runtimeWatchdog);
16920
+ runtimeFailureCaught = true;
16852
16921
  if (nodeProcess) {
16853
16922
  setTimeout(() => {
16854
16923
  nodeProcess.removeListener("unhandledRejection", onUnhandled);
@@ -16878,7 +16947,7 @@ function syncViewOf(db) {
16878
16947
  }
16879
16948
  };
16880
16949
  }
16881
- var SQL_WASM_URL, SQL_WASM_FILENAME, TransactionalSQLJSOperations, SqljsEngine;
16950
+ var SQL_WASM_URL, SQL_WASM_FILENAME, SQLJS_RUNTIME_INIT_TIMEOUT_MS, sqlJsWasmMismatch, sqlJsLoading, TransactionalSQLJSOperations, SqljsEngine;
16882
16951
  var init_SqljsEngine = __esm({
16883
16952
  "src/engines/SqljsEngine.ts"() {
16884
16953
  "use strict";
@@ -16889,6 +16958,9 @@ var init_SqljsEngine = __esm({
16889
16958
  init_environment();
16890
16959
  SQL_WASM_URL = "/sql-wasm.wasm";
16891
16960
  SQL_WASM_FILENAME = "sql-wasm.wasm";
16961
+ SQLJS_RUNTIME_INIT_TIMEOUT_MS = 5e3;
16962
+ sqlJsWasmMismatch = null;
16963
+ sqlJsLoading = null;
16892
16964
  TransactionalSQLJSOperations = class {
16893
16965
  db;
16894
16966
  engine;
@@ -19741,6 +19813,7 @@ var init_OpfsEngine = __esm({
19741
19813
  }
19742
19814
  this.requestPersistence();
19743
19815
  entry.docIds.add(docId);
19816
+ entry.served.add(docId);
19744
19817
  const routing = this.docs.get(docId);
19745
19818
  if (routing && routing.entry === entry) routing.sessions++;
19746
19819
  else this.docs.set(docId, { entry, sessions: 1 });
@@ -20093,7 +20166,7 @@ var init_OpfsEngine = __esm({
20093
20166
  if (!attached) {
20094
20167
  return this.purgeWithoutAttachment(docId, namespace, force);
20095
20168
  }
20096
- return this.purgeThroughAttachment(docId, namespace, attached, force);
20169
+ return this.purgeThroughAttachment(docId, namespace, attached, force, { running: true });
20097
20170
  }
20098
20171
  /**
20099
20172
  * No attachment for the namespace: what is on disk decides what happens.
@@ -20129,8 +20202,13 @@ var init_OpfsEngine = __esm({
20129
20202
  }
20130
20203
  return this.purgeThroughAttachment(docId, namespace, entry, force);
20131
20204
  }
20132
- /** An attachment for the namespace: the worker decides, then the leader acts. */
20133
- async purgeThroughAttachment(docId, namespace, entry, force) {
20205
+ /**
20206
+ * An attachment for the namespace: the worker decides, then the leader acts.
20207
+ *
20208
+ * `running`: the worker was already up for this tab before the purge, rather
20209
+ * than spawned to answer it.
20210
+ */
20211
+ async purgeThroughAttachment(docId, namespace, entry, force, options = {}) {
20134
20212
  let reply;
20135
20213
  try {
20136
20214
  reply = await entry.attachment.purge(docId, force);
@@ -20160,6 +20238,12 @@ var init_OpfsEngine = __esm({
20160
20238
  Logger.info(
20161
20239
  `[OpfsEngine] ${docId}: local store purged (${reply.pendingOps} unacknowledged write(s) dropped, ${reply.remainingDocuments} document(s) left in the pool).`
20162
20240
  );
20241
+ if (reply.nothingStored && options.running && !entry.served.has(docId)) {
20242
+ Logger.debug(
20243
+ `[OpfsEngine] ${docId}: nothing of it was stored, so the running pool stays.`
20244
+ );
20245
+ return { ...purgedReport(reply), poolRemoved: false };
20246
+ }
20163
20247
  if (reply.remainingDocuments > 0) {
20164
20248
  Logger.debug(
20165
20249
  `[OpfsEngine] ${docId}: its pool still holds ${reply.remainingDocuments} document(s), so its directory stays.`
@@ -20560,7 +20644,8 @@ var init_OpfsEngine = __esm({
20560
20644
  poolName: namespace.poolName,
20561
20645
  attachment,
20562
20646
  engine: new RemoteQueryEngine(attachment),
20563
- docIds: /* @__PURE__ */ new Set()
20647
+ docIds: /* @__PURE__ */ new Set(),
20648
+ served: /* @__PURE__ */ new Set()
20564
20649
  };
20565
20650
  this.workers.set(namespace.poolName, entry);
20566
20651
  return entry;
package/dist/node.cjs CHANGED
@@ -12295,7 +12295,40 @@ async function preflightNodeWasm(wasmPath) {
12295
12295
  );
12296
12296
  }
12297
12297
  }
12298
- async function loadSqlJsModule(locateFile) {
12298
+ function wasmMismatchError(wasmPath, cause) {
12299
+ const detail = cause instanceof Error ? cause.message : cause == null ? "" : String(cause);
12300
+ return new Error(
12301
+ `[SqljsEngine] sql.js wasm mismatch: "${wasmPath}" was built for a different sql.js version than the sql.js JavaScript loading it` + (detail ? ` (${detail})` : "") + ". Serve the sql-wasm.wasm from the installed sql.js package (node_modules/sql.js/dist/sql-wasm.wasm)."
12302
+ );
12303
+ }
12304
+ function isLinkError(reason) {
12305
+ if (typeof WebAssembly !== "undefined" && reason instanceof WebAssembly.LinkError) {
12306
+ return true;
12307
+ }
12308
+ return /LinkError/.test(
12309
+ reason instanceof Error ? reason.message : String(reason)
12310
+ );
12311
+ }
12312
+ function loadSqlJsModule(locateFile) {
12313
+ if (sqlJsWasmMismatch) {
12314
+ return Promise.reject(sqlJsWasmMismatch);
12315
+ }
12316
+ sqlJsLoading ??= guardedLoadSqlJs(locateFile).finally(() => {
12317
+ sqlJsLoading = null;
12318
+ });
12319
+ return sqlJsLoading;
12320
+ }
12321
+ async function guardedLoadSqlJs(locateFile) {
12322
+ const wasmPath = locateFile(SQL_WASM_FILENAME);
12323
+ let runtimeStarted = false;
12324
+ let runtimeFinished = false;
12325
+ let runtimeFailureCaught = false;
12326
+ let runtimeWatchdog;
12327
+ const recordMismatch = (cause) => {
12328
+ sqlJsWasmMismatch ??= wasmMismatchError(wasmPath, cause);
12329
+ return sqlJsWasmMismatch;
12330
+ };
12331
+ const runtimeFailed = () => runtimeStarted && !runtimeFinished;
12299
12332
  let abortReject;
12300
12333
  const toError = (reason) => reason instanceof Error ? reason : new Error(
12301
12334
  `[SqljsEngine] sql.js aborted during initialization: ${String(
@@ -12304,6 +12337,11 @@ async function loadSqlJsModule(locateFile) {
12304
12337
  );
12305
12338
  const nodeProcess = isNode() && typeof process !== "undefined" ? process : void 0;
12306
12339
  const onUnhandled = (reason) => {
12340
+ if (runtimeFailed() && !runtimeFailureCaught) {
12341
+ runtimeFailureCaught = true;
12342
+ abortReject?.(recordMismatch(reason));
12343
+ return;
12344
+ }
12307
12345
  const err = toError(reason);
12308
12346
  if (/Aborted/.test(err.message) || /Aborted/.test(String(reason))) {
12309
12347
  abortReject?.(err);
@@ -12326,11 +12364,36 @@ async function loadSqlJsModule(locateFile) {
12326
12364
  const initPromise = (0, import_sql10.default)({
12327
12365
  locateFile,
12328
12366
  // Layer 1: emscripten onAbort.
12329
- onAbort: (reason) => abortReject?.(toError(reason))
12367
+ onAbort: (reason) => abortReject?.(
12368
+ isLinkError(reason) ? recordMismatch(reason) : toError(reason)
12369
+ ),
12370
+ preRun: [
12371
+ () => {
12372
+ runtimeStarted = true;
12373
+ runtimeWatchdog = setTimeout(() => {
12374
+ if (runtimeFailed()) {
12375
+ abortReject?.(recordMismatch(void 0));
12376
+ }
12377
+ }, SQLJS_RUNTIME_INIT_TIMEOUT_MS);
12378
+ }
12379
+ ],
12380
+ postRun: [
12381
+ () => {
12382
+ runtimeFinished = true;
12383
+ }
12384
+ ]
12330
12385
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
12331
12386
  });
12332
- return await Promise.race([initPromise, abortGuard]);
12387
+ try {
12388
+ return await Promise.race([initPromise, abortGuard]);
12389
+ } catch (error) {
12390
+ if (sqlJsWasmMismatch) throw sqlJsWasmMismatch;
12391
+ if (isLinkError(error) || runtimeFailed()) throw recordMismatch(error);
12392
+ throw error;
12393
+ }
12333
12394
  } finally {
12395
+ clearTimeout(runtimeWatchdog);
12396
+ runtimeFailureCaught = true;
12334
12397
  if (nodeProcess) {
12335
12398
  setTimeout(() => {
12336
12399
  nodeProcess.removeListener("unhandledRejection", onUnhandled);
@@ -12360,7 +12423,7 @@ function syncViewOf(db) {
12360
12423
  }
12361
12424
  };
12362
12425
  }
12363
- var import_sql10, import_async_mutex, import_meta, SQL_WASM_URL, SQL_WASM_FILENAME, TransactionalSQLJSOperations, SqljsEngine;
12426
+ var import_sql10, import_async_mutex, import_meta, SQL_WASM_URL, SQL_WASM_FILENAME, SQLJS_RUNTIME_INIT_TIMEOUT_MS, sqlJsWasmMismatch, sqlJsLoading, TransactionalSQLJSOperations, SqljsEngine;
12364
12427
  var init_SqljsEngine = __esm({
12365
12428
  "src/engines/SqljsEngine.ts"() {
12366
12429
  "use strict";
@@ -12374,6 +12437,9 @@ var init_SqljsEngine = __esm({
12374
12437
  import_meta = {};
12375
12438
  SQL_WASM_URL = "/sql-wasm.wasm";
12376
12439
  SQL_WASM_FILENAME = "sql-wasm.wasm";
12440
+ SQLJS_RUNTIME_INIT_TIMEOUT_MS = 5e3;
12441
+ sqlJsWasmMismatch = null;
12442
+ sqlJsLoading = null;
12377
12443
  TransactionalSQLJSOperations = class {
12378
12444
  db;
12379
12445
  engine;
@@ -14702,7 +14768,13 @@ function purgeRecordLog(sql, docId, force) {
14702
14768
  );
14703
14769
  return { purged: false, reason: "pending", pendingOps: reply.pendingOps };
14704
14770
  }
14705
- return { purged: true, pendingOps: reply.pendingOps, handedBack: reply.handedBack };
14771
+ return {
14772
+ purged: true,
14773
+ // The store held nothing of this document: the purge removed nothing.
14774
+ ...reply.nothingStored ? { nothingStored: true } : {},
14775
+ pendingOps: reply.pendingOps,
14776
+ handedBack: reply.handedBack
14777
+ };
14706
14778
  }
14707
14779
  function purgeDocumentQueryRows(sql, docId) {
14708
14780
  const tables = Array.from(
@@ -19820,6 +19892,7 @@ var init_OpfsEngine = __esm({
19820
19892
  }
19821
19893
  this.requestPersistence();
19822
19894
  entry.docIds.add(docId);
19895
+ entry.served.add(docId);
19823
19896
  const routing = this.docs.get(docId);
19824
19897
  if (routing && routing.entry === entry) routing.sessions++;
19825
19898
  else this.docs.set(docId, { entry, sessions: 1 });
@@ -20172,7 +20245,7 @@ var init_OpfsEngine = __esm({
20172
20245
  if (!attached) {
20173
20246
  return this.purgeWithoutAttachment(docId, namespace, force);
20174
20247
  }
20175
- return this.purgeThroughAttachment(docId, namespace, attached, force);
20248
+ return this.purgeThroughAttachment(docId, namespace, attached, force, { running: true });
20176
20249
  }
20177
20250
  /**
20178
20251
  * No attachment for the namespace: what is on disk decides what happens.
@@ -20208,8 +20281,13 @@ var init_OpfsEngine = __esm({
20208
20281
  }
20209
20282
  return this.purgeThroughAttachment(docId, namespace, entry, force);
20210
20283
  }
20211
- /** An attachment for the namespace: the worker decides, then the leader acts. */
20212
- async purgeThroughAttachment(docId, namespace, entry, force) {
20284
+ /**
20285
+ * An attachment for the namespace: the worker decides, then the leader acts.
20286
+ *
20287
+ * `running`: the worker was already up for this tab before the purge, rather
20288
+ * than spawned to answer it.
20289
+ */
20290
+ async purgeThroughAttachment(docId, namespace, entry, force, options = {}) {
20213
20291
  let reply;
20214
20292
  try {
20215
20293
  reply = await entry.attachment.purge(docId, force);
@@ -20239,6 +20317,12 @@ var init_OpfsEngine = __esm({
20239
20317
  Logger.info(
20240
20318
  `[OpfsEngine] ${docId}: local store purged (${reply.pendingOps} unacknowledged write(s) dropped, ${reply.remainingDocuments} document(s) left in the pool).`
20241
20319
  );
20320
+ if (reply.nothingStored && options.running && !entry.served.has(docId)) {
20321
+ Logger.debug(
20322
+ `[OpfsEngine] ${docId}: nothing of it was stored, so the running pool stays.`
20323
+ );
20324
+ return { ...purgedReport(reply), poolRemoved: false };
20325
+ }
20242
20326
  if (reply.remainingDocuments > 0) {
20243
20327
  Logger.debug(
20244
20328
  `[OpfsEngine] ${docId}: its pool still holds ${reply.remainingDocuments} document(s), so its directory stays.`
@@ -20639,7 +20723,8 @@ var init_OpfsEngine = __esm({
20639
20723
  poolName: namespace.poolName,
20640
20724
  attachment,
20641
20725
  engine: new RemoteQueryEngine(attachment),
20642
- docIds: /* @__PURE__ */ new Set()
20726
+ docIds: /* @__PURE__ */ new Set(),
20727
+ served: /* @__PURE__ */ new Set()
20643
20728
  };
20644
20729
  this.workers.set(namespace.poolName, entry);
20645
20730
  return entry;
package/dist/node.js CHANGED
@@ -12280,7 +12280,40 @@ async function preflightNodeWasm(wasmPath) {
12280
12280
  );
12281
12281
  }
12282
12282
  }
12283
- async function loadSqlJsModule(locateFile) {
12283
+ function wasmMismatchError(wasmPath, cause) {
12284
+ const detail = cause instanceof Error ? cause.message : cause == null ? "" : String(cause);
12285
+ return new Error(
12286
+ `[SqljsEngine] sql.js wasm mismatch: "${wasmPath}" was built for a different sql.js version than the sql.js JavaScript loading it` + (detail ? ` (${detail})` : "") + ". Serve the sql-wasm.wasm from the installed sql.js package (node_modules/sql.js/dist/sql-wasm.wasm)."
12287
+ );
12288
+ }
12289
+ function isLinkError(reason) {
12290
+ if (typeof WebAssembly !== "undefined" && reason instanceof WebAssembly.LinkError) {
12291
+ return true;
12292
+ }
12293
+ return /LinkError/.test(
12294
+ reason instanceof Error ? reason.message : String(reason)
12295
+ );
12296
+ }
12297
+ function loadSqlJsModule(locateFile) {
12298
+ if (sqlJsWasmMismatch) {
12299
+ return Promise.reject(sqlJsWasmMismatch);
12300
+ }
12301
+ sqlJsLoading ??= guardedLoadSqlJs(locateFile).finally(() => {
12302
+ sqlJsLoading = null;
12303
+ });
12304
+ return sqlJsLoading;
12305
+ }
12306
+ async function guardedLoadSqlJs(locateFile) {
12307
+ const wasmPath = locateFile(SQL_WASM_FILENAME);
12308
+ let runtimeStarted = false;
12309
+ let runtimeFinished = false;
12310
+ let runtimeFailureCaught = false;
12311
+ let runtimeWatchdog;
12312
+ const recordMismatch = (cause) => {
12313
+ sqlJsWasmMismatch ??= wasmMismatchError(wasmPath, cause);
12314
+ return sqlJsWasmMismatch;
12315
+ };
12316
+ const runtimeFailed = () => runtimeStarted && !runtimeFinished;
12284
12317
  let abortReject;
12285
12318
  const toError = (reason) => reason instanceof Error ? reason : new Error(
12286
12319
  `[SqljsEngine] sql.js aborted during initialization: ${String(
@@ -12289,6 +12322,11 @@ async function loadSqlJsModule(locateFile) {
12289
12322
  );
12290
12323
  const nodeProcess = isNode() && typeof process !== "undefined" ? process : void 0;
12291
12324
  const onUnhandled = (reason) => {
12325
+ if (runtimeFailed() && !runtimeFailureCaught) {
12326
+ runtimeFailureCaught = true;
12327
+ abortReject?.(recordMismatch(reason));
12328
+ return;
12329
+ }
12292
12330
  const err = toError(reason);
12293
12331
  if (/Aborted/.test(err.message) || /Aborted/.test(String(reason))) {
12294
12332
  abortReject?.(err);
@@ -12311,11 +12349,36 @@ async function loadSqlJsModule(locateFile) {
12311
12349
  const initPromise = initSqlJs({
12312
12350
  locateFile,
12313
12351
  // Layer 1: emscripten onAbort.
12314
- onAbort: (reason) => abortReject?.(toError(reason))
12352
+ onAbort: (reason) => abortReject?.(
12353
+ isLinkError(reason) ? recordMismatch(reason) : toError(reason)
12354
+ ),
12355
+ preRun: [
12356
+ () => {
12357
+ runtimeStarted = true;
12358
+ runtimeWatchdog = setTimeout(() => {
12359
+ if (runtimeFailed()) {
12360
+ abortReject?.(recordMismatch(void 0));
12361
+ }
12362
+ }, SQLJS_RUNTIME_INIT_TIMEOUT_MS);
12363
+ }
12364
+ ],
12365
+ postRun: [
12366
+ () => {
12367
+ runtimeFinished = true;
12368
+ }
12369
+ ]
12315
12370
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
12316
12371
  });
12317
- return await Promise.race([initPromise, abortGuard]);
12372
+ try {
12373
+ return await Promise.race([initPromise, abortGuard]);
12374
+ } catch (error) {
12375
+ if (sqlJsWasmMismatch) throw sqlJsWasmMismatch;
12376
+ if (isLinkError(error) || runtimeFailed()) throw recordMismatch(error);
12377
+ throw error;
12378
+ }
12318
12379
  } finally {
12380
+ clearTimeout(runtimeWatchdog);
12381
+ runtimeFailureCaught = true;
12319
12382
  if (nodeProcess) {
12320
12383
  setTimeout(() => {
12321
12384
  nodeProcess.removeListener("unhandledRejection", onUnhandled);
@@ -12345,7 +12408,7 @@ function syncViewOf(db) {
12345
12408
  }
12346
12409
  };
12347
12410
  }
12348
- var SQL_WASM_URL, SQL_WASM_FILENAME, TransactionalSQLJSOperations, SqljsEngine;
12411
+ var SQL_WASM_URL, SQL_WASM_FILENAME, SQLJS_RUNTIME_INIT_TIMEOUT_MS, sqlJsWasmMismatch, sqlJsLoading, TransactionalSQLJSOperations, SqljsEngine;
12349
12412
  var init_SqljsEngine = __esm({
12350
12413
  "src/engines/SqljsEngine.ts"() {
12351
12414
  "use strict";
@@ -12356,6 +12419,9 @@ var init_SqljsEngine = __esm({
12356
12419
  init_environment();
12357
12420
  SQL_WASM_URL = "/sql-wasm.wasm";
12358
12421
  SQL_WASM_FILENAME = "sql-wasm.wasm";
12422
+ SQLJS_RUNTIME_INIT_TIMEOUT_MS = 5e3;
12423
+ sqlJsWasmMismatch = null;
12424
+ sqlJsLoading = null;
12359
12425
  TransactionalSQLJSOperations = class {
12360
12426
  db;
12361
12427
  engine;
@@ -14684,7 +14750,13 @@ function purgeRecordLog(sql, docId, force) {
14684
14750
  );
14685
14751
  return { purged: false, reason: "pending", pendingOps: reply.pendingOps };
14686
14752
  }
14687
- return { purged: true, pendingOps: reply.pendingOps, handedBack: reply.handedBack };
14753
+ return {
14754
+ purged: true,
14755
+ // The store held nothing of this document: the purge removed nothing.
14756
+ ...reply.nothingStored ? { nothingStored: true } : {},
14757
+ pendingOps: reply.pendingOps,
14758
+ handedBack: reply.handedBack
14759
+ };
14688
14760
  }
14689
14761
  function purgeDocumentQueryRows(sql, docId) {
14690
14762
  const tables = Array.from(
@@ -19801,6 +19873,7 @@ var init_OpfsEngine = __esm({
19801
19873
  }
19802
19874
  this.requestPersistence();
19803
19875
  entry.docIds.add(docId);
19876
+ entry.served.add(docId);
19804
19877
  const routing = this.docs.get(docId);
19805
19878
  if (routing && routing.entry === entry) routing.sessions++;
19806
19879
  else this.docs.set(docId, { entry, sessions: 1 });
@@ -20153,7 +20226,7 @@ var init_OpfsEngine = __esm({
20153
20226
  if (!attached) {
20154
20227
  return this.purgeWithoutAttachment(docId, namespace, force);
20155
20228
  }
20156
- return this.purgeThroughAttachment(docId, namespace, attached, force);
20229
+ return this.purgeThroughAttachment(docId, namespace, attached, force, { running: true });
20157
20230
  }
20158
20231
  /**
20159
20232
  * No attachment for the namespace: what is on disk decides what happens.
@@ -20189,8 +20262,13 @@ var init_OpfsEngine = __esm({
20189
20262
  }
20190
20263
  return this.purgeThroughAttachment(docId, namespace, entry, force);
20191
20264
  }
20192
- /** An attachment for the namespace: the worker decides, then the leader acts. */
20193
- async purgeThroughAttachment(docId, namespace, entry, force) {
20265
+ /**
20266
+ * An attachment for the namespace: the worker decides, then the leader acts.
20267
+ *
20268
+ * `running`: the worker was already up for this tab before the purge, rather
20269
+ * than spawned to answer it.
20270
+ */
20271
+ async purgeThroughAttachment(docId, namespace, entry, force, options = {}) {
20194
20272
  let reply;
20195
20273
  try {
20196
20274
  reply = await entry.attachment.purge(docId, force);
@@ -20220,6 +20298,12 @@ var init_OpfsEngine = __esm({
20220
20298
  Logger.info(
20221
20299
  `[OpfsEngine] ${docId}: local store purged (${reply.pendingOps} unacknowledged write(s) dropped, ${reply.remainingDocuments} document(s) left in the pool).`
20222
20300
  );
20301
+ if (reply.nothingStored && options.running && !entry.served.has(docId)) {
20302
+ Logger.debug(
20303
+ `[OpfsEngine] ${docId}: nothing of it was stored, so the running pool stays.`
20304
+ );
20305
+ return { ...purgedReport(reply), poolRemoved: false };
20306
+ }
20223
20307
  if (reply.remainingDocuments > 0) {
20224
20308
  Logger.debug(
20225
20309
  `[OpfsEngine] ${docId}: its pool still holds ${reply.remainingDocuments} document(s), so its directory stays.`
@@ -20620,7 +20704,8 @@ var init_OpfsEngine = __esm({
20620
20704
  poolName: namespace.poolName,
20621
20705
  attachment,
20622
20706
  engine: new RemoteQueryEngine(attachment),
20623
- docIds: /* @__PURE__ */ new Set()
20707
+ docIds: /* @__PURE__ */ new Set(),
20708
+ served: /* @__PURE__ */ new Set()
20624
20709
  };
20625
20710
  this.workers.set(namespace.poolName, entry);
20626
20711
  return entry;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-bao",
3
- "version": "0.12.0-alpha.11",
3
+ "version": "0.12.0-alpha.12",
4
4
  "description": "A library providing data modeling capabilities which support live updates and queries.",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",