@waffle-os/runtime 0.0.155 → 0.0.157

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.
Files changed (77) hide show
  1. package/browser/filesystem-backend.ts +11 -1
  2. package/browser/preview-protocol.ts +2 -0
  3. package/browser/preview.ts +128 -18
  4. package/browser/runtime-terminals.ts +65 -24
  5. package/browser/shared-fs.ts +141 -14
  6. package/browser/worker.ts +73 -6
  7. package/core/descriptors.ts +19 -2
  8. package/core/devices.ts +237 -2
  9. package/core/fd-reopen.ts +105 -20
  10. package/core/filesystem.ts +79 -0
  11. package/core/index.ts +4 -0
  12. package/core/open-file.ts +212 -18
  13. package/core/wasi.ts +449 -47
  14. package/dist/browser/filesystem-backend.js +4 -2
  15. package/dist/browser/preview.js +120 -17
  16. package/dist/browser/runtime-terminals.js +53 -10
  17. package/dist/browser/shared-fs.js +124 -11
  18. package/dist/browser/worker.js +59 -6
  19. package/dist/core/descriptors.js +9 -1
  20. package/dist/core/devices.js +211 -1
  21. package/dist/core/fd-reopen.js +79 -18
  22. package/dist/core/filesystem.js +14 -0
  23. package/dist/core/open-file.js +166 -13
  24. package/dist/core/wasi.js +402 -29
  25. package/dist/fs/backend.js +51 -3
  26. package/dist/fs/client.js +204 -7
  27. package/dist/fs/cold-read-cache.js +23 -0
  28. package/dist/fs/index.js +21 -1
  29. package/dist/fs/indexeddb.js +30 -2
  30. package/dist/fs/locks.js +1112 -0
  31. package/dist/fs/memory.js +126 -2
  32. package/dist/fs/multitab-backend.js +317 -196
  33. package/dist/fs/multitab-protocol.js +2 -0
  34. package/dist/fs/persistent.js +32 -7
  35. package/dist/fs/protocol.js +9 -3
  36. package/dist/fs/service.js +387 -22
  37. package/dist/fs/shared.js +54 -1
  38. package/dist/fs/storage-geometry.js +2 -0
  39. package/dist/interrupt/rewriter-internal.js +4 -1
  40. package/dist/proc/abi.js +80 -6
  41. package/dist/proc/errors.js +1 -0
  42. package/dist/proc/filesystem-codec.js +40 -9
  43. package/dist/proc/index.js +10 -1
  44. package/dist/proc/pty.js +193 -0
  45. package/dist/proc/worker-client.js +11 -2
  46. package/dist/proc/worker-pipes.js +51 -0
  47. package/dist/runtime.js +11 -2
  48. package/dist/search/engine.js +53 -0
  49. package/dist/tooling/program/program-transformer.json +1 -1
  50. package/fs/backend.ts +54 -2
  51. package/fs/client.ts +230 -6
  52. package/fs/cold-read-cache.ts +38 -0
  53. package/fs/index.ts +22 -1
  54. package/fs/indexeddb.ts +36 -2
  55. package/fs/locks.ts +1406 -0
  56. package/fs/memory.ts +132 -1
  57. package/fs/multitab-backend.ts +563 -304
  58. package/fs/multitab-protocol.ts +2 -0
  59. package/fs/persistent.ts +46 -4
  60. package/fs/protocol.ts +17 -5
  61. package/fs/service.ts +469 -23
  62. package/fs/shared.ts +60 -2
  63. package/fs/storage-geometry.ts +3 -0
  64. package/index.ts +2 -0
  65. package/interrupt/rewriter-internal.ts +4 -1
  66. package/package.json +2 -2
  67. package/proc/abi.ts +79 -6
  68. package/proc/errors.ts +1 -0
  69. package/proc/filesystem-codec.ts +39 -9
  70. package/proc/index.ts +11 -0
  71. package/proc/pty.ts +227 -0
  72. package/proc/worker-client.ts +28 -6
  73. package/proc/worker-pipes.ts +57 -0
  74. package/runtime.ts +12 -2
  75. package/search/engine.ts +49 -0
  76. package/testing/indexeddb-storage-browser.ts +173 -2
  77. package/tooling/program/program-transformer.json +1 -1
@@ -6,10 +6,29 @@ import {
6
6
  mutationValue
7
7
  } from "./publication.js";
8
8
  import { IdentityAllocator } from "./file-identity.js";
9
- import { fileTimeMs, fileTimeNow, msFileTime } from "../core/filesystem.js";
9
+ import {
10
+ fileTimeMs,
11
+ fileTimeNow,
12
+ msFileTime,
13
+ PROBE_PATH_LIMIT
14
+ } from "../core/filesystem.js";
10
15
  import { MemFS } from "./memory.js";
11
16
  import { FilesystemError } from "./protocol.js";
12
- import { SharedFilesystemState } from "./shared.js";
17
+ import { machineIdFromWorkspace, SharedFilesystemState } from "./shared.js";
18
+ import { validateMachineId } from "../core/devices.js";
19
+ function probePaths(value) {
20
+ if (!Array.isArray(value) || value.length > PROBE_PATH_LIMIT)
21
+ throw new FilesystemError(28);
22
+ for (const path of value)
23
+ if (typeof path !== "string")
24
+ throw new FilesystemError(28);
25
+ return value;
26
+ }
27
+ function probeKind(value) {
28
+ if (value !== "file" && value !== "directory")
29
+ throw new FilesystemError(28);
30
+ return value;
31
+ }
13
32
  function sameTimes(left, right) {
14
33
  return left !== undefined && left.atime === right.atime && left.mtime === right.mtime && left.ctime === right.ctime;
15
34
  }
@@ -33,9 +52,10 @@ export class MemoryBackend {
33
52
  return node.identity ??= this.shared.identities.create();
34
53
  }
35
54
  workspaceId;
55
+ machineId;
36
56
  fs;
37
57
  watchAccount;
38
- constructor(fs = new MemFS, reservation = { workspace: crypto.randomUUID(), era: 1n }, startupRoot, watchLimits, sharedRoot) {
58
+ constructor(fs = new MemFS, reservation = { workspace: crypto.randomUUID(), era: 1n }, startupRoot, watchLimits, sharedRoot, machineId) {
39
59
  if (sharedRoot && startupRoot)
40
60
  throw new TypeError("an imported root cannot also own a startup root");
41
61
  if (startupRoot instanceof MemoryBackend) {
@@ -45,6 +65,7 @@ export class MemoryBackend {
45
65
  this.watchAccount = startupRoot.watchAccount;
46
66
  this.fs = startupRoot.fs;
47
67
  this.workspaceId = startupRoot.workspaceId;
68
+ this.machineId = startupRoot.machineId;
48
69
  this.shared = startupRoot.shared;
49
70
  this.startupRoot = startupRoot.startupRoot;
50
71
  this.sharedPhase = startupRoot.sharedPhase;
@@ -60,7 +81,9 @@ export class MemoryBackend {
60
81
  this.sharedPhase = "imported";
61
82
  this.fs = fs;
62
83
  this.workspaceId = reservation.workspace;
84
+ this.machineId = machineId !== undefined ? validateMachineId(machineId) : machineIdFromWorkspace(reservation.workspace);
63
85
  this.shared.identities.configure(reservation);
86
+ this.shared.setMachineId(this.machineId);
64
87
  const seen = new Set;
65
88
  const pending = [fs.root];
66
89
  while (pending.length) {
@@ -99,6 +122,7 @@ export class MemoryBackend {
99
122
  this.shared = new SharedFilesystemState(buffer, maxBytes, undefined, segments, undefined, publication);
100
123
  if (this.sharedPhase !== "imported")
101
124
  this.shared.identities.copyFrom(previous.identities);
125
+ this.shared.setMachineId(this.machineId);
102
126
  this.shared.continueUnlinks(previous);
103
127
  this.sharedPhase = "attached";
104
128
  }
@@ -273,6 +297,9 @@ export class MemoryBackend {
273
297
  case "realpath":
274
298
  this.check(a, "directory");
275
299
  return this.fs.realpath(a, b, c === true);
300
+ case "probe":
301
+ this.check(a, "directory");
302
+ return this.fs.probe(a, probePaths(b), probeKind(c), args[3] === true);
276
303
  case "readlink":
277
304
  this.check(a);
278
305
  return this.fs.readlink(a);
@@ -311,6 +338,8 @@ export class MemoryBackend {
311
338
  }
312
339
  case "lookup":
313
340
  return this.lookup(a);
341
+ case "statPath":
342
+ return this.statPath(a, b !== false);
314
343
  case "readFile": {
315
344
  const existing = this.existing(a);
316
345
  if (!existing)
@@ -324,6 +353,25 @@ export class MemoryBackend {
324
353
  throw new FilesystemError(52, `unknown filesystem operation ${operation}`);
325
354
  }
326
355
  }
356
+ statPath(path, follow) {
357
+ const node = this.fs.lookup(path, follow);
358
+ if (!node)
359
+ return;
360
+ const handle = this.fs.handleForNode(node);
361
+ const fsStat = this.fs.stat(handle);
362
+ const stat = this.shared.stat(handle);
363
+ return {
364
+ kind: node.kind,
365
+ size: stat ? stat.size : fsStat.size,
366
+ ino: node.ino,
367
+ mtime: fileTimeMs(stat ? stat.mtime : fsStat.mtime),
368
+ ctime: fileTimeMs(stat ? stat.ctime : fsStat.ctime),
369
+ atime: fileTimeMs(stat ? stat.atime : fsStat.atime),
370
+ mode: stat ? stat.mode : fsStat.mode,
371
+ nlink: stat ? stat.nlink : fsStat.nlink,
372
+ ...node.kind === "symlink" ? { target: node.target } : {}
373
+ };
374
+ }
327
375
  lookup(path) {
328
376
  const node = this.fs.lookup(path, false);
329
377
  if (!node)
package/dist/fs/client.js CHANGED
@@ -34,6 +34,7 @@ import {
34
34
  isFileTime,
35
35
  permissionsFor
36
36
  } from "../core/filesystem.js";
37
+ import { ExecutionUnwind } from "../core/unwind.js";
37
38
  import {
38
39
  ArrayBufferConstructor,
39
40
  Int32ArrayConstructor,
@@ -153,7 +154,8 @@ const PLAIN_REPLIES = new SetConstructor([
153
154
  "setTimes",
154
155
  "setMode",
155
156
  "read",
156
- "readlink"
157
+ "readlink",
158
+ "probe"
157
159
  ]);
158
160
 
159
161
  export class FilesystemCompletion {
@@ -313,13 +315,43 @@ export class FilesystemCompletion {
313
315
  wait() {
314
316
  return this.#wait();
315
317
  }
316
- #wait(cancellation) {
318
+ waitInterruptible(control) {
319
+ return this.#wait(undefined, control);
320
+ }
321
+ checkWaitControl(control) {
322
+ if (!control)
323
+ return;
324
+ try {
325
+ control.checkActive?.();
326
+ } catch (error) {
327
+ this.cancel();
328
+ throw error;
329
+ }
330
+ if (!control.interruptPending?.())
331
+ return;
332
+ this.cancel();
333
+ throw new FilesystemError(27);
334
+ }
335
+ parkWait(signal, observed, control) {
336
+ if (control?.wait) {
337
+ try {
338
+ control.wait(signal, observed, 10);
339
+ } catch (error) {
340
+ this.cancel();
341
+ throw error;
342
+ }
343
+ return;
344
+ }
345
+ atomicsWait(signal, 0, observed, control?.interruptPending ? 10 : undefined);
346
+ }
347
+ #wait(cancellation, control) {
317
348
  const signal = this.client.signal;
318
349
  while (!this.ready) {
319
350
  const observed = atomicsLoad(signal, 0);
320
351
  this.check(cancellation);
352
+ this.checkWaitControl(control);
321
353
  if (!this.ready)
322
- atomicsWait(signal, 0, observed);
354
+ this.parkWait(signal, observed, control);
323
355
  }
324
356
  this.check(cancellation);
325
357
  if (this.consumed)
@@ -451,6 +483,51 @@ function clientWatches(cells, revoked, remote, canDrain, call) {
451
483
  }
452
484
  };
453
485
  }
486
+ function isTerminalLockReleaseErrno(code) {
487
+ return code === 2 || code === 8 || code === 11 || code === 15 || code === 28 || code === 44 || code === 58;
488
+ }
489
+ function clientLocks(owner, call, release, hasActivePosixLocks) {
490
+ return {
491
+ owner,
492
+ flock: (handle, ofdId, operation, control) => {
493
+ try {
494
+ return call("lockFlock", [handle, ofdId, operation], control);
495
+ } catch (error) {
496
+ if (isInstance(error, ExecutionUnwind))
497
+ throw error;
498
+ return errno(error);
499
+ }
500
+ },
501
+ setRangeLock: (handle, ofdId, pid, range, blocking, control) => {
502
+ try {
503
+ return call("lockRange", [handle, ofdId ?? null, pid, range.type, range.start, range.len, blocking], blocking ? control : undefined);
504
+ } catch (error) {
505
+ if (isInstance(error, ExecutionUnwind))
506
+ throw error;
507
+ return errno(error);
508
+ }
509
+ },
510
+ getRangeLock: (handle, ofdId, range) => {
511
+ try {
512
+ return call("lockGetRange", [handle, ofdId ?? null, range.type, range.start, range.len]);
513
+ } catch (error) {
514
+ return errno(error);
515
+ }
516
+ },
517
+ releaseOfd: (handle, ofdId, inFlight = false) => {
518
+ release("lockReleaseOfd", [handle, ofdId, inFlight]);
519
+ },
520
+ releasePosix: (handle) => {
521
+ release("lockReleasePosix", [handle]);
522
+ },
523
+ handoffPosix: (successorOwner) => {
524
+ try {
525
+ call("lockHandoffPosix", [successorOwner]);
526
+ } catch {}
527
+ },
528
+ hasActivePosixLocks
529
+ };
530
+ }
454
531
  function observeResolveWait(completion, writer) {
455
532
  const cancellation = {};
456
533
  writer.begin(completion.id);
@@ -499,6 +576,13 @@ export class FilesystemClient {
499
576
  section;
500
577
  shared;
501
578
  watches;
579
+ locks;
580
+ get machineId() {
581
+ return this.shared.getMachineId();
582
+ }
583
+ pendingLockReleases = [];
584
+ lockReleaseTimer;
585
+ flushingLockReleases = false;
502
586
  watchingRevocation = false;
503
587
  resident;
504
588
  workspaceStop;
@@ -520,6 +604,16 @@ export class FilesystemClient {
520
604
  this.section = new FilesystemTransportSection(connection, this.requests.memory, this.revoked);
521
605
  this.shared = new SharedFilesystemState(connection.shared, connection.maxBytes, connection.owner, connection.segments, this.section, connection.publication?.buffers);
522
606
  this.watches = clientWatches(() => this.shared.watch.cells, this.revoked, () => this.shared.isDirectDisabled(), () => !this.shared.isWatchDrainDisabled(), (operation, args) => this.call(operation, args));
607
+ this.locks = clientLocks(connection.owner, (operation, args, control) => {
608
+ if (this.pendingLockReleases.length > 0)
609
+ this.flushPendingLockReleases();
610
+ const completion = this.submit(operation, args);
611
+ try {
612
+ return control ? completion.waitInterruptible(control) : completion.wait();
613
+ } finally {
614
+ completion.release();
615
+ }
616
+ }, (operation, args) => this.enqueueLockRelease(operation, args), () => this.shared.posixLockCount() > 0);
523
617
  connection.port.onmessage = ({ data }) => {
524
618
  if (data.segment)
525
619
  this.attachSegment(data.segment, data.buffer);
@@ -783,10 +877,10 @@ export class FilesystemClient {
783
877
  mapSet(this.capabilityEntries, reservation.handle, reservation.targetKey);
784
878
  }
785
879
  releaseReservation(reservation) {
786
- const state = new Int32ArrayConstructor(reservation.state);
787
- const wasClosed = atomicsLoad(state, 0);
788
- releaseFilesystemReservation(reservation);
789
- if (!wasClosed) {
880
+ this.finishReleasedReservation(reservation, releaseFilesystemReservation(reservation));
881
+ }
882
+ finishReleasedReservation(reservation, released) {
883
+ if (released) {
790
884
  const count = mapGet(this.openHandles, reservation.handle) ?? 0;
791
885
  if (count > 1)
792
886
  mapSet(this.openHandles, reservation.handle, count - 1);
@@ -907,7 +1001,105 @@ export class FilesystemClient {
907
1001
  const completion = mapGet(this.completions, id);
908
1002
  return completion && { kind: 3, id, ready: completion.ready };
909
1003
  }
1004
+ retainLockReleaseHandle(handle) {
1005
+ if (!Number.isInteger(handle) || handle < 0)
1006
+ return;
1007
+ try {
1008
+ this.retain(handle);
1009
+ return handle;
1010
+ } catch {
1011
+ return;
1012
+ }
1013
+ }
1014
+ releaseLockReleaseHandle(retainedHandle) {
1015
+ if (retainedHandle === undefined)
1016
+ return;
1017
+ try {
1018
+ this.release(retainedHandle);
1019
+ } catch {}
1020
+ }
1021
+ finishNextLockRelease() {
1022
+ const removed = this.pendingLockReleases.splice(0, 1);
1023
+ if (removed.length > 0)
1024
+ this.releaseLockReleaseHandle(removed[0].retainedHandle);
1025
+ }
1026
+ enqueueLockRelease(operation, args) {
1027
+ if (this.disposed || this.revoked())
1028
+ return;
1029
+ const handle = typeof args[0] === "number" ? args[0] : -1;
1030
+ const retainedHandle = this.retainLockReleaseHandle(handle);
1031
+ arrayAppend(this.pendingLockReleases, { operation, args, retainedHandle });
1032
+ this.flushPendingLockReleases();
1033
+ }
1034
+ scheduleLockReleaseRetry() {
1035
+ if (this.lockReleaseTimer !== undefined || this.disposed || this.revoked())
1036
+ return;
1037
+ this.lockReleaseTimer = scheduleTimeout(() => {
1038
+ this.lockReleaseTimer = undefined;
1039
+ this.flushPendingLockReleases();
1040
+ }, 1);
1041
+ }
1042
+ clearLockReleaseQueue() {
1043
+ const pending = this.pendingLockReleases.splice(0);
1044
+ for (let i = 0;i < pending.length; i++)
1045
+ this.releaseLockReleaseHandle(pending[i].retainedHandle);
1046
+ if (this.lockReleaseTimer !== undefined) {
1047
+ cancelTimeout(this.lockReleaseTimer);
1048
+ this.lockReleaseTimer = undefined;
1049
+ }
1050
+ }
1051
+ deliverNextLockRelease() {
1052
+ const next = this.pendingLockReleases[0];
1053
+ let completion;
1054
+ try {
1055
+ completion = this.submit(next.operation, next.args);
1056
+ } catch (error) {
1057
+ if (this.revoked() || isTerminalLockReleaseErrno(errno(error))) {
1058
+ this.finishNextLockRelease();
1059
+ return true;
1060
+ }
1061
+ this.scheduleLockReleaseRetry();
1062
+ return false;
1063
+ }
1064
+ try {
1065
+ completion.wait();
1066
+ this.finishNextLockRelease();
1067
+ return true;
1068
+ } catch (error) {
1069
+ if (this.revoked() || isTerminalLockReleaseErrno(errno(error))) {
1070
+ this.finishNextLockRelease();
1071
+ return true;
1072
+ }
1073
+ this.scheduleLockReleaseRetry();
1074
+ return false;
1075
+ } finally {
1076
+ completion.release();
1077
+ }
1078
+ }
1079
+ flushPendingLockReleases() {
1080
+ if (this.flushingLockReleases || this.pendingLockReleases.length === 0)
1081
+ return;
1082
+ if (this.disposed || this.revoked()) {
1083
+ this.clearLockReleaseQueue();
1084
+ return;
1085
+ }
1086
+ this.flushingLockReleases = true;
1087
+ try {
1088
+ while (this.pendingLockReleases.length > 0) {
1089
+ if (this.disposed || this.revoked()) {
1090
+ this.clearLockReleaseQueue();
1091
+ return;
1092
+ }
1093
+ if (!this.deliverNextLockRelease())
1094
+ return;
1095
+ }
1096
+ } finally {
1097
+ this.flushingLockReleases = false;
1098
+ }
1099
+ }
910
1100
  call(operation, args = [], data) {
1101
+ if (this.pendingLockReleases.length > 0)
1102
+ this.flushPendingLockReleases();
911
1103
  const completion = this.submit(operation, args, data);
912
1104
  try {
913
1105
  return completion.wait();
@@ -965,6 +1157,8 @@ export class FilesystemClient {
965
1157
  dispose() {
966
1158
  if (this.finishedDispose)
967
1159
  return;
1160
+ this.flushPendingLockReleases();
1161
+ this.clearLockReleaseQueue();
968
1162
  this.disposed = true;
969
1163
  this.resident?.invalidate();
970
1164
  this.beginWorkspaceStop();
@@ -2105,6 +2299,9 @@ export class FilesystemClient {
2105
2299
  return errno(error);
2106
2300
  }
2107
2301
  }
2302
+ probe(directory, paths, kind, rooted = false) {
2303
+ return this.call("probe", [directory, paths, kind, rooted]);
2304
+ }
2108
2305
  readlink(handle) {
2109
2306
  this.retain(handle);
2110
2307
  try {
@@ -1,14 +1,36 @@
1
1
  import { PAGE } from "./shared-capacity.js";
2
2
  const COLD_READ_CACHE_BYTES = 32 * 1024 * 1024;
3
3
  export const COLD_PAGE_OVERHEAD = 256;
4
+ const READAHEAD_PAGES = 16;
5
+ export const READAHEAD_BYTES = READAHEAD_PAGES * PAGE;
6
+ const STREAMS = 64;
4
7
 
5
8
  export class ColdReadCache {
6
9
  limit;
7
10
  pages = new Map;
11
+ ends = new Map;
8
12
  bytes = 0;
9
13
  constructor(limit = COLD_READ_CACHE_BYTES) {
10
14
  this.limit = limit;
11
15
  }
16
+ readWindow(key, offset, count, size) {
17
+ if (this.ends.get(key) !== offset)
18
+ return { offset, length: count };
19
+ const windowPages = Math.min(READAHEAD_PAGES, Math.floor(this.limit / 2 / (PAGE + COLD_PAGE_OVERHEAD)));
20
+ if (windowPages < 1)
21
+ return { offset, length: count };
22
+ const start = Math.floor(offset / PAGE) * PAGE;
23
+ const end = Math.min(size, start + windowPages * PAGE);
24
+ if (end < offset + count)
25
+ return { offset, length: count };
26
+ return { offset: start, length: end - start };
27
+ }
28
+ noteRead(key, end) {
29
+ this.ends.delete(key);
30
+ if (this.ends.size >= STREAMS)
31
+ this.ends.delete(this.ends.keys().next().value);
32
+ this.ends.set(key, end);
33
+ }
12
34
  read(key, offset, length) {
13
35
  const first = Math.floor(offset / PAGE);
14
36
  const last = Math.floor((offset + length - 1) / PAGE);
@@ -46,6 +68,7 @@ export class ColdReadCache {
46
68
  }
47
69
  clear() {
48
70
  this.pages.clear();
71
+ this.ends.clear();
49
72
  this.bytes = 0;
50
73
  }
51
74
  remove(name) {
package/dist/fs/index.js CHANGED
@@ -28,7 +28,11 @@ export {
28
28
  sharedRevoked
29
29
  } from "./shared.js";
30
30
  export { PersistentBackend } from "./persistent.js";
31
- export { IndexedDBStorage } from "./indexeddb.js";
31
+ export {
32
+ IndexedDBStorage,
33
+ IndexedDBStorageBusyError,
34
+ IndexedDBStorageMissingError
35
+ } from "./indexeddb.js";
32
36
  export { OpfsStorage } from "./opfs.js";
33
37
  export { getVfsDescriptor, VfsError } from "./vfs.js";
34
38
  export * from "./workspace.js";
@@ -43,6 +47,22 @@ export {
43
47
  } from "./workspace-export.js";
44
48
  export { WorkspaceArchiveError } from "./workspace-archive.js";
45
49
  export { FilesystemWatch } from "./watch.js";
50
+ export {
51
+ LOCK_SH,
52
+ LOCK_EX,
53
+ LOCK_NB,
54
+ LOCK_UN,
55
+ F_GETLK,
56
+ F_SETLK,
57
+ F_SETLKW,
58
+ F_OFD_GETLK,
59
+ F_OFD_SETLK,
60
+ F_OFD_SETLKW,
61
+ F_RDLCK,
62
+ F_WRLCK,
63
+ F_UNLCK
64
+ } from "../core/filesystem.js";
65
+ export { LockTable, LockContentionError } from "./locks.js";
46
66
  export {
47
67
  openWorkspaceRead,
48
68
  WorkspaceReadContractError
@@ -71,6 +71,13 @@ export class IndexedDBStorageBusyError extends Error {
71
71
  }
72
72
  }
73
73
 
74
+ export class IndexedDBStorageMissingError extends Error {
75
+ name = "IndexedDBStorageMissingError";
76
+ constructor(message = "IndexedDB storage database does not exist") {
77
+ super(message);
78
+ }
79
+ }
80
+
74
81
  class NativeDatabaseOpening {
75
82
  signal;
76
83
  versionChangeAbort;
@@ -80,6 +87,7 @@ class NativeDatabaseOpening {
80
87
  database;
81
88
  closing;
82
89
  blocked = false;
90
+ missing = false;
83
91
  abort = () => this.outcome.reject(this.signal?.reason);
84
92
  constructor(factory, name, signal, versionChangeAbort, options) {
85
93
  this.signal = signal;
@@ -92,10 +100,18 @@ class NativeDatabaseOpening {
92
100
  this.completion.resolve();
93
101
  return;
94
102
  }
103
+ const createIfMissing = options.createIfMissing ?? true;
95
104
  try {
96
105
  const request = factory.open(name, 1);
97
106
  signal?.addEventListener("abort", this.abort, { once: true });
98
- request.onupgradeneeded = () => createStores(request.result);
107
+ request.onupgradeneeded = () => {
108
+ if (createIfMissing) {
109
+ createStores(request.result);
110
+ return;
111
+ }
112
+ this.missing = true;
113
+ request.transaction?.abort();
114
+ };
99
115
  request.onsuccess = () => this.delivered(request.result);
100
116
  request.onerror = () => this.failed(request.error);
101
117
  request.onblocked = () => this.blockedRequest();
@@ -113,6 +129,11 @@ class NativeDatabaseOpening {
113
129
  delivered(database) {
114
130
  this.stopObserving();
115
131
  this.database = database;
132
+ if (this.missing) {
133
+ this.outcome.reject(new IndexedDBStorageMissingError);
134
+ this.retire().catch(() => {});
135
+ return;
136
+ }
116
137
  if (this.signal?.aborted || this.blocked) {
117
138
  this.retire().catch(() => {});
118
139
  return;
@@ -125,6 +146,11 @@ class NativeDatabaseOpening {
125
146
  }
126
147
  failed(error) {
127
148
  this.stopObserving();
149
+ if (this.missing) {
150
+ this.outcome.reject(new IndexedDBStorageMissingError);
151
+ this.completion.resolve();
152
+ return;
153
+ }
128
154
  this.outcome.reject(error?.name === "VersionError" ? new VfsError("INVALID_VFS_FORMAT", "indexeddb", "Incompatible IndexedDB database version") : error ?? new Error("Cannot open filesystem storage"));
129
155
  this.completion.resolve();
130
156
  }
@@ -970,7 +996,9 @@ export class IndexedDBStorage {
970
996
  }
971
997
  }, committedMode, () => maintenance.release());
972
998
  } catch (error) {
973
- Promise.all(debts).then(() => maintenance.release()).catch(() => {});
999
+ const cleanup = Promise.all(debts).then(() => maintenance.release()).catch(() => {});
1000
+ if (error instanceof IndexedDBStorageMissingError)
1001
+ await cleanup;
974
1002
  throw error;
975
1003
  }
976
1004
  }