@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,1140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Licensed under BSL 1.1. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `WebTransferBackend`: e3's transfer backend in a browser — the uploads and
|
|
7
|
+
* downloads a client sends and fetches, and the jobs that outlast a request,
|
|
8
|
+
* kept beside a {@link WebStorage}'s repositories.
|
|
9
|
+
*
|
|
10
|
+
* Every record is an East value, in beast2, in the storage's records adapter:
|
|
11
|
+
* IndexedDB in a browser, so a job one tab started is polled from another, as
|
|
12
|
+
* a request may reach any instance of a server. What a job stages — an
|
|
13
|
+
* upload's parts, a package's zip, an export's zip — is blobs of the storage's
|
|
14
|
+
* blobs adapter, OPFS files in a browser, under the repository, so its
|
|
15
|
+
* removal takes them. Every job runs through e3-core's shared handlers, on the
|
|
16
|
+
* {@link TaskRunner} the host gives each repository.
|
|
17
|
+
*
|
|
18
|
+
* A job, and an upload's commit, records the session that runs it — the
|
|
19
|
+
* tab's, as `WebStorage`'s lock holders do — until it ends. What a tab that
|
|
20
|
+
* closed left unfinished is never run again: the next tab records it failed,
|
|
21
|
+
* naming why, and removes what it staged, since the client that knew it was
|
|
22
|
+
* in the tab that closed, and the repository may have changed since. What a
|
|
23
|
+
* tab that lives runs is left to it.
|
|
24
|
+
*
|
|
25
|
+
* @packageDocumentation
|
|
26
|
+
*/
|
|
27
|
+
import { DateTimeType, IntegerType, OptionType, StringType, StructType, decodeBeast2For, encodeBeast2For, equalFor, none, some, variant, } from '@elaraai/east';
|
|
28
|
+
import { transferPartCount, transferPartRange, urlPathToTreePath } from '@elaraai/e3-types';
|
|
29
|
+
import { DatasetCommitStatusType, DatasetTypeMismatchError, DatasetUploadType, ExportStoppedError, PackageExportType, PackageImportType, RepoGcJobType, SplitCallJobType, WorkspaceDeployJobType, WorkspaceLockError, adoptProgressToIntakeFile, computeHash, datasetAdoptObject, handleProcessDeploy, handleProcessExport, handleProcessGc, handleProcessImport, handleProcessSplitCall, lockStateToHolderInfo, } from '@elaraai/e3-core/portable';
|
|
30
|
+
import { stagedKeys, transferKeys } from '../storage/WebStorage.js';
|
|
31
|
+
// =============================================================================
|
|
32
|
+
// Record forms
|
|
33
|
+
// =============================================================================
|
|
34
|
+
/** A dataset upload: the upload, its plan as parts, and its commit. */
|
|
35
|
+
const UploadRecordType = StructType({
|
|
36
|
+
upload: DatasetUploadType,
|
|
37
|
+
/** The size of every part but the last, once planned */
|
|
38
|
+
partBytes: OptionType(IntegerType),
|
|
39
|
+
/** How its commit stands, once asked for */
|
|
40
|
+
commit: OptionType(DatasetCommitStatusType),
|
|
41
|
+
/** The session running its commit, while one does */
|
|
42
|
+
owner: OptionType(StringType),
|
|
43
|
+
createdAt: DateTimeType,
|
|
44
|
+
/** When its commit finished */
|
|
45
|
+
finishedAt: OptionType(DateTimeType),
|
|
46
|
+
});
|
|
47
|
+
/** A download URL's object. */
|
|
48
|
+
const DownloadRecordType = StructType({ repo: StringType, hash: StringType, createdAt: DateTimeType });
|
|
49
|
+
/** A job: the job as e3-core's stores keep it, the session that runs it until
|
|
50
|
+
* it ends, and when it ended. */
|
|
51
|
+
const ImportRecordType = StructType({ job: PackageImportType, owner: OptionType(StringType), finishedAt: OptionType(DateTimeType) });
|
|
52
|
+
const DeployRecordType = StructType({ job: WorkspaceDeployJobType, owner: OptionType(StringType), finishedAt: OptionType(DateTimeType) });
|
|
53
|
+
const GcRecordType = StructType({ job: RepoGcJobType, owner: OptionType(StringType), finishedAt: OptionType(DateTimeType) });
|
|
54
|
+
const SplitRecordType = StructType({ job: SplitCallJobType, owner: OptionType(StringType), finishedAt: OptionType(DateTimeType) });
|
|
55
|
+
/** An export: the job, and how many rounds of its zip are written. The
|
|
56
|
+
* checkpoint a round goes on from is the job's own, in memory: a progress
|
|
57
|
+
* report writes the record, and the checkpoint grows with the zip. */
|
|
58
|
+
const ExportRecordType = StructType({
|
|
59
|
+
job: PackageExportType,
|
|
60
|
+
owner: OptionType(StringType),
|
|
61
|
+
finishedAt: OptionType(DateTimeType),
|
|
62
|
+
/** How many rounds of the zip are written, each a blob */
|
|
63
|
+
rounds: IntegerType,
|
|
64
|
+
});
|
|
65
|
+
const uploadForm = { encode: encodeBeast2For(UploadRecordType), decode: decodeBeast2For(UploadRecordType) };
|
|
66
|
+
const downloadForm = { encode: encodeBeast2For(DownloadRecordType), decode: decodeBeast2For(DownloadRecordType) };
|
|
67
|
+
const importForm = { encode: encodeBeast2For(ImportRecordType), decode: decodeBeast2For(ImportRecordType) };
|
|
68
|
+
const exportForm = { encode: encodeBeast2For(ExportRecordType), decode: decodeBeast2For(ExportRecordType) };
|
|
69
|
+
const deployForm = { encode: encodeBeast2For(DeployRecordType), decode: decodeBeast2For(DeployRecordType) };
|
|
70
|
+
const gcForm = { encode: encodeBeast2For(GcRecordType), decode: decodeBeast2For(GcRecordType) };
|
|
71
|
+
const splitForm = { encode: encodeBeast2For(SplitRecordType), decode: decodeBeast2For(SplitRecordType) };
|
|
72
|
+
/** Whether two owners are the same session, or both none. */
|
|
73
|
+
const sameOwner = equalFor(OptionType(StringType));
|
|
74
|
+
const sameString = equalFor(StringType);
|
|
75
|
+
/** A record, as it reads: none, one that does not decode — an earlier
|
|
76
|
+
* release's — or its value. */
|
|
77
|
+
async function readAs(read, key, form) {
|
|
78
|
+
const bytes = await read.get(key);
|
|
79
|
+
if (bytes === null)
|
|
80
|
+
return { kind: 'none' };
|
|
81
|
+
try {
|
|
82
|
+
return { kind: 'record', value: form.decode(bytes) };
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return { kind: 'undecodable' };
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/** A record's value; `null` when there is none, or it does not decode. */
|
|
89
|
+
async function readRecord(read, key, form) {
|
|
90
|
+
const found = await readAs(read, key, form);
|
|
91
|
+
return found.kind === 'record' ? found.value : null;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Changes a record in one transaction: what `next` makes of it is written,
|
|
95
|
+
* unless it gives `null`, which leaves it as it is.
|
|
96
|
+
*
|
|
97
|
+
* @returns What the record holds after: `null` when there is none
|
|
98
|
+
*/
|
|
99
|
+
function changeRecord(records, key, form, next) {
|
|
100
|
+
return records.transact(async (tx) => {
|
|
101
|
+
const current = await readRecord(tx, key, form);
|
|
102
|
+
if (current === null)
|
|
103
|
+
return null;
|
|
104
|
+
const changed = next(current);
|
|
105
|
+
if (changed === null)
|
|
106
|
+
return current;
|
|
107
|
+
tx.put(key, form.encode(changed));
|
|
108
|
+
return changed;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** Writes a record whole. */
|
|
112
|
+
async function putRecord(records, key, form, value) {
|
|
113
|
+
await records.transact((tx) => {
|
|
114
|
+
tx.put(key, form.encode(value));
|
|
115
|
+
return Promise.resolve();
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
/** Deletes a record. */
|
|
119
|
+
async function deleteRecord(records, key) {
|
|
120
|
+
await records.transact((tx) => {
|
|
121
|
+
tx.delete(key);
|
|
122
|
+
return Promise.resolve();
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
// =============================================================================
|
|
126
|
+
// Shared
|
|
127
|
+
// =============================================================================
|
|
128
|
+
/** The part size a dataset upload is planned with unless the host sets one:
|
|
129
|
+
* 8 MiB, since a part crosses between the page and its e3 worker whole. */
|
|
130
|
+
export const DEFAULT_WEB_PART_BYTES = 8 * 1024 * 1024;
|
|
131
|
+
/** The most bytes of a zip an export round writes before it stops at the
|
|
132
|
+
* next entry: 16 MiB unless the host sets it. */
|
|
133
|
+
export const DEFAULT_EXPORT_ROUND_BYTES = 16 * 1024 * 1024;
|
|
134
|
+
/** How long a finished job's status and a finished commit's stay readable for
|
|
135
|
+
* a poll, as the local server keeps a split call's, unless the host sets
|
|
136
|
+
* it: ten minutes. */
|
|
137
|
+
export const DEFAULT_RESULT_TTL_MS = 10 * 60 * 1000;
|
|
138
|
+
/** How long a record nothing finished or fetched is kept, with what it
|
|
139
|
+
* staged, unless the host sets it: a day. */
|
|
140
|
+
export const DEFAULT_RECORD_RETENTION_MS = 24 * 60 * 60 * 1000;
|
|
141
|
+
/** How often a live tab forgets what is past its retention, unless the host
|
|
142
|
+
* sets it: every ten minutes. */
|
|
143
|
+
export const DEFAULT_RETENTION_INTERVAL_MS = 10 * 60 * 1000;
|
|
144
|
+
/** Why a job or a commit a closed tab left is recorded failed. */
|
|
145
|
+
const CLOSED_TAB = 'its tab closed before it finished';
|
|
146
|
+
/** Why one no tab runs, which nothing will, is recorded failed. */
|
|
147
|
+
const STOPPED = 'it stopped before it finished';
|
|
148
|
+
/** The least time between two writes of a commit's progress to its record. */
|
|
149
|
+
const PROGRESS_INTERVAL_MS = 500;
|
|
150
|
+
/** Settles once what the host has queued before it has run. */
|
|
151
|
+
function nextTask() {
|
|
152
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
153
|
+
}
|
|
154
|
+
/** An error's message. */
|
|
155
|
+
function messageOf(err) {
|
|
156
|
+
return err instanceof Error ? err.message : String(err);
|
|
157
|
+
}
|
|
158
|
+
/** Bytes gathered whole, in order. */
|
|
159
|
+
function joined(chunks) {
|
|
160
|
+
if (chunks.length === 1)
|
|
161
|
+
return chunks[0];
|
|
162
|
+
let size = 0;
|
|
163
|
+
for (const chunk of chunks)
|
|
164
|
+
size += chunk.length;
|
|
165
|
+
const bytes = new Uint8Array(size);
|
|
166
|
+
let at = 0;
|
|
167
|
+
for (const chunk of chunks) {
|
|
168
|
+
bytes.set(chunk, at);
|
|
169
|
+
at += chunk.length;
|
|
170
|
+
}
|
|
171
|
+
return bytes;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Whose a record's owner is: no session's, this tab's, another tab's that
|
|
175
|
+
* lives, or one that has ended.
|
|
176
|
+
*/
|
|
177
|
+
async function ownerOf(locks, owner) {
|
|
178
|
+
if (owner.type === 'none')
|
|
179
|
+
return 'none';
|
|
180
|
+
if (sameOwner(owner, some(locks.session)))
|
|
181
|
+
return 'self';
|
|
182
|
+
return (await locks.isAlive(owner.value)) ? 'alive' : 'dead';
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Removes what a transfer whose record does not decode staged: its record
|
|
186
|
+
* names its repository, so every repository's staging of its id goes.
|
|
187
|
+
*/
|
|
188
|
+
async function forgetStaged(host, id) {
|
|
189
|
+
for (const repo of await host.storage.repos.list())
|
|
190
|
+
await host.blobs.deletePrefix(stagedKeys.of(repo, id));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The jobs this worker runs, or is taking: a job asked to run while it runs,
|
|
194
|
+
* or is being taken, here starts nothing new.
|
|
195
|
+
*/
|
|
196
|
+
class Running {
|
|
197
|
+
jobs = new Set();
|
|
198
|
+
/** Whether a job runs, or is being taken, here. */
|
|
199
|
+
has(kind, id) {
|
|
200
|
+
return this.jobs.has(`${kind}/${id}`);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Reserves a job for this worker, at once.
|
|
204
|
+
*
|
|
205
|
+
* @returns Whether it was free to reserve: not when it runs, or is being
|
|
206
|
+
* taken, here already
|
|
207
|
+
*/
|
|
208
|
+
reserve(kind, id) {
|
|
209
|
+
const job = `${kind}/${id}`;
|
|
210
|
+
if (this.jobs.has(job))
|
|
211
|
+
return false;
|
|
212
|
+
this.jobs.add(job);
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
215
|
+
/** Lets a job go, once it has ended or was not taken. */
|
|
216
|
+
release(kind, id) {
|
|
217
|
+
this.jobs.delete(`${kind}/${id}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// =============================================================================
|
|
221
|
+
// Dataset uploads
|
|
222
|
+
// =============================================================================
|
|
223
|
+
/**
|
|
224
|
+
* Dataset uploads: each part staged as a blob, and committed as the object the
|
|
225
|
+
* staged bytes hash to.
|
|
226
|
+
*
|
|
227
|
+
* @remarks
|
|
228
|
+
* A part is staged whole, in the blob of its number, so a part sent again
|
|
229
|
+
* replaces itself, and parts arrive in any order. The commit copies every part
|
|
230
|
+
* into one buffer of the upload's size, checking each is there at its planned
|
|
231
|
+
* size, hashes the bytes once, and stores them as the object they hash to —
|
|
232
|
+
* refused, with nothing written, when that is not the upload's hash — then
|
|
233
|
+
* adopts that object (`datasetAdoptObject`): a collection is taken in by
|
|
234
|
+
* intake units on the repository's runner, as the local server's `object`
|
|
235
|
+
* commit takes one in.
|
|
236
|
+
*/
|
|
237
|
+
class WebDatasetUploadStore {
|
|
238
|
+
host;
|
|
239
|
+
partBytes;
|
|
240
|
+
/** The commits this worker runs, or is taking, by upload: a commit asked
|
|
241
|
+
* for while one runs here is answered by it */
|
|
242
|
+
commits = new Map();
|
|
243
|
+
constructor(host, partBytes) {
|
|
244
|
+
this.host = host;
|
|
245
|
+
this.partBytes = partBytes;
|
|
246
|
+
}
|
|
247
|
+
key(id) {
|
|
248
|
+
return transferKeys.of('upload', id);
|
|
249
|
+
}
|
|
250
|
+
async create(id, record) {
|
|
251
|
+
await putRecord(this.host.records, this.key(id), uploadForm, {
|
|
252
|
+
upload: record, partBytes: none, commit: none, owner: none, createdAt: new Date(), finishedAt: none,
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
async get(id) {
|
|
256
|
+
return (await readRecord(this.host.records, this.key(id), uploadForm))?.upload ?? null;
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Forgets an upload: its record and every part it staged.
|
|
260
|
+
*
|
|
261
|
+
* @param id - The upload's id
|
|
262
|
+
*/
|
|
263
|
+
async delete(id) {
|
|
264
|
+
const record = await readRecord(this.host.records, this.key(id), uploadForm);
|
|
265
|
+
await deleteRecord(this.host.records, this.key(id));
|
|
266
|
+
if (record !== null)
|
|
267
|
+
await this.host.blobs.deletePrefix(stagedKeys.of(record.upload.repo, id));
|
|
268
|
+
}
|
|
269
|
+
async createParts(id, _record) {
|
|
270
|
+
await changeRecord(this.host.records, this.key(id), uploadForm, (current) => ({ ...current, partBytes: some(this.partBytes) }));
|
|
271
|
+
return this.partBytes;
|
|
272
|
+
}
|
|
273
|
+
async getPartBytes(id) {
|
|
274
|
+
const record = await readRecord(this.host.records, this.key(id), uploadForm);
|
|
275
|
+
return record?.partBytes.type === 'some' ? record.partBytes.value : null;
|
|
276
|
+
}
|
|
277
|
+
getPartUpload(id, _record, part) {
|
|
278
|
+
// e3-web's own byte endpoint takes the part whole, so its PUT needs no
|
|
279
|
+
// headers beyond its bytes; the route resolves the URL against its origin.
|
|
280
|
+
return Promise.resolve({ url: `/api/uploads/${id}/parts/${part}`, headers: {} });
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Stages one part of an upload, replacing any it staged before.
|
|
284
|
+
*
|
|
285
|
+
* @param id - The upload's id
|
|
286
|
+
* @param repo - The repository the upload is for
|
|
287
|
+
* @param part - The part's number, from 1
|
|
288
|
+
* @param bytes - The part's bytes, whole or a chunk at a time
|
|
289
|
+
* @returns The part's size, as staged
|
|
290
|
+
*/
|
|
291
|
+
stagePart(id, repo, part, bytes) {
|
|
292
|
+
return this.host.blobs.write(stagedKeys.part(repo, id, part), bytes);
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* Commits an upload, or answers how its commit stands.
|
|
296
|
+
*
|
|
297
|
+
* @remarks
|
|
298
|
+
* A commit asked for while one runs here is answered by it, and it is
|
|
299
|
+
* registered as it is asked for, so a poll from then on hears it under
|
|
300
|
+
* way. One a tab that lives runs is answered as it stands. One a tab that
|
|
301
|
+
* closed left is recorded failed, naming why, and what it staged removed:
|
|
302
|
+
* it is never run again.
|
|
303
|
+
*/
|
|
304
|
+
commit(id, record) {
|
|
305
|
+
const running = this.commits.get(id);
|
|
306
|
+
if (running !== undefined)
|
|
307
|
+
return running;
|
|
308
|
+
const committing = this.commitOnce(id, record);
|
|
309
|
+
this.commits.set(id, committing);
|
|
310
|
+
void committing.finally(() => {
|
|
311
|
+
if (this.commits.get(id) === committing)
|
|
312
|
+
this.commits.delete(id);
|
|
313
|
+
});
|
|
314
|
+
return committing;
|
|
315
|
+
}
|
|
316
|
+
/** Runs a commit here, or answers how it stands; never rejects. */
|
|
317
|
+
async commitOnce(id, record) {
|
|
318
|
+
try {
|
|
319
|
+
const current = await readRecord(this.host.records, this.key(id), uploadForm);
|
|
320
|
+
if (current === null)
|
|
321
|
+
return variant('failed', { message: `the upload '${id}' is not known` });
|
|
322
|
+
if (current.commit.type === 'some') {
|
|
323
|
+
if (current.commit.value.type !== 'processing')
|
|
324
|
+
return current.commit.value;
|
|
325
|
+
const owner = await ownerOf(this.host.locks, current.owner);
|
|
326
|
+
if (owner === 'alive')
|
|
327
|
+
return current.commit.value;
|
|
328
|
+
return await this.abandon(id, owner === 'dead' ? CLOSED_TAB : STOPPED);
|
|
329
|
+
}
|
|
330
|
+
const session = this.host.locks.session;
|
|
331
|
+
const claim = await this.host.records.transact(async (tx) => {
|
|
332
|
+
const now = await readRecord(tx, this.key(id), uploadForm);
|
|
333
|
+
if (now === null)
|
|
334
|
+
return { taken: false, status: variant('failed', { message: `the upload '${id}' is not known` }) };
|
|
335
|
+
// Another tab claimed it meanwhile: how far it has got.
|
|
336
|
+
if (now.commit.type === 'some')
|
|
337
|
+
return { taken: false, status: now.commit.value };
|
|
338
|
+
tx.put(this.key(id), uploadForm.encode({ ...now, commit: some(variant('processing', none)), owner: some(session) }));
|
|
339
|
+
return { taken: true };
|
|
340
|
+
});
|
|
341
|
+
if (!claim.taken)
|
|
342
|
+
return claim.status;
|
|
343
|
+
const status = await this.verifyAndAdopt(id, record);
|
|
344
|
+
// Its end is recorded before what it staged goes: a tab that closes
|
|
345
|
+
// between the two leaves the parts to gc's sweep.
|
|
346
|
+
await changeRecord(this.host.records, this.key(id), uploadForm, (now) => ({
|
|
347
|
+
...now, commit: some(status), owner: none, finishedAt: some(new Date()),
|
|
348
|
+
}));
|
|
349
|
+
await this.host.blobs.deletePrefix(stagedKeys.of(record.repo, id));
|
|
350
|
+
return status;
|
|
351
|
+
}
|
|
352
|
+
catch (err) {
|
|
353
|
+
return variant('failed', { message: messageOf(err) });
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Records a commit no tab will finish failed, and removes what it staged.
|
|
358
|
+
*
|
|
359
|
+
* @returns The commit's status, as recorded
|
|
360
|
+
*/
|
|
361
|
+
async abandon(id, why) {
|
|
362
|
+
const failed = variant('failed', { message: why });
|
|
363
|
+
const record = await changeRecord(this.host.records, this.key(id), uploadForm, (current) => (current.commit.type === 'some' && current.commit.value.type === 'processing'
|
|
364
|
+
? { ...current, commit: some(failed), owner: none, finishedAt: some(new Date()) }
|
|
365
|
+
: null));
|
|
366
|
+
if (record === null)
|
|
367
|
+
return failed;
|
|
368
|
+
await this.host.blobs.deletePrefix(stagedKeys.of(record.upload.repo, id));
|
|
369
|
+
return record.commit.type === 'some' ? record.commit.value : failed;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* How an upload's commit stands: as its record says, or under way while it
|
|
373
|
+
* is being taken here, before its record says so; `null` before one is
|
|
374
|
+
* asked for.
|
|
375
|
+
*/
|
|
376
|
+
async getCommitStatus(id) {
|
|
377
|
+
const record = await readRecord(this.host.records, this.key(id), uploadForm);
|
|
378
|
+
if (record?.commit.type === 'some')
|
|
379
|
+
return record.commit.value;
|
|
380
|
+
return this.commits.has(id) ? variant('processing', none) : null;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* Checks the staged parts are the upload's bytes, and points the upload's
|
|
384
|
+
* dataset at them; never rejects.
|
|
385
|
+
*
|
|
386
|
+
* @remarks
|
|
387
|
+
* The parts are copied, in order, into one buffer of the upload's size, each
|
|
388
|
+
* checked against its planned range, and the bytes are hashed once: they are
|
|
389
|
+
* stored as the object they hash to only when that is the upload's hash, so
|
|
390
|
+
* a store holds no object of bytes no one asked for. A collection's object
|
|
391
|
+
* is then taken in by intake units, how far they have got written to the
|
|
392
|
+
* upload's record as they go.
|
|
393
|
+
*/
|
|
394
|
+
async verifyAndAdopt(id, record) {
|
|
395
|
+
const { repo, workspace, path, hash, size } = record;
|
|
396
|
+
try {
|
|
397
|
+
const partBytes = await this.getPartBytes(id);
|
|
398
|
+
if (partBytes === null)
|
|
399
|
+
return variant('failed', { message: 'the upload was not planned as parts' });
|
|
400
|
+
const count = transferPartCount(size, partBytes);
|
|
401
|
+
const staged = new Uint8Array(Number(size));
|
|
402
|
+
for (let part = 1; part <= count; part++) {
|
|
403
|
+
const range = transferPartRange(size, partBytes, part);
|
|
404
|
+
const bytes = await this.host.blobs.read(stagedKeys.part(repo, id, part));
|
|
405
|
+
if (bytes === null)
|
|
406
|
+
return variant('failed', { message: `part ${part} of ${count} was not sent` });
|
|
407
|
+
if (bytes.length !== range.end - range.start) {
|
|
408
|
+
return variant('failed', { message: `part ${part} of ${count} is ${range.end - range.start} bytes, and ${bytes.length} were staged` });
|
|
409
|
+
}
|
|
410
|
+
staged.set(bytes, range.start);
|
|
411
|
+
}
|
|
412
|
+
const digest = computeHash(staged);
|
|
413
|
+
if (!sameString(digest, hash)) {
|
|
414
|
+
return variant('failed', { message: `the staged bytes hash to ${digest}, not the upload's ${hash}` });
|
|
415
|
+
}
|
|
416
|
+
await this.host.storage.objects.writeHashed(repo, digest, staged);
|
|
417
|
+
let reported = 0;
|
|
418
|
+
const report = (progress) => {
|
|
419
|
+
const now = Date.now();
|
|
420
|
+
if (now - reported < PROGRESS_INTERVAL_MS)
|
|
421
|
+
return;
|
|
422
|
+
reported = now;
|
|
423
|
+
const intake = adoptProgressToIntakeFile(path, progress);
|
|
424
|
+
void changeRecord(this.host.records, this.key(id), uploadForm, (current) => (current.commit.type === 'some' && current.commit.value.type === 'processing' ? { ...current, commit: some(variant('processing', some(intake))) } : null)).catch(() => undefined);
|
|
425
|
+
};
|
|
426
|
+
await datasetAdoptObject(this.host.storage, repo, workspace, urlPathToTreePath(path), hash, this.host.getRunner(repo), { onProgress: report });
|
|
427
|
+
return variant('completed', null);
|
|
428
|
+
}
|
|
429
|
+
catch (err) {
|
|
430
|
+
if (err instanceof DatasetTypeMismatchError)
|
|
431
|
+
return variant('type_mismatch', { path: err.path, message: err.message });
|
|
432
|
+
return variant('failed', { message: messageOf(err) });
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Records the commits closed tabs left failed, and forgets what is past its
|
|
437
|
+
* retention: a finished commit's answer once a poll has had a while to read
|
|
438
|
+
* it, and an upload no one committed, with what each staged.
|
|
439
|
+
*/
|
|
440
|
+
async sweep(now) {
|
|
441
|
+
const { resultTtlMs, recordMs } = this.host.retention;
|
|
442
|
+
for (const key of await this.host.records.keys(transferKeys.kind('upload'))) {
|
|
443
|
+
const id = key[2];
|
|
444
|
+
const found = await readAs(this.host.records, key, uploadForm);
|
|
445
|
+
if (found.kind === 'none')
|
|
446
|
+
continue;
|
|
447
|
+
if (found.kind === 'undecodable') {
|
|
448
|
+
await deleteRecord(this.host.records, key);
|
|
449
|
+
await forgetStaged(this.host, id);
|
|
450
|
+
continue;
|
|
451
|
+
}
|
|
452
|
+
const record = found.value;
|
|
453
|
+
if (record.finishedAt.type === 'some') {
|
|
454
|
+
if (now - record.finishedAt.value.getTime() > resultTtlMs)
|
|
455
|
+
await this.delete(id);
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
if (record.commit.type === 'none') {
|
|
459
|
+
if (now - record.createdAt.getTime() > recordMs)
|
|
460
|
+
await this.delete(id);
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
if (this.commits.has(id))
|
|
464
|
+
continue;
|
|
465
|
+
const owner = await ownerOf(this.host.locks, record.owner);
|
|
466
|
+
if (owner === 'dead')
|
|
467
|
+
await this.abandon(id, CLOSED_TAB);
|
|
468
|
+
else if (owner !== 'alive')
|
|
469
|
+
await this.abandon(id, STOPPED);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
// =============================================================================
|
|
474
|
+
// Downloads
|
|
475
|
+
// =============================================================================
|
|
476
|
+
/**
|
|
477
|
+
* Download URLs: each names an object of a repository's, which e3-web's own
|
|
478
|
+
* byte endpoint serves from the object store, once.
|
|
479
|
+
*/
|
|
480
|
+
class WebDatasetDownloadStore {
|
|
481
|
+
host;
|
|
482
|
+
constructor(host) {
|
|
483
|
+
this.host = host;
|
|
484
|
+
}
|
|
485
|
+
async getDownloadUrl(repo, hash) {
|
|
486
|
+
const id = crypto.randomUUID();
|
|
487
|
+
await putRecord(this.host.records, transferKeys.of('download', id), downloadForm, { repo, hash, createdAt: new Date() });
|
|
488
|
+
return `/api/downloads/${id}`;
|
|
489
|
+
}
|
|
490
|
+
async get(id) {
|
|
491
|
+
const record = await readRecord(this.host.records, transferKeys.of('download', id), downloadForm);
|
|
492
|
+
return record === null ? null : { repo: record.repo, hash: record.hash };
|
|
493
|
+
}
|
|
494
|
+
async delete(id) {
|
|
495
|
+
await deleteRecord(this.host.records, transferKeys.of('download', id));
|
|
496
|
+
}
|
|
497
|
+
/** Forgets the download URLs no one fetched within the retention. */
|
|
498
|
+
async sweep(now) {
|
|
499
|
+
for (const key of await this.host.records.keys(transferKeys.kind('download'))) {
|
|
500
|
+
const record = await readRecord(this.host.records, key, downloadForm);
|
|
501
|
+
if (record === null || now - record.createdAt.getTime() > this.host.retention.recordMs)
|
|
502
|
+
await deleteRecord(this.host.records, key);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
// =============================================================================
|
|
507
|
+
// The jobs
|
|
508
|
+
// =============================================================================
|
|
509
|
+
/**
|
|
510
|
+
* What a job store shares: its records, the session that runs each job until
|
|
511
|
+
* it ends, the jobs a closed tab left, recorded failed, and retention.
|
|
512
|
+
*
|
|
513
|
+
* @typeParam J - The job, as e3-core's store interface has it
|
|
514
|
+
* @typeParam R - Its record: the job, the session running it, and when it
|
|
515
|
+
* ended
|
|
516
|
+
*/
|
|
517
|
+
class JobStore {
|
|
518
|
+
host;
|
|
519
|
+
running;
|
|
520
|
+
kind;
|
|
521
|
+
form;
|
|
522
|
+
constructor(host, running, kind, form) {
|
|
523
|
+
this.host = host;
|
|
524
|
+
this.running = running;
|
|
525
|
+
this.kind = kind;
|
|
526
|
+
this.form = form;
|
|
527
|
+
}
|
|
528
|
+
key(id) {
|
|
529
|
+
return transferKeys.of(this.kind, id);
|
|
530
|
+
}
|
|
531
|
+
/** How long a finished job's record stays readable: the result TTL, unless
|
|
532
|
+
* a kind keeps it longer. */
|
|
533
|
+
resultTtl() {
|
|
534
|
+
return this.host.retention.resultTtlMs;
|
|
535
|
+
}
|
|
536
|
+
/** Removes what a job staged: once it has failed, or its record goes. */
|
|
537
|
+
discard(_id, _record) {
|
|
538
|
+
return Promise.resolve();
|
|
539
|
+
}
|
|
540
|
+
/** A job's record, or `null`. */
|
|
541
|
+
record(id) {
|
|
542
|
+
return readRecord(this.host.records, this.key(id), this.form);
|
|
543
|
+
}
|
|
544
|
+
/** Files a job, owned by this tab's session until it ends: a job created
|
|
545
|
+
* ended is no one's. */
|
|
546
|
+
async create(id, job) {
|
|
547
|
+
const owner = this.finished(job.status) ? none : some(this.host.locks.session);
|
|
548
|
+
await putRecord(this.host.records, this.key(id), this.form, this.fresh(job, owner));
|
|
549
|
+
}
|
|
550
|
+
async get(id) {
|
|
551
|
+
return (await this.record(id))?.job ?? null;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Records a job's status: one that ends it is run by no session after, and
|
|
555
|
+
* one that fails it removes what it staged.
|
|
556
|
+
*
|
|
557
|
+
* @throws {Error} When there is no such job
|
|
558
|
+
*/
|
|
559
|
+
async updateStatus(id, status) {
|
|
560
|
+
const ended = this.finished(status);
|
|
561
|
+
const changed = await changeRecord(this.host.records, this.key(id), this.form, (current) => this.withStatus(id, current, status, ended));
|
|
562
|
+
if (changed === null)
|
|
563
|
+
throw new Error(`${this.kind} job ${id} not found`);
|
|
564
|
+
if (this.failed(status))
|
|
565
|
+
await this.discard(id, changed);
|
|
566
|
+
}
|
|
567
|
+
async delete(id) {
|
|
568
|
+
const record = await this.record(id);
|
|
569
|
+
await deleteRecord(this.host.records, this.key(id));
|
|
570
|
+
if (record !== null)
|
|
571
|
+
await this.discard(id, record);
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Runs a job in this worker, in the background, unless it runs here
|
|
575
|
+
* already, has ended, or a tab that lives runs it. One a tab that closed
|
|
576
|
+
* left is recorded failed, naming why: it is never run again.
|
|
577
|
+
*
|
|
578
|
+
* @remarks
|
|
579
|
+
* The job is reserved for this worker as it is asked for, before anything
|
|
580
|
+
* is awaited, so a job asked to run twice at once runs once.
|
|
581
|
+
*
|
|
582
|
+
* @param id - The job's id
|
|
583
|
+
* @param repo - The repository the route named, which must be the job's
|
|
584
|
+
* @throws {Error} When the repository started no such job
|
|
585
|
+
*/
|
|
586
|
+
async execute(id, repo) {
|
|
587
|
+
const reserved = this.running.reserve(this.kind, id);
|
|
588
|
+
let started = false;
|
|
589
|
+
try {
|
|
590
|
+
const record = await this.record(id);
|
|
591
|
+
if (record === null || !sameString(record.job.repo, repo))
|
|
592
|
+
throw new Error(`${this.kind} job ${id} not found`);
|
|
593
|
+
// It runs, or is being taken, here already.
|
|
594
|
+
if (!reserved || this.finished(record.job.status))
|
|
595
|
+
return;
|
|
596
|
+
const owner = await ownerOf(this.host.locks, record.owner);
|
|
597
|
+
if (owner === 'alive')
|
|
598
|
+
return;
|
|
599
|
+
if (owner === 'dead') {
|
|
600
|
+
await this.abandon(id, CLOSED_TAB);
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const session = some(this.host.locks.session);
|
|
604
|
+
const taken = await changeRecord(this.host.records, this.key(id), this.form, (current) =>
|
|
605
|
+
// Ended, or taken by another session, meanwhile: not this one's.
|
|
606
|
+
(!this.finished(current.job.status) && sameOwner(current.owner, record.owner) ? this.withOwner(current, session) : null));
|
|
607
|
+
if (taken === null || this.finished(taken.job.status) || !sameOwner(taken.owner, session))
|
|
608
|
+
return;
|
|
609
|
+
started = true;
|
|
610
|
+
void this.run(id, taken);
|
|
611
|
+
}
|
|
612
|
+
finally {
|
|
613
|
+
if (reserved && !started)
|
|
614
|
+
this.running.release(this.kind, id);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
/** Runs a job to its end: one that ends without recording how is recorded
|
|
618
|
+
* failed, so no tab takes it for running. */
|
|
619
|
+
async run(id, record) {
|
|
620
|
+
let thrown = null;
|
|
621
|
+
try {
|
|
622
|
+
await this.work(id, record);
|
|
623
|
+
}
|
|
624
|
+
catch (err) {
|
|
625
|
+
thrown = err;
|
|
626
|
+
}
|
|
627
|
+
finally {
|
|
628
|
+
try {
|
|
629
|
+
const current = await this.record(id);
|
|
630
|
+
if (current !== null && !this.finished(current.job.status)) {
|
|
631
|
+
await this.updateStatus(id, this.failure(thrown === null ? STOPPED : `${STOPPED}: ${messageOf(thrown)}`));
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
catch {
|
|
635
|
+
// The record is gone, or the storage closed: nothing is left to end.
|
|
636
|
+
}
|
|
637
|
+
finally {
|
|
638
|
+
this.running.release(this.kind, id);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
/** Records a job no tab will finish failed, naming why, and removes what
|
|
643
|
+
* it staged. */
|
|
644
|
+
async abandon(id, why) {
|
|
645
|
+
const failure = this.failure(why);
|
|
646
|
+
const changed = await changeRecord(this.host.records, this.key(id), this.form, (current) => (this.finished(current.job.status) ? null : this.withStatus(id, current, failure, true)));
|
|
647
|
+
if (changed !== null)
|
|
648
|
+
await this.discard(id, changed);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* Records the jobs closed tabs left failed, and forgets the jobs that ended
|
|
652
|
+
* longer ago than their retention, and the records that do not decode, with
|
|
653
|
+
* what each staged.
|
|
654
|
+
*
|
|
655
|
+
* @remarks
|
|
656
|
+
* A job no tab runs and none will — one this tab filed and never ran, say —
|
|
657
|
+
* is recorded failed once it is older than the record retention.
|
|
658
|
+
*/
|
|
659
|
+
async sweep(now) {
|
|
660
|
+
const { recordMs } = this.host.retention;
|
|
661
|
+
for (const key of await this.host.records.keys(transferKeys.kind(this.kind))) {
|
|
662
|
+
const id = key[2];
|
|
663
|
+
const found = await readAs(this.host.records, key, this.form);
|
|
664
|
+
if (found.kind === 'none')
|
|
665
|
+
continue;
|
|
666
|
+
if (found.kind === 'undecodable') {
|
|
667
|
+
await deleteRecord(this.host.records, key);
|
|
668
|
+
await forgetStaged(this.host, id);
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
const record = found.value;
|
|
672
|
+
if (record.finishedAt.type === 'some') {
|
|
673
|
+
if (now - record.finishedAt.value.getTime() > this.resultTtl())
|
|
674
|
+
await this.delete(id);
|
|
675
|
+
continue;
|
|
676
|
+
}
|
|
677
|
+
if (this.running.has(this.kind, id))
|
|
678
|
+
continue;
|
|
679
|
+
const owner = await ownerOf(this.host.locks, record.owner);
|
|
680
|
+
if (owner === 'dead')
|
|
681
|
+
await this.abandon(id, CLOSED_TAB);
|
|
682
|
+
else if (owner !== 'alive' && now - record.job.createdAt.getTime() > recordMs)
|
|
683
|
+
await this.abandon(id, STOPPED);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
/**
|
|
688
|
+
* Package imports: the zip a client uploads is staged as a blob, and the job
|
|
689
|
+
* reads it as a {@link ZipSource}, by ranges where it lies.
|
|
690
|
+
*/
|
|
691
|
+
class WebPackageImportStore extends JobStore {
|
|
692
|
+
constructor(host, running) {
|
|
693
|
+
super(host, running, 'import', importForm);
|
|
694
|
+
}
|
|
695
|
+
fresh(job, owner) {
|
|
696
|
+
return { job, owner, finishedAt: none };
|
|
697
|
+
}
|
|
698
|
+
withStatus(_id, record, status, ended) {
|
|
699
|
+
return { ...record, job: { ...record.job, status }, ...(ended && { owner: none, finishedAt: some(new Date()) }) };
|
|
700
|
+
}
|
|
701
|
+
withOwner(record, owner) {
|
|
702
|
+
return { ...record, owner };
|
|
703
|
+
}
|
|
704
|
+
finished(status) {
|
|
705
|
+
return status.type === 'completed' || status.type === 'failed';
|
|
706
|
+
}
|
|
707
|
+
failed(status) {
|
|
708
|
+
return status.type === 'failed';
|
|
709
|
+
}
|
|
710
|
+
failure(message) {
|
|
711
|
+
return variant('failed', { message });
|
|
712
|
+
}
|
|
713
|
+
async discard(id, record) {
|
|
714
|
+
await this.host.blobs.deletePrefix(stagedKeys.of(record.job.repo, id));
|
|
715
|
+
}
|
|
716
|
+
getUploadUrl(id, _repo) {
|
|
717
|
+
return Promise.resolve(`/api/uploads/${id}`);
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Stages an import's zip, whole, and marks the import uploaded.
|
|
721
|
+
*
|
|
722
|
+
* @param id - The import's id
|
|
723
|
+
* @param repo - The repository the import is for
|
|
724
|
+
* @param bytes - The zip's bytes, whole or a chunk at a time
|
|
725
|
+
* @returns The zip's size, as staged
|
|
726
|
+
*/
|
|
727
|
+
async stageZip(id, repo, bytes) {
|
|
728
|
+
const size = await this.host.blobs.write(stagedKeys.zip(repo, id), bytes);
|
|
729
|
+
await this.updateStatus(id, variant('uploaded', null));
|
|
730
|
+
return size;
|
|
731
|
+
}
|
|
732
|
+
async work(id, record) {
|
|
733
|
+
const repo = record.job.repo;
|
|
734
|
+
const key = stagedKeys.zip(repo, id);
|
|
735
|
+
const stat = await this.host.blobs.stat(key);
|
|
736
|
+
if (stat === null) {
|
|
737
|
+
await this.updateStatus(id, variant('failed', { message: 'the package\'s zip was not uploaded' }));
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
const blobs = this.host.blobs;
|
|
741
|
+
const zip = {
|
|
742
|
+
size: stat.size,
|
|
743
|
+
read: async (offset, length) => {
|
|
744
|
+
const bytes = await blobs.readRange(key, offset, length);
|
|
745
|
+
if (bytes === null)
|
|
746
|
+
throw new Error(`the staged zip of import ${id} is gone`);
|
|
747
|
+
return bytes;
|
|
748
|
+
},
|
|
749
|
+
};
|
|
750
|
+
try {
|
|
751
|
+
await handleProcessImport({ storage: this.host.storage, importStore: this }, { id, repo, zip });
|
|
752
|
+
}
|
|
753
|
+
finally {
|
|
754
|
+
// The zip is the job's to remove, once it has ended.
|
|
755
|
+
await blobs.delete(key).catch(() => false);
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Package and workspace exports: the job writes its zip through e3-core's
|
|
761
|
+
* portable sink a round at a time, each round's bytes a blob, and the next
|
|
762
|
+
* round goes on from the checkpoint the last one stopped at.
|
|
763
|
+
*
|
|
764
|
+
* @remarks
|
|
765
|
+
* A round stops at the first entry after it has written its share of bytes,
|
|
766
|
+
* and the worker answers what waits — a page's requests — before the next.
|
|
767
|
+
* A workspace's export holds the workspace's lock across its rounds, as the
|
|
768
|
+
* shared handler asks of a caller that runs one job over several calls, so
|
|
769
|
+
* nothing changes the workspace between them. The last round's blob is
|
|
770
|
+
* counted with the job's completion, in one write. The download serves the
|
|
771
|
+
* rounds' blobs in order: the zip an export never stopped writes. An export
|
|
772
|
+
* that fails removes its rounds.
|
|
773
|
+
*/
|
|
774
|
+
class WebPackageExportStore extends JobStore {
|
|
775
|
+
roundBytes;
|
|
776
|
+
/** The round each export running here writes, which its completion
|
|
777
|
+
* counts */
|
|
778
|
+
writing = new Map();
|
|
779
|
+
constructor(host, running, roundBytes) {
|
|
780
|
+
super(host, running, 'export', exportForm);
|
|
781
|
+
this.roundBytes = roundBytes;
|
|
782
|
+
}
|
|
783
|
+
fresh(job, owner) {
|
|
784
|
+
return { job, owner, finishedAt: none, rounds: 0n };
|
|
785
|
+
}
|
|
786
|
+
withStatus(id, record, status, ended) {
|
|
787
|
+
const round = this.writing.get(id);
|
|
788
|
+
return {
|
|
789
|
+
...record,
|
|
790
|
+
job: { ...record.job, status },
|
|
791
|
+
// The zip is whole once its last round is written, which this counts.
|
|
792
|
+
...(status.type === 'completed' && round !== undefined && { rounds: BigInt(round + 1) }),
|
|
793
|
+
...(ended && { owner: none, finishedAt: some(new Date()) }),
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
withOwner(record, owner) {
|
|
797
|
+
return { ...record, owner };
|
|
798
|
+
}
|
|
799
|
+
finished(status) {
|
|
800
|
+
return status.type === 'completed' || status.type === 'failed';
|
|
801
|
+
}
|
|
802
|
+
failed(status) {
|
|
803
|
+
return status.type === 'failed';
|
|
804
|
+
}
|
|
805
|
+
failure(message) {
|
|
806
|
+
return variant('failed', { message });
|
|
807
|
+
}
|
|
808
|
+
async discard(id, record) {
|
|
809
|
+
await this.host.blobs.deletePrefix(stagedKeys.of(record.job.repo, id));
|
|
810
|
+
}
|
|
811
|
+
getDownloadUrl(id, _repo) {
|
|
812
|
+
return Promise.resolve(`/api/downloads/${id}`);
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* The zip of a completed export: its rounds' blobs, in order.
|
|
816
|
+
*
|
|
817
|
+
* @param id - The export's id
|
|
818
|
+
* @returns The zip, or `null` when the export has not completed, or what it
|
|
819
|
+
* wrote is gone
|
|
820
|
+
*/
|
|
821
|
+
async zipOf(id) {
|
|
822
|
+
const record = await this.record(id);
|
|
823
|
+
if (record === null || record.job.status.type !== 'completed')
|
|
824
|
+
return null;
|
|
825
|
+
const rounds = [];
|
|
826
|
+
for (let round = 0; round < Number(record.rounds); round++) {
|
|
827
|
+
const bytes = await this.host.blobs.read(stagedKeys.exported(record.job.repo, id, round));
|
|
828
|
+
if (bytes === null)
|
|
829
|
+
return null;
|
|
830
|
+
rounds.push(bytes);
|
|
831
|
+
}
|
|
832
|
+
return joined(rounds);
|
|
833
|
+
}
|
|
834
|
+
/**
|
|
835
|
+
* How many rounds of its zip an export has written.
|
|
836
|
+
*
|
|
837
|
+
* @param id - The export's id
|
|
838
|
+
* @returns Its rounds, or `null` for no such export
|
|
839
|
+
*/
|
|
840
|
+
async progressOf(id) {
|
|
841
|
+
const record = await this.record(id);
|
|
842
|
+
return record === null ? null : { rounds: Number(record.rounds) };
|
|
843
|
+
}
|
|
844
|
+
async work(id, record) {
|
|
845
|
+
const { repo, workspace } = record.job;
|
|
846
|
+
const blobs = this.host.blobs;
|
|
847
|
+
// A workspace's export holds the workspace from its first round to its
|
|
848
|
+
// last, as one call of the local server's holds it.
|
|
849
|
+
let lock = null;
|
|
850
|
+
if (workspace.type === 'some') {
|
|
851
|
+
lock = await this.host.storage.locks.acquire(repo, workspace.value, variant('export', null));
|
|
852
|
+
if (lock === null) {
|
|
853
|
+
// Its holder, as e3-core names a lock's holder wherever a lock refuses.
|
|
854
|
+
const state = await this.host.storage.locks.getState(repo, workspace.value);
|
|
855
|
+
const refused = new WorkspaceLockError(workspace.value, state === null ? undefined : lockStateToHolderInfo(state));
|
|
856
|
+
await this.updateStatus(id, this.failure(refused.message));
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
}
|
|
860
|
+
try {
|
|
861
|
+
let resume;
|
|
862
|
+
for (let round = 0;; round++) {
|
|
863
|
+
this.writing.set(id, round);
|
|
864
|
+
const chunks = [];
|
|
865
|
+
let written = 0;
|
|
866
|
+
const stop = new AbortController();
|
|
867
|
+
const key = stagedKeys.exported(repo, id, round);
|
|
868
|
+
const sink = new WritableStream({
|
|
869
|
+
// The zip writer hands over each chunk it writes, and keeps none.
|
|
870
|
+
write: (chunk) => {
|
|
871
|
+
chunks.push(chunk);
|
|
872
|
+
written += chunk.length;
|
|
873
|
+
// Past its share, the round stops once the entry it writes is
|
|
874
|
+
// written.
|
|
875
|
+
if (written >= this.roundBytes)
|
|
876
|
+
stop.abort();
|
|
877
|
+
},
|
|
878
|
+
// The zip is whole: its last round is written before the job is
|
|
879
|
+
// recorded completed, which counts it.
|
|
880
|
+
close: async () => {
|
|
881
|
+
await blobs.write(key, joined(chunks));
|
|
882
|
+
},
|
|
883
|
+
});
|
|
884
|
+
try {
|
|
885
|
+
await handleProcessExport({ storage: this.host.storage, exportStore: this, signal: stop.signal, ...(lock !== null && { lock }) }, {
|
|
886
|
+
id,
|
|
887
|
+
repo,
|
|
888
|
+
zip: sink,
|
|
889
|
+
...(resume !== undefined && { resume }),
|
|
890
|
+
});
|
|
891
|
+
return;
|
|
892
|
+
}
|
|
893
|
+
catch (err) {
|
|
894
|
+
if (!(err instanceof ExportStoppedError))
|
|
895
|
+
throw err;
|
|
896
|
+
await blobs.write(key, joined(chunks));
|
|
897
|
+
const next = await changeRecord(this.host.records, this.key(id), exportForm, (current) => ({ ...current, rounds: BigInt(round + 1) }));
|
|
898
|
+
if (next === null)
|
|
899
|
+
return;
|
|
900
|
+
resume = err.checkpoint;
|
|
901
|
+
// The worker answers what waits — a page's requests — before the
|
|
902
|
+
// next round: over storage in memory, a round never leaves the
|
|
903
|
+
// microtask queue, and the rounds of a large export would hold the
|
|
904
|
+
// worker until the last.
|
|
905
|
+
await nextTask();
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
}
|
|
909
|
+
catch (err) {
|
|
910
|
+
// The handler records an export's own failure; one of its rounds' blob
|
|
911
|
+
// or record is recorded here, so the job ends rather than waits.
|
|
912
|
+
const current = await this.record(id);
|
|
913
|
+
if (current !== null && !this.finished(current.job.status)) {
|
|
914
|
+
await this.updateStatus(id, this.failure(messageOf(err)));
|
|
915
|
+
}
|
|
916
|
+
throw err;
|
|
917
|
+
}
|
|
918
|
+
finally {
|
|
919
|
+
this.writing.delete(id);
|
|
920
|
+
await lock?.release().catch(() => undefined);
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
/** Workspace deploys: run through the shared handler, on the repository's
|
|
925
|
+
* runner. */
|
|
926
|
+
class WebWorkspaceDeployStore extends JobStore {
|
|
927
|
+
constructor(host, running) {
|
|
928
|
+
super(host, running, 'deploy', deployForm);
|
|
929
|
+
}
|
|
930
|
+
fresh(job, owner) {
|
|
931
|
+
return { job, owner, finishedAt: none };
|
|
932
|
+
}
|
|
933
|
+
withStatus(_id, record, status, ended) {
|
|
934
|
+
return { ...record, job: { ...record.job, status }, ...(ended && { owner: none, finishedAt: some(new Date()) }) };
|
|
935
|
+
}
|
|
936
|
+
withOwner(record, owner) {
|
|
937
|
+
return { ...record, owner };
|
|
938
|
+
}
|
|
939
|
+
finished(status) {
|
|
940
|
+
return status.type === 'completed' || status.type === 'failed';
|
|
941
|
+
}
|
|
942
|
+
failed(status) {
|
|
943
|
+
return status.type === 'failed';
|
|
944
|
+
}
|
|
945
|
+
failure(message) {
|
|
946
|
+
return variant('failed', { message });
|
|
947
|
+
}
|
|
948
|
+
work(id, record) {
|
|
949
|
+
const repo = record.job.repo;
|
|
950
|
+
return handleProcessDeploy({ storage: this.host.storage, deployStore: this, runner: this.host.getRunner(repo) }, { id, repo });
|
|
951
|
+
}
|
|
952
|
+
}
|
|
953
|
+
/** Repository gc: run through the shared handler, its history's prune taking
|
|
954
|
+
* the repository's runner's judgement of what still runs. */
|
|
955
|
+
class WebRepoGcStore extends JobStore {
|
|
956
|
+
constructor(host, running) {
|
|
957
|
+
super(host, running, 'gc', gcForm);
|
|
958
|
+
}
|
|
959
|
+
fresh(job, owner) {
|
|
960
|
+
return { job, owner, finishedAt: none };
|
|
961
|
+
}
|
|
962
|
+
withStatus(_id, record, status, ended) {
|
|
963
|
+
return { ...record, job: { ...record.job, status }, ...(ended && { owner: none, finishedAt: some(new Date()) }) };
|
|
964
|
+
}
|
|
965
|
+
withOwner(record, owner) {
|
|
966
|
+
return { ...record, owner };
|
|
967
|
+
}
|
|
968
|
+
finished(status) {
|
|
969
|
+
return status.status.type !== 'running';
|
|
970
|
+
}
|
|
971
|
+
failed(status) {
|
|
972
|
+
return status.status.type === 'failed';
|
|
973
|
+
}
|
|
974
|
+
failure(message) {
|
|
975
|
+
return { status: variant('failed', null), stats: none, error: some(message) };
|
|
976
|
+
}
|
|
977
|
+
work(id, record) {
|
|
978
|
+
const repo = record.job.repo;
|
|
979
|
+
return handleProcessGc({ storage: this.host.storage, gcStore: this, runner: this.host.getRunner(repo) }, { id, repo });
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
/** Split calls: run through the shared handler, on the repository's runner,
|
|
983
|
+
* and forgotten a while after they finish, as the local server forgets
|
|
984
|
+
* them. */
|
|
985
|
+
class WebSplitCallStore extends JobStore {
|
|
986
|
+
constructor(host, running) {
|
|
987
|
+
super(host, running, 'split', splitForm);
|
|
988
|
+
}
|
|
989
|
+
fresh(job, owner) {
|
|
990
|
+
return { job, owner, finishedAt: this.finished(job.status) ? some(new Date()) : none };
|
|
991
|
+
}
|
|
992
|
+
withStatus(_id, record, status, ended) {
|
|
993
|
+
return { ...record, job: { ...record.job, status }, ...(ended && { owner: none, finishedAt: some(new Date()) }) };
|
|
994
|
+
}
|
|
995
|
+
withOwner(record, owner) {
|
|
996
|
+
return { ...record, owner };
|
|
997
|
+
}
|
|
998
|
+
finished(status) {
|
|
999
|
+
return status.type !== 'processing';
|
|
1000
|
+
}
|
|
1001
|
+
failed(status) {
|
|
1002
|
+
return status.type === 'failed';
|
|
1003
|
+
}
|
|
1004
|
+
failure(message) {
|
|
1005
|
+
return variant('failed', { message });
|
|
1006
|
+
}
|
|
1007
|
+
work(id, record) {
|
|
1008
|
+
const repo = record.job.repo;
|
|
1009
|
+
return handleProcessSplitCall({ storage: this.host.storage, splitCallStore: this, runner: this.host.getRunner(repo) }, { id, repo });
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
/**
|
|
1013
|
+
* e3's {@link TransferBackend} in a browser: uploads staged in the storage's
|
|
1014
|
+
* blobs, downloads served from its object store, and every job — import,
|
|
1015
|
+
* export, deploy, gc, split call — run in this worker through e3-core's shared
|
|
1016
|
+
* handlers, its status in the storage's records.
|
|
1017
|
+
*
|
|
1018
|
+
* @remarks
|
|
1019
|
+
* Its URLs are e3-web's own byte endpoints (`createWebDataEndpoints`),
|
|
1020
|
+
* root-relative, which the routes resolve against the request's origin.
|
|
1021
|
+
* Every job and commit records this tab's session until it ends; {@link start}
|
|
1022
|
+
* records what closed tabs left failed — never running it again — and forgets
|
|
1023
|
+
* what is past its retention, and goes on doing so while the tab lives.
|
|
1024
|
+
*
|
|
1025
|
+
* @example
|
|
1026
|
+
* ```ts
|
|
1027
|
+
* const storage = await openWebStorage();
|
|
1028
|
+
* const transfer = new WebTransferBackend({ storage, getRunner });
|
|
1029
|
+
* await transfer.start();
|
|
1030
|
+
* // …
|
|
1031
|
+
* await transfer.close();
|
|
1032
|
+
* ```
|
|
1033
|
+
*/
|
|
1034
|
+
export class WebTransferBackend {
|
|
1035
|
+
datasetUpload;
|
|
1036
|
+
datasetDownload;
|
|
1037
|
+
packageImport;
|
|
1038
|
+
packageExport;
|
|
1039
|
+
workspaceDeploy;
|
|
1040
|
+
repoGc;
|
|
1041
|
+
splitCall;
|
|
1042
|
+
retention;
|
|
1043
|
+
timer = null;
|
|
1044
|
+
sweeping = null;
|
|
1045
|
+
closed = false;
|
|
1046
|
+
/**
|
|
1047
|
+
* @param options - The storage, each repository's runner, the part size,
|
|
1048
|
+
* the export rounds' size and the retention
|
|
1049
|
+
* @throws {RangeError} When a size is not a whole number of bytes greater
|
|
1050
|
+
* than zero, or a retention not a whole number of milliseconds greater
|
|
1051
|
+
* than zero
|
|
1052
|
+
*/
|
|
1053
|
+
constructor(options) {
|
|
1054
|
+
const partBytes = options.partBytes ?? DEFAULT_WEB_PART_BYTES;
|
|
1055
|
+
const roundBytes = options.exportRoundBytes ?? DEFAULT_EXPORT_ROUND_BYTES;
|
|
1056
|
+
for (const [what, value] of [['part size', partBytes], ['export round size', roundBytes]]) {
|
|
1057
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
1058
|
+
throw new RangeError(`a transfer backend's ${what} is a whole number of bytes greater than zero, not ${value}`);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
this.retention = {
|
|
1062
|
+
resultTtlMs: options.retention?.resultTtlMs ?? DEFAULT_RESULT_TTL_MS,
|
|
1063
|
+
recordMs: options.retention?.recordMs ?? DEFAULT_RECORD_RETENTION_MS,
|
|
1064
|
+
intervalMs: options.retention?.intervalMs ?? DEFAULT_RETENTION_INTERVAL_MS,
|
|
1065
|
+
};
|
|
1066
|
+
for (const [what, value] of Object.entries(this.retention)) {
|
|
1067
|
+
if (!Number.isSafeInteger(value) || value < 1) {
|
|
1068
|
+
throw new RangeError(`a transfer backend's retention ${what} is a whole number of milliseconds greater than zero, not ${value}`);
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
const { storage, getRunner } = options;
|
|
1072
|
+
const host = {
|
|
1073
|
+
records: storage.adapters.records,
|
|
1074
|
+
blobs: storage.adapters.blobs,
|
|
1075
|
+
locks: storage.adapters.locks,
|
|
1076
|
+
storage,
|
|
1077
|
+
getRunner,
|
|
1078
|
+
retention: this.retention,
|
|
1079
|
+
};
|
|
1080
|
+
const running = new Running();
|
|
1081
|
+
this.datasetUpload = new WebDatasetUploadStore(host, BigInt(partBytes));
|
|
1082
|
+
this.datasetDownload = new WebDatasetDownloadStore(host);
|
|
1083
|
+
this.packageImport = new WebPackageImportStore(host, running);
|
|
1084
|
+
this.packageExport = new WebPackageExportStore(host, running, roundBytes);
|
|
1085
|
+
this.workspaceDeploy = new WebWorkspaceDeployStore(host, running);
|
|
1086
|
+
this.repoGc = new WebRepoGcStore(host, running);
|
|
1087
|
+
this.splitCall = new WebSplitCallStore(host, running);
|
|
1088
|
+
}
|
|
1089
|
+
/**
|
|
1090
|
+
* Starts the backend: records what closed tabs left — every job, and every
|
|
1091
|
+
* upload's commit, whose session has ended — failed, naming why, and
|
|
1092
|
+
* removes what it staged, so nothing a closed tab began runs again over a
|
|
1093
|
+
* repository that may have changed since; and forgets what finished longer
|
|
1094
|
+
* ago than the result TTL, or that nothing finished or fetched within the
|
|
1095
|
+
* record retention, with what it staged. It goes on doing so, every
|
|
1096
|
+
* retention interval, while the tab lives, until {@link close}.
|
|
1097
|
+
*
|
|
1098
|
+
* @remarks
|
|
1099
|
+
* A job or a commit a tab that lives runs is left to it.
|
|
1100
|
+
*/
|
|
1101
|
+
async start() {
|
|
1102
|
+
await this.sweep();
|
|
1103
|
+
if (this.timer !== null || this.closed)
|
|
1104
|
+
return;
|
|
1105
|
+
this.timer = setInterval(() => {
|
|
1106
|
+
void this.sweep().catch(() => undefined);
|
|
1107
|
+
}, this.retention.intervalMs);
|
|
1108
|
+
this.timer.unref?.();
|
|
1109
|
+
}
|
|
1110
|
+
/** One pass over every store: one at a time, a pass asked for while one
|
|
1111
|
+
* runs answered by it. */
|
|
1112
|
+
sweep() {
|
|
1113
|
+
this.sweeping ??= (async () => {
|
|
1114
|
+
try {
|
|
1115
|
+
const now = Date.now();
|
|
1116
|
+
await this.datasetUpload.sweep(now);
|
|
1117
|
+
await this.datasetDownload.sweep(now);
|
|
1118
|
+
await this.packageImport.sweep(now);
|
|
1119
|
+
await this.packageExport.sweep(now);
|
|
1120
|
+
await this.workspaceDeploy.sweep(now);
|
|
1121
|
+
await this.repoGc.sweep(now);
|
|
1122
|
+
await this.splitCall.sweep(now);
|
|
1123
|
+
}
|
|
1124
|
+
finally {
|
|
1125
|
+
this.sweeping = null;
|
|
1126
|
+
}
|
|
1127
|
+
})();
|
|
1128
|
+
return this.sweeping;
|
|
1129
|
+
}
|
|
1130
|
+
/** Stops the backend's own work: the retention it goes on doing while the
|
|
1131
|
+
* tab lives. What its jobs do stops with the tab. */
|
|
1132
|
+
async close() {
|
|
1133
|
+
this.closed = true;
|
|
1134
|
+
if (this.timer !== null)
|
|
1135
|
+
clearInterval(this.timer);
|
|
1136
|
+
this.timer = null;
|
|
1137
|
+
await this.sweeping?.catch(() => undefined);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
//# sourceMappingURL=WebTransferBackend.js.map
|