@elaraai/e3-web 0.0.0-stage → 1.0.86
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/LICENSE.md +54 -0
- package/README.md +395 -2
- package/dist/src/app.d.ts +91 -0
- package/dist/src/app.d.ts.map +1 -0
- package/dist/src/app.js +112 -0
- package/dist/src/app.js.map +1 -0
- package/dist/src/bridge/bridge.spec.d.ts +6 -0
- package/dist/src/bridge/bridge.spec.d.ts.map +1 -0
- package/dist/src/bridge/bridge.spec.js +56 -0
- package/dist/src/bridge/bridge.spec.js.map +1 -0
- package/dist/src/bridge/page.d.ts +149 -0
- package/dist/src/bridge/page.d.ts.map +1 -0
- package/dist/src/bridge/page.js +328 -0
- package/dist/src/bridge/page.js.map +1 -0
- package/dist/src/bridge/protocol.d.ts +181 -0
- package/dist/src/bridge/protocol.d.ts.map +1 -0
- package/dist/src/bridge/protocol.js +113 -0
- package/dist/src/bridge/protocol.js.map +1 -0
- package/dist/src/bridge/worker.d.ts +124 -0
- package/dist/src/bridge/worker.d.ts.map +1 -0
- package/dist/src/bridge/worker.js +185 -0
- package/dist/src/bridge/worker.js.map +1 -0
- package/dist/src/browser/bridge.page.d.ts +12 -0
- package/dist/src/browser/bridge.page.d.ts.map +1 -0
- package/dist/src/browser/bridge.page.js +79 -0
- package/dist/src/browser/bridge.page.js.map +1 -0
- package/dist/src/browser/bridge.spec.d.ts +6 -0
- package/dist/src/browser/bridge.spec.d.ts.map +1 -0
- package/dist/src/browser/bridge.spec.js +105 -0
- package/dist/src/browser/bridge.spec.js.map +1 -0
- package/dist/src/browser/bridge.worker.d.ts +6 -0
- package/dist/src/browser/bridge.worker.d.ts.map +1 -0
- package/dist/src/browser/bridge.worker.js +35 -0
- package/dist/src/browser/bridge.worker.js.map +1 -0
- package/dist/src/browser/compliance.d.ts +68 -0
- package/dist/src/browser/compliance.d.ts.map +1 -0
- package/dist/src/browser/compliance.js +30 -0
- package/dist/src/browser/compliance.js.map +1 -0
- package/dist/src/browser/compliance.page.d.ts +6 -0
- package/dist/src/browser/compliance.page.d.ts.map +1 -0
- package/dist/src/browser/compliance.page.js +204 -0
- package/dist/src/browser/compliance.page.js.map +1 -0
- package/dist/src/browser/compliance.spec.d.ts +6 -0
- package/dist/src/browser/compliance.spec.d.ts.map +1 -0
- package/dist/src/browser/compliance.spec.js +147 -0
- package/dist/src/browser/compliance.spec.js.map +1 -0
- package/dist/src/browser/e3-api-1.spec.d.ts +6 -0
- package/dist/src/browser/e3-api-1.spec.d.ts.map +1 -0
- package/dist/src/browser/e3-api-1.spec.js +12 -0
- package/dist/src/browser/e3-api-1.spec.js.map +1 -0
- package/dist/src/browser/e3-api-2.spec.d.ts +6 -0
- package/dist/src/browser/e3-api-2.spec.d.ts.map +1 -0
- package/dist/src/browser/e3-api-2.spec.js +12 -0
- package/dist/src/browser/e3-api-2.spec.js.map +1 -0
- package/dist/src/browser/e3-api-3.spec.d.ts +6 -0
- package/dist/src/browser/e3-api-3.spec.d.ts.map +1 -0
- package/dist/src/browser/e3-api-3.spec.js +12 -0
- package/dist/src/browser/e3-api-3.spec.js.map +1 -0
- package/dist/src/browser/e3-api-parts.spec.d.ts +6 -0
- package/dist/src/browser/e3-api-parts.spec.d.ts.map +1 -0
- package/dist/src/browser/e3-api-parts.spec.js +45 -0
- package/dist/src/browser/e3-api-parts.spec.js.map +1 -0
- package/dist/src/browser/e3-api.d.ts +28 -0
- package/dist/src/browser/e3-api.d.ts.map +1 -0
- package/dist/src/browser/e3-api.js +144 -0
- package/dist/src/browser/e3-api.js.map +1 -0
- package/dist/src/browser/e3-client.worker.d.ts +6 -0
- package/dist/src/browser/e3-client.worker.d.ts.map +1 -0
- package/dist/src/browser/e3-client.worker.js +19 -0
- package/dist/src/browser/e3-client.worker.js.map +1 -0
- package/dist/src/browser/e3-forward.d.ts +27 -0
- package/dist/src/browser/e3-forward.d.ts.map +1 -0
- package/dist/src/browser/e3-forward.js +81 -0
- package/dist/src/browser/e3-forward.js.map +1 -0
- package/dist/src/browser/e3-forward.spec.d.ts +6 -0
- package/dist/src/browser/e3-forward.spec.d.ts.map +1 -0
- package/dist/src/browser/e3-forward.spec.js +84 -0
- package/dist/src/browser/e3-forward.spec.js.map +1 -0
- package/dist/src/browser/e3-port.worker.d.ts +6 -0
- package/dist/src/browser/e3-port.worker.d.ts.map +1 -0
- package/dist/src/browser/e3-port.worker.js +21 -0
- package/dist/src/browser/e3-port.worker.js.map +1 -0
- package/dist/src/browser/e3-unit.worker.d.ts +6 -0
- package/dist/src/browser/e3-unit.worker.d.ts.map +1 -0
- package/dist/src/browser/e3-unit.worker.js +18 -0
- package/dist/src/browser/e3-unit.worker.js.map +1 -0
- package/dist/src/browser/e3-wire.d.ts +43 -0
- package/dist/src/browser/e3-wire.d.ts.map +1 -0
- package/dist/src/browser/e3-wire.js +32 -0
- package/dist/src/browser/e3-wire.js.map +1 -0
- package/dist/src/browser/e3.page.d.ts +21 -0
- package/dist/src/browser/e3.page.d.ts.map +1 -0
- package/dist/src/browser/e3.page.js +119 -0
- package/dist/src/browser/e3.page.js.map +1 -0
- package/dist/src/browser/e3.spec.d.ts +6 -0
- package/dist/src/browser/e3.spec.d.ts.map +1 -0
- package/dist/src/browser/e3.spec.js +358 -0
- package/dist/src/browser/e3.spec.js.map +1 -0
- package/dist/src/browser/e3.worker.d.ts +6 -0
- package/dist/src/browser/e3.worker.d.ts.map +1 -0
- package/dist/src/browser/e3.worker.js +38 -0
- package/dist/src/browser/e3.worker.js.map +1 -0
- package/dist/src/browser/harness.d.ts +175 -0
- package/dist/src/browser/harness.d.ts.map +1 -0
- package/dist/src/browser/harness.js +375 -0
- package/dist/src/browser/harness.js.map +1 -0
- package/dist/src/browser/page.d.ts +47 -0
- package/dist/src/browser/page.d.ts.map +1 -0
- package/dist/src/browser/page.js +47 -0
- package/dist/src/browser/page.js.map +1 -0
- package/dist/src/browser/repository.page.d.ts +85 -0
- package/dist/src/browser/repository.page.d.ts.map +1 -0
- package/dist/src/browser/repository.page.js +271 -0
- package/dist/src/browser/repository.page.js.map +1 -0
- package/dist/src/browser/runner.page.d.ts +6 -0
- package/dist/src/browser/runner.page.d.ts.map +1 -0
- package/dist/src/browser/runner.page.js +62 -0
- package/dist/src/browser/runner.page.js.map +1 -0
- package/dist/src/browser/runner.spec.d.ts +6 -0
- package/dist/src/browser/runner.spec.d.ts.map +1 -0
- package/dist/src/browser/runner.spec.js +46 -0
- package/dist/src/browser/runner.spec.js.map +1 -0
- package/dist/src/browser/storage.page.d.ts +6 -0
- package/dist/src/browser/storage.page.d.ts.map +1 -0
- package/dist/src/browser/storage.page.js +289 -0
- package/dist/src/browser/storage.page.js.map +1 -0
- package/dist/src/browser/storage.spec.d.ts +6 -0
- package/dist/src/browser/storage.spec.d.ts.map +1 -0
- package/dist/src/browser/storage.spec.js +211 -0
- package/dist/src/browser/storage.spec.js.map +1 -0
- package/dist/src/browser/unit.worker.d.ts +6 -0
- package/dist/src/browser/unit.worker.d.ts.map +1 -0
- package/dist/src/browser/unit.worker.js +17 -0
- package/dist/src/browser/unit.worker.js.map +1 -0
- package/dist/src/execution/WebTaskRunner.d.ts +159 -0
- package/dist/src/execution/WebTaskRunner.d.ts.map +1 -0
- package/dist/src/execution/WebTaskRunner.js +581 -0
- package/dist/src/execution/WebTaskRunner.js.map +1 -0
- package/dist/src/execution/WebTaskRunner.spec.d.ts +6 -0
- package/dist/src/execution/WebTaskRunner.spec.d.ts.map +1 -0
- package/dist/src/execution/WebTaskRunner.spec.js +91 -0
- package/dist/src/execution/WebTaskRunner.spec.js.map +1 -0
- package/dist/src/execution/e3-platform.d.ts +41 -0
- package/dist/src/execution/e3-platform.d.ts.map +1 -0
- package/dist/src/execution/e3-platform.js +52 -0
- package/dist/src/execution/e3-platform.js.map +1 -0
- package/dist/src/execution/in-process.d.ts +32 -0
- package/dist/src/execution/in-process.d.ts.map +1 -0
- package/dist/src/execution/in-process.js +89 -0
- package/dist/src/execution/in-process.js.map +1 -0
- package/dist/src/execution/pool.d.ts +172 -0
- package/dist/src/execution/pool.d.ts.map +1 -0
- package/dist/src/execution/pool.js +352 -0
- package/dist/src/execution/pool.js.map +1 -0
- package/dist/src/execution/pool.spec.d.ts +6 -0
- package/dist/src/execution/pool.spec.d.ts.map +1 -0
- package/dist/src/execution/pool.spec.js +252 -0
- package/dist/src/execution/pool.spec.js.map +1 -0
- package/dist/src/execution/protocol.d.ts +108 -0
- package/dist/src/execution/protocol.d.ts.map +1 -0
- package/dist/src/execution/protocol.js +27 -0
- package/dist/src/execution/protocol.js.map +1 -0
- package/dist/src/execution/unit-server.d.ts +131 -0
- package/dist/src/execution/unit-server.d.ts.map +1 -0
- package/dist/src/execution/unit-server.js +142 -0
- package/dist/src/execution/unit-server.js.map +1 -0
- package/dist/src/index.d.ts +39 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +37 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/index.spec.d.ts +6 -0
- package/dist/src/index.spec.d.ts.map +1 -0
- package/dist/src/index.spec.js +212 -0
- package/dist/src/index.spec.js.map +1 -0
- package/dist/src/node.d.ts +12 -0
- package/dist/src/node.d.ts.map +1 -0
- package/dist/src/node.js +12 -0
- package/dist/src/node.js.map +1 -0
- package/dist/src/storage/WebStateStore.d.ts +64 -0
- package/dist/src/storage/WebStateStore.d.ts.map +1 -0
- package/dist/src/storage/WebStateStore.js +237 -0
- package/dist/src/storage/WebStateStore.js.map +1 -0
- package/dist/src/storage/WebStorage.d.ts +225 -0
- package/dist/src/storage/WebStorage.d.ts.map +1 -0
- package/dist/src/storage/WebStorage.js +1462 -0
- package/dist/src/storage/WebStorage.js.map +1 -0
- package/dist/src/storage/adapters.d.ts +610 -0
- package/dist/src/storage/adapters.d.ts.map +1 -0
- package/dist/src/storage/adapters.js +246 -0
- package/dist/src/storage/adapters.js.map +1 -0
- package/dist/src/storage/indexeddb.d.ts +93 -0
- package/dist/src/storage/indexeddb.d.ts.map +1 -0
- package/dist/src/storage/indexeddb.js +384 -0
- package/dist/src/storage/indexeddb.js.map +1 -0
- package/dist/src/storage/indexeddb.spec.d.ts +6 -0
- package/dist/src/storage/indexeddb.spec.d.ts.map +1 -0
- package/dist/src/storage/indexeddb.spec.js +77 -0
- package/dist/src/storage/indexeddb.spec.js.map +1 -0
- package/dist/src/storage/memory.d.ts +275 -0
- package/dist/src/storage/memory.d.ts.map +1 -0
- package/dist/src/storage/memory.js +724 -0
- package/dist/src/storage/memory.js.map +1 -0
- package/dist/src/storage/memory.spec.d.ts +6 -0
- package/dist/src/storage/memory.spec.d.ts.map +1 -0
- package/dist/src/storage/memory.spec.js +71 -0
- package/dist/src/storage/memory.spec.js.map +1 -0
- package/dist/src/storage/node-files.d.ts +38 -0
- package/dist/src/storage/node-files.d.ts.map +1 -0
- package/dist/src/storage/node-files.js +148 -0
- package/dist/src/storage/node-files.js.map +1 -0
- package/dist/src/storage/node-files.spec.d.ts +6 -0
- package/dist/src/storage/node-files.spec.d.ts.map +1 -0
- package/dist/src/storage/node-files.spec.js +40 -0
- package/dist/src/storage/node-files.spec.js.map +1 -0
- package/dist/src/storage/opfs.d.ts +145 -0
- package/dist/src/storage/opfs.d.ts.map +1 -0
- package/dist/src/storage/opfs.js +514 -0
- package/dist/src/storage/opfs.js.map +1 -0
- package/dist/src/storage/opfs.spec.d.ts +6 -0
- package/dist/src/storage/opfs.spec.d.ts.map +1 -0
- package/dist/src/storage/opfs.spec.js +51 -0
- package/dist/src/storage/opfs.spec.js.map +1 -0
- package/dist/src/storage/record-keys.d.ts +78 -0
- package/dist/src/storage/record-keys.d.ts.map +1 -0
- package/dist/src/storage/record-keys.js +71 -0
- package/dist/src/storage/record-keys.js.map +1 -0
- package/dist/src/storage/stores.spec.d.ts +6 -0
- package/dist/src/storage/stores.spec.d.ts.map +1 -0
- package/dist/src/storage/stores.spec.js +326 -0
- package/dist/src/storage/stores.spec.js.map +1 -0
- package/dist/src/storage/web-locks.d.ts +103 -0
- package/dist/src/storage/web-locks.d.ts.map +1 -0
- package/dist/src/storage/web-locks.js +228 -0
- package/dist/src/storage/web-locks.js.map +1 -0
- package/dist/src/testing/adapter-contract.d.ts +116 -0
- package/dist/src/testing/adapter-contract.d.ts.map +1 -0
- package/dist/src/testing/adapter-contract.js +769 -0
- package/dist/src/testing/adapter-contract.js.map +1 -0
- package/dist/src/testing/assert.d.ts +75 -0
- package/dist/src/testing/assert.d.ts.map +1 -0
- package/dist/src/testing/assert.js +146 -0
- package/dist/src/testing/assert.js.map +1 -0
- package/dist/src/testing/assert.spec.d.ts +6 -0
- package/dist/src/testing/assert.spec.d.ts.map +1 -0
- package/dist/src/testing/assert.spec.js +62 -0
- package/dist/src/testing/assert.spec.js.map +1 -0
- package/dist/src/testing/bridge-contract.d.ts +55 -0
- package/dist/src/testing/bridge-contract.d.ts.map +1 -0
- package/dist/src/testing/bridge-contract.js +376 -0
- package/dist/src/testing/bridge-contract.js.map +1 -0
- package/dist/src/testing/bridge-handler.d.ts +35 -0
- package/dist/src/testing/bridge-handler.d.ts.map +1 -0
- package/dist/src/testing/bridge-handler.js +108 -0
- package/dist/src/testing/bridge-handler.js.map +1 -0
- package/dist/src/testing/callers.d.ts +29 -0
- package/dist/src/testing/callers.d.ts.map +1 -0
- package/dist/src/testing/callers.js +24 -0
- package/dist/src/testing/callers.js.map +1 -0
- package/dist/src/testing/e3-fixtures.d.ts +38 -0
- package/dist/src/testing/e3-fixtures.d.ts.map +1 -0
- package/dist/src/testing/e3-fixtures.js +126 -0
- package/dist/src/testing/e3-fixtures.js.map +1 -0
- package/dist/src/testing/hold-platform.d.ts +33 -0
- package/dist/src/testing/hold-platform.d.ts.map +1 -0
- package/dist/src/testing/hold-platform.js +41 -0
- package/dist/src/testing/hold-platform.js.map +1 -0
- package/dist/src/testing/runner-cases.d.ts +112 -0
- package/dist/src/testing/runner-cases.d.ts.map +1 -0
- package/dist/src/testing/runner-cases.js +959 -0
- package/dist/src/testing/runner-cases.js.map +1 -0
- package/dist/src/testing/runner-fixtures.d.ts +13 -0
- package/dist/src/testing/runner-fixtures.d.ts.map +1 -0
- package/dist/src/testing/runner-fixtures.js +173 -0
- package/dist/src/testing/runner-fixtures.js.map +1 -0
- package/dist/src/testing/test-platform.d.ts +73 -0
- package/dist/src/testing/test-platform.d.ts.map +1 -0
- package/dist/src/testing/test-platform.js +143 -0
- package/dist/src/testing/test-platform.js.map +1 -0
- package/dist/src/transfer/WebTransferBackend.d.ts +780 -0
- package/dist/src/transfer/WebTransferBackend.d.ts.map +1 -0
- package/dist/src/transfer/WebTransferBackend.js +1140 -0
- package/dist/src/transfer/WebTransferBackend.js.map +1 -0
- package/dist/src/transfer/WebTransferBackend.spec.d.ts +6 -0
- package/dist/src/transfer/WebTransferBackend.spec.d.ts.map +1 -0
- package/dist/src/transfer/WebTransferBackend.spec.js +582 -0
- package/dist/src/transfer/WebTransferBackend.spec.js.map +1 -0
- package/dist/src/transfer/endpoints.d.ts +46 -0
- package/dist/src/transfer/endpoints.d.ts.map +1 -0
- package/dist/src/transfer/endpoints.js +157 -0
- package/dist/src/transfer/endpoints.js.map +1 -0
- package/dist/src/units.d.ts +51 -0
- package/dist/src/units.d.ts.map +1 -0
- package/dist/src/units.js +74 -0
- package/dist/src/units.js.map +1 -0
- package/dist/src/worker.d.ts +126 -0
- package/dist/src/worker.d.ts.map +1 -0
- package/dist/src/worker.js +186 -0
- package/dist/src/worker.js.map +1 -0
- package/dist/src/worker.spec.d.ts +6 -0
- package/dist/src/worker.spec.d.ts.map +1 -0
- package/dist/src/worker.spec.js +195 -0
- package/dist/src/worker.spec.js.map +1 -0
- package/package.json +76 -3
|
@@ -0,0 +1,724 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Licensed under BSL 1.1. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The adapters in memory: records, blobs, locks and files a process keeps
|
|
7
|
+
* while it runs.
|
|
8
|
+
*
|
|
9
|
+
* e3's Node test pass runs every store contract suite over them, and a page
|
|
10
|
+
* opened with `persist: false` keeps its repositories in them, starting fresh
|
|
11
|
+
* on every load. Each answers as its browser counterpart does — the records'
|
|
12
|
+
* transactions end as IndexedDB's do, the locks queue as Web Locks do — so
|
|
13
|
+
* what passes over them in Node passes over IndexedDB, OPFS and Web Locks in
|
|
14
|
+
* a browser.
|
|
15
|
+
*
|
|
16
|
+
* Nothing here reaches Node: a browser bundle may import this module.
|
|
17
|
+
*
|
|
18
|
+
* @packageDocumentation
|
|
19
|
+
*/
|
|
20
|
+
import { AdapterClosedError, FileNotFoundError, RecordsTransactionError, attempt, checkBlobKey, checkLockRequest, checkBlobPrefix, checkRange, checkRecordKey, checkScan, chunksOf, compareKeys, copyBytes, isUnder, readChunkOf, } from './adapters.js';
|
|
21
|
+
/**
|
|
22
|
+
* Records in memory, in key order, which any number of adapters open — as
|
|
23
|
+
* tabs open one IndexedDB database.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Every read and transaction takes its turn, one after another, so a
|
|
27
|
+
* transaction applies whole before anything reads past it, as IndexedDB runs
|
|
28
|
+
* a transaction before a read made after it.
|
|
29
|
+
*/
|
|
30
|
+
export class MemoryRecordStore {
|
|
31
|
+
/** The records, in key order */
|
|
32
|
+
records = [];
|
|
33
|
+
/** Settles when the last turn taken has run */
|
|
34
|
+
tail = Promise.resolve();
|
|
35
|
+
/**
|
|
36
|
+
* Runs `fn` once every turn taken before it has run.
|
|
37
|
+
*
|
|
38
|
+
* @param fn - What runs in the turn
|
|
39
|
+
* @returns What `fn` returns
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
turn(fn) {
|
|
43
|
+
const result = this.tail.then(fn);
|
|
44
|
+
this.tail = result.then(() => undefined, () => undefined);
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
/** The index of the first record whose key is not below `key`. */
|
|
48
|
+
lowerBound(key) {
|
|
49
|
+
let low = 0;
|
|
50
|
+
let high = this.records.length;
|
|
51
|
+
while (low < high) {
|
|
52
|
+
const mid = (low + high) >>> 1;
|
|
53
|
+
if (compareKeys(this.records[mid].key, key) < 0)
|
|
54
|
+
low = mid + 1;
|
|
55
|
+
else
|
|
56
|
+
high = mid;
|
|
57
|
+
}
|
|
58
|
+
return low;
|
|
59
|
+
}
|
|
60
|
+
/** The index of the first record whose key is above `key`. */
|
|
61
|
+
upperBound(key) {
|
|
62
|
+
let low = 0;
|
|
63
|
+
let high = this.records.length;
|
|
64
|
+
while (low < high) {
|
|
65
|
+
const mid = (low + high) >>> 1;
|
|
66
|
+
if (compareKeys(this.records[mid].key, key) <= 0)
|
|
67
|
+
low = mid + 1;
|
|
68
|
+
else
|
|
69
|
+
high = mid;
|
|
70
|
+
}
|
|
71
|
+
return low;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The record of a key.
|
|
75
|
+
*
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
78
|
+
find(key) {
|
|
79
|
+
const record = this.records[this.lowerBound(key)];
|
|
80
|
+
return record !== undefined && compareKeys(record.key, key) === 0 ? record : undefined;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Writes a record, or deletes it for `null`.
|
|
84
|
+
*
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
set(key, value) {
|
|
88
|
+
const at = this.lowerBound(key);
|
|
89
|
+
const record = this.records[at];
|
|
90
|
+
const found = record !== undefined && compareKeys(record.key, key) === 0;
|
|
91
|
+
if (value === null) {
|
|
92
|
+
if (found)
|
|
93
|
+
this.records.splice(at, 1);
|
|
94
|
+
}
|
|
95
|
+
else if (found) {
|
|
96
|
+
record.value = value;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
this.records.splice(at, 0, { key: [...key], value });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The records under a prefix a scan asks for, in the order it asks.
|
|
104
|
+
*
|
|
105
|
+
* @internal
|
|
106
|
+
*/
|
|
107
|
+
range(prefix, options) {
|
|
108
|
+
let start = this.lowerBound(prefix);
|
|
109
|
+
if (options.after !== undefined)
|
|
110
|
+
start = Math.max(start, this.upperBound(options.after));
|
|
111
|
+
// The keys under a prefix are contiguous in key order.
|
|
112
|
+
let end = start;
|
|
113
|
+
const { before } = options;
|
|
114
|
+
while (end < this.records.length) {
|
|
115
|
+
const key = this.records[end].key;
|
|
116
|
+
if (!isUnder(key, prefix) || (before !== undefined && compareKeys(key, before) >= 0))
|
|
117
|
+
break;
|
|
118
|
+
end++;
|
|
119
|
+
}
|
|
120
|
+
const found = this.records.slice(start, end);
|
|
121
|
+
if (options.reverse === true)
|
|
122
|
+
found.reverse();
|
|
123
|
+
return options.limit === undefined ? found : found.slice(0, options.limit);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Records in memory: an adapter over a {@link MemoryRecordStore}.
|
|
128
|
+
*
|
|
129
|
+
* @remarks
|
|
130
|
+
* A transaction ends as an IndexedDB transaction does: when its work goes on
|
|
131
|
+
* to anything but its own operations. Its operations answer at once, so work
|
|
132
|
+
* that awaits only them finishes before the task it started in ends; work
|
|
133
|
+
* still running after that task has awaited something else, and is refused
|
|
134
|
+
* with {@link RecordsTransactionError}, its writes undone.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* const store = new MemoryRecordStore();
|
|
139
|
+
* const records = openMemoryRecords(store);
|
|
140
|
+
* await records.transact(async (tx) => { tx.put(['refs', 'main'], bytes); });
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export class MemoryRecords {
|
|
144
|
+
store;
|
|
145
|
+
closed = false;
|
|
146
|
+
/**
|
|
147
|
+
* @param store - The records this adapter reads and writes
|
|
148
|
+
*/
|
|
149
|
+
constructor(store) {
|
|
150
|
+
this.store = store;
|
|
151
|
+
}
|
|
152
|
+
assertOpen() {
|
|
153
|
+
if (this.closed)
|
|
154
|
+
throw new AdapterClosedError('records');
|
|
155
|
+
}
|
|
156
|
+
get(key) {
|
|
157
|
+
return this.store.turn(() => {
|
|
158
|
+
this.assertOpen();
|
|
159
|
+
checkRecordKey(key);
|
|
160
|
+
const record = this.store.find(key);
|
|
161
|
+
return record === undefined ? null : copyBytes(record.value);
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
scan(prefix, options = {}) {
|
|
165
|
+
return this.store.turn(() => {
|
|
166
|
+
this.assertOpen();
|
|
167
|
+
checkRecordKey(prefix);
|
|
168
|
+
checkScan(options);
|
|
169
|
+
return this.store.range(prefix, options).map(({ key, value }) => ({ key: [...key], value: copyBytes(value) }));
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
keys(prefix, options = {}) {
|
|
173
|
+
return this.store.turn(() => {
|
|
174
|
+
this.assertOpen();
|
|
175
|
+
checkRecordKey(prefix);
|
|
176
|
+
checkScan(options);
|
|
177
|
+
return this.store.range(prefix, options).map(({ key }) => [...key]);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
transact(work) {
|
|
181
|
+
return this.store.turn(async () => {
|
|
182
|
+
this.assertOpen();
|
|
183
|
+
const tx = new MemoryTransaction(this.store);
|
|
184
|
+
// The task this turn runs in ends once the microtasks it queued have
|
|
185
|
+
// run: work that awaits only its own operations has finished by then.
|
|
186
|
+
let taskEnded = false;
|
|
187
|
+
const timer = setTimeout(() => {
|
|
188
|
+
taskEnded = true;
|
|
189
|
+
tx.lapse();
|
|
190
|
+
}, 0);
|
|
191
|
+
try {
|
|
192
|
+
const result = await work(tx);
|
|
193
|
+
if (taskEnded)
|
|
194
|
+
throw new RecordsTransactionError('its work finished after the transaction had ended');
|
|
195
|
+
tx.finish();
|
|
196
|
+
return result;
|
|
197
|
+
}
|
|
198
|
+
catch (err) {
|
|
199
|
+
tx.undo();
|
|
200
|
+
throw err;
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
clearTimeout(timer);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
close() {
|
|
208
|
+
this.closed = true;
|
|
209
|
+
return Promise.resolve();
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Opens an adapter over records in memory.
|
|
214
|
+
*
|
|
215
|
+
* @param store - The records to open; a new, empty store when not given
|
|
216
|
+
* @returns The adapter
|
|
217
|
+
*/
|
|
218
|
+
export function openMemoryRecords(store = new MemoryRecordStore()) {
|
|
219
|
+
return new MemoryRecords(store);
|
|
220
|
+
}
|
|
221
|
+
/** A transaction over a {@link MemoryRecordStore}, which writes in place and
|
|
222
|
+
* keeps what each record held before, to undo it. */
|
|
223
|
+
class MemoryTransaction {
|
|
224
|
+
store;
|
|
225
|
+
/** Why the transaction takes no more operations, once it takes none */
|
|
226
|
+
ended = null;
|
|
227
|
+
/** What each record the transaction wrote held before it did, by key */
|
|
228
|
+
before = new Map();
|
|
229
|
+
constructor(store) {
|
|
230
|
+
this.store = store;
|
|
231
|
+
}
|
|
232
|
+
/** Ends the transaction as IndexedDB ends one whose work has gone on to
|
|
233
|
+
* something else. */
|
|
234
|
+
lapse() {
|
|
235
|
+
this.ended ??= 'its work awaited something else, and the transaction ended';
|
|
236
|
+
}
|
|
237
|
+
/** Ends the transaction once its work has finished. */
|
|
238
|
+
finish() {
|
|
239
|
+
this.ended ??= 'it was used after its work had finished';
|
|
240
|
+
}
|
|
241
|
+
/** Restores every record the transaction wrote, and ends it. */
|
|
242
|
+
undo() {
|
|
243
|
+
this.finish();
|
|
244
|
+
for (const { key, value } of this.before.values())
|
|
245
|
+
this.store.set(key, value);
|
|
246
|
+
this.before.clear();
|
|
247
|
+
}
|
|
248
|
+
guard() {
|
|
249
|
+
if (this.ended !== null)
|
|
250
|
+
throw new RecordsTransactionError(this.ended);
|
|
251
|
+
}
|
|
252
|
+
/** Keeps what a record held before the transaction first wrote it. */
|
|
253
|
+
remember(key) {
|
|
254
|
+
const id = JSON.stringify(key);
|
|
255
|
+
if (this.before.has(id))
|
|
256
|
+
return;
|
|
257
|
+
this.before.set(id, { key: [...key], value: this.store.find(key)?.value ?? null });
|
|
258
|
+
}
|
|
259
|
+
/** Answers a read, or rejects when the transaction takes none. */
|
|
260
|
+
read(answer) {
|
|
261
|
+
return attempt(() => {
|
|
262
|
+
this.guard();
|
|
263
|
+
return answer();
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
get(key) {
|
|
267
|
+
return this.read(() => {
|
|
268
|
+
checkRecordKey(key);
|
|
269
|
+
const record = this.store.find(key);
|
|
270
|
+
return record === undefined ? null : copyBytes(record.value);
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
scan(prefix, options = {}) {
|
|
274
|
+
return this.read(() => {
|
|
275
|
+
checkRecordKey(prefix);
|
|
276
|
+
checkScan(options);
|
|
277
|
+
return this.store.range(prefix, options).map(({ key, value }) => ({ key: [...key], value: copyBytes(value) }));
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
keys(prefix, options = {}) {
|
|
281
|
+
return this.read(() => {
|
|
282
|
+
checkRecordKey(prefix);
|
|
283
|
+
checkScan(options);
|
|
284
|
+
return this.store.range(prefix, options).map(({ key }) => [...key]);
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
put(key, value) {
|
|
288
|
+
this.guard();
|
|
289
|
+
checkRecordKey(key);
|
|
290
|
+
this.remember(key);
|
|
291
|
+
this.store.set(key, copyBytes(value));
|
|
292
|
+
}
|
|
293
|
+
delete(key) {
|
|
294
|
+
this.guard();
|
|
295
|
+
checkRecordKey(key);
|
|
296
|
+
this.remember(key);
|
|
297
|
+
this.store.set(key, null);
|
|
298
|
+
}
|
|
299
|
+
deletePrefix(prefix) {
|
|
300
|
+
this.guard();
|
|
301
|
+
checkRecordKey(prefix);
|
|
302
|
+
for (const { key } of this.store.range(prefix, {})) {
|
|
303
|
+
this.remember(key);
|
|
304
|
+
this.store.set(key, null);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Blobs in memory.
|
|
310
|
+
*
|
|
311
|
+
* @remarks
|
|
312
|
+
* A write takes its source whole before it stores anything, so a reader never
|
|
313
|
+
* sees part of one, and a write whose source fails stores nothing: no write
|
|
314
|
+
* leaves anything behind for {@link sweep}.
|
|
315
|
+
*/
|
|
316
|
+
export class MemoryBlobs {
|
|
317
|
+
/** The blobs, by their keys as JSON */
|
|
318
|
+
blobs = new Map();
|
|
319
|
+
read(key) {
|
|
320
|
+
return attempt(() => {
|
|
321
|
+
checkBlobKey(key);
|
|
322
|
+
const blob = this.blobs.get(JSON.stringify(key));
|
|
323
|
+
return blob === undefined ? null : copyBytes(blob.data);
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
readRange(key, offset, length) {
|
|
327
|
+
return attempt(() => {
|
|
328
|
+
checkBlobKey(key);
|
|
329
|
+
checkRange(offset, length);
|
|
330
|
+
const blob = this.blobs.get(JSON.stringify(key));
|
|
331
|
+
return blob === undefined ? null : blob.data.slice(offset, offset + length);
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
stat(key) {
|
|
335
|
+
return attempt(() => {
|
|
336
|
+
checkBlobKey(key);
|
|
337
|
+
const blob = this.blobs.get(JSON.stringify(key));
|
|
338
|
+
return blob === undefined ? null : { size: blob.data.length, lastModified: blob.lastModified };
|
|
339
|
+
});
|
|
340
|
+
}
|
|
341
|
+
async write(key, data) {
|
|
342
|
+
checkBlobKey(key);
|
|
343
|
+
const bytes = await collect(data);
|
|
344
|
+
this.blobs.set(JSON.stringify(key), { key: [...key], data: bytes, lastModified: Date.now() });
|
|
345
|
+
return bytes.length;
|
|
346
|
+
}
|
|
347
|
+
delete(key) {
|
|
348
|
+
return attempt(() => {
|
|
349
|
+
checkBlobKey(key);
|
|
350
|
+
return this.blobs.delete(JSON.stringify(key));
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
deletePrefix(prefix) {
|
|
354
|
+
return attempt(() => {
|
|
355
|
+
checkBlobPrefix(prefix);
|
|
356
|
+
for (const [id, { key }] of [...this.blobs]) {
|
|
357
|
+
if (isUnder(key, prefix))
|
|
358
|
+
this.blobs.delete(id);
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
list(prefix) {
|
|
363
|
+
return attempt(() => {
|
|
364
|
+
checkBlobPrefix(prefix);
|
|
365
|
+
return [...this.blobs.values()]
|
|
366
|
+
.filter(({ key }) => isUnder(key, prefix))
|
|
367
|
+
.map(({ key, data, lastModified }) => ({ key: [...key], size: data.length, lastModified }))
|
|
368
|
+
.sort((a, b) => compareKeys(a.key, b.key));
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
sweep(_options) {
|
|
372
|
+
// A write stores nothing until it has its source whole: none leaves
|
|
373
|
+
// anything behind.
|
|
374
|
+
return Promise.resolve({ deleted: 0, skippedYoung: 0 });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
/** A source's bytes, whole, in memory of their own. */
|
|
378
|
+
async function collect(data) {
|
|
379
|
+
if (data instanceof Uint8Array)
|
|
380
|
+
return copyBytes(data);
|
|
381
|
+
const chunks = [];
|
|
382
|
+
let size = 0;
|
|
383
|
+
for await (const chunk of chunksOf(data)) {
|
|
384
|
+
chunks.push(copyBytes(chunk));
|
|
385
|
+
size += chunk.length;
|
|
386
|
+
}
|
|
387
|
+
const bytes = new Uint8Array(size);
|
|
388
|
+
let offset = 0;
|
|
389
|
+
for (const chunk of chunks) {
|
|
390
|
+
bytes.set(chunk, offset);
|
|
391
|
+
offset += chunk.length;
|
|
392
|
+
}
|
|
393
|
+
return bytes;
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* The locks of one origin, in memory: what the sessions opened over it share,
|
|
397
|
+
* as the tabs of an origin share its Web Locks.
|
|
398
|
+
*
|
|
399
|
+
* @remarks
|
|
400
|
+
* It grants by Web Locks' rules. A name is held by any number of shared holds
|
|
401
|
+
* or one exclusive hold. A request is granted when nothing waits for its name
|
|
402
|
+
* ahead of it and nothing holding the name conflicts with it; one that cannot
|
|
403
|
+
* be granted waits, or is refused when it may not wait. Requests that wait are
|
|
404
|
+
* granted in the order they were made.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* const space = new MemoryLockSpace();
|
|
409
|
+
* const first = await space.open(); // a tab
|
|
410
|
+
* const second = await space.open(); // another tab
|
|
411
|
+
* const hold = await first.acquire('main', 'exclusive');
|
|
412
|
+
* await second.acquire('main', 'exclusive'); // null: the first tab holds it
|
|
413
|
+
* ```
|
|
414
|
+
*/
|
|
415
|
+
export class MemoryLockSpace {
|
|
416
|
+
/** The mode of each hold of a name, by name */
|
|
417
|
+
holds = new Map();
|
|
418
|
+
/** The requests waiting for a name, first first, by name */
|
|
419
|
+
queues = new Map();
|
|
420
|
+
/**
|
|
421
|
+
* Opens a session over the space: a tab's.
|
|
422
|
+
*
|
|
423
|
+
* @returns The session's adapter
|
|
424
|
+
*/
|
|
425
|
+
open() {
|
|
426
|
+
const session = crypto.randomUUID();
|
|
427
|
+
if (!this.take(sessionLock(session), 'exclusive')) {
|
|
428
|
+
return Promise.reject(new Error(`the session ${session} is open already`));
|
|
429
|
+
}
|
|
430
|
+
return Promise.resolve(new MemoryLocks(this, session));
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* The mode of each hold of a name.
|
|
434
|
+
*
|
|
435
|
+
* @internal
|
|
436
|
+
*/
|
|
437
|
+
modes(name) {
|
|
438
|
+
return [...(this.holds.get(name) ?? [])].sort();
|
|
439
|
+
}
|
|
440
|
+
/** Whether nothing holding a name conflicts with a hold in `mode`. */
|
|
441
|
+
free(name, mode) {
|
|
442
|
+
const held = this.holds.get(name) ?? [];
|
|
443
|
+
return mode === 'exclusive' ? held.length === 0 : !held.includes('exclusive');
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Takes a lock if nothing waits for it and nothing holding it conflicts.
|
|
447
|
+
*
|
|
448
|
+
* @returns Whether it was taken
|
|
449
|
+
* @internal
|
|
450
|
+
*/
|
|
451
|
+
take(name, mode) {
|
|
452
|
+
if (this.queues.has(name) || !this.free(name, mode))
|
|
453
|
+
return false;
|
|
454
|
+
this.hold(name, mode);
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
hold(name, mode) {
|
|
458
|
+
const held = this.holds.get(name);
|
|
459
|
+
if (held === undefined)
|
|
460
|
+
this.holds.set(name, [mode]);
|
|
461
|
+
else
|
|
462
|
+
held.push(mode);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Queues a request behind every one made before it.
|
|
466
|
+
*
|
|
467
|
+
* @internal
|
|
468
|
+
*/
|
|
469
|
+
enqueue(name, waiter) {
|
|
470
|
+
const queue = this.queues.get(name);
|
|
471
|
+
if (queue === undefined)
|
|
472
|
+
this.queues.set(name, [waiter]);
|
|
473
|
+
else
|
|
474
|
+
queue.push(waiter);
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Takes a request off its queue, if it is still on it, and grants what its
|
|
478
|
+
* going lets through.
|
|
479
|
+
*
|
|
480
|
+
* @returns Whether it was still waiting
|
|
481
|
+
* @internal
|
|
482
|
+
*/
|
|
483
|
+
dequeue(name, waiter) {
|
|
484
|
+
const queue = this.queues.get(name);
|
|
485
|
+
const at = queue?.indexOf(waiter) ?? -1;
|
|
486
|
+
if (queue === undefined || at < 0)
|
|
487
|
+
return false;
|
|
488
|
+
queue.splice(at, 1);
|
|
489
|
+
if (queue.length === 0)
|
|
490
|
+
this.queues.delete(name);
|
|
491
|
+
this.process(name);
|
|
492
|
+
return true;
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Releases a hold of a name, and grants what its going lets through.
|
|
496
|
+
*
|
|
497
|
+
* @internal
|
|
498
|
+
*/
|
|
499
|
+
release(name, mode) {
|
|
500
|
+
const held = this.holds.get(name) ?? [];
|
|
501
|
+
const at = held.indexOf(mode);
|
|
502
|
+
if (at >= 0)
|
|
503
|
+
held.splice(at, 1);
|
|
504
|
+
if (held.length === 0)
|
|
505
|
+
this.holds.delete(name);
|
|
506
|
+
this.process(name);
|
|
507
|
+
}
|
|
508
|
+
/** Grants the requests at the head of a name's queue while they can be. */
|
|
509
|
+
process(name) {
|
|
510
|
+
const queue = this.queues.get(name);
|
|
511
|
+
while (queue !== undefined && queue.length > 0 && this.free(name, queue[0].mode)) {
|
|
512
|
+
const next = queue.shift();
|
|
513
|
+
if (queue.length === 0)
|
|
514
|
+
this.queues.delete(name);
|
|
515
|
+
this.hold(name, next.mode);
|
|
516
|
+
next.answer(true);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* Refuses every request of a session still waiting.
|
|
521
|
+
*
|
|
522
|
+
* @internal
|
|
523
|
+
*/
|
|
524
|
+
refuseWaiting(session) {
|
|
525
|
+
for (const [name, queue] of [...this.queues]) {
|
|
526
|
+
for (const waiter of queue.filter((each) => each.session === session)) {
|
|
527
|
+
if (this.dequeue(name, waiter))
|
|
528
|
+
waiter.answer(false);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
/** The lock a session holds for as long as it lives. */
|
|
534
|
+
function sessionLock(session) {
|
|
535
|
+
return `session:${session}`;
|
|
536
|
+
}
|
|
537
|
+
/** The lock a name given to a session's {@link MemoryLocks.acquire} is. */
|
|
538
|
+
function namedLock(name) {
|
|
539
|
+
return `lock:${name}`;
|
|
540
|
+
}
|
|
541
|
+
/**
|
|
542
|
+
* A session over a {@link MemoryLockSpace}: the locks one tab takes.
|
|
543
|
+
*/
|
|
544
|
+
export class MemoryLocks {
|
|
545
|
+
space;
|
|
546
|
+
session;
|
|
547
|
+
closed = false;
|
|
548
|
+
/** The holds this session has not released */
|
|
549
|
+
holds = new Set();
|
|
550
|
+
/**
|
|
551
|
+
* @param space - The locks the session takes
|
|
552
|
+
* @param session - The session's id, whose lock the space holds for it
|
|
553
|
+
* @internal
|
|
554
|
+
*/
|
|
555
|
+
constructor(space, session) {
|
|
556
|
+
this.space = space;
|
|
557
|
+
this.session = session;
|
|
558
|
+
}
|
|
559
|
+
assertOpen() {
|
|
560
|
+
if (this.closed)
|
|
561
|
+
throw new AdapterClosedError('locks');
|
|
562
|
+
}
|
|
563
|
+
async acquire(name, mode, options = {}) {
|
|
564
|
+
this.assertOpen();
|
|
565
|
+
checkLockRequest(mode, options);
|
|
566
|
+
const lock = namedLock(name);
|
|
567
|
+
if (this.space.take(lock, mode))
|
|
568
|
+
return this.hold(lock, mode);
|
|
569
|
+
if (options.wait !== true)
|
|
570
|
+
return null;
|
|
571
|
+
const granted = await new Promise((resolve) => {
|
|
572
|
+
let timer;
|
|
573
|
+
const waiter = {
|
|
574
|
+
mode,
|
|
575
|
+
session: this.session,
|
|
576
|
+
answer: (answer) => {
|
|
577
|
+
clearTimeout(timer);
|
|
578
|
+
resolve(answer);
|
|
579
|
+
},
|
|
580
|
+
};
|
|
581
|
+
this.space.enqueue(lock, waiter);
|
|
582
|
+
if (options.timeout !== undefined) {
|
|
583
|
+
timer = setTimeout(() => {
|
|
584
|
+
if (this.space.dequeue(lock, waiter))
|
|
585
|
+
resolve(false);
|
|
586
|
+
}, options.timeout);
|
|
587
|
+
}
|
|
588
|
+
});
|
|
589
|
+
return granted ? this.hold(lock, mode) : null;
|
|
590
|
+
}
|
|
591
|
+
/** A hold of a lock the space has granted this session. */
|
|
592
|
+
hold(lock, mode) {
|
|
593
|
+
let released = false;
|
|
594
|
+
const hold = {
|
|
595
|
+
release: () => {
|
|
596
|
+
if (released)
|
|
597
|
+
return Promise.resolve();
|
|
598
|
+
released = true;
|
|
599
|
+
this.holds.delete(hold);
|
|
600
|
+
this.space.release(lock, mode);
|
|
601
|
+
return Promise.resolve();
|
|
602
|
+
},
|
|
603
|
+
};
|
|
604
|
+
this.holds.add(hold);
|
|
605
|
+
return hold;
|
|
606
|
+
}
|
|
607
|
+
held(name) {
|
|
608
|
+
return attempt(() => {
|
|
609
|
+
this.assertOpen();
|
|
610
|
+
return this.space.modes(namedLock(name));
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
isAlive(session) {
|
|
614
|
+
return attempt(() => {
|
|
615
|
+
this.assertOpen();
|
|
616
|
+
return this.space.modes(sessionLock(session)).length > 0;
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
async close() {
|
|
620
|
+
if (this.closed)
|
|
621
|
+
return;
|
|
622
|
+
this.closed = true;
|
|
623
|
+
this.space.refuseWaiting(this.session);
|
|
624
|
+
for (const hold of [...this.holds])
|
|
625
|
+
await hold.release();
|
|
626
|
+
this.space.release(sessionLock(this.session), 'exclusive');
|
|
627
|
+
}
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* The names of a path of memory or OPFS files: an absolute path of
|
|
631
|
+
* `/`-separated names, none of them empty, `.` or `..`; `/` alone is the root.
|
|
632
|
+
*
|
|
633
|
+
* @param path - The path
|
|
634
|
+
* @returns Its names, in order: none for the root
|
|
635
|
+
* @throws {TypeError} When the path is not of that form
|
|
636
|
+
*/
|
|
637
|
+
export function pathNames(path) {
|
|
638
|
+
if (typeof path !== 'string' || !path.startsWith('/')) {
|
|
639
|
+
throw new TypeError(`a path is absolute, beginning with '/', not ${JSON.stringify(path)}`);
|
|
640
|
+
}
|
|
641
|
+
if (path === '/')
|
|
642
|
+
return [];
|
|
643
|
+
const names = path.slice(1).split('/');
|
|
644
|
+
if (names.some((name) => name === '' || name === '.' || name === '..')) {
|
|
645
|
+
throw new TypeError(`a path's names are neither empty, '.' nor '..': ${JSON.stringify(path)}`);
|
|
646
|
+
}
|
|
647
|
+
return names;
|
|
648
|
+
}
|
|
649
|
+
/**
|
|
650
|
+
* Files in memory, named by absolute `/`-separated paths.
|
|
651
|
+
*
|
|
652
|
+
* @remarks
|
|
653
|
+
* The root directory, `/`, is there from the start; every other directory is
|
|
654
|
+
* made by {@link mkdir}. What a page opened with `persist: false` names by
|
|
655
|
+
* path lives here. A read yields a file in slices of the adapter's read size.
|
|
656
|
+
*/
|
|
657
|
+
export class MemoryFiles {
|
|
658
|
+
/** The directories, by path: the root among them */
|
|
659
|
+
dirs = new Set(['/']);
|
|
660
|
+
/** The files, by path */
|
|
661
|
+
files = new Map();
|
|
662
|
+
/** How many bytes a read yields at a time */
|
|
663
|
+
readChunk;
|
|
664
|
+
/**
|
|
665
|
+
* @param options - How many bytes a read yields at a time
|
|
666
|
+
* @throws {RangeError} When the read size is not a whole number of bytes
|
|
667
|
+
* greater than zero
|
|
668
|
+
*/
|
|
669
|
+
constructor(options = {}) {
|
|
670
|
+
this.readChunk = readChunkOf(options);
|
|
671
|
+
}
|
|
672
|
+
stat(path) {
|
|
673
|
+
return attempt(() => {
|
|
674
|
+
pathNames(path);
|
|
675
|
+
const file = this.files.get(path);
|
|
676
|
+
return file === undefined ? null : { size: file.data.length, lastModified: file.lastModified };
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
// The chunks are in memory already: there is nothing to await, and a
|
|
680
|
+
// reader iterates them as it iterates any file's.
|
|
681
|
+
// eslint-disable-next-line @typescript-eslint/require-await
|
|
682
|
+
async *read(path) {
|
|
683
|
+
pathNames(path);
|
|
684
|
+
const file = this.files.get(path);
|
|
685
|
+
if (file === undefined)
|
|
686
|
+
throw new FileNotFoundError(path);
|
|
687
|
+
for (let offset = 0; offset < file.data.length; offset += this.readChunk) {
|
|
688
|
+
yield file.data.slice(offset, offset + this.readChunk);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
async write(path, data) {
|
|
692
|
+
const names = pathNames(path);
|
|
693
|
+
if (names.length === 0 || this.dirs.has(path))
|
|
694
|
+
throw new TypeError(`${path} is a directory, not a file`);
|
|
695
|
+
const dir = parentPath(names);
|
|
696
|
+
if (!this.dirs.has(dir))
|
|
697
|
+
throw new FileNotFoundError(dir);
|
|
698
|
+
const bytes = await collect(data);
|
|
699
|
+
this.files.set(path, { data: bytes, lastModified: Date.now() });
|
|
700
|
+
return bytes.length;
|
|
701
|
+
}
|
|
702
|
+
remove(path) {
|
|
703
|
+
return attempt(() => {
|
|
704
|
+
pathNames(path);
|
|
705
|
+
return this.files.delete(path);
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
mkdir(path) {
|
|
709
|
+
return attempt(() => {
|
|
710
|
+
const names = pathNames(path);
|
|
711
|
+
for (let depth = 1; depth <= names.length; depth++) {
|
|
712
|
+
const dir = `/${names.slice(0, depth).join('/')}`;
|
|
713
|
+
if (this.files.has(dir))
|
|
714
|
+
throw new TypeError(`${dir} is a file, not a directory`);
|
|
715
|
+
this.dirs.add(dir);
|
|
716
|
+
}
|
|
717
|
+
});
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
/** The path of the directory the file a path's names name is in. */
|
|
721
|
+
function parentPath(names) {
|
|
722
|
+
return names.length <= 1 ? '/' : `/${names.slice(0, -1).join('/')}`;
|
|
723
|
+
}
|
|
724
|
+
//# sourceMappingURL=memory.js.map
|