@elaraai/e3-web 0.0.0-stage → 1.0.85
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,1462 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Licensed under BSL 1.1. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `WebStorage`: e3's storage backend over the four adapters — records, blobs,
|
|
7
|
+
* locks and files — so e3 keeps its repositories in a browser: records in
|
|
8
|
+
* IndexedDB, objects as OPFS files, locks through Web Locks and files as OPFS
|
|
9
|
+
* files; or in memory, for Node's test pass and a page that keeps nothing past
|
|
10
|
+
* a reload.
|
|
11
|
+
*
|
|
12
|
+
* Every record is an East value, in beast2, under a key of the records
|
|
13
|
+
* adapter's ({@link recordKeys}). An object is a blob of its own, which a
|
|
14
|
+
* record of the object catalogue names by its hash, beside its size, when it
|
|
15
|
+
* was last written and gc's unreachable note. Each write of an object's bytes
|
|
16
|
+
* is a new blob, so a delete that races a write takes the version the note
|
|
17
|
+
* stood over, never the one the write stored.
|
|
18
|
+
*
|
|
19
|
+
* @packageDocumentation
|
|
20
|
+
*/
|
|
21
|
+
import { DateTimeType, IntegerType, NullType, OptionType, StringType, StructType, decodeBeast2For, encodeBeast2For, equalFor, none, some, variant, } from '@elaraai/east';
|
|
22
|
+
import { DataflowRunType, DatasetRefType, ExecutionOwnerType, ExecutionStatusType, LockHolderVariantType, LockProgressType, LockStateType, RepoMetadataType, RepositoryRecordType, RepositoryUpgradeProgressType, decodeExecutionStatus, } from '@elaraai/e3-types';
|
|
23
|
+
import { DatasetRefConflictError, ExecutionCorruptError, ObjectNotFoundError, RepoAlreadyExistsError, RepoNotFoundError, RepoStatusConflictError, checkHash, checkId, checkName, checkPageLimit, completeUtf8Length, computeHash, executionRoots, isObjectHash, isUuidv7, newRepositoryRecord, packageRoots, workspaceRoots, } from '@elaraai/e3-core/portable';
|
|
24
|
+
import { openIndexedDbRecords } from './indexeddb.js';
|
|
25
|
+
import { MemoryBlobs, MemoryFiles, MemoryLockSpace, openMemoryRecords } from './memory.js';
|
|
26
|
+
import { OpfsBlobs, OpfsFiles, opfsDirectory } from './opfs.js';
|
|
27
|
+
import { recordKeys } from './record-keys.js';
|
|
28
|
+
import { WebStateStore } from './WebStateStore.js';
|
|
29
|
+
import { openWebLocks } from './web-locks.js';
|
|
30
|
+
// =============================================================================
|
|
31
|
+
// Where each record is
|
|
32
|
+
// =============================================================================
|
|
33
|
+
// The key of each record, which the run state store over the same records
|
|
34
|
+
// shares (`record-keys.ts`)
|
|
35
|
+
export { recordKeys };
|
|
36
|
+
/** The blob of an object: one per write of its bytes. */
|
|
37
|
+
function blobKey(repo, blob) {
|
|
38
|
+
return [repo, 'objects', blob.slice(0, 2), blob];
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The key of each record a transfer backend over a `WebStorage` keeps, in its
|
|
42
|
+
* records adapter: `['transfer', <kind>, <id>]`.
|
|
43
|
+
*
|
|
44
|
+
* @remarks
|
|
45
|
+
* A job is found by its id alone, as a route asks for it, and its record names
|
|
46
|
+
* the repository that started it, which the route checks. gc's sweep reads
|
|
47
|
+
* which transfers have a record ({@link WebRepoStore.gcSweepBackend}).
|
|
48
|
+
*/
|
|
49
|
+
export const transferKeys = {
|
|
50
|
+
/** Every transfer's record */
|
|
51
|
+
all: () => ['transfer'],
|
|
52
|
+
/** Every record of a kind */
|
|
53
|
+
kind: (kind) => ['transfer', kind],
|
|
54
|
+
/** A record of a kind */
|
|
55
|
+
of: (kind, id) => ['transfer', kind, id],
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* The key of each blob a transfer stages, in the blobs adapter: under its
|
|
59
|
+
* repository, which takes them with it when it is removed.
|
|
60
|
+
*/
|
|
61
|
+
export const stagedKeys = {
|
|
62
|
+
/** Everything every transfer of a repository staged */
|
|
63
|
+
all: (repo) => [repo, 'transfer'],
|
|
64
|
+
/** Everything a transfer staged */
|
|
65
|
+
of: (repo, id) => [repo, 'transfer', id],
|
|
66
|
+
/** A part of a dataset upload, from 1 */
|
|
67
|
+
part: (repo, id, part) => [repo, 'transfer', id, `part-${String(part).padStart(6, '0')}`],
|
|
68
|
+
/** A package import's zip */
|
|
69
|
+
zip: (repo, id) => [repo, 'transfer', id, 'zip'],
|
|
70
|
+
/** A round of an export's zip, from 0: the bytes one round wrote */
|
|
71
|
+
exported: (repo, id, round) => [repo, 'transfer', id, `export-${String(round).padStart(6, '0')}`],
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* How long what a transfer staged is kept, however its record stands: a day,
|
|
75
|
+
* the longest a transfer backend keeps a record nothing finished or fetched.
|
|
76
|
+
* gc sweeps what is older, as it sweeps what no record names.
|
|
77
|
+
*/
|
|
78
|
+
export const TRANSFER_RETENTION_MS = 24 * 60 * 60 * 1000;
|
|
79
|
+
/** Runs writes that read nothing as one transaction. */
|
|
80
|
+
function writeRecords(records, writes) {
|
|
81
|
+
return records.transact((tx) => {
|
|
82
|
+
writes(tx);
|
|
83
|
+
return Promise.resolve();
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
// =============================================================================
|
|
87
|
+
// Record forms
|
|
88
|
+
// =============================================================================
|
|
89
|
+
const encodeHash = encodeBeast2For(StringType);
|
|
90
|
+
const decodeHash = decodeBeast2For(StringType);
|
|
91
|
+
/** A hash record's hash, or `null` when it does not decode as one. */
|
|
92
|
+
function hashOf(data) {
|
|
93
|
+
if (data === null)
|
|
94
|
+
return null;
|
|
95
|
+
try {
|
|
96
|
+
const hash = decodeHash(data);
|
|
97
|
+
return isObjectHash(hash) ? hash : null;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* An object's entry in the catalogue: the blob its bytes are, their size, when
|
|
105
|
+
* it was last written or re-referenced, and gc's unreachable note.
|
|
106
|
+
*/
|
|
107
|
+
const ObjectRecordType = StructType({
|
|
108
|
+
/** The blob its bytes are */
|
|
109
|
+
blob: StringType,
|
|
110
|
+
/** Its size in bytes */
|
|
111
|
+
size: IntegerType,
|
|
112
|
+
/** When it was last written or re-referenced, in epoch milliseconds */
|
|
113
|
+
writtenAt: IntegerType,
|
|
114
|
+
/** When a sweep beside running work first saw it unreachable, while the note stands */
|
|
115
|
+
unreachableSince: OptionType(IntegerType),
|
|
116
|
+
});
|
|
117
|
+
const encodeObject = encodeBeast2For(ObjectRecordType);
|
|
118
|
+
const decodeObject = decodeBeast2For(ObjectRecordType);
|
|
119
|
+
/** A write of an object's blob in flight: the object, and when it began. */
|
|
120
|
+
const PendingRecordType = StructType({
|
|
121
|
+
/** The object's hash */
|
|
122
|
+
hash: StringType,
|
|
123
|
+
/** When the write began, in epoch milliseconds */
|
|
124
|
+
at: IntegerType,
|
|
125
|
+
});
|
|
126
|
+
const encodePending = encodeBeast2For(PendingRecordType);
|
|
127
|
+
const decodePending = decodeBeast2For(PendingRecordType);
|
|
128
|
+
/** A dataset's ref, and the revision its write minted. */
|
|
129
|
+
const RevisionedRefType = StructType({ revision: StringType, ref: DatasetRefType });
|
|
130
|
+
const encodeRevisioned = encodeBeast2For(RevisionedRefType);
|
|
131
|
+
const decodeRevisioned = decodeBeast2For(RevisionedRefType);
|
|
132
|
+
/** A lock's holder, and when it acquired the lock: what stamps a progress
|
|
133
|
+
* report as the holder's. */
|
|
134
|
+
const HolderStampType = StructType({ holder: LockHolderVariantType, acquiredAt: DateTimeType });
|
|
135
|
+
const sameHolder = equalFor(HolderStampType);
|
|
136
|
+
/** What an exclusive holder last reported, stamped with the holder. */
|
|
137
|
+
const ProgressRecordType = StructType({ holder: LockHolderVariantType, acquiredAt: DateTimeType, progress: LockProgressType });
|
|
138
|
+
const encodeProgress = encodeBeast2For(ProgressRecordType);
|
|
139
|
+
const decodeProgress = decodeBeast2For(ProgressRecordType);
|
|
140
|
+
const encodeRepositoryRecord = encodeBeast2For(RepositoryRecordType);
|
|
141
|
+
const decodeRepositoryRecord = decodeBeast2For(RepositoryRecordType);
|
|
142
|
+
const encodeUpgradeProgress = encodeBeast2For(RepositoryUpgradeProgressType);
|
|
143
|
+
const decodeUpgradeProgress = decodeBeast2For(RepositoryUpgradeProgressType);
|
|
144
|
+
const encodeMetadata = encodeBeast2For(RepoMetadataType);
|
|
145
|
+
const decodeMetadata = decodeBeast2For(RepoMetadataType);
|
|
146
|
+
const encodeStatus = encodeBeast2For(ExecutionStatusType);
|
|
147
|
+
const encodeOwner = encodeBeast2For(ExecutionOwnerType);
|
|
148
|
+
const decodeOwner = decodeBeast2For(ExecutionOwnerType);
|
|
149
|
+
const encodeRun = encodeBeast2For(DataflowRunType);
|
|
150
|
+
const decodeRun = decodeBeast2For(DataflowRunType);
|
|
151
|
+
const encodeLockState = encodeBeast2For(LockStateType);
|
|
152
|
+
const decodeLockState = decodeBeast2For(LockStateType);
|
|
153
|
+
/** An attempt's place in its task's index of running attempts: its key says
|
|
154
|
+
* which attempt, and it holds an East `null`. */
|
|
155
|
+
const RUNNING_PLACE = encodeBeast2For(NullType)(null);
|
|
156
|
+
/** A run's place in its task's index of its runs: its key says which run, and
|
|
157
|
+
* it holds an East `null`. */
|
|
158
|
+
const RUN_PLACE = RUNNING_PLACE;
|
|
159
|
+
/**
|
|
160
|
+
* Whether a lock's state record is the one a holder wrote: of its holder, and
|
|
161
|
+
* of when it acquired the lock. One that does not decode is no holder's.
|
|
162
|
+
*/
|
|
163
|
+
function writtenBy(data, stamp) {
|
|
164
|
+
if (data === null)
|
|
165
|
+
return false;
|
|
166
|
+
let state;
|
|
167
|
+
try {
|
|
168
|
+
state = decodeLockState(data);
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
return sameHolder({ holder: state.holder, acquiredAt: state.acquiredAt }, stamp);
|
|
174
|
+
}
|
|
175
|
+
/** An object's entry, read in a transaction or outside one. */
|
|
176
|
+
async function readObject(read, repo, hash) {
|
|
177
|
+
const data = await read.get(recordKeys.object(repo, hash));
|
|
178
|
+
return data === null ? null : decodeObject(data);
|
|
179
|
+
}
|
|
180
|
+
/** An object's entry once written or re-referenced now: its note cleared. */
|
|
181
|
+
function referenced(record, now) {
|
|
182
|
+
return encodeObject({ ...record, writtenAt: BigInt(now), unreachableSince: none });
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Checks an execution's names are of the forms e3 writes, as every store
|
|
186
|
+
* checks them before it makes a path or a key of them.
|
|
187
|
+
*
|
|
188
|
+
* @throws {InvalidNameError} When a hash is not a SHA-256 in lowercase hex, or
|
|
189
|
+
* the id is not a UUIDv7
|
|
190
|
+
*/
|
|
191
|
+
function checkExecution(taskHash, inputsHash, executionId) {
|
|
192
|
+
checkHash('task hash', taskHash);
|
|
193
|
+
checkHash('inputs hash', inputsHash);
|
|
194
|
+
if (executionId !== undefined)
|
|
195
|
+
checkId('execution id', executionId);
|
|
196
|
+
}
|
|
197
|
+
/** A source's bytes, gathered whole from its chunks. */
|
|
198
|
+
async function collect(source) {
|
|
199
|
+
const chunks = [];
|
|
200
|
+
let size = 0;
|
|
201
|
+
for await (const chunk of source) {
|
|
202
|
+
chunks.push(chunk.slice());
|
|
203
|
+
size += chunk.length;
|
|
204
|
+
}
|
|
205
|
+
const bytes = new Uint8Array(size);
|
|
206
|
+
let offset = 0;
|
|
207
|
+
for (const chunk of chunks) {
|
|
208
|
+
bytes.set(chunk, offset);
|
|
209
|
+
offset += chunk.length;
|
|
210
|
+
}
|
|
211
|
+
return bytes;
|
|
212
|
+
}
|
|
213
|
+
// =============================================================================
|
|
214
|
+
// Objects
|
|
215
|
+
// =============================================================================
|
|
216
|
+
/** How many bytes a placement reads of an object at a time. */
|
|
217
|
+
const PLACE_CHUNK = 1024 * 1024;
|
|
218
|
+
/**
|
|
219
|
+
* Objects as blobs, named by their catalogue's entries.
|
|
220
|
+
*
|
|
221
|
+
* @remarks
|
|
222
|
+
* A write of bytes the store does not hold records its write in flight, writes
|
|
223
|
+
* a new blob, and then names it in the catalogue, in two transactions around
|
|
224
|
+
* the blob's write; one of bytes it holds re-references them instead. Every
|
|
225
|
+
* write and {@link touch} clears the object's unreachable note in the
|
|
226
|
+
* transaction that finds it.
|
|
227
|
+
*
|
|
228
|
+
* East's SHA-256 names bytes whole, so a streamed write, and a file adopted,
|
|
229
|
+
* are held whole to be named.
|
|
230
|
+
*/
|
|
231
|
+
export class WebObjectStore {
|
|
232
|
+
records;
|
|
233
|
+
blobs;
|
|
234
|
+
files;
|
|
235
|
+
/** Objects are blobs apart from any file: placing one copies its bytes. */
|
|
236
|
+
placement = 'download';
|
|
237
|
+
constructor(records, blobs, files) {
|
|
238
|
+
this.records = records;
|
|
239
|
+
this.blobs = blobs;
|
|
240
|
+
this.files = files;
|
|
241
|
+
}
|
|
242
|
+
async write(repo, data) {
|
|
243
|
+
const hash = computeHash(data);
|
|
244
|
+
await this.store(repo, hash, data);
|
|
245
|
+
return hash;
|
|
246
|
+
}
|
|
247
|
+
async writeStream(repo, stream) {
|
|
248
|
+
return this.write(repo, await collect(stream));
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Stores bytes the caller has hashed, under that hash, without hashing them
|
|
252
|
+
* again: what a transfer's commit stores once it has checked the bytes it
|
|
253
|
+
* staged against the hash its upload declared.
|
|
254
|
+
*
|
|
255
|
+
* @param repo - Repository identifier
|
|
256
|
+
* @param hash - The bytes' SHA-256, as the caller computed it
|
|
257
|
+
* @param data - The bytes
|
|
258
|
+
* @returns The hash
|
|
259
|
+
* @throws {Error} When a sweep took the write in flight for abandoned:
|
|
260
|
+
* nothing is stored
|
|
261
|
+
* @internal
|
|
262
|
+
*/
|
|
263
|
+
async writeHashed(repo, hash, data) {
|
|
264
|
+
await this.store(repo, hash, data);
|
|
265
|
+
return hash;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Stores bytes under their hash, or re-references the object when the store
|
|
269
|
+
* holds it.
|
|
270
|
+
*
|
|
271
|
+
* @throws {Error} When a sweep took the write in flight for abandoned, as
|
|
272
|
+
* one with no age gate beside running work may: nothing is stored
|
|
273
|
+
*/
|
|
274
|
+
async store(repo, hash, data) {
|
|
275
|
+
const blob = crypto.randomUUID();
|
|
276
|
+
const held = await this.records.transact(async (tx) => {
|
|
277
|
+
const record = await readObject(tx, repo, hash);
|
|
278
|
+
if (record !== null) {
|
|
279
|
+
tx.put(recordKeys.object(repo, hash), referenced(record, Date.now()));
|
|
280
|
+
return true;
|
|
281
|
+
}
|
|
282
|
+
tx.put(recordKeys.pending(repo, blob), encodePending({ hash, at: BigInt(Date.now()) }));
|
|
283
|
+
return false;
|
|
284
|
+
});
|
|
285
|
+
if (held)
|
|
286
|
+
return;
|
|
287
|
+
try {
|
|
288
|
+
await this.blobs.write(blobKey(repo, blob), data);
|
|
289
|
+
}
|
|
290
|
+
catch (err) {
|
|
291
|
+
// The blob store kept nothing of it; the record in flight goes too, or
|
|
292
|
+
// else gc's sweep takes it once it is past the age gate.
|
|
293
|
+
await writeRecords(this.records, (tx) => tx.delete(recordKeys.pending(repo, blob))).catch(() => undefined);
|
|
294
|
+
throw err;
|
|
295
|
+
}
|
|
296
|
+
const redundant = await this.records.transact(async (tx) => {
|
|
297
|
+
if ((await tx.get(recordKeys.pending(repo, blob))) === null) {
|
|
298
|
+
throw new Error(`the write of object ${hash} was taken for abandoned by gc's sweep before it finished: write it again`);
|
|
299
|
+
}
|
|
300
|
+
tx.delete(recordKeys.pending(repo, blob));
|
|
301
|
+
const record = await readObject(tx, repo, hash);
|
|
302
|
+
if (record !== null) {
|
|
303
|
+
tx.put(recordKeys.object(repo, hash), referenced(record, Date.now()));
|
|
304
|
+
return true;
|
|
305
|
+
}
|
|
306
|
+
tx.put(recordKeys.object(repo, hash), encodeObject({
|
|
307
|
+
blob, size: BigInt(data.length), writtenAt: BigInt(Date.now()), unreachableSince: none,
|
|
308
|
+
}));
|
|
309
|
+
return false;
|
|
310
|
+
});
|
|
311
|
+
// Another write stored the same bytes first: this one's blob names nothing.
|
|
312
|
+
if (redundant)
|
|
313
|
+
await this.blobs.delete(blobKey(repo, blob));
|
|
314
|
+
}
|
|
315
|
+
async touch(repo, hashes) {
|
|
316
|
+
for (const hash of hashes)
|
|
317
|
+
checkHash('object hash', hash);
|
|
318
|
+
if (hashes.length === 0)
|
|
319
|
+
return [];
|
|
320
|
+
return this.records.transact(async (tx) => {
|
|
321
|
+
const now = Date.now();
|
|
322
|
+
const held = [];
|
|
323
|
+
for (const hash of hashes) {
|
|
324
|
+
const record = await readObject(tx, repo, hash);
|
|
325
|
+
if (record !== null)
|
|
326
|
+
tx.put(recordKeys.object(repo, hash), referenced(record, now));
|
|
327
|
+
held.push(record !== null);
|
|
328
|
+
}
|
|
329
|
+
return held;
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
/** An object's entry, or `ObjectNotFoundError`; `InvalidNameError` for a
|
|
333
|
+
* hash that is not of its form. */
|
|
334
|
+
async entry(repo, hash) {
|
|
335
|
+
checkHash('object hash', hash);
|
|
336
|
+
const record = await readObject(this.records, repo, hash);
|
|
337
|
+
if (record === null)
|
|
338
|
+
throw new ObjectNotFoundError(hash);
|
|
339
|
+
return record;
|
|
340
|
+
}
|
|
341
|
+
async read(repo, hash) {
|
|
342
|
+
const data = await this.blobs.read(blobKey(repo, (await this.entry(repo, hash)).blob));
|
|
343
|
+
// Deleted between its entry's read and its blob's
|
|
344
|
+
if (data === null)
|
|
345
|
+
throw new ObjectNotFoundError(hash);
|
|
346
|
+
return data;
|
|
347
|
+
}
|
|
348
|
+
async readRange(repo, hash, offset, length) {
|
|
349
|
+
const data = await this.blobs.readRange(blobKey(repo, (await this.entry(repo, hash)).blob), offset, length);
|
|
350
|
+
if (data === null)
|
|
351
|
+
throw new ObjectNotFoundError(hash);
|
|
352
|
+
return data;
|
|
353
|
+
}
|
|
354
|
+
async adoptFile(repo, file, hash) {
|
|
355
|
+
if (hash !== undefined && (await this.touch(repo, [hash]))[0] === true) {
|
|
356
|
+
const record = await readObject(this.records, repo, hash);
|
|
357
|
+
// Deleted meanwhile: the file, which is at hand, is stored again below
|
|
358
|
+
if (record !== null)
|
|
359
|
+
return { hash, size: Number(record.size) };
|
|
360
|
+
}
|
|
361
|
+
const data = await collect(this.files.read(file));
|
|
362
|
+
const digest = computeHash(data);
|
|
363
|
+
if (hash !== undefined && digest !== hash) {
|
|
364
|
+
throw new Error(`adoptFile: ${file} holds ${digest}, not the ${hash} it was adopted as`);
|
|
365
|
+
}
|
|
366
|
+
await this.store(repo, digest, data);
|
|
367
|
+
return { hash: digest, size: data.length };
|
|
368
|
+
}
|
|
369
|
+
async materialize(repo, hash, destPath) {
|
|
370
|
+
const record = await this.entry(repo, hash);
|
|
371
|
+
const key = blobKey(repo, record.blob);
|
|
372
|
+
const size = Number(record.size);
|
|
373
|
+
const blobs = this.blobs;
|
|
374
|
+
async function* ranges() {
|
|
375
|
+
for (let offset = 0; offset < size; offset += PLACE_CHUNK) {
|
|
376
|
+
const chunk = await blobs.readRange(key, offset, Math.min(PLACE_CHUNK, size - offset));
|
|
377
|
+
if (chunk === null)
|
|
378
|
+
throw new ObjectNotFoundError(hash);
|
|
379
|
+
yield chunk;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
await this.files.write(destPath, ranges());
|
|
383
|
+
}
|
|
384
|
+
async exists(repo, hash) {
|
|
385
|
+
checkHash('object hash', hash);
|
|
386
|
+
return (await readObject(this.records, repo, hash)) !== null;
|
|
387
|
+
}
|
|
388
|
+
async stat(repo, hash) {
|
|
389
|
+
return { size: Number((await this.entry(repo, hash)).size) };
|
|
390
|
+
}
|
|
391
|
+
async list(repo) {
|
|
392
|
+
return (await this.records.keys(recordKeys.kind(repo, 'object'))).map((key) => key[3]);
|
|
393
|
+
}
|
|
394
|
+
async count(repo) {
|
|
395
|
+
return (await this.records.keys(recordKeys.kind(repo, 'object'))).length;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
// =============================================================================
|
|
399
|
+
// Refs
|
|
400
|
+
// =============================================================================
|
|
401
|
+
/**
|
|
402
|
+
* The repository record, package and workspace refs, execution attempts with
|
|
403
|
+
* their owners and plans, the adoption memo and dataflow runs, each a record.
|
|
404
|
+
*/
|
|
405
|
+
class WebRefStore {
|
|
406
|
+
records;
|
|
407
|
+
constructor(records) {
|
|
408
|
+
this.records = records;
|
|
409
|
+
}
|
|
410
|
+
async repositoryRead(repo) {
|
|
411
|
+
const data = await this.records.get(recordKeys.record(repo));
|
|
412
|
+
if (data === null)
|
|
413
|
+
return null;
|
|
414
|
+
// One that does not decode is none this e3 reads: an older e3 wrote it.
|
|
415
|
+
try {
|
|
416
|
+
return decodeRepositoryRecord(data);
|
|
417
|
+
}
|
|
418
|
+
catch {
|
|
419
|
+
return null;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
async repositoryWrite(repo, record) {
|
|
423
|
+
await writeRecords(this.records, (tx) => tx.put(recordKeys.record(repo), encodeRepositoryRecord(record)));
|
|
424
|
+
}
|
|
425
|
+
async repositoryUpgradeRead(repo) {
|
|
426
|
+
const data = await this.records.get(recordKeys.upgrade(repo));
|
|
427
|
+
if (data === null)
|
|
428
|
+
return null;
|
|
429
|
+
// One that does not decode says nowhere a step stopped, so the step under
|
|
430
|
+
// way starts again, which a step's idempotence makes safe.
|
|
431
|
+
try {
|
|
432
|
+
return decodeUpgradeProgress(data);
|
|
433
|
+
}
|
|
434
|
+
catch {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async repositoryUpgradeWrite(repo, progress) {
|
|
439
|
+
await writeRecords(this.records, (tx) => {
|
|
440
|
+
if (progress === null)
|
|
441
|
+
tx.delete(recordKeys.upgrade(repo));
|
|
442
|
+
else
|
|
443
|
+
tx.put(recordKeys.upgrade(repo), encodeUpgradeProgress(progress));
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
async packageList(repo) {
|
|
447
|
+
return (await this.records.keys(recordKeys.kind(repo, 'package'))).map((key) => ({ name: key[3], version: key[4] }));
|
|
448
|
+
}
|
|
449
|
+
async packageResolve(repo, name, version) {
|
|
450
|
+
checkName('package', name);
|
|
451
|
+
checkName('package version', version);
|
|
452
|
+
return hashOf(await this.records.get(recordKeys.package(repo, name, version)));
|
|
453
|
+
}
|
|
454
|
+
async packageWrite(repo, name, version, hash) {
|
|
455
|
+
checkName('package', name);
|
|
456
|
+
checkName('package version', version);
|
|
457
|
+
await writeRecords(this.records, (tx) => tx.put(recordKeys.package(repo, name, version), encodeHash(hash)));
|
|
458
|
+
}
|
|
459
|
+
async packageRemove(repo, name, version) {
|
|
460
|
+
checkName('package', name);
|
|
461
|
+
checkName('package version', version);
|
|
462
|
+
await writeRecords(this.records, (tx) => tx.delete(recordKeys.package(repo, name, version)));
|
|
463
|
+
}
|
|
464
|
+
async workspaceList(repo) {
|
|
465
|
+
return (await this.records.keys(recordKeys.kind(repo, 'workspace'))).map((key) => key[3]);
|
|
466
|
+
}
|
|
467
|
+
async workspaceRead(repo, name) {
|
|
468
|
+
checkName('workspace', name);
|
|
469
|
+
return this.records.get(recordKeys.workspace(repo, name));
|
|
470
|
+
}
|
|
471
|
+
async workspaceWrite(repo, name, state) {
|
|
472
|
+
checkName('workspace', name);
|
|
473
|
+
await writeRecords(this.records, (tx) => tx.put(recordKeys.workspace(repo, name), state));
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Removes a workspace's record, and with it everything kept under its name:
|
|
477
|
+
* its dataset refs, its runs' records, and its runs' states and events. Its
|
|
478
|
+
* locks are the lock service's, which takes the state a closed tab left for
|
|
479
|
+
* no one's, and writes over it when the lock is next held.
|
|
480
|
+
*/
|
|
481
|
+
async workspaceRemove(repo, name) {
|
|
482
|
+
checkName('workspace', name);
|
|
483
|
+
await writeRecords(this.records, (tx) => {
|
|
484
|
+
tx.delete(recordKeys.workspace(repo, name));
|
|
485
|
+
tx.deletePrefix([...recordKeys.kind(repo, 'dataset'), name]);
|
|
486
|
+
tx.deletePrefix([...recordKeys.kind(repo, 'run'), name]);
|
|
487
|
+
tx.deletePrefix([...recordKeys.kind(repo, 'state'), name]);
|
|
488
|
+
tx.deletePrefix([...recordKeys.kind(repo, 'event'), name]);
|
|
489
|
+
});
|
|
490
|
+
}
|
|
491
|
+
async executionGet(repo, taskHash, inputsHash, executionId) {
|
|
492
|
+
const data = await this.executionReadBytes(repo, taskHash, inputsHash, executionId);
|
|
493
|
+
return data === null ? null : statusOf(taskHash, inputsHash, data);
|
|
494
|
+
}
|
|
495
|
+
/** Reads the attempt's record as it is. */
|
|
496
|
+
async executionReadBytes(repo, taskHash, inputsHash, executionId) {
|
|
497
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
498
|
+
return this.records.get(recordKeys.execution(repo, taskHash, inputsHash, executionId));
|
|
499
|
+
}
|
|
500
|
+
/** Writes an attempt's status, its place in the index of running attempts,
|
|
501
|
+
* and a run's place in its task's index of runs, in one transaction. */
|
|
502
|
+
async executionWrite(repo, taskHash, inputsHash, executionId, status) {
|
|
503
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
504
|
+
await writeRecords(this.records, (tx) => {
|
|
505
|
+
tx.put(recordKeys.execution(repo, taskHash, inputsHash, executionId), encodeStatus(status));
|
|
506
|
+
if (status.type === 'running')
|
|
507
|
+
tx.put(recordKeys.running(repo, taskHash, inputsHash, executionId), RUNNING_PLACE);
|
|
508
|
+
else
|
|
509
|
+
tx.delete(recordKeys.running(repo, taskHash, inputsHash, executionId));
|
|
510
|
+
if (!status.value.unit)
|
|
511
|
+
tx.put(recordKeys.taskRun(repo, taskHash, executionId, inputsHash), RUN_PLACE);
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
async executionDelete(repo, taskHash, inputsHash, executionId) {
|
|
515
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
516
|
+
await writeRecords(this.records, (tx) => {
|
|
517
|
+
tx.delete(recordKeys.execution(repo, taskHash, inputsHash, executionId));
|
|
518
|
+
tx.delete(recordKeys.owner(repo, taskHash, inputsHash, executionId));
|
|
519
|
+
tx.delete(recordKeys.running(repo, taskHash, inputsHash, executionId));
|
|
520
|
+
tx.delete(recordKeys.taskRun(repo, taskHash, executionId, inputsHash));
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
async executionListIds(repo, taskHash, inputsHash) {
|
|
524
|
+
checkExecution(taskHash, inputsHash);
|
|
525
|
+
const keys = await this.records.keys([...recordKeys.kind(repo, 'execution'), taskHash, inputsHash]);
|
|
526
|
+
return keys.map((key) => key[5]).filter(isUuidv7);
|
|
527
|
+
}
|
|
528
|
+
/** Lists every attempt with its status, in one scan of its records. */
|
|
529
|
+
async executionListAttempts(repo, taskHash, inputsHash) {
|
|
530
|
+
checkExecution(taskHash, inputsHash);
|
|
531
|
+
const attempts = [];
|
|
532
|
+
for (const { key, value } of await this.records.scan([...recordKeys.kind(repo, 'execution'), taskHash, inputsHash])) {
|
|
533
|
+
const executionId = key[5];
|
|
534
|
+
if (!isUuidv7(executionId))
|
|
535
|
+
continue;
|
|
536
|
+
let status;
|
|
537
|
+
try {
|
|
538
|
+
status = decodeExecutionStatus(value);
|
|
539
|
+
}
|
|
540
|
+
catch {
|
|
541
|
+
status = null;
|
|
542
|
+
}
|
|
543
|
+
attempts.push({ executionId, status });
|
|
544
|
+
}
|
|
545
|
+
return attempts;
|
|
546
|
+
}
|
|
547
|
+
async executionGetLatest(repo, taskHash, inputsHash) {
|
|
548
|
+
checkExecution(taskHash, inputsHash);
|
|
549
|
+
const [latest] = await this.records.scan([...recordKeys.kind(repo, 'execution'), taskHash, inputsHash], { reverse: true, limit: 1 });
|
|
550
|
+
return latest === undefined ? null : statusOf(taskHash, inputsHash, latest.value);
|
|
551
|
+
}
|
|
552
|
+
/** Lists every execution: each task and inputs an attempt or a plan is recorded under. */
|
|
553
|
+
async executionList(repo) {
|
|
554
|
+
const found = new Map();
|
|
555
|
+
for (const kind of ['execution', 'plan']) {
|
|
556
|
+
for (const key of await this.records.keys(recordKeys.kind(repo, kind))) {
|
|
557
|
+
const [taskHash, inputsHash] = [key[3], key[4]];
|
|
558
|
+
found.set(JSON.stringify([taskHash, inputsHash]), { taskHash, inputsHash });
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return [...found.values()];
|
|
562
|
+
}
|
|
563
|
+
async executionListForTask(repo, taskHash) {
|
|
564
|
+
checkHash('task hash', taskHash);
|
|
565
|
+
const found = new Set();
|
|
566
|
+
for (const kind of ['execution', 'plan']) {
|
|
567
|
+
for (const key of await this.records.keys([...recordKeys.kind(repo, kind), taskHash]))
|
|
568
|
+
found.add(key[4]);
|
|
569
|
+
}
|
|
570
|
+
return [...found];
|
|
571
|
+
}
|
|
572
|
+
/** Lists the latest attempt of each of a task's inputs, in one scan of its attempts. */
|
|
573
|
+
async executionListLatest(repo, taskHash) {
|
|
574
|
+
checkHash('task hash', taskHash);
|
|
575
|
+
const latest = new Map();
|
|
576
|
+
// In key order, so an inputs' last attempt is its latest.
|
|
577
|
+
for (const { key, value } of await this.records.scan([...recordKeys.kind(repo, 'execution'), taskHash]))
|
|
578
|
+
latest.set(key[4], value);
|
|
579
|
+
return [...latest].map(([inputsHash, data]) => ({ inputsHash, status: statusOf(taskHash, inputsHash, data) }));
|
|
580
|
+
}
|
|
581
|
+
/** Lists the task's places in the index of running attempts, and reads the
|
|
582
|
+
* latest attempt of each inputs they name, keeping those recorded running. */
|
|
583
|
+
async executionListRunning(repo, taskHash) {
|
|
584
|
+
checkHash('task hash', taskHash);
|
|
585
|
+
const inputs = new Set((await this.records.keys([...recordKeys.kind(repo, 'running'), taskHash])).map((key) => key[4]));
|
|
586
|
+
const listed = [];
|
|
587
|
+
for (const inputsHash of inputs) {
|
|
588
|
+
const status = await this.executionGetLatest(repo, taskHash, inputsHash);
|
|
589
|
+
if (status?.type === 'running')
|
|
590
|
+
listed.push({ inputsHash, status });
|
|
591
|
+
}
|
|
592
|
+
return listed;
|
|
593
|
+
}
|
|
594
|
+
/** Scans a page of the task's index of runs from its latest, and reads each
|
|
595
|
+
* run's record: a run's place and its record are written and deleted in
|
|
596
|
+
* one transaction, so each place has its record, but for a run deleted
|
|
597
|
+
* between the scan and the read, which it passes over. */
|
|
598
|
+
async executionListRuns(repo, taskHash, { before, limit }) {
|
|
599
|
+
checkHash('task hash', taskHash);
|
|
600
|
+
if (before !== undefined)
|
|
601
|
+
checkId('execution id', before);
|
|
602
|
+
checkPageLimit(limit);
|
|
603
|
+
const index = [...recordKeys.kind(repo, 'taskrun'), taskHash];
|
|
604
|
+
const places = await this.records.keys(index, { reverse: true, limit, ...(before !== undefined && { before: [...index, before] }) });
|
|
605
|
+
const runs = [];
|
|
606
|
+
for (const place of places) {
|
|
607
|
+
const [executionId, inputsHash] = [place[4], place[5]];
|
|
608
|
+
const data = await this.records.get(recordKeys.execution(repo, taskHash, inputsHash, executionId));
|
|
609
|
+
if (data !== null)
|
|
610
|
+
runs.push({ inputsHash, executionId, status: statusOf(taskHash, inputsHash, data) });
|
|
611
|
+
}
|
|
612
|
+
return runs;
|
|
613
|
+
}
|
|
614
|
+
async executionOwnerWrite(repo, taskHash, inputsHash, executionId, owner) {
|
|
615
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
616
|
+
await writeRecords(this.records, (tx) => tx.put(recordKeys.owner(repo, taskHash, inputsHash, executionId), encodeOwner(owner)));
|
|
617
|
+
}
|
|
618
|
+
async executionOwnerRead(repo, taskHash, inputsHash, executionId) {
|
|
619
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
620
|
+
const data = await this.records.get(recordKeys.owner(repo, taskHash, inputsHash, executionId));
|
|
621
|
+
if (data === null)
|
|
622
|
+
return null;
|
|
623
|
+
// The owner is advisory: one that does not decode is none recorded, so a
|
|
624
|
+
// stale `running` record, which a dead owner's is, is left alone.
|
|
625
|
+
try {
|
|
626
|
+
return decodeOwner(data);
|
|
627
|
+
}
|
|
628
|
+
catch {
|
|
629
|
+
return null;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
async executionPlanWrite(repo, taskHash, inputsHash, planHash) {
|
|
633
|
+
checkExecution(taskHash, inputsHash);
|
|
634
|
+
await writeRecords(this.records, (tx) => {
|
|
635
|
+
if (planHash === null)
|
|
636
|
+
tx.delete(recordKeys.plan(repo, taskHash, inputsHash));
|
|
637
|
+
else
|
|
638
|
+
tx.put(recordKeys.plan(repo, taskHash, inputsHash), encodeHash(planHash));
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
async executionPlanRead(repo, taskHash, inputsHash) {
|
|
642
|
+
checkExecution(taskHash, inputsHash);
|
|
643
|
+
return hashOf(await this.records.get(recordKeys.plan(repo, taskHash, inputsHash)));
|
|
644
|
+
}
|
|
645
|
+
async adoptionWrite(repo, sourceHash, manifestHash) {
|
|
646
|
+
checkHash('object hash', sourceHash);
|
|
647
|
+
await writeRecords(this.records, (tx) => tx.put(recordKeys.adoption(repo, sourceHash), encodeHash(manifestHash)));
|
|
648
|
+
}
|
|
649
|
+
async adoptionRead(repo, sourceHash) {
|
|
650
|
+
if (!isObjectHash(sourceHash))
|
|
651
|
+
return null;
|
|
652
|
+
return hashOf(await this.records.get(recordKeys.adoption(repo, sourceHash)));
|
|
653
|
+
}
|
|
654
|
+
async adoptionList(repo) {
|
|
655
|
+
return (await this.records.scan(recordKeys.kind(repo, 'adoption'))).map(({ key, value }) => ({ sourceHash: key[3], manifestHash: hashOf(value) }));
|
|
656
|
+
}
|
|
657
|
+
async adoptionDelete(repo, sourceHash) {
|
|
658
|
+
if (!isObjectHash(sourceHash))
|
|
659
|
+
return;
|
|
660
|
+
await writeRecords(this.records, (tx) => tx.delete(recordKeys.adoption(repo, sourceHash)));
|
|
661
|
+
}
|
|
662
|
+
/** A run's key, its workspace and id checked. */
|
|
663
|
+
runKey(repo, workspace, runId) {
|
|
664
|
+
checkName('workspace', workspace);
|
|
665
|
+
checkId('run id', runId);
|
|
666
|
+
return recordKeys.run(repo, workspace, runId);
|
|
667
|
+
}
|
|
668
|
+
async dataflowRunGet(repo, workspace, runId) {
|
|
669
|
+
const data = await this.records.get(this.runKey(repo, workspace, runId));
|
|
670
|
+
return data === null ? null : decodeRun(data);
|
|
671
|
+
}
|
|
672
|
+
async dataflowRunWrite(repo, workspace, run) {
|
|
673
|
+
const key = this.runKey(repo, workspace, run.runId);
|
|
674
|
+
await writeRecords(this.records, (tx) => tx.put(key, encodeRun(run)));
|
|
675
|
+
}
|
|
676
|
+
async dataflowRunList(repo, workspace) {
|
|
677
|
+
checkName('workspace', workspace);
|
|
678
|
+
return (await this.records.keys([...recordKeys.kind(repo, 'run'), workspace])).map((key) => key[4]).filter(isUuidv7);
|
|
679
|
+
}
|
|
680
|
+
async dataflowRunGetLatest(repo, workspace) {
|
|
681
|
+
checkName('workspace', workspace);
|
|
682
|
+
const [latest] = await this.records.scan([...recordKeys.kind(repo, 'run'), workspace], { reverse: true, limit: 1 });
|
|
683
|
+
return latest === undefined ? null : decodeRun(latest.value);
|
|
684
|
+
}
|
|
685
|
+
async dataflowRunDelete(repo, workspace, runId) {
|
|
686
|
+
const key = this.runKey(repo, workspace, runId);
|
|
687
|
+
await writeRecords(this.records, (tx) => tx.delete(key));
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
/** An attempt's status, or `ExecutionCorruptError` for one that does not decode. */
|
|
691
|
+
function statusOf(taskHash, inputsHash, data) {
|
|
692
|
+
try {
|
|
693
|
+
return decodeExecutionStatus(data);
|
|
694
|
+
}
|
|
695
|
+
catch (err) {
|
|
696
|
+
throw new ExecutionCorruptError(taskHash, inputsHash, err instanceof Error ? err : new Error(`${err}`));
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
// =============================================================================
|
|
700
|
+
// Locks
|
|
701
|
+
// =============================================================================
|
|
702
|
+
/**
|
|
703
|
+
* Locks through the locks adapter — Web Locks in a browser — whose state
|
|
704
|
+
* another tab reads from the records.
|
|
705
|
+
*
|
|
706
|
+
* @remarks
|
|
707
|
+
* The lock itself is the adapter's: a Web Lock, which the browser frees when
|
|
708
|
+
* the tab that holds it closes. An exclusive holder writes its state, and its
|
|
709
|
+
* progress reports, as records while it holds the lock, and deletes them before
|
|
710
|
+
* it lets the lock go. A state is the holder's only while the lock is held and
|
|
711
|
+
* its session alive, so a tab closed mid-hold leaves a state no reader takes
|
|
712
|
+
* for held, and the next holder writes over.
|
|
713
|
+
*
|
|
714
|
+
* A holder is this tab's session: a `process` with pid 0 and the session's id
|
|
715
|
+
* as its `bootId`.
|
|
716
|
+
*/
|
|
717
|
+
class WebLockService {
|
|
718
|
+
records;
|
|
719
|
+
locks;
|
|
720
|
+
constructor(records, locks) {
|
|
721
|
+
this.records = records;
|
|
722
|
+
this.locks = locks;
|
|
723
|
+
}
|
|
724
|
+
/** The adapter's name for a repository's resource. */
|
|
725
|
+
nameOf(repo, resource) {
|
|
726
|
+
return JSON.stringify([repo, resource]);
|
|
727
|
+
}
|
|
728
|
+
/** This session, as a lock's holder. */
|
|
729
|
+
holder() {
|
|
730
|
+
return variant('process', { pid: 0n, bootId: this.locks.session, startTime: 0n, command: 'e3-web' });
|
|
731
|
+
}
|
|
732
|
+
async acquire(repo, resource, operation, options) {
|
|
733
|
+
checkName('lock', resource);
|
|
734
|
+
const mode = options?.mode ?? 'exclusive';
|
|
735
|
+
// A waiting acquire waits 30 s unless told otherwise, as every service's does.
|
|
736
|
+
const wait = options?.wait === true ? { wait: true, timeout: options.timeout ?? 30_000 } : {};
|
|
737
|
+
const hold = await this.locks.acquire(this.nameOf(repo, resource), mode, wait);
|
|
738
|
+
if (hold === null)
|
|
739
|
+
return null;
|
|
740
|
+
if (mode === 'shared') {
|
|
741
|
+
// A shared holder's report is not kept: progress is the exclusive holder's.
|
|
742
|
+
return { resource, report: () => Promise.resolve(), release: () => hold.release() };
|
|
743
|
+
}
|
|
744
|
+
const state = { operation, holder: this.holder(), acquiredAt: new Date(), expiresAt: none };
|
|
745
|
+
try {
|
|
746
|
+
await writeRecords(this.records, (tx) => {
|
|
747
|
+
tx.put(recordKeys.lock(repo, resource), encodeLockState(state));
|
|
748
|
+
tx.delete(recordKeys.progress(repo, resource));
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
catch (err) {
|
|
752
|
+
await hold.release();
|
|
753
|
+
throw err;
|
|
754
|
+
}
|
|
755
|
+
const stamp = { holder: state.holder, acquiredAt: state.acquiredAt };
|
|
756
|
+
let released = false;
|
|
757
|
+
// Reports land in order, one at a time, and the release waits for the last.
|
|
758
|
+
let reporting = Promise.resolve();
|
|
759
|
+
return {
|
|
760
|
+
resource,
|
|
761
|
+
report: (progress) => {
|
|
762
|
+
if (released)
|
|
763
|
+
return Promise.resolve();
|
|
764
|
+
reporting = reporting.catch(() => undefined).then(() => writeRecords(this.records, (tx) => tx.put(recordKeys.progress(repo, resource), encodeProgress({ ...stamp, progress }))));
|
|
765
|
+
return reporting;
|
|
766
|
+
},
|
|
767
|
+
release: async () => {
|
|
768
|
+
if (released)
|
|
769
|
+
return;
|
|
770
|
+
released = true;
|
|
771
|
+
await reporting.catch(() => undefined);
|
|
772
|
+
try {
|
|
773
|
+
await this.records.transact(async (tx) => {
|
|
774
|
+
if (writtenBy(await tx.get(recordKeys.lock(repo, resource)), stamp))
|
|
775
|
+
tx.delete(recordKeys.lock(repo, resource));
|
|
776
|
+
tx.delete(recordKeys.progress(repo, resource));
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
finally {
|
|
780
|
+
await hold.release();
|
|
781
|
+
}
|
|
782
|
+
},
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
async getState(repo, resource) {
|
|
786
|
+
checkName('lock', resource);
|
|
787
|
+
const data = await this.records.get(recordKeys.lock(repo, resource));
|
|
788
|
+
if (data === null)
|
|
789
|
+
return null;
|
|
790
|
+
let state;
|
|
791
|
+
try {
|
|
792
|
+
state = decodeLockState(data);
|
|
793
|
+
}
|
|
794
|
+
catch {
|
|
795
|
+
return null;
|
|
796
|
+
}
|
|
797
|
+
// A state is a holder's only while the lock is held, and the holder alive.
|
|
798
|
+
if (!(await this.locks.held(this.nameOf(repo, resource))).includes('exclusive'))
|
|
799
|
+
return null;
|
|
800
|
+
return (await this.isHolderAlive(state.holder)) ? state : null;
|
|
801
|
+
}
|
|
802
|
+
async getProgress(repo, resource) {
|
|
803
|
+
const state = await this.getState(repo, resource);
|
|
804
|
+
if (state === null)
|
|
805
|
+
return null;
|
|
806
|
+
const data = await this.records.get(recordKeys.progress(repo, resource));
|
|
807
|
+
if (data === null)
|
|
808
|
+
return null;
|
|
809
|
+
let record;
|
|
810
|
+
try {
|
|
811
|
+
record = decodeProgress(data);
|
|
812
|
+
}
|
|
813
|
+
catch {
|
|
814
|
+
return null; // a report is advisory: one that does not decode is none
|
|
815
|
+
}
|
|
816
|
+
// Only the live holder's report: a closed tab's is no one's.
|
|
817
|
+
return sameHolder({ holder: record.holder, acquiredAt: record.acquiredAt }, { holder: state.holder, acquiredAt: state.acquiredAt })
|
|
818
|
+
? record.progress
|
|
819
|
+
: null;
|
|
820
|
+
}
|
|
821
|
+
/**
|
|
822
|
+
* Whether a lock's holder is alive: a tab's session, pid 0, whose session
|
|
823
|
+
* Web Locks holds. No other holder takes a lock over these stores.
|
|
824
|
+
*/
|
|
825
|
+
async isHolderAlive(holder) {
|
|
826
|
+
if (holder.type !== 'process' || holder.value.pid !== 0n)
|
|
827
|
+
return false;
|
|
828
|
+
return this.locks.isAlive(holder.value.bootId);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
// =============================================================================
|
|
832
|
+
// Logs
|
|
833
|
+
// =============================================================================
|
|
834
|
+
/**
|
|
835
|
+
* How a log chunk's key names the byte of the log it starts at: in decimal,
|
|
836
|
+
* zero-padded to the sixteen digits any safe integer fits in, so keys order as
|
|
837
|
+
* the bytes do.
|
|
838
|
+
*/
|
|
839
|
+
function logStartName(start) {
|
|
840
|
+
return start.toString().padStart(16, '0');
|
|
841
|
+
}
|
|
842
|
+
/** The byte of the log a chunk starts at, as its key names it. */
|
|
843
|
+
function logStartOf(key) {
|
|
844
|
+
return Number(key[key.length - 1]);
|
|
845
|
+
}
|
|
846
|
+
/** The log a read finds no chunk of: never written, or removed. */
|
|
847
|
+
const EMPTY_LOG = { data: '', offset: 0, size: 0, totalSize: 0, complete: true };
|
|
848
|
+
/**
|
|
849
|
+
* Execution logs, each append a record of its own under the attempt's
|
|
850
|
+
* stream, keyed by the byte of the log it starts at, so a read cuts a window
|
|
851
|
+
* of bytes from the few chunks it covers, as a local log file's read seeks to
|
|
852
|
+
* it, however many appends the log took.
|
|
853
|
+
*/
|
|
854
|
+
class WebLogStore {
|
|
855
|
+
records;
|
|
856
|
+
constructor(records) {
|
|
857
|
+
this.records = records;
|
|
858
|
+
}
|
|
859
|
+
/** The prefix of the records an attempt's stream is kept in, a chunk each. */
|
|
860
|
+
streamKey(repo, taskHash, inputsHash, executionId, stream) {
|
|
861
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
862
|
+
return [...recordKeys.kind(repo, 'log'), taskHash, inputsHash, executionId, stream];
|
|
863
|
+
}
|
|
864
|
+
/** Appends a chunk where the stream's last chunk ends, in the transaction
|
|
865
|
+
* that reads the last chunk, so two appends never take one place. */
|
|
866
|
+
async append(repo, taskHash, inputsHash, executionId, stream, data) {
|
|
867
|
+
const prefix = this.streamKey(repo, taskHash, inputsHash, executionId, stream);
|
|
868
|
+
const bytes = new TextEncoder().encode(data);
|
|
869
|
+
if (bytes.length === 0)
|
|
870
|
+
return;
|
|
871
|
+
await this.records.transact(async (tx) => {
|
|
872
|
+
const [last] = await tx.scan(prefix, { reverse: true, limit: 1 });
|
|
873
|
+
tx.put([...prefix, logStartName(last === undefined ? 0 : logStartOf(last.key) + last.value.length)], bytes);
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
/**
|
|
877
|
+
* Reads a window of a stream's bytes from the chunks it covers: the log's
|
|
878
|
+
* size from its last chunk, the chunk holding the window's first byte, and
|
|
879
|
+
* the chunks that start inside the window.
|
|
880
|
+
*
|
|
881
|
+
* @remarks
|
|
882
|
+
* A chunk, once appended, never changes, and an append only adds one after
|
|
883
|
+
* the last, so the three reads answer as of the first: a chunk appended
|
|
884
|
+
* meanwhile starts past the window.
|
|
885
|
+
*
|
|
886
|
+
* @throws {RangeError} When the offset or the limit is not a whole number of
|
|
887
|
+
* zero or more
|
|
888
|
+
*/
|
|
889
|
+
async read(repo, taskHash, inputsHash, executionId, stream, options) {
|
|
890
|
+
const prefix = this.streamKey(repo, taskHash, inputsHash, executionId, stream);
|
|
891
|
+
const offset = options?.offset ?? 0;
|
|
892
|
+
const limit = options?.limit ?? 65536;
|
|
893
|
+
for (const [what, value] of [['offset', offset], ['limit', limit]]) {
|
|
894
|
+
if (!(Number.isSafeInteger(value) && value >= 0))
|
|
895
|
+
throw new RangeError(`a log window's ${what} is a whole number of zero or more, not ${value}`);
|
|
896
|
+
}
|
|
897
|
+
const [last] = await this.records.scan(prefix, { reverse: true, limit: 1 });
|
|
898
|
+
if (last === undefined)
|
|
899
|
+
return EMPTY_LOG;
|
|
900
|
+
const totalSize = logStartOf(last.key) + last.value.length;
|
|
901
|
+
const window = new Uint8Array(Math.max(0, Math.min(limit, totalSize - offset)));
|
|
902
|
+
if (window.length > 0) {
|
|
903
|
+
const [first] = await this.records.scan(prefix, { before: [...prefix, logStartName(offset + 1)], reverse: true, limit: 1 });
|
|
904
|
+
if (first === undefined)
|
|
905
|
+
return EMPTY_LOG; // removed meanwhile
|
|
906
|
+
const rest = await this.records.scan(prefix, { after: first.key, before: [...prefix, logStartName(offset + window.length)] });
|
|
907
|
+
for (const { key, value } of [first, ...rest]) {
|
|
908
|
+
const start = logStartOf(key);
|
|
909
|
+
const from = Math.max(offset, start);
|
|
910
|
+
const to = Math.min(offset + window.length, start + value.length);
|
|
911
|
+
if (from < to)
|
|
912
|
+
window.set(value.subarray(from - start, to - start), from - offset);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
// Stop short of a split character, unless the window reaches the log's end.
|
|
916
|
+
const size = offset + window.length >= totalSize ? window.length : completeUtf8Length(window) || window.length;
|
|
917
|
+
return {
|
|
918
|
+
data: new TextDecoder().decode(window.subarray(0, size)),
|
|
919
|
+
offset,
|
|
920
|
+
size,
|
|
921
|
+
totalSize,
|
|
922
|
+
complete: offset + size >= totalSize,
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
/** Holds nothing to flush: an append is a record of the transaction that
|
|
926
|
+
* wrote it, readable by every tab once the append resolves. The attempt's
|
|
927
|
+
* names are checked all the same, as every method checks them. */
|
|
928
|
+
async flush(_repo, taskHash, inputsHash, executionId) {
|
|
929
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
930
|
+
return Promise.resolve();
|
|
931
|
+
}
|
|
932
|
+
async remove(repo, taskHash, inputsHash, executionId) {
|
|
933
|
+
checkExecution(taskHash, inputsHash, executionId);
|
|
934
|
+
await writeRecords(this.records, (tx) => tx.deletePrefix([...recordKeys.kind(repo, 'log'), taskHash, inputsHash, executionId]));
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
// =============================================================================
|
|
938
|
+
// Dataset refs
|
|
939
|
+
// =============================================================================
|
|
940
|
+
/**
|
|
941
|
+
* Per-dataset refs, each a record beside the revision its write minted — a
|
|
942
|
+
* fresh UUID per write, so two byte-identical refs still differ — compared and
|
|
943
|
+
* swapped in one transaction.
|
|
944
|
+
*/
|
|
945
|
+
class WebDatasetRefStore {
|
|
946
|
+
records;
|
|
947
|
+
constructor(records) {
|
|
948
|
+
this.records = records;
|
|
949
|
+
}
|
|
950
|
+
/** A ref's key, its workspace checked. */
|
|
951
|
+
key(repo, ws, path) {
|
|
952
|
+
checkName('workspace', ws);
|
|
953
|
+
return recordKeys.dataset(repo, ws, path);
|
|
954
|
+
}
|
|
955
|
+
async read(repo, ws, path) {
|
|
956
|
+
const data = await this.records.get(this.key(repo, ws, path));
|
|
957
|
+
return data === null ? null : decodeRevisioned(data).ref;
|
|
958
|
+
}
|
|
959
|
+
async write(repo, ws, path, ref) {
|
|
960
|
+
const key = this.key(repo, ws, path);
|
|
961
|
+
await writeRecords(this.records, (tx) => tx.put(key, encodeRevisioned({ revision: crypto.randomUUID(), ref })));
|
|
962
|
+
}
|
|
963
|
+
async readVersioned(repo, ws, path) {
|
|
964
|
+
const data = await this.records.get(this.key(repo, ws, path));
|
|
965
|
+
return data === null ? null : decodeRevisioned(data);
|
|
966
|
+
}
|
|
967
|
+
async writeIf(repo, ws, path, ref, expectedRevision) {
|
|
968
|
+
const key = this.key(repo, ws, path);
|
|
969
|
+
return this.records.transact(async (tx) => {
|
|
970
|
+
const current = await tx.get(key);
|
|
971
|
+
const currentRevision = current === null ? null : decodeRevisioned(current).revision;
|
|
972
|
+
if (currentRevision !== expectedRevision)
|
|
973
|
+
throw new DatasetRefConflictError(ws, path, expectedRevision, currentRevision);
|
|
974
|
+
const revision = crypto.randomUUID();
|
|
975
|
+
tx.put(key, encodeRevisioned({ revision, ref }));
|
|
976
|
+
return { revision };
|
|
977
|
+
});
|
|
978
|
+
}
|
|
979
|
+
async list(repo, ws) {
|
|
980
|
+
checkName('workspace', ws);
|
|
981
|
+
return (await this.records.keys([...recordKeys.kind(repo, 'dataset'), ws])).map((key) => key[4]);
|
|
982
|
+
}
|
|
983
|
+
/** Reads every ref of the workspace in one scan of its records. */
|
|
984
|
+
async readAll(repo, ws) {
|
|
985
|
+
checkName('workspace', ws);
|
|
986
|
+
const all = new Map();
|
|
987
|
+
for (const { key, value } of await this.records.scan([...recordKeys.kind(repo, 'dataset'), ws]))
|
|
988
|
+
all.set(key[4], decodeRevisioned(value).ref);
|
|
989
|
+
return all;
|
|
990
|
+
}
|
|
991
|
+
async remove(repo, ws, path) {
|
|
992
|
+
const key = this.key(repo, ws, path);
|
|
993
|
+
await writeRecords(this.records, (tx) => tx.delete(key));
|
|
994
|
+
}
|
|
995
|
+
async removeAll(repo, ws) {
|
|
996
|
+
checkName('workspace', ws);
|
|
997
|
+
await writeRecords(this.records, (tx) => tx.deletePrefix([...recordKeys.kind(repo, 'dataset'), ws]));
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
// =============================================================================
|
|
1001
|
+
// Repositories and gc's primitives
|
|
1002
|
+
// =============================================================================
|
|
1003
|
+
/** How many objects a page of gc's object scan lists. */
|
|
1004
|
+
const SCAN_PAGE = 1000;
|
|
1005
|
+
/** The kinds of a repository's records that are its objects: the catalogue,
|
|
1006
|
+
* and the writes of blobs in flight. */
|
|
1007
|
+
const OBJECT_KINDS = new Set(['object', 'pending']);
|
|
1008
|
+
/**
|
|
1009
|
+
* Repositories — each a metadata record, with its records under its name and
|
|
1010
|
+
* its blobs under its name — and gc's primitives over the catalogue.
|
|
1011
|
+
*/
|
|
1012
|
+
class WebRepoStore {
|
|
1013
|
+
records;
|
|
1014
|
+
blobs;
|
|
1015
|
+
refs;
|
|
1016
|
+
datasets;
|
|
1017
|
+
upgrades;
|
|
1018
|
+
constructor(records, blobs, refs, datasets, upgrades) {
|
|
1019
|
+
this.records = records;
|
|
1020
|
+
this.blobs = blobs;
|
|
1021
|
+
this.refs = refs;
|
|
1022
|
+
this.datasets = datasets;
|
|
1023
|
+
this.upgrades = upgrades;
|
|
1024
|
+
}
|
|
1025
|
+
async list() {
|
|
1026
|
+
return (await this.records.keys(recordKeys.repositories())).map((key) => key[1]);
|
|
1027
|
+
}
|
|
1028
|
+
async exists(repo) {
|
|
1029
|
+
checkName('repository', repo);
|
|
1030
|
+
return (await this.records.get(recordKeys.repository(repo))) !== null;
|
|
1031
|
+
}
|
|
1032
|
+
async getMetadata(repo) {
|
|
1033
|
+
checkName('repository', repo);
|
|
1034
|
+
const data = await this.records.get(recordKeys.repository(repo));
|
|
1035
|
+
return data === null ? null : decodeMetadata(data);
|
|
1036
|
+
}
|
|
1037
|
+
/** Creates a repository: its metadata and its record, in one transaction. */
|
|
1038
|
+
async create(repo) {
|
|
1039
|
+
checkName('repository', repo);
|
|
1040
|
+
const now = new Date();
|
|
1041
|
+
await this.records.transact(async (tx) => {
|
|
1042
|
+
if ((await tx.get(recordKeys.repository(repo))) !== null)
|
|
1043
|
+
throw new RepoAlreadyExistsError(repo);
|
|
1044
|
+
tx.put(recordKeys.record(repo), encodeRepositoryRecord(newRepositoryRecord(this.upgrades)));
|
|
1045
|
+
tx.put(recordKeys.repository(repo), encodeMetadata({ name: repo, status: variant('active', null), createdAt: now, statusChangedAt: now }));
|
|
1046
|
+
});
|
|
1047
|
+
}
|
|
1048
|
+
async setStatus(repo, status, expected) {
|
|
1049
|
+
checkName('repository', repo);
|
|
1050
|
+
await this.records.transact(async (tx) => {
|
|
1051
|
+
const data = await tx.get(recordKeys.repository(repo));
|
|
1052
|
+
if (data === null)
|
|
1053
|
+
throw new RepoNotFoundError(repo);
|
|
1054
|
+
const current = decodeMetadata(data);
|
|
1055
|
+
if (expected !== undefined && !(Array.isArray(expected) ? expected : [expected]).includes(current.status.type)) {
|
|
1056
|
+
throw new RepoStatusConflictError(repo, expected, current.status.type);
|
|
1057
|
+
}
|
|
1058
|
+
tx.put(recordKeys.repository(repo), encodeMetadata({ ...current, status: variant(status, null), statusChangedAt: new Date() }));
|
|
1059
|
+
});
|
|
1060
|
+
}
|
|
1061
|
+
async remove(repo) {
|
|
1062
|
+
checkName('repository', repo);
|
|
1063
|
+
// Whatever the batches left goes with it.
|
|
1064
|
+
await this.deleteRefsBatch(repo);
|
|
1065
|
+
await this.deleteObjectsBatch(repo);
|
|
1066
|
+
await writeRecords(this.records, (tx) => tx.delete(recordKeys.repository(repo)));
|
|
1067
|
+
}
|
|
1068
|
+
async deleteRefsBatch(repo, _cursor) {
|
|
1069
|
+
checkName('repository', repo);
|
|
1070
|
+
return this.records.transact(async (tx) => {
|
|
1071
|
+
const keys = (await tx.keys(recordKeys.of(repo))).filter((key) => !OBJECT_KINDS.has(key[2]));
|
|
1072
|
+
for (const kind of new Set(keys.map((key) => key[2])))
|
|
1073
|
+
tx.deletePrefix(recordKeys.kind(repo, kind));
|
|
1074
|
+
return { status: 'done', deleted: keys.length };
|
|
1075
|
+
});
|
|
1076
|
+
}
|
|
1077
|
+
async deleteObjectsBatch(repo, _cursor) {
|
|
1078
|
+
checkName('repository', repo);
|
|
1079
|
+
const deleted = await this.records.transact(async (tx) => {
|
|
1080
|
+
const count = (await tx.keys(recordKeys.kind(repo, 'object'))).length;
|
|
1081
|
+
for (const kind of OBJECT_KINDS)
|
|
1082
|
+
tx.deletePrefix(recordKeys.kind(repo, kind));
|
|
1083
|
+
return count;
|
|
1084
|
+
});
|
|
1085
|
+
await this.blobs.deletePrefix([repo]);
|
|
1086
|
+
return { status: 'done', deleted };
|
|
1087
|
+
}
|
|
1088
|
+
async gcScanPackageRoots(repo, _cursor) {
|
|
1089
|
+
return { roots: await packageRoots(this.refs, repo) };
|
|
1090
|
+
}
|
|
1091
|
+
async gcScanWorkspaceRoots(repo, _cursor) {
|
|
1092
|
+
return { roots: await workspaceRoots(this.refs, this.datasets, repo) };
|
|
1093
|
+
}
|
|
1094
|
+
async gcScanExecutionRoots(repo, _cursor) {
|
|
1095
|
+
return { roots: await executionRoots(this.refs, repo) };
|
|
1096
|
+
}
|
|
1097
|
+
/** A page of the catalogue, a thousand objects at a time; the cursor is the
|
|
1098
|
+
* last page's last hash. */
|
|
1099
|
+
async gcScanObjects(repo, cursor) {
|
|
1100
|
+
const entries = await this.records.scan(recordKeys.kind(repo, 'object'), {
|
|
1101
|
+
...(cursor !== undefined && { after: recordKeys.object(repo, cursor) }),
|
|
1102
|
+
limit: SCAN_PAGE + 1,
|
|
1103
|
+
});
|
|
1104
|
+
const objects = entries.slice(0, SCAN_PAGE).map(({ key, value }) => {
|
|
1105
|
+
const record = decodeObject(value);
|
|
1106
|
+
return {
|
|
1107
|
+
hash: key[3],
|
|
1108
|
+
lastModified: Number(record.writtenAt),
|
|
1109
|
+
size: Number(record.size),
|
|
1110
|
+
unreachableSince: record.unreachableSince.type === 'some' ? Number(record.unreachableSince.value) : null,
|
|
1111
|
+
};
|
|
1112
|
+
});
|
|
1113
|
+
return entries.length > SCAN_PAGE ? { objects, cursor: objects[objects.length - 1].hash } : { objects };
|
|
1114
|
+
}
|
|
1115
|
+
/** Deletes objects' entries, and then their blobs: a blob a failure leaves
|
|
1116
|
+
* is the backend's sweep's. */
|
|
1117
|
+
async gcDeleteObjects(repo, hashes) {
|
|
1118
|
+
for (const hash of hashes)
|
|
1119
|
+
checkHash('object hash', hash);
|
|
1120
|
+
const blobs = await this.records.transact(async (tx) => {
|
|
1121
|
+
const gone = [];
|
|
1122
|
+
for (const hash of hashes) {
|
|
1123
|
+
const record = await readObject(tx, repo, hash);
|
|
1124
|
+
if (record === null)
|
|
1125
|
+
continue;
|
|
1126
|
+
tx.delete(recordKeys.object(repo, hash));
|
|
1127
|
+
gone.push(record.blob);
|
|
1128
|
+
}
|
|
1129
|
+
return gone;
|
|
1130
|
+
});
|
|
1131
|
+
for (const blob of blobs)
|
|
1132
|
+
await this.blobs.delete(blobKey(repo, blob));
|
|
1133
|
+
}
|
|
1134
|
+
async gcNoteUnreachable(repo, hashes, at) {
|
|
1135
|
+
for (const hash of hashes)
|
|
1136
|
+
checkHash('object hash', hash);
|
|
1137
|
+
if (hashes.length === 0)
|
|
1138
|
+
return [];
|
|
1139
|
+
return this.records.transact(async (tx) => {
|
|
1140
|
+
const sinces = [];
|
|
1141
|
+
for (const hash of hashes) {
|
|
1142
|
+
const record = await readObject(tx, repo, hash);
|
|
1143
|
+
if (record === null) {
|
|
1144
|
+
sinces.push(at);
|
|
1145
|
+
}
|
|
1146
|
+
else if (record.unreachableSince.type === 'some') {
|
|
1147
|
+
sinces.push(Number(record.unreachableSince.value));
|
|
1148
|
+
}
|
|
1149
|
+
else {
|
|
1150
|
+
tx.put(recordKeys.object(repo, hash), encodeObject({ ...record, unreachableSince: some(BigInt(at)) }));
|
|
1151
|
+
sinces.push(at);
|
|
1152
|
+
}
|
|
1153
|
+
}
|
|
1154
|
+
return sinces;
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
async gcClearUnreachable(repo, hashes) {
|
|
1158
|
+
for (const hash of hashes)
|
|
1159
|
+
checkHash('object hash', hash);
|
|
1160
|
+
if (hashes.length === 0)
|
|
1161
|
+
return;
|
|
1162
|
+
await this.records.transact(async (tx) => {
|
|
1163
|
+
for (const hash of hashes) {
|
|
1164
|
+
const record = await readObject(tx, repo, hash);
|
|
1165
|
+
if (record !== null && record.unreachableSince.type === 'some') {
|
|
1166
|
+
tx.put(recordKeys.object(repo, hash), encodeObject({ ...record, unreachableSince: none }));
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
});
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Deletes an object while its note stands at `since`: its entry in the
|
|
1173
|
+
* transaction that finds the note, and then the blob the entry named. A
|
|
1174
|
+
* write after that stores its bytes as a blob of its own, which the delete
|
|
1175
|
+
* never takes.
|
|
1176
|
+
*/
|
|
1177
|
+
async gcDeleteUnreachable(repo, hash, since) {
|
|
1178
|
+
checkHash('object hash', hash);
|
|
1179
|
+
const blob = await this.records.transact(async (tx) => {
|
|
1180
|
+
const record = await readObject(tx, repo, hash);
|
|
1181
|
+
if (record === null || record.unreachableSince.type !== 'some' || Number(record.unreachableSince.value) !== since)
|
|
1182
|
+
return null;
|
|
1183
|
+
tx.delete(recordKeys.object(repo, hash));
|
|
1184
|
+
return record.blob;
|
|
1185
|
+
});
|
|
1186
|
+
if (blob === null)
|
|
1187
|
+
return false;
|
|
1188
|
+
await this.blobs.delete(blobKey(repo, blob));
|
|
1189
|
+
return true;
|
|
1190
|
+
}
|
|
1191
|
+
/** A gc run's part's key, its run and name checked. */
|
|
1192
|
+
gcRunKey(repo, run, name) {
|
|
1193
|
+
checkId('gc run id', run);
|
|
1194
|
+
if (!/^[a-z0-9][a-z0-9.]*$/.test(name))
|
|
1195
|
+
throw new Error(`'${name}' is not the name of a gc run's part`);
|
|
1196
|
+
return recordKeys.gcRun(repo, run, name);
|
|
1197
|
+
}
|
|
1198
|
+
async gcRunWrite(repo, run, name, data) {
|
|
1199
|
+
const key = this.gcRunKey(repo, run, name);
|
|
1200
|
+
await writeRecords(this.records, (tx) => tx.put(key, data));
|
|
1201
|
+
}
|
|
1202
|
+
async gcRunRead(repo, run, name) {
|
|
1203
|
+
return this.records.get(this.gcRunKey(repo, run, name));
|
|
1204
|
+
}
|
|
1205
|
+
async gcRunDelete(repo, run) {
|
|
1206
|
+
checkId('gc run id', run);
|
|
1207
|
+
await writeRecords(this.records, (tx) => tx.deletePrefix([...recordKeys.kind(repo, 'gc'), run]));
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Sweeps what writes and deletes that never finished left: what the blob
|
|
1211
|
+
* store staged (a tab closed mid-write), a write in flight recorded longer
|
|
1212
|
+
* ago than the age gate, with its blob, and the blobs nothing names — a
|
|
1213
|
+
* delete cut short between an entry and its blob, or a write's blob that
|
|
1214
|
+
* another write of the same bytes beat to the catalogue — and what transfers
|
|
1215
|
+
* staged ({@link stagedKeys}) that no transfer's record names, past the age
|
|
1216
|
+
* gate, or that is older than {@link TRANSFER_RETENTION_MS}.
|
|
1217
|
+
*
|
|
1218
|
+
* @remarks
|
|
1219
|
+
* A write in flight younger than the age gate is left; one older is taken
|
|
1220
|
+
* for abandoned, and if it is still going it fails rather than store
|
|
1221
|
+
* anything, as a local repository's write does when a sweep takes its
|
|
1222
|
+
* staging file.
|
|
1223
|
+
*
|
|
1224
|
+
* The blobs are listed before the records that name them are read, and the
|
|
1225
|
+
* writes in flight before the entries: a blob is there only once its write
|
|
1226
|
+
* has recorded itself in flight, and a write's record in flight goes in the
|
|
1227
|
+
* transaction that names its blob in the catalogue, so a blob a write has
|
|
1228
|
+
* not finished with is named by one read or the other.
|
|
1229
|
+
*/
|
|
1230
|
+
async gcSweepBackend(repo, _reachable, options) {
|
|
1231
|
+
const { minAge, dryRun } = options;
|
|
1232
|
+
const staged = await this.blobs.sweep({ minAge, dryRun });
|
|
1233
|
+
let deletedPartials = staged.deleted;
|
|
1234
|
+
let skippedYoung = staged.skippedYoung;
|
|
1235
|
+
// Writes in flight longer than the age gate: abandoned
|
|
1236
|
+
const now = Date.now();
|
|
1237
|
+
for (const { key, value } of await this.records.scan(recordKeys.kind(repo, 'pending'))) {
|
|
1238
|
+
if (minAge > 0 && now - Number(decodePending(value).at) < minAge) {
|
|
1239
|
+
skippedYoung++;
|
|
1240
|
+
continue;
|
|
1241
|
+
}
|
|
1242
|
+
if (!dryRun) {
|
|
1243
|
+
const taken = await this.records.transact(async (tx) => {
|
|
1244
|
+
if ((await tx.get(key)) === null)
|
|
1245
|
+
return false; // finished meanwhile
|
|
1246
|
+
tx.delete(key);
|
|
1247
|
+
return true;
|
|
1248
|
+
});
|
|
1249
|
+
if (!taken)
|
|
1250
|
+
continue;
|
|
1251
|
+
await this.blobs.delete(blobKey(repo, key[3]));
|
|
1252
|
+
}
|
|
1253
|
+
deletedPartials++;
|
|
1254
|
+
}
|
|
1255
|
+
// Blobs nothing names
|
|
1256
|
+
const listed = await this.blobs.list([repo, 'objects']);
|
|
1257
|
+
const named = new Set();
|
|
1258
|
+
for (const key of await this.records.keys(recordKeys.kind(repo, 'pending')))
|
|
1259
|
+
named.add(key[3]);
|
|
1260
|
+
for (const { value } of await this.records.scan(recordKeys.kind(repo, 'object')))
|
|
1261
|
+
named.add(decodeObject(value).blob);
|
|
1262
|
+
for (const { key } of listed) {
|
|
1263
|
+
if (named.has(key[key.length - 1]))
|
|
1264
|
+
continue;
|
|
1265
|
+
if (!dryRun)
|
|
1266
|
+
await this.blobs.delete(key);
|
|
1267
|
+
deletedPartials++;
|
|
1268
|
+
}
|
|
1269
|
+
// What transfers staged that no record names — a tab closed between a
|
|
1270
|
+
// transfer's record going and its staging — or that is past its
|
|
1271
|
+
// retention, as a local repository's sweep takes its staged transfers.
|
|
1272
|
+
// The blobs are listed before the records are read, so a transfer
|
|
1273
|
+
// staging now is named by its record.
|
|
1274
|
+
const transfers = new Map();
|
|
1275
|
+
for (const { key, lastModified } of await this.blobs.list(stagedKeys.all(repo))) {
|
|
1276
|
+
const id = key[2];
|
|
1277
|
+
if (id === undefined)
|
|
1278
|
+
continue;
|
|
1279
|
+
const seen = transfers.get(id) ?? { count: 0, newest: 0 };
|
|
1280
|
+
transfers.set(id, { count: seen.count + 1, newest: Math.max(seen.newest, lastModified) });
|
|
1281
|
+
}
|
|
1282
|
+
if (transfers.size > 0) {
|
|
1283
|
+
const recorded = new Set((await this.records.keys(transferKeys.all())).map((key) => key[2]));
|
|
1284
|
+
for (const [id, { count, newest }] of transfers) {
|
|
1285
|
+
const stale = now - newest > TRANSFER_RETENTION_MS;
|
|
1286
|
+
if (recorded.has(id) && !stale)
|
|
1287
|
+
continue;
|
|
1288
|
+
if (!stale && minAge > 0 && now - newest < minAge) {
|
|
1289
|
+
skippedYoung += count;
|
|
1290
|
+
continue;
|
|
1291
|
+
}
|
|
1292
|
+
if (!dryRun)
|
|
1293
|
+
await this.blobs.deletePrefix(stagedKeys.of(repo, id));
|
|
1294
|
+
deletedPartials += count;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
return { deletedPartials, skippedYoung };
|
|
1298
|
+
}
|
|
1299
|
+
}
|
|
1300
|
+
// =============================================================================
|
|
1301
|
+
// The backend
|
|
1302
|
+
// =============================================================================
|
|
1303
|
+
/**
|
|
1304
|
+
* The upgrades of e3-web's own layout — its records' keys and its blobs — in
|
|
1305
|
+
* the order they apply: none yet. A release that changes the layout appends
|
|
1306
|
+
* its step here, and never edits, reorders or removes one a release shipped.
|
|
1307
|
+
*/
|
|
1308
|
+
export const WEB_REPOSITORY_UPGRADES = [];
|
|
1309
|
+
/**
|
|
1310
|
+
* e3's storage backend over the four adapters: a browser's IndexedDB, OPFS
|
|
1311
|
+
* and Web Locks, or memory.
|
|
1312
|
+
*
|
|
1313
|
+
* @remarks
|
|
1314
|
+
* A repository is named, as the in-memory backend's is: every store takes its
|
|
1315
|
+
* name. Two tabs over one origin's adapters share its repositories as two
|
|
1316
|
+
* processes share a directory of local ones: records change in transactions,
|
|
1317
|
+
* locks exclude across tabs, and a closed tab's locks are free.
|
|
1318
|
+
*
|
|
1319
|
+
* @example
|
|
1320
|
+
* ```ts
|
|
1321
|
+
* const storage = await openWebStorage();
|
|
1322
|
+
* await storage.repos.create('default');
|
|
1323
|
+
* const hash = await storage.objects.write('default', bytes);
|
|
1324
|
+
* ```
|
|
1325
|
+
*/
|
|
1326
|
+
export class WebStorage {
|
|
1327
|
+
adapters;
|
|
1328
|
+
upgrades = WEB_REPOSITORY_UPGRADES;
|
|
1329
|
+
objects;
|
|
1330
|
+
refs;
|
|
1331
|
+
locks;
|
|
1332
|
+
logs;
|
|
1333
|
+
repos;
|
|
1334
|
+
datasets;
|
|
1335
|
+
/** Every repository's runs' states, in the same records */
|
|
1336
|
+
states;
|
|
1337
|
+
/**
|
|
1338
|
+
* @param adapters - The adapters the repositories are kept over
|
|
1339
|
+
*/
|
|
1340
|
+
constructor(adapters) {
|
|
1341
|
+
this.adapters = adapters;
|
|
1342
|
+
const { records, blobs, locks, files } = adapters;
|
|
1343
|
+
this.objects = new WebObjectStore(records, blobs, files);
|
|
1344
|
+
this.refs = new WebRefStore(records);
|
|
1345
|
+
this.locks = new WebLockService(records, locks);
|
|
1346
|
+
this.logs = new WebLogStore(records);
|
|
1347
|
+
this.datasets = new WebDatasetRefStore(records);
|
|
1348
|
+
this.repos = new WebRepoStore(records, blobs, this.refs, this.datasets, this.upgrades);
|
|
1349
|
+
this.states = new WebStateStore(records);
|
|
1350
|
+
}
|
|
1351
|
+
/**
|
|
1352
|
+
* The store of a repository's dataflow runs' states: a {@link WebStateStore}
|
|
1353
|
+
* over the backend's own records, which keeps each repository's runs under
|
|
1354
|
+
* its records, where a workspace's removal and the repository's take them.
|
|
1355
|
+
*
|
|
1356
|
+
* @param _repo - Repository identifier
|
|
1357
|
+
* @returns The run state store
|
|
1358
|
+
*/
|
|
1359
|
+
runStates(_repo) {
|
|
1360
|
+
return this.states;
|
|
1361
|
+
}
|
|
1362
|
+
async validateRepository(repo) {
|
|
1363
|
+
if (!(await this.repos.exists(repo)))
|
|
1364
|
+
throw new RepoNotFoundError(repo);
|
|
1365
|
+
}
|
|
1366
|
+
/**
|
|
1367
|
+
* Closes the backend: its records' connection, and its session's locks.
|
|
1368
|
+
* What it keeps stays for the next backend over the same adapters.
|
|
1369
|
+
*/
|
|
1370
|
+
async close() {
|
|
1371
|
+
await this.adapters.records.close();
|
|
1372
|
+
await this.adapters.locks.close();
|
|
1373
|
+
}
|
|
1374
|
+
}
|
|
1375
|
+
/**
|
|
1376
|
+
* The browser APIs a persisted storage keeps its repositories with, in the
|
|
1377
|
+
* order {@link openWebStorage} checks them: each by the name a refusal gives
|
|
1378
|
+
* it, and its path from the global object.
|
|
1379
|
+
*
|
|
1380
|
+
* @remarks
|
|
1381
|
+
* A write moves the file it staged into place, and only ever a file, so
|
|
1382
|
+
* `move` is looked for on a file handle: Chromium defines it on
|
|
1383
|
+
* `FileSystemFileHandle.prototype` alone, and a browser that defines it on
|
|
1384
|
+
* `FileSystemHandle.prototype` gives it to file handles through the prototype
|
|
1385
|
+
* chain the path is read along.
|
|
1386
|
+
*/
|
|
1387
|
+
const PERSISTED_APIS = [
|
|
1388
|
+
{ name: 'IndexedDB', path: ['indexedDB'] },
|
|
1389
|
+
{ name: 'navigator.storage.getDirectory', path: ['navigator', 'storage', 'getDirectory'] },
|
|
1390
|
+
{ name: 'FileSystemFileHandle.createWritable', path: ['FileSystemFileHandle', 'prototype', 'createWritable'] },
|
|
1391
|
+
{ name: 'FileSystemFileHandle.move', path: ['FileSystemFileHandle', 'prototype', 'move'] },
|
|
1392
|
+
{ name: 'navigator.locks', path: ['navigator', 'locks'] },
|
|
1393
|
+
];
|
|
1394
|
+
/** Whether the global object leads to something by a path of properties,
|
|
1395
|
+
* each read along its object's prototype chain. */
|
|
1396
|
+
function reaches(path) {
|
|
1397
|
+
let at = globalThis;
|
|
1398
|
+
for (const name of path) {
|
|
1399
|
+
if (at === null || at === undefined)
|
|
1400
|
+
return false;
|
|
1401
|
+
at = at[name];
|
|
1402
|
+
}
|
|
1403
|
+
return at !== null && at !== undefined;
|
|
1404
|
+
}
|
|
1405
|
+
/**
|
|
1406
|
+
* Opens e3's storage in a browser.
|
|
1407
|
+
*
|
|
1408
|
+
* @remarks
|
|
1409
|
+
* Persisted, the records are the IndexedDB database of its name, the objects
|
|
1410
|
+
* are OPFS files under `/<name>/blobs`, the files `adoptFile` and
|
|
1411
|
+
* `materialize` name are OPFS files under `/<name>/files`, and the locks are
|
|
1412
|
+
* Web Locks whose names begin with `<name>:`. Before it opens any of them it
|
|
1413
|
+
* checks the page has every API it keeps repositories with — IndexedDB, OPFS
|
|
1414
|
+
* whose file handles have `createWritable` and `move`, and Web Locks — so a
|
|
1415
|
+
* browser that lacks one is refused at once, rather than at the first write
|
|
1416
|
+
* that needs it. Not persisted, each is in memory, and the page starts with
|
|
1417
|
+
* none on every load.
|
|
1418
|
+
*
|
|
1419
|
+
* @param options - Whether it persists, and its name
|
|
1420
|
+
* @returns The storage, over IndexedDB, OPFS and Web Locks, or over memory
|
|
1421
|
+
* @throws {Error} When it persists and the page lacks one of the APIs it keeps
|
|
1422
|
+
* repositories with — in order, `IndexedDB`,
|
|
1423
|
+
* `navigator.storage.getDirectory`, `FileSystemFileHandle.createWritable`,
|
|
1424
|
+
* `FileSystemFileHandle.move` and `navigator.locks` — naming the first
|
|
1425
|
+
* missing, having opened nothing
|
|
1426
|
+
*
|
|
1427
|
+
* @example
|
|
1428
|
+
* ```ts
|
|
1429
|
+
* const storage = await openWebStorage();
|
|
1430
|
+
* if (!(await storage.repos.exists('default'))) await storage.repos.create('default');
|
|
1431
|
+
* ```
|
|
1432
|
+
*/
|
|
1433
|
+
export async function openWebStorage(options = {}) {
|
|
1434
|
+
if (options.persist === false) {
|
|
1435
|
+
return new WebStorage({
|
|
1436
|
+
records: openMemoryRecords(),
|
|
1437
|
+
blobs: new MemoryBlobs(),
|
|
1438
|
+
locks: await new MemoryLockSpace().open(),
|
|
1439
|
+
files: new MemoryFiles(),
|
|
1440
|
+
});
|
|
1441
|
+
}
|
|
1442
|
+
const missing = PERSISTED_APIS.find(({ path }) => !reaches(path));
|
|
1443
|
+
if (missing !== undefined) {
|
|
1444
|
+
throw new Error(`e3-web cannot keep repositories in this page: it has no ${missing.name} ` +
|
|
1445
|
+
'(OPFS and Web Locks need a secure context: https:, or http://localhost). ' +
|
|
1446
|
+
'Open the storage with persist: false to keep repositories in memory instead.');
|
|
1447
|
+
}
|
|
1448
|
+
const name = options.name ?? 'e3';
|
|
1449
|
+
const records = await openIndexedDbRecords(name);
|
|
1450
|
+
try {
|
|
1451
|
+
const root = await navigator.storage.getDirectory();
|
|
1452
|
+
const blobs = new OpfsBlobs(await opfsDirectory(`/${name}/blobs`, root));
|
|
1453
|
+
const files = new OpfsFiles(await opfsDirectory(`/${name}/files`, root));
|
|
1454
|
+
const locks = await openWebLocks({ prefix: `${name}:` });
|
|
1455
|
+
return new WebStorage({ records, blobs, locks, files });
|
|
1456
|
+
}
|
|
1457
|
+
catch (err) {
|
|
1458
|
+
await records.close();
|
|
1459
|
+
throw err;
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
//# sourceMappingURL=WebStorage.js.map
|