@minnowdb/core 0.6.9 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/dist/block-format/block.js +254 -325
  2. package/dist/block-format/checksum.js +30 -52
  3. package/dist/block-format/codecs.js +112 -125
  4. package/dist/block-format/column.js +187 -205
  5. package/dist/block-format/index.js +5 -1
  6. package/dist/block-format/physical.js +371 -387
  7. package/dist/block-format/types.js +4 -1
  8. package/dist/block-format/unicode.js +30 -39
  9. package/dist/date-value.d.ts +5 -0
  10. package/dist/date-value.js +71 -41
  11. package/dist/engine/artifact-cache.js +106 -116
  12. package/dist/engine/batch.js +36 -44
  13. package/dist/engine/buffered-writer.js +156 -158
  14. package/dist/engine/byte-estimates.js +20 -22
  15. package/dist/engine/cache-limits.js +24 -25
  16. package/dist/engine/cancellation.js +5 -3
  17. package/dist/engine/catalog.js +64 -73
  18. package/dist/engine/client.js +715 -780
  19. package/dist/engine/database.js +15952 -18465
  20. package/dist/engine/defaults.js +104 -111
  21. package/dist/engine/errors.js +113 -128
  22. package/dist/engine/fts.js +233 -280
  23. package/dist/engine/group-index.js +287 -321
  24. package/dist/engine/index.js +23 -12
  25. package/dist/engine/join-index.js +177 -186
  26. package/dist/engine/keyed-live.js +168 -173
  27. package/dist/engine/live-api.js +0 -1
  28. package/dist/engine/live-equal.js +32 -37
  29. package/dist/engine/live.js +603 -647
  30. package/dist/engine/memory.js +109 -111
  31. package/dist/engine/optimizer.d.ts +19 -1
  32. package/dist/engine/optimizer.js +2642 -2515
  33. package/dist/engine/point-read.d.ts +4 -2
  34. package/dist/engine/point-read.js +121 -171
  35. package/dist/engine/query-api.js +12 -3
  36. package/dist/engine/query-cache.js +61 -69
  37. package/dist/engine/query.d.ts +45 -0
  38. package/dist/engine/query.js +8506 -9375
  39. package/dist/engine/result-wire.js +221 -252
  40. package/dist/engine/schema-wire.js +97 -112
  41. package/dist/engine/schema.js +1099 -1254
  42. package/dist/engine/sort-keys.js +315 -389
  43. package/dist/engine/sql-domains.d.ts +7 -1
  44. package/dist/engine/sql-domains.js +648 -734
  45. package/dist/engine/sql-driver.js +0 -1
  46. package/dist/engine/sql-functions.d.ts +11 -0
  47. package/dist/engine/sql-functions.js +1092 -0
  48. package/dist/engine/sql-json.js +199 -218
  49. package/dist/engine/sql-semantics.d.ts +25 -4
  50. package/dist/engine/sql-semantics.js +579 -513
  51. package/dist/engine/typed-live.js +271 -293
  52. package/dist/engine/vector.d.ts +7 -1
  53. package/dist/engine/vector.js +4513 -4580
  54. package/dist/engine/worker-host.d.ts +8 -63
  55. package/dist/engine/worker-host.js +28 -1130
  56. package/dist/engine/worker-indexeddb.d.ts +1 -0
  57. package/dist/engine/worker-indexeddb.js +3 -0
  58. package/dist/engine/worker-memory.d.ts +1 -0
  59. package/dist/engine/worker-memory.js +3 -0
  60. package/dist/engine/worker-opfs.d.ts +1 -0
  61. package/dist/engine/worker-opfs.js +3 -0
  62. package/dist/engine/worker-server.d.ts +93 -0
  63. package/dist/engine/worker-server.js +999 -0
  64. package/dist/engine/worker-store-indexeddb.d.ts +2 -0
  65. package/dist/engine/worker-store-indexeddb.js +11 -0
  66. package/dist/engine/worker-store-memory.d.ts +2 -0
  67. package/dist/engine/worker-store-memory.js +6 -0
  68. package/dist/engine/worker-store-opfs.d.ts +2 -0
  69. package/dist/engine/worker-store-opfs.js +9 -0
  70. package/dist/engine/worker.js +0 -12
  71. package/dist/engine/write-block-planner.js +93 -100
  72. package/dist/plan/index.js +15 -16
  73. package/dist/plan/model.d.ts +9 -1
  74. package/dist/plan/model.js +20 -27
  75. package/dist/storage/index.js +0 -12
  76. package/dist/storage/indexeddb.js +11776 -13171
  77. package/dist/storage/memory.js +1198 -1241
  78. package/dist/storage/opfs/files.js +238 -295
  79. package/dist/storage/opfs/index.js +9 -3
  80. package/dist/storage/opfs/leader.js +4386 -4856
  81. package/dist/storage/opfs/rpc.js +223 -259
  82. package/dist/storage/opfs/snapshot-ledger.js +219 -260
  83. package/dist/storage/opfs/store.js +884 -1003
  84. package/dist/storage/persistence.js +59 -74
  85. package/dist/storage/snapshot-stream.js +788 -826
  86. package/dist/storage/snapshot.js +36 -37
  87. package/dist/storage/toolkit/extents.js +444 -520
  88. package/dist/storage/toolkit/index.js +28 -29
  89. package/dist/storage/toolkit/record-core.js +5792 -6377
  90. package/dist/storage/toolkit/sync-file.js +34 -42
  91. package/dist/storage/toolkit/wal.js +92 -127
  92. package/dist/storage/toolkit/wire.js +232 -256
  93. package/dist/storage/types.js +2952 -3228
  94. package/dist/testing/block-store-conformance.js +1556 -1629
  95. package/dist/testing/index.js +310 -291
  96. package/dist/testing/opfs-shim.js +280 -312
  97. package/dist/testing/simulator.js +496 -534
  98. package/dist/testing/sqllogictest.js +425 -365
  99. package/dist/transactions/index.js +1464 -1712
  100. package/dist/worker-protocol/index.js +70 -69
  101. package/package.json +20 -2
  102. package/postgres-feature-profile.json +15 -5
  103. package/sql-feature-matrix.json +252 -2
@@ -1,329 +1,297 @@
1
- /**
2
- * An in-memory Origin Private File System for Node tests: enough of
3
- * `FileSystemDirectoryHandle` / `FileSystemFileHandle` / `FileSystemSyncAccessHandle` for the
4
- * OPFS block store, with the semantics that make OPFS interesting to test against:
5
- *
6
- * - **Exclusive sync-access locks.** One open handle per file across every directory handle on
7
- * the same shim — a second `createSyncAccessHandle` throws `NoModificationAllowedError`,
8
- * exactly the cross-tab arbitration the store's command log builds on. `getFile` and
9
- * `removeEntry` on a locked file also refuse, the strictest reading of the underspecified
10
- * parts of the spec, so a store that passes here never depends on browser-specific leniency.
11
- * - **Real DOMException identities.** `NotFoundError`, `TypeMismatchError`,
12
- * `InvalidModificationError`, `NoModificationAllowedError`, and injected
13
- * `QuotaExceededError`s are actual `DOMException`s, since error identity is part of the
14
- * storage contract.
15
- * - **Injectable write faults and short transfers.** `setWriteFault` throws from inside sync
16
- * writes, creates, or flushes; `setTransferLimit` makes reads or writes return any valid short
17
- * byte count, including zero progress, at deterministic offsets.
18
- *
19
- * For applications, `MemoryBlockStore` remains the right store in Node tests. This shim is for
20
- * adapter authors: it hosts `OpfsBlockStore` (pass `root`) or anything built on the storage
21
- * toolkit's `SyncFileHandle` surface, in plain vitest, with crashes and quota failures on tap.
22
- */
23
- export class MemoryOpfs {
24
- #rootNode = { kind: "directory", name: "", children: new Map() };
25
- #writeFault = null;
26
- #deleteFault = null;
27
- #transferLimit = null;
28
- /** The tree's root, shaped like `navigator.storage.getDirectory()`'s result. */
29
- get root() {
30
- return new ShimDirectoryHandle(this, this.#rootNode, "");
31
- }
32
- /** Installs (or clears, with `null`) a fault thrown from inside write paths. */
33
- setWriteFault(fault) {
34
- this.#writeFault = fault;
35
- }
36
- /** Installs a fault at file/directory removal, for post-commit cleanup tests. */
37
- setDeleteFault(fault) {
38
- this.#deleteFault = fault;
39
- }
40
- /** Limits individual sync-handle transfers so tests can deterministically exercise short I/O. */
41
- setTransferLimit(limit) {
42
- this.#transferLimit = limit;
43
- }
44
- maybeFault(path, phase) {
45
- this.#writeFault?.(path, phase);
46
- }
47
- maybeDeleteFault(path) {
48
- this.#deleteFault?.(path);
49
- }
50
- limitTransfer(path, operation, requestedBytes, at) {
51
- const limited = this.#transferLimit?.(path, operation, requestedBytes, at) ?? requestedBytes;
52
- if (!Number.isSafeInteger(limited) || limited < 0 || limited > requestedBytes) {
53
- throw new RangeError(`Invalid test transfer limit ${String(limited)} for ${String(requestedBytes)} requested bytes`);
54
- }
55
- return limited;
56
- }
57
- /** Test-side peek: the file's current bytes, or undefined. Bypasses locks on purpose. */
58
- readFileBytes(path) {
59
- const node = this.#find(path);
60
- return node?.kind === "file" ? node.bytes.slice() : undefined;
61
- }
62
- /** Test-side poke: writes bytes directly, creating parents. Bypasses locks on purpose. */
63
- writeFileBytes(path, bytes) {
64
- const segments = path.split("/").filter((segment) => segment.length > 0);
65
- const name = segments.pop();
66
- if (name === undefined)
67
- throw new Error("A file path cannot be empty");
68
- let directory = this.#rootNode;
69
- for (const segment of segments) {
70
- let child = directory.children.get(segment);
71
- if (child === undefined) {
72
- child = { kind: "directory", name: segment, children: new Map() };
73
- directory.children.set(segment, child);
74
- }
75
- if (child.kind !== "directory")
76
- throw new Error(`Not a directory: ${segment}`);
77
- directory = child;
78
- }
79
- const existing = directory.children.get(name);
80
- if (existing?.kind === "directory")
81
- throw new Error(`Not a file: ${path}`);
82
- directory.children.set(name, {
83
- kind: "file",
84
- name,
85
- bytes: bytes.slice(),
86
- lockHolder: existing?.lockHolder ?? null,
87
- });
88
- }
89
- /** Test-side in-place corruption: existing sync handles observe the changed byte. */
90
- corruptFileByte(path, offset) {
91
- const node = this.#find(path);
92
- if (node?.kind !== "file")
93
- throw new Error(`Not a file: ${path}`);
94
- if (!Number.isSafeInteger(offset) || offset < 0 || offset >= node.bytes.byteLength) {
95
- throw new RangeError(`Invalid corruption offset ${String(offset)} for ${path}`);
96
- }
97
- node.bytes[offset] = (node.bytes[offset] ?? 0) ^ 0xff;
98
- }
99
- #find(path) {
100
- let node = this.#rootNode;
101
- for (const segment of path.split("/").filter((segment) => segment.length > 0)) {
102
- if (node.kind !== "directory")
103
- return undefined;
104
- const child = node.children.get(segment);
105
- if (child === undefined)
106
- return undefined;
107
- node = child;
108
- }
109
- return node;
110
- }
1
+ class MemoryOpfs {
2
+ #rootNode = { kind: "directory", name: "", children: /* @__PURE__ */ new Map() };
3
+ #writeFault = null;
4
+ #deleteFault = null;
5
+ #transferLimit = null;
6
+ get root() {
7
+ return new ShimDirectoryHandle(this, this.#rootNode, "");
8
+ }
9
+ setWriteFault(fault) {
10
+ this.#writeFault = fault;
11
+ }
12
+ setDeleteFault(fault) {
13
+ this.#deleteFault = fault;
14
+ }
15
+ setTransferLimit(limit) {
16
+ this.#transferLimit = limit;
17
+ }
18
+ maybeFault(path, phase) {
19
+ this.#writeFault?.(path, phase);
20
+ }
21
+ maybeDeleteFault(path) {
22
+ this.#deleteFault?.(path);
23
+ }
24
+ limitTransfer(path, operation, requestedBytes, at) {
25
+ const limited = this.#transferLimit?.(path, operation, requestedBytes, at) ?? requestedBytes;
26
+ if (!Number.isSafeInteger(limited) || limited < 0 || limited > requestedBytes) {
27
+ throw new RangeError(`Invalid test transfer limit ${String(limited)} for ${String(requestedBytes)} requested bytes`);
28
+ }
29
+ return limited;
30
+ }
31
+ readFileBytes(path) {
32
+ const node = this.#find(path);
33
+ return node?.kind === "file" ? node.bytes.slice() : void 0;
34
+ }
35
+ writeFileBytes(path, bytes) {
36
+ const segments = path.split("/").filter((segment) => segment.length > 0);
37
+ const name = segments.pop();
38
+ if (name === void 0)
39
+ throw new Error("A file path cannot be empty");
40
+ let directory = this.#rootNode;
41
+ for (const segment of segments) {
42
+ let child = directory.children.get(segment);
43
+ if (child === void 0) {
44
+ child = { kind: "directory", name: segment, children: /* @__PURE__ */ new Map() };
45
+ directory.children.set(segment, child);
46
+ }
47
+ if (child.kind !== "directory")
48
+ throw new Error(`Not a directory: ${segment}`);
49
+ directory = child;
50
+ }
51
+ const existing = directory.children.get(name);
52
+ if (existing?.kind === "directory")
53
+ throw new Error(`Not a file: ${path}`);
54
+ directory.children.set(name, {
55
+ kind: "file",
56
+ name,
57
+ bytes: bytes.slice(),
58
+ lockHolder: existing?.lockHolder ?? null
59
+ });
60
+ }
61
+ corruptFileByte(path, offset) {
62
+ const node = this.#find(path);
63
+ if (node?.kind !== "file")
64
+ throw new Error(`Not a file: ${path}`);
65
+ if (!Number.isSafeInteger(offset) || offset < 0 || offset >= node.bytes.byteLength) {
66
+ throw new RangeError(`Invalid corruption offset ${String(offset)} for ${path}`);
67
+ }
68
+ node.bytes[offset] = (node.bytes[offset] ?? 0) ^ 255;
69
+ }
70
+ #find(path) {
71
+ let node = this.#rootNode;
72
+ for (const segment of path.split("/").filter((segment2) => segment2.length > 0)) {
73
+ if (node.kind !== "directory")
74
+ return void 0;
75
+ const child = node.children.get(segment);
76
+ if (child === void 0)
77
+ return void 0;
78
+ node = child;
79
+ }
80
+ return node;
81
+ }
111
82
  }
112
83
  function notFound(name) {
113
- return new DOMException(`A requested file or directory could not be found: ${name}`, "NotFoundError");
84
+ return new DOMException(`A requested file or directory could not be found: ${name}`, "NotFoundError");
114
85
  }
115
86
  function typeMismatch(name) {
116
- return new DOMException(`The path is not of the requested type: ${name}`, "TypeMismatchError");
87
+ return new DOMException(`The path is not of the requested type: ${name}`, "TypeMismatchError");
117
88
  }
118
89
  function locked(name) {
119
- return new DOMException(`Access handles cannot be created if there is another open access handle: ${name}`, "NoModificationAllowedError");
90
+ return new DOMException(`Access handles cannot be created if there is another open access handle: ${name}`, "NoModificationAllowedError");
120
91
  }
121
92
  class ShimDirectoryHandle {
122
- kind = "directory";
123
- #shim;
124
- #node;
125
- #path;
126
- constructor(shim, node, path) {
127
- this.#shim = shim;
128
- this.#node = node;
129
- this.#path = path;
130
- }
131
- get name() {
132
- return this.#node.name;
133
- }
134
- #childPath(name) {
135
- return this.#path.length === 0 ? name : `${this.#path}/${name}`;
136
- }
137
- async getFileHandle(name, options) {
138
- validateEntryName(name);
139
- const existing = this.#node.children.get(name);
140
- if (existing === undefined) {
141
- if (options?.create !== true)
142
- throw notFound(name);
143
- this.#shim.maybeFault(this.#childPath(name), "create");
144
- const node = { kind: "file", name, bytes: new Uint8Array(0), lockHolder: null };
145
- this.#node.children.set(name, node);
146
- return new ShimFileHandle(this.#shim, node, this.#childPath(name));
147
- }
148
- if (existing.kind !== "file")
149
- throw typeMismatch(name);
150
- return new ShimFileHandle(this.#shim, existing, this.#childPath(name));
151
- }
152
- async getDirectoryHandle(name, options) {
153
- validateEntryName(name);
154
- const existing = this.#node.children.get(name);
155
- if (existing === undefined) {
156
- if (options?.create !== true)
157
- throw notFound(name);
158
- this.#shim.maybeFault(this.#childPath(name), "create");
159
- const node = { kind: "directory", name, children: new Map() };
160
- this.#node.children.set(name, node);
161
- return new ShimDirectoryHandle(this.#shim, node, this.#childPath(name));
162
- }
163
- if (existing.kind !== "directory")
164
- throw typeMismatch(name);
165
- return new ShimDirectoryHandle(this.#shim, existing, this.#childPath(name));
166
- }
167
- async removeEntry(name, options) {
168
- validateEntryName(name);
169
- const existing = this.#node.children.get(name);
170
- if (existing === undefined)
171
- throw notFound(name);
172
- this.#shim.maybeDeleteFault(this.#childPath(name));
173
- if (existing.kind === "file") {
174
- if (existing.lockHolder !== null)
175
- throw locked(name);
176
- }
177
- else if (options?.recursive !== true) {
178
- if (existing.children.size > 0) {
179
- throw new DOMException(`The directory is not empty: ${name}`, "InvalidModificationError");
180
- }
181
- }
182
- else {
183
- assertSubtreeUnlocked(existing, name);
184
- }
185
- this.#node.children.delete(name);
186
- }
187
- async isSameEntry(other) {
188
- return other instanceof ShimDirectoryHandle && other.#node === this.#node;
189
- }
190
- async resolve() {
191
- return null;
192
- }
193
- async *entries() {
194
- // Snapshot first: callers may mutate while iterating, as with the real API.
195
- for (const [name, node] of [...this.#node.children.entries()]) {
196
- if (this.#node.children.get(name) !== node)
197
- continue;
198
- yield [
199
- name,
200
- node.kind === "file"
201
- ? new ShimFileHandle(this.#shim, node, this.#childPath(name))
202
- : new ShimDirectoryHandle(this.#shim, node, this.#childPath(name)),
203
- ];
204
- }
205
- }
206
- async *keys() {
207
- for await (const [name] of this.entries())
208
- yield name;
209
- }
210
- async *values() {
211
- for await (const [, handle] of this.entries())
212
- yield handle;
213
- }
214
- [Symbol.asyncIterator]() {
215
- return this.entries();
216
- }
93
+ kind = "directory";
94
+ #shim;
95
+ #node;
96
+ #path;
97
+ constructor(shim, node, path) {
98
+ this.#shim = shim;
99
+ this.#node = node;
100
+ this.#path = path;
101
+ }
102
+ get name() {
103
+ return this.#node.name;
104
+ }
105
+ #childPath(name) {
106
+ return this.#path.length === 0 ? name : `${this.#path}/${name}`;
107
+ }
108
+ async getFileHandle(name, options) {
109
+ validateEntryName(name);
110
+ const existing = this.#node.children.get(name);
111
+ if (existing === void 0) {
112
+ if (options?.create !== true)
113
+ throw notFound(name);
114
+ this.#shim.maybeFault(this.#childPath(name), "create");
115
+ const node = { kind: "file", name, bytes: new Uint8Array(0), lockHolder: null };
116
+ this.#node.children.set(name, node);
117
+ return new ShimFileHandle(this.#shim, node, this.#childPath(name));
118
+ }
119
+ if (existing.kind !== "file")
120
+ throw typeMismatch(name);
121
+ return new ShimFileHandle(this.#shim, existing, this.#childPath(name));
122
+ }
123
+ async getDirectoryHandle(name, options) {
124
+ validateEntryName(name);
125
+ const existing = this.#node.children.get(name);
126
+ if (existing === void 0) {
127
+ if (options?.create !== true)
128
+ throw notFound(name);
129
+ this.#shim.maybeFault(this.#childPath(name), "create");
130
+ const node = { kind: "directory", name, children: /* @__PURE__ */ new Map() };
131
+ this.#node.children.set(name, node);
132
+ return new ShimDirectoryHandle(this.#shim, node, this.#childPath(name));
133
+ }
134
+ if (existing.kind !== "directory")
135
+ throw typeMismatch(name);
136
+ return new ShimDirectoryHandle(this.#shim, existing, this.#childPath(name));
137
+ }
138
+ async removeEntry(name, options) {
139
+ validateEntryName(name);
140
+ const existing = this.#node.children.get(name);
141
+ if (existing === void 0)
142
+ throw notFound(name);
143
+ this.#shim.maybeDeleteFault(this.#childPath(name));
144
+ if (existing.kind === "file") {
145
+ if (existing.lockHolder !== null)
146
+ throw locked(name);
147
+ } else if (options?.recursive !== true) {
148
+ if (existing.children.size > 0) {
149
+ throw new DOMException(`The directory is not empty: ${name}`, "InvalidModificationError");
150
+ }
151
+ } else {
152
+ assertSubtreeUnlocked(existing, name);
153
+ }
154
+ this.#node.children.delete(name);
155
+ }
156
+ async isSameEntry(other) {
157
+ return other instanceof ShimDirectoryHandle && other.#node === this.#node;
158
+ }
159
+ async resolve() {
160
+ return null;
161
+ }
162
+ async *entries() {
163
+ for (const [name, node] of [...this.#node.children.entries()]) {
164
+ if (this.#node.children.get(name) !== node)
165
+ continue;
166
+ yield [
167
+ name,
168
+ node.kind === "file" ? new ShimFileHandle(this.#shim, node, this.#childPath(name)) : new ShimDirectoryHandle(this.#shim, node, this.#childPath(name))
169
+ ];
170
+ }
171
+ }
172
+ async *keys() {
173
+ for await (const [name] of this.entries())
174
+ yield name;
175
+ }
176
+ async *values() {
177
+ for await (const [, handle] of this.entries())
178
+ yield handle;
179
+ }
180
+ [Symbol.asyncIterator]() {
181
+ return this.entries();
182
+ }
217
183
  }
218
184
  function assertSubtreeUnlocked(node, name) {
219
- for (const child of node.children.values()) {
220
- if (child.kind === "file") {
221
- if (child.lockHolder !== null)
222
- throw locked(`${name}/${child.name}`);
223
- }
224
- else {
225
- assertSubtreeUnlocked(child, `${name}/${child.name}`);
226
- }
227
- }
185
+ for (const child of node.children.values()) {
186
+ if (child.kind === "file") {
187
+ if (child.lockHolder !== null)
188
+ throw locked(`${name}/${child.name}`);
189
+ } else {
190
+ assertSubtreeUnlocked(child, `${name}/${child.name}`);
191
+ }
192
+ }
228
193
  }
229
194
  function validateEntryName(name) {
230
- if (name.length === 0 || name === "." || name === ".." || name.includes("/")) {
231
- throw new TypeError(`Entry name is invalid: ${name}`);
232
- }
195
+ if (name.length === 0 || name === "." || name === ".." || name.includes("/")) {
196
+ throw new TypeError(`Entry name is invalid: ${name}`);
197
+ }
233
198
  }
234
199
  class ShimFileHandle {
235
- kind = "file";
236
- #shim;
237
- #node;
238
- #path;
239
- constructor(shim, node, path) {
240
- this.#shim = shim;
241
- this.#node = node;
242
- this.#path = path;
243
- }
244
- get name() {
245
- return this.#node.name;
246
- }
247
- async getFile() {
248
- if (this.#node.lockHolder !== null)
249
- throw locked(this.#node.name);
250
- return new File([this.#node.bytes.slice()], this.#node.name);
251
- }
252
- async createSyncAccessHandle() {
253
- if (this.#node.lockHolder !== null)
254
- throw locked(this.#node.name);
255
- const handle = new ShimSyncAccessHandle(this.#shim, this.#node, this.#path);
256
- this.#node.lockHolder = handle;
257
- return handle;
258
- }
259
- async isSameEntry(other) {
260
- return other instanceof ShimFileHandle && other.#node === this.#node;
261
- }
200
+ kind = "file";
201
+ #shim;
202
+ #node;
203
+ #path;
204
+ constructor(shim, node, path) {
205
+ this.#shim = shim;
206
+ this.#node = node;
207
+ this.#path = path;
208
+ }
209
+ get name() {
210
+ return this.#node.name;
211
+ }
212
+ async getFile() {
213
+ if (this.#node.lockHolder !== null)
214
+ throw locked(this.#node.name);
215
+ return new File([this.#node.bytes.slice()], this.#node.name);
216
+ }
217
+ async createSyncAccessHandle() {
218
+ if (this.#node.lockHolder !== null)
219
+ throw locked(this.#node.name);
220
+ const handle = new ShimSyncAccessHandle(this.#shim, this.#node, this.#path);
221
+ this.#node.lockHolder = handle;
222
+ return handle;
223
+ }
224
+ async isSameEntry(other) {
225
+ return other instanceof ShimFileHandle && other.#node === this.#node;
226
+ }
262
227
  }
263
228
  class ShimSyncAccessHandle {
264
- #shim;
265
- #node;
266
- #path;
267
- #closed = false;
268
- constructor(shim, node, path) {
269
- this.#shim = shim;
270
- this.#node = node;
271
- this.#path = path;
272
- }
273
- #assertOpen() {
274
- if (this.#closed) {
275
- throw new DOMException("The access handle is closed", "InvalidStateError");
276
- }
277
- }
278
- getSize() {
279
- this.#assertOpen();
280
- return this.#node.bytes.byteLength;
281
- }
282
- read(buffer, options) {
283
- this.#assertOpen();
284
- const at = options?.at ?? 0;
285
- const target = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
286
- const limit = this.#shim.limitTransfer(this.#path, "read", target.byteLength, at);
287
- const source = this.#node.bytes.subarray(at, at + limit);
288
- target.set(source);
289
- return source.byteLength;
290
- }
291
- write(buffer, options) {
292
- this.#assertOpen();
293
- this.#shim.maybeFault(this.#path, "write");
294
- const at = options?.at ?? 0;
295
- const requested = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
296
- const limit = this.#shim.limitTransfer(this.#path, "write", requested.byteLength, at);
297
- const source = requested.subarray(0, limit);
298
- const end = at + source.byteLength;
299
- if (source.byteLength > 0 && end > this.#node.bytes.byteLength) {
300
- const grown = new Uint8Array(end);
301
- grown.set(this.#node.bytes);
302
- this.#node.bytes = grown;
303
- }
304
- this.#node.bytes.set(source, at);
305
- return source.byteLength;
306
- }
307
- truncate(size) {
308
- this.#assertOpen();
309
- this.#shim.maybeFault(this.#path, "write");
310
- if (size <= this.#node.bytes.byteLength) {
311
- this.#node.bytes = this.#node.bytes.slice(0, size);
312
- return;
313
- }
314
- const grown = new Uint8Array(size);
315
- grown.set(this.#node.bytes);
316
- this.#node.bytes = grown;
317
- }
318
- flush() {
319
- this.#assertOpen();
320
- this.#shim.maybeFault(this.#path, "flush");
321
- }
322
- close() {
323
- if (this.#closed)
324
- return;
325
- this.#closed = true;
326
- if (this.#node.lockHolder === this)
327
- this.#node.lockHolder = null;
328
- }
229
+ #shim;
230
+ #node;
231
+ #path;
232
+ #closed = false;
233
+ constructor(shim, node, path) {
234
+ this.#shim = shim;
235
+ this.#node = node;
236
+ this.#path = path;
237
+ }
238
+ #assertOpen() {
239
+ if (this.#closed) {
240
+ throw new DOMException("The access handle is closed", "InvalidStateError");
241
+ }
242
+ }
243
+ getSize() {
244
+ this.#assertOpen();
245
+ return this.#node.bytes.byteLength;
246
+ }
247
+ read(buffer, options) {
248
+ this.#assertOpen();
249
+ const at = options?.at ?? 0;
250
+ const target = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
251
+ const limit = this.#shim.limitTransfer(this.#path, "read", target.byteLength, at);
252
+ const source = this.#node.bytes.subarray(at, at + limit);
253
+ target.set(source);
254
+ return source.byteLength;
255
+ }
256
+ write(buffer, options) {
257
+ this.#assertOpen();
258
+ this.#shim.maybeFault(this.#path, "write");
259
+ const at = options?.at ?? 0;
260
+ const requested = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength);
261
+ const limit = this.#shim.limitTransfer(this.#path, "write", requested.byteLength, at);
262
+ const source = requested.subarray(0, limit);
263
+ const end = at + source.byteLength;
264
+ if (source.byteLength > 0 && end > this.#node.bytes.byteLength) {
265
+ const grown = new Uint8Array(end);
266
+ grown.set(this.#node.bytes);
267
+ this.#node.bytes = grown;
268
+ }
269
+ this.#node.bytes.set(source, at);
270
+ return source.byteLength;
271
+ }
272
+ truncate(size) {
273
+ this.#assertOpen();
274
+ this.#shim.maybeFault(this.#path, "write");
275
+ if (size <= this.#node.bytes.byteLength) {
276
+ this.#node.bytes = this.#node.bytes.slice(0, size);
277
+ return;
278
+ }
279
+ const grown = new Uint8Array(size);
280
+ grown.set(this.#node.bytes);
281
+ this.#node.bytes = grown;
282
+ }
283
+ flush() {
284
+ this.#assertOpen();
285
+ this.#shim.maybeFault(this.#path, "flush");
286
+ }
287
+ close() {
288
+ if (this.#closed)
289
+ return;
290
+ this.#closed = true;
291
+ if (this.#node.lockHolder === this)
292
+ this.#node.lockHolder = null;
293
+ }
329
294
  }
295
+ export {
296
+ MemoryOpfs
297
+ };