@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,328 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Licensed under BSL 1.1. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* The page's side of `e3.fetch`: {@link createWebE3}, which connects a page to
|
|
7
|
+
* its e3 worker, and gives the page the `fetch` the worker answers.
|
|
8
|
+
*
|
|
9
|
+
* @packageDocumentation
|
|
10
|
+
*/
|
|
11
|
+
import { WEB_E3_ORIGIN, errorText, holdLifeline, protocolMessage, watchLifeline, } from './protocol.js';
|
|
12
|
+
/** Each request's number: unique among the requests of every connection in
|
|
13
|
+
* this realm, so two connections over one worker never take each other's
|
|
14
|
+
* answers. */
|
|
15
|
+
let nextRequestId = 0;
|
|
16
|
+
/** What an `error` event says of the worker's failure. */
|
|
17
|
+
function failureOf(event) {
|
|
18
|
+
const { message, filename, lineno } = event;
|
|
19
|
+
if (typeof message !== 'string' || message === '')
|
|
20
|
+
return 'its script did not load, or raised an error it did not describe';
|
|
21
|
+
return typeof filename === 'string' && filename !== '' ? `${message} (${filename}:${lineno ?? 0})` : message;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The page's connection to its e3 worker: the requests it has posted, and why
|
|
25
|
+
* it answers no more once it does not.
|
|
26
|
+
*/
|
|
27
|
+
class Connection {
|
|
28
|
+
worker;
|
|
29
|
+
port;
|
|
30
|
+
/** The requests posted and not yet settled, by number */
|
|
31
|
+
pending = new Map();
|
|
32
|
+
/** Why the worker answers no more requests, once it does not */
|
|
33
|
+
ended = null;
|
|
34
|
+
/** Whether the worker has said it is ready */
|
|
35
|
+
readied = false;
|
|
36
|
+
/** Aborted as the connection ends: withdraws the wait on the worker's
|
|
37
|
+
* lifeline, and frees the page's */
|
|
38
|
+
ending = new AbortController();
|
|
39
|
+
/** Settles once the worker is ready, or has failed to start */
|
|
40
|
+
ready;
|
|
41
|
+
settleReady;
|
|
42
|
+
/**
|
|
43
|
+
* @param worker - The worker, or a port to it
|
|
44
|
+
* @param lifeline - Whether the page holds a lifeline, over a port
|
|
45
|
+
*/
|
|
46
|
+
constructor(worker, lifeline) {
|
|
47
|
+
this.worker = worker;
|
|
48
|
+
this.ready = new Promise((resolve, reject) => {
|
|
49
|
+
this.settleReady = { resolve, reject };
|
|
50
|
+
});
|
|
51
|
+
this.port = worker;
|
|
52
|
+
this.port.addEventListener('message', (event) => this.receive(event.data));
|
|
53
|
+
this.port.addEventListener('messageerror', () => this.end('the e3 worker failed: a message from it could not be read'));
|
|
54
|
+
this.port.addEventListener('error', (event) => this.end(`the e3 worker failed: ${failureOf(event)}`));
|
|
55
|
+
// A port's close event, where the platform fires one, is the worker's end.
|
|
56
|
+
this.port.addEventListener('close', () => this.end('the e3 worker has stopped: its port has closed'));
|
|
57
|
+
if (!('terminate' in worker))
|
|
58
|
+
worker.start();
|
|
59
|
+
void this.hello(lifeline && !('terminate' in worker));
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Says hello to the worker, which answers whether it is ready, however long
|
|
63
|
+
* ago it booted. Over a port, the page first takes the lifeline the e3 at
|
|
64
|
+
* the other end waits on, held until the connection ends.
|
|
65
|
+
*/
|
|
66
|
+
async hello(withLifeline) {
|
|
67
|
+
const ended = new Promise((resolve) => {
|
|
68
|
+
this.ending.signal.addEventListener('abort', () => resolve(), { once: true });
|
|
69
|
+
});
|
|
70
|
+
const lifeline = withLifeline ? await holdLifeline('e3-web:page', ended) : null;
|
|
71
|
+
if (this.ended !== null)
|
|
72
|
+
return;
|
|
73
|
+
try {
|
|
74
|
+
this.post({ kind: 'hello', lifeline }, []);
|
|
75
|
+
}
|
|
76
|
+
catch (err) {
|
|
77
|
+
this.end(`the e3 worker could not be reached: ${errorText(err)}`);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Takes a message from the worker. */
|
|
81
|
+
receive(data) {
|
|
82
|
+
const message = protocolMessage(data);
|
|
83
|
+
if (message === null || this.ended !== null)
|
|
84
|
+
return;
|
|
85
|
+
switch (message.kind) {
|
|
86
|
+
case 'ready':
|
|
87
|
+
// Another connection's answer, over the same worker, once this one has
|
|
88
|
+
// its own.
|
|
89
|
+
if (this.readied)
|
|
90
|
+
return;
|
|
91
|
+
this.readied = true;
|
|
92
|
+
this.watch(message.lifeline);
|
|
93
|
+
this.settleReady.resolve(message);
|
|
94
|
+
return;
|
|
95
|
+
case 'boot-error':
|
|
96
|
+
this.end(`the e3 worker did not start: ${message.message}`);
|
|
97
|
+
return;
|
|
98
|
+
case 'response':
|
|
99
|
+
this.respond(message);
|
|
100
|
+
return;
|
|
101
|
+
case 'error': {
|
|
102
|
+
const entry = this.take(message.id);
|
|
103
|
+
entry?.fail(new TypeError(`e3.fetch: the e3 worker could not answer ${entry.method} ${entry.url}: ${message.message}`));
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/** Settles a request with the worker's response. */
|
|
109
|
+
respond(message) {
|
|
110
|
+
// A request given up meanwhile is settled already.
|
|
111
|
+
const entry = this.take(message.id);
|
|
112
|
+
if (entry === undefined)
|
|
113
|
+
return;
|
|
114
|
+
let response;
|
|
115
|
+
try {
|
|
116
|
+
response = new Response(message.body, { status: message.status, statusText: message.statusText, headers: message.headers });
|
|
117
|
+
}
|
|
118
|
+
catch (err) {
|
|
119
|
+
entry.fail(new TypeError(`e3.fetch: the e3 worker's answer to ${entry.method} ${entry.url} is no response: ${errorText(err)}`));
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
entry.resolve(response);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Waits on the Web Lock the worker holds while it lives: the browser frees
|
|
126
|
+
* it once the worker has stopped, however it stopped — terminated by the
|
|
127
|
+
* page's own code, or closed by itself — which no event tells the page.
|
|
128
|
+
*/
|
|
129
|
+
watch(lifeline) {
|
|
130
|
+
watchLifeline(lifeline, this.ending.signal, () => {
|
|
131
|
+
this.end('the e3 worker has stopped: it was terminated, or it closed itself');
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
/** Takes a pending request away, to settle it. */
|
|
135
|
+
take(id) {
|
|
136
|
+
const entry = this.pending.get(id);
|
|
137
|
+
this.pending.delete(id);
|
|
138
|
+
return entry;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Ends the connection: stops the worker, or closes the port to it, fails the
|
|
142
|
+
* boot if it has not finished, and fails every request pending; later
|
|
143
|
+
* requests are refused. The page's lifeline is freed.
|
|
144
|
+
*
|
|
145
|
+
* @param reason - What happened, as every failure names it
|
|
146
|
+
*/
|
|
147
|
+
end(reason) {
|
|
148
|
+
if (this.ended !== null)
|
|
149
|
+
return;
|
|
150
|
+
this.ended = reason;
|
|
151
|
+
this.ending.abort();
|
|
152
|
+
if ('terminate' in this.worker)
|
|
153
|
+
this.worker.terminate();
|
|
154
|
+
else
|
|
155
|
+
this.worker.close();
|
|
156
|
+
this.settleReady.reject(new Error(`createWebE3: ${reason}`));
|
|
157
|
+
for (const entry of this.pending.values())
|
|
158
|
+
entry.fail(new TypeError(`e3.fetch: ${reason}`));
|
|
159
|
+
this.pending.clear();
|
|
160
|
+
}
|
|
161
|
+
/** Refuses a request that cannot be sent: its signal has aborted, or the
|
|
162
|
+
* worker answers no more. */
|
|
163
|
+
check(signal) {
|
|
164
|
+
if (signal.aborted)
|
|
165
|
+
throw signal.reason;
|
|
166
|
+
if (this.ended !== null)
|
|
167
|
+
throw new TypeError(`e3.fetch: ${this.ended}`);
|
|
168
|
+
}
|
|
169
|
+
/** Answers a request in the worker: {@link WebE3.fetch}. */
|
|
170
|
+
async fetch(input, init) {
|
|
171
|
+
// As `fetch` does: method, headers, body and signal normalized, and
|
|
172
|
+
// refused as `fetch` refuses them.
|
|
173
|
+
const request = new Request(input, init);
|
|
174
|
+
if (new URL(request.url).origin !== WEB_E3_ORIGIN) {
|
|
175
|
+
throw new TypeError(`e3.fetch: e3.fetch answers ${WEB_E3_ORIGIN} alone (e3.apiUrl), and ${request.url} is not on it`);
|
|
176
|
+
}
|
|
177
|
+
this.check(request.signal);
|
|
178
|
+
let body = null;
|
|
179
|
+
if (request.body !== null) {
|
|
180
|
+
// The request's own copy of the caller's body, which is the caller's
|
|
181
|
+
// still; this copy is transferred.
|
|
182
|
+
try {
|
|
183
|
+
body = await request.arrayBuffer();
|
|
184
|
+
}
|
|
185
|
+
catch (err) {
|
|
186
|
+
throw new TypeError(`e3.fetch: the body of ${request.method} ${request.url} could not be read: ${errorText(err)}`);
|
|
187
|
+
}
|
|
188
|
+
this.check(request.signal);
|
|
189
|
+
}
|
|
190
|
+
return this.send(request, body);
|
|
191
|
+
}
|
|
192
|
+
/** Posts a request to the worker, and settles once it is answered, given
|
|
193
|
+
* up or failed. */
|
|
194
|
+
send(request, body) {
|
|
195
|
+
const id = nextRequestId++;
|
|
196
|
+
const { method, url, signal } = request;
|
|
197
|
+
return new Promise((resolve, reject) => {
|
|
198
|
+
const onAbort = () => {
|
|
199
|
+
if (this.take(id) === undefined)
|
|
200
|
+
return;
|
|
201
|
+
this.post({ kind: 'abort', id }, []);
|
|
202
|
+
reject(signal.reason);
|
|
203
|
+
};
|
|
204
|
+
this.pending.set(id, {
|
|
205
|
+
method,
|
|
206
|
+
url,
|
|
207
|
+
resolve: (response) => {
|
|
208
|
+
signal.removeEventListener('abort', onAbort);
|
|
209
|
+
resolve(response);
|
|
210
|
+
},
|
|
211
|
+
fail: (err) => {
|
|
212
|
+
signal.removeEventListener('abort', onAbort);
|
|
213
|
+
reject(err);
|
|
214
|
+
},
|
|
215
|
+
});
|
|
216
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
217
|
+
try {
|
|
218
|
+
this.post({ kind: 'request', id, method, url, headers: [...request.headers], body }, body === null ? [] : [body]);
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
this.take(id)?.fail(new TypeError(`e3.fetch: ${method} ${url} could not be posted to the e3 worker: ${errorText(err)}`));
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
post(message, transfer) {
|
|
226
|
+
this.port.postMessage(message, transfer);
|
|
227
|
+
}
|
|
228
|
+
/** Closes the worker: {@link WebE3.close}. */
|
|
229
|
+
close() {
|
|
230
|
+
// Over a port, the e3 at the other end is told: a browser's port says
|
|
231
|
+
// nothing as it closes.
|
|
232
|
+
if (this.ended === null && !('terminate' in this.worker)) {
|
|
233
|
+
try {
|
|
234
|
+
this.post({ kind: 'close' }, []);
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
// The port is closed already.
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
this.end('e3.close() has closed the e3 worker');
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Asks the browser to keep the origin's storage through storage pressure, or
|
|
245
|
+
* only whether it does, and answers whether it does.
|
|
246
|
+
*/
|
|
247
|
+
async function keepsRepositories(ask) {
|
|
248
|
+
const storage = globalThis.navigator?.storage;
|
|
249
|
+
try {
|
|
250
|
+
// `persist` is a page's alone; a worker has `persisted` only.
|
|
251
|
+
if (ask && typeof storage?.persist === 'function')
|
|
252
|
+
return await storage.persist();
|
|
253
|
+
if (typeof storage?.persisted === 'function')
|
|
254
|
+
return await storage.persisted();
|
|
255
|
+
}
|
|
256
|
+
catch {
|
|
257
|
+
// A browser that refuses the question keeps nothing through pressure.
|
|
258
|
+
}
|
|
259
|
+
return false;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Connects a page to its e3 worker, once the worker has booted.
|
|
263
|
+
*
|
|
264
|
+
* The worker runs e3 itself — its storage, its runner and its API — and this
|
|
265
|
+
* gives the page {@link WebE3.fetch}, which the worker answers, and
|
|
266
|
+
* {@link WebE3.apiUrl}, the URL e3's client and e3-ui-components are given
|
|
267
|
+
* with it. No request reaches the network.
|
|
268
|
+
*
|
|
269
|
+
* @param worker - The e3 worker: a dedicated worker running e3, or a port to
|
|
270
|
+
* one. It is the connection's from now on, and {@link WebE3.close} closes it
|
|
271
|
+
* @param options - Whether to ask the browser to keep the repositories
|
|
272
|
+
* @returns The e3, once the worker says it is ready
|
|
273
|
+
*
|
|
274
|
+
* @throws {Error} When the worker does not start: its boot failed — a
|
|
275
|
+
* browser API it needs is missing, say, which the message names — or it
|
|
276
|
+
* failed or stopped before it was ready. The worker is closed.
|
|
277
|
+
*
|
|
278
|
+
* @remarks
|
|
279
|
+
* - The page says hello, and the worker answers whether it is ready, and
|
|
280
|
+
* whether it keeps repositories past the page, however long ago it booted:
|
|
281
|
+
* a page may connect at any time. When it keeps them, the page asks the
|
|
282
|
+
* browser to keep them ({@link WebE3.persisted}).
|
|
283
|
+
* - The page learns that the worker has stopped — terminated by other code,
|
|
284
|
+
* or closed by itself, which no event tells it — through a Web Lock the
|
|
285
|
+
* worker holds while it lives; an `error` or `messageerror` event of the
|
|
286
|
+
* worker, or its port's closing, ends the connection too. Each fails every
|
|
287
|
+
* request pending, naming what happened, and refuses every later one.
|
|
288
|
+
* - Over a port, the page holds a Web Lock of its own while it is connected,
|
|
289
|
+
* which the e3 at the other end waits on: the e3 closes once the page has
|
|
290
|
+
* gone — its tab closed — as it does when {@link WebE3.close} tells it.
|
|
291
|
+
* - A body crosses between the page and the worker whole, as an
|
|
292
|
+
* `ArrayBuffer` transferred rather than copied.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```ts
|
|
296
|
+
* import { createWebE3 } from '@elaraai/e3-web';
|
|
297
|
+
* import { repoList } from '@elaraai/e3-api-client';
|
|
298
|
+
*
|
|
299
|
+
* const e3 = await createWebE3(new Worker(new URL('./e3.worker.ts', import.meta.url), { type: 'module' }));
|
|
300
|
+
* const repos = await repoList(e3.apiUrl, { token: null, fetch: e3.fetch });
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
export function createWebE3(worker, options = {}) {
|
|
304
|
+
return connectWebE3(worker, options);
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Connects to an e3 worker: {@link createWebE3}, with whether the page holds
|
|
308
|
+
* a lifeline over a port.
|
|
309
|
+
*
|
|
310
|
+
* @param worker - The e3 worker, or a port to one
|
|
311
|
+
* @param options - Whether to ask the browser to keep the repositories, and
|
|
312
|
+
* whether to hold a lifeline
|
|
313
|
+
* @returns The e3, once the worker says it is ready
|
|
314
|
+
* @throws {Error} As {@link createWebE3} throws
|
|
315
|
+
* @internal
|
|
316
|
+
*/
|
|
317
|
+
export async function connectWebE3(worker, options = {}) {
|
|
318
|
+
const connection = new Connection(worker, options.lifeline ?? true);
|
|
319
|
+
const ready = await connection.ready;
|
|
320
|
+
const persisted = ready.persist && await keepsRepositories(options.requestPersistence ?? true);
|
|
321
|
+
return {
|
|
322
|
+
apiUrl: WEB_E3_ORIGIN,
|
|
323
|
+
fetch: (input, init) => connection.fetch(input, init),
|
|
324
|
+
persisted,
|
|
325
|
+
close: () => connection.close(),
|
|
326
|
+
};
|
|
327
|
+
}
|
|
328
|
+
//# sourceMappingURL=page.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"page.js","sourceRoot":"","sources":["../../../src/bridge/page.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AAEH,OAAO,EACL,aAAa,EACb,SAAS,EACT,YAAY,EACZ,eAAe,EACf,aAAa,GAKd,MAAM,eAAe,CAAC;AAmHvB;;eAEe;AACf,IAAI,aAAa,GAAG,CAAC,CAAC;AAEtB,0DAA0D;AAC1D,SAAS,SAAS,CAAC,KAAY;IAC7B,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,KAA4B,CAAC;IACnE,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,KAAK,EAAE;QAAE,OAAO,iEAAiE,CAAC;IAC5H,OAAO,OAAO,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,EAAE,CAAC,CAAC,CAAC,GAAG,OAAO,KAAK,QAAQ,IAAI,MAAM,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC;AAC/G,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU;IAmBe;IAlBZ,IAAI,CAAW;IAChC,yDAAyD;IACxC,OAAO,GAAG,IAAI,GAAG,EAAmB,CAAC;IACtD,gEAAgE;IACxD,K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4B,EAAE,UAA0B,EAAE;IAC3F,MAAM,UAAU,GAAG,IAAI,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,QAAQ,IAAI,IAAI,CAAC,CAAC;IACpE,MAAM,KAAK,GAAG,MAAM,UAAU,CAAC,KAAK,CAAC;IACrC,MAAM,SAAS,GAAG,KAAK,CAAC,OAAO,IAAI,MAAM,iBAAiB,CAAC,OAAO,CAAC,kBAAkB,IAAI,IAAI,CAAC,CAAC;IAC/F,OAAO;QACL,MAAM,EAAE,aAAa;QACrB,KAAK,EAAE,CAAC,KAAwB,EAAE,IAAkB,EAAqB,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC;QACzG,SAAS;QACT,KAAK,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE;KAChC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Licensed under BSL 1.1. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `e3.fetch`'s protocol: the messages a page and its e3 worker exchange.
|
|
7
|
+
*
|
|
8
|
+
* The page posts each request to the worker, and the worker posts back its
|
|
9
|
+
* answer: a response, or the failure that stands for a network error. Bodies
|
|
10
|
+
* travel as `ArrayBuffer`s, transferred rather than copied, so a large body
|
|
11
|
+
* crosses between the threads without a copy. The messages are plain objects
|
|
12
|
+
* that `postMessage` clones, and they travel over a dedicated worker's
|
|
13
|
+
* `postMessage`, or a `MessagePort`'s: one protocol over either transport.
|
|
14
|
+
*
|
|
15
|
+
* - **The page posts** a `hello` as it connects, a `request` for each
|
|
16
|
+
* request, an `abort` when a request's signal aborts, and, over a port, a
|
|
17
|
+
* `close` when the page closes its connection.
|
|
18
|
+
* - **The worker posts**, to each `hello`, `ready` once it has booted, or
|
|
19
|
+
* `boot-error` when its boot failed: a page that connects at any time,
|
|
20
|
+
* however long after the worker booted, is answered. Then a `response`, or
|
|
21
|
+
* an `error`, for each request.
|
|
22
|
+
*
|
|
23
|
+
* Each side may hold a lifeline: a Web Lock it holds while it lives, which the
|
|
24
|
+
* other waits on to learn it has gone, as no event says. The worker names its
|
|
25
|
+
* own in `ready`, and the page, over a port, names its own in `hello`: the e3
|
|
26
|
+
* at the other end of a port outlives no page that has gone.
|
|
27
|
+
*
|
|
28
|
+
* @packageDocumentation
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* The one origin `e3.fetch` answers: `https://e3-web.invalid`.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* `.invalid` is a top-level domain reserved never to resolve (RFC 2606), so a
|
|
35
|
+
* request to it that bypasses `e3.fetch` — the global `fetch`, given e3's URL
|
|
36
|
+
* by mistake — fails at once, and none reaches the network. A URL e3 hands a
|
|
37
|
+
* client to fetch later, such as an upload's or a download's, is on this
|
|
38
|
+
* origin too.
|
|
39
|
+
*/
|
|
40
|
+
export declare const WEB_E3_ORIGIN = "https://e3-web.invalid";
|
|
41
|
+
/**
|
|
42
|
+
* A request's or a response's headers, a pair each, in the order the
|
|
43
|
+
* `Headers` they came from iterates them: lowercase names, a header given
|
|
44
|
+
* twice combined as `fetch` combines it, and each `Set-Cookie` apart.
|
|
45
|
+
*/
|
|
46
|
+
export type HeaderPairs = Array<[string, string]>;
|
|
47
|
+
/**
|
|
48
|
+
* The page asks the worker to answer a request.
|
|
49
|
+
*/
|
|
50
|
+
export interface RequestMessage {
|
|
51
|
+
readonly kind: 'request';
|
|
52
|
+
/** The request's number, unique among the page's requests to the worker */
|
|
53
|
+
readonly id: number;
|
|
54
|
+
/** Its method, normalized as a `Request` normalizes it */
|
|
55
|
+
readonly method: string;
|
|
56
|
+
/** Its URL, absolute, on {@link WEB_E3_ORIGIN} */
|
|
57
|
+
readonly url: string;
|
|
58
|
+
/** Its headers */
|
|
59
|
+
readonly headers: HeaderPairs;
|
|
60
|
+
/** Its body, whole and transferred, or `null` for a request with none */
|
|
61
|
+
readonly body: ArrayBuffer | null;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* The page has given up a request: its signal aborted. The worker aborts the
|
|
65
|
+
* signal of the `Request` its handler was given.
|
|
66
|
+
*/
|
|
67
|
+
export interface AbortMessage {
|
|
68
|
+
readonly kind: 'abort';
|
|
69
|
+
/** The request's number */
|
|
70
|
+
readonly id: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* The page connects: the worker answers with `ready`, or with `boot-error`,
|
|
74
|
+
* once its boot has settled, to every hello, whenever it comes.
|
|
75
|
+
*/
|
|
76
|
+
export interface HelloMessage {
|
|
77
|
+
readonly kind: 'hello';
|
|
78
|
+
/** The Web Lock the page holds for as long as it is connected, which the
|
|
79
|
+
* worker at the other end of a port waits on to learn the page has gone;
|
|
80
|
+
* `null` when it holds none */
|
|
81
|
+
readonly lifeline: string | null;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The page has closed its connection, over a port: what the worker served it
|
|
85
|
+
* is wanted no more.
|
|
86
|
+
*/
|
|
87
|
+
export interface CloseMessage {
|
|
88
|
+
readonly kind: 'close';
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The worker has booted, and answers requests: its answer to a hello.
|
|
92
|
+
*/
|
|
93
|
+
export interface ReadyMessage {
|
|
94
|
+
readonly kind: 'ready';
|
|
95
|
+
/** Whether the worker keeps repositories past the page's life, which the
|
|
96
|
+
* page asks the browser to keep through storage pressure */
|
|
97
|
+
readonly persist: boolean;
|
|
98
|
+
/** The Web Lock the worker holds for as long as it lives, which the page
|
|
99
|
+
* waits on to learn that it has stopped; `null` when it holds none */
|
|
100
|
+
readonly lifeline: string | null;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The worker's boot failed, and it answers no request: its answer to a
|
|
104
|
+
* hello.
|
|
105
|
+
*/
|
|
106
|
+
export interface BootErrorMessage {
|
|
107
|
+
readonly kind: 'boot-error';
|
|
108
|
+
/** What failed: a browser API the boot lacks, named, say */
|
|
109
|
+
readonly message: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The worker's response to a request.
|
|
113
|
+
*/
|
|
114
|
+
export interface ResponseMessage {
|
|
115
|
+
readonly kind: 'response';
|
|
116
|
+
/** The request's number */
|
|
117
|
+
readonly id: number;
|
|
118
|
+
readonly status: number;
|
|
119
|
+
readonly statusText: string;
|
|
120
|
+
readonly headers: HeaderPairs;
|
|
121
|
+
/** Its body, whole and transferred, or `null` for a response with none:
|
|
122
|
+
* a null-body status's, or a `HEAD` request's */
|
|
123
|
+
readonly body: ArrayBuffer | null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* A request the worker answered with no response: its handler threw, or its
|
|
127
|
+
* response's body failed. The page's `fetch` rejects, as `fetch` rejects a
|
|
128
|
+
* network failure; an app answers its own errors with responses.
|
|
129
|
+
*/
|
|
130
|
+
export interface ErrorMessage {
|
|
131
|
+
readonly kind: 'error';
|
|
132
|
+
/** The request's number */
|
|
133
|
+
readonly id: number;
|
|
134
|
+
/** What failed */
|
|
135
|
+
readonly message: string;
|
|
136
|
+
}
|
|
137
|
+
/** A message the page posts to the worker. */
|
|
138
|
+
export type PageMessage = HelloMessage | RequestMessage | AbortMessage | CloseMessage;
|
|
139
|
+
/** A message the worker posts to the page. */
|
|
140
|
+
export type WorkerMessage = ReadyMessage | BootErrorMessage | ResponseMessage | ErrorMessage;
|
|
141
|
+
/**
|
|
142
|
+
* What a message's data is, when it is one of the protocol's: an object with
|
|
143
|
+
* a `kind`. A side ignores a message of a kind it does not take.
|
|
144
|
+
*
|
|
145
|
+
* @param data - The data of a message event
|
|
146
|
+
* @returns The message, or `null` when the data is no protocol message
|
|
147
|
+
*/
|
|
148
|
+
export declare function protocolMessage<M extends PageMessage | WorkerMessage>(data: unknown): M | null;
|
|
149
|
+
/**
|
|
150
|
+
* Takes a lifeline: a Web Lock of a fresh name, which this agent holds until
|
|
151
|
+
* `released` settles, or for as long as it lives. Another agent waiting on it
|
|
152
|
+
* ({@link watchLifeline}) learns that this one has gone, however it went —
|
|
153
|
+
* closed, terminated, or its tab closed — as the browser frees the locks of an
|
|
154
|
+
* agent that has gone, and no event says so.
|
|
155
|
+
*
|
|
156
|
+
* @param prefix - What the name begins with, before a fresh id
|
|
157
|
+
* @param released - Settles once the lifeline is let go: never, for one held
|
|
158
|
+
* while the agent lives
|
|
159
|
+
* @returns The lock's name, once held, or `null` when the agent has no Web
|
|
160
|
+
* Locks, or may not take one
|
|
161
|
+
*/
|
|
162
|
+
export declare function holdLifeline(prefix: string, released: Promise<void>): Promise<string | null>;
|
|
163
|
+
/**
|
|
164
|
+
* Waits on another agent's lifeline ({@link holdLifeline}), and says when it
|
|
165
|
+
* has gone.
|
|
166
|
+
*
|
|
167
|
+
* @param name - The lifeline's name, or `null` for none: nothing is waited on
|
|
168
|
+
* @param signal - Withdraws the wait, once the waiter no longer cares
|
|
169
|
+
* @param gone - Told once the lifeline is freed, unless the wait was
|
|
170
|
+
* withdrawn first
|
|
171
|
+
*/
|
|
172
|
+
export declare function watchLifeline(name: string | null, signal: AbortSignal, gone: () => void): void;
|
|
173
|
+
/**
|
|
174
|
+
* What an error says, as a message carries it: its message, after its name
|
|
175
|
+
* when that says more than `Error`.
|
|
176
|
+
*
|
|
177
|
+
* @param err - What was thrown
|
|
178
|
+
* @returns The text
|
|
179
|
+
*/
|
|
180
|
+
export declare function errorText(err: unknown): string;
|
|
181
|
+
//# sourceMappingURL=protocol.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.d.ts","sourceRoot":"","sources":["../../../src/bridge/protocol.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH;;;;;;;;;GASG;AACH,eAAO,MAAM,aAAa,2BAA2B,CAAC;AAEtD;;;;GAIG;AACH,MAAM,MAAM,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AM,CAG9C"}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Licensed under BSL 1.1. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* `e3.fetch`'s protocol: the messages a page and its e3 worker exchange.
|
|
7
|
+
*
|
|
8
|
+
* The page posts each request to the worker, and the worker posts back its
|
|
9
|
+
* answer: a response, or the failure that stands for a network error. Bodies
|
|
10
|
+
* travel as `ArrayBuffer`s, transferred rather than copied, so a large body
|
|
11
|
+
* crosses between the threads without a copy. The messages are plain objects
|
|
12
|
+
* that `postMessage` clones, and they travel over a dedicated worker's
|
|
13
|
+
* `postMessage`, or a `MessagePort`'s: one protocol over either transport.
|
|
14
|
+
*
|
|
15
|
+
* - **The page posts** a `hello` as it connects, a `request` for each
|
|
16
|
+
* request, an `abort` when a request's signal aborts, and, over a port, a
|
|
17
|
+
* `close` when the page closes its connection.
|
|
18
|
+
* - **The worker posts**, to each `hello`, `ready` once it has booted, or
|
|
19
|
+
* `boot-error` when its boot failed: a page that connects at any time,
|
|
20
|
+
* however long after the worker booted, is answered. Then a `response`, or
|
|
21
|
+
* an `error`, for each request.
|
|
22
|
+
*
|
|
23
|
+
* Each side may hold a lifeline: a Web Lock it holds while it lives, which the
|
|
24
|
+
* other waits on to learn it has gone, as no event says. The worker names its
|
|
25
|
+
* own in `ready`, and the page, over a port, names its own in `hello`: the e3
|
|
26
|
+
* at the other end of a port outlives no page that has gone.
|
|
27
|
+
*
|
|
28
|
+
* @packageDocumentation
|
|
29
|
+
*/
|
|
30
|
+
/**
|
|
31
|
+
* The one origin `e3.fetch` answers: `https://e3-web.invalid`.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* `.invalid` is a top-level domain reserved never to resolve (RFC 2606), so a
|
|
35
|
+
* request to it that bypasses `e3.fetch` — the global `fetch`, given e3's URL
|
|
36
|
+
* by mistake — fails at once, and none reaches the network. A URL e3 hands a
|
|
37
|
+
* client to fetch later, such as an upload's or a download's, is on this
|
|
38
|
+
* origin too.
|
|
39
|
+
*/
|
|
40
|
+
export const WEB_E3_ORIGIN = 'https://e3-web.invalid';
|
|
41
|
+
/**
|
|
42
|
+
* What a message's data is, when it is one of the protocol's: an object with
|
|
43
|
+
* a `kind`. A side ignores a message of a kind it does not take.
|
|
44
|
+
*
|
|
45
|
+
* @param data - The data of a message event
|
|
46
|
+
* @returns The message, or `null` when the data is no protocol message
|
|
47
|
+
*/
|
|
48
|
+
export function protocolMessage(data) {
|
|
49
|
+
return typeof data === 'object' && data !== null && 'kind' in data ? data : null;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Takes a lifeline: a Web Lock of a fresh name, which this agent holds until
|
|
53
|
+
* `released` settles, or for as long as it lives. Another agent waiting on it
|
|
54
|
+
* ({@link watchLifeline}) learns that this one has gone, however it went —
|
|
55
|
+
* closed, terminated, or its tab closed — as the browser frees the locks of an
|
|
56
|
+
* agent that has gone, and no event says so.
|
|
57
|
+
*
|
|
58
|
+
* @param prefix - What the name begins with, before a fresh id
|
|
59
|
+
* @param released - Settles once the lifeline is let go: never, for one held
|
|
60
|
+
* while the agent lives
|
|
61
|
+
* @returns The lock's name, once held, or `null` when the agent has no Web
|
|
62
|
+
* Locks, or may not take one
|
|
63
|
+
*/
|
|
64
|
+
export async function holdLifeline(prefix, released) {
|
|
65
|
+
const locks = globalThis.navigator?.locks;
|
|
66
|
+
if (locks === undefined)
|
|
67
|
+
return null;
|
|
68
|
+
const name = `${prefix}:${crypto.randomUUID()}`;
|
|
69
|
+
try {
|
|
70
|
+
await new Promise((held, refused) => {
|
|
71
|
+
locks.request(name, () => {
|
|
72
|
+
held();
|
|
73
|
+
return released;
|
|
74
|
+
}).catch(refused);
|
|
75
|
+
});
|
|
76
|
+
return name;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// An agent that cannot hold a Web Lock names none: the other side cannot
|
|
80
|
+
// learn when it goes, and is told only what events say.
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Waits on another agent's lifeline ({@link holdLifeline}), and says when it
|
|
86
|
+
* has gone.
|
|
87
|
+
*
|
|
88
|
+
* @param name - The lifeline's name, or `null` for none: nothing is waited on
|
|
89
|
+
* @param signal - Withdraws the wait, once the waiter no longer cares
|
|
90
|
+
* @param gone - Told once the lifeline is freed, unless the wait was
|
|
91
|
+
* withdrawn first
|
|
92
|
+
*/
|
|
93
|
+
export function watchLifeline(name, signal, gone) {
|
|
94
|
+
const locks = globalThis.navigator?.locks;
|
|
95
|
+
if (name === null || locks === undefined || signal.aborted)
|
|
96
|
+
return;
|
|
97
|
+
locks.request(name, { signal }, () => {
|
|
98
|
+
gone();
|
|
99
|
+
}).catch(() => undefined); // the wait was withdrawn
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* What an error says, as a message carries it: its message, after its name
|
|
103
|
+
* when that says more than `Error`.
|
|
104
|
+
*
|
|
105
|
+
* @param err - What was thrown
|
|
106
|
+
* @returns The text
|
|
107
|
+
*/
|
|
108
|
+
export function errorText(err) {
|
|
109
|
+
if (!(err instanceof Error))
|
|
110
|
+
return `${err}`;
|
|
111
|
+
return err.name === 'Error' ? err.message : `${err.name}: ${err.message}`;
|
|
112
|
+
}
|
|
113
|
+
//# sourceMappingURL=protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"protocol.js","sourceRoot":"","sources":["../../../src/bridge/protocol.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,wBAAwB,CAAC;AAiHtD;;;;;;GAMG;AACH,MAAM,UAAU,eAAe,CAAwC,IAAa;IAClF,OAAO,OAAO,IAAI,KAAK,QAAQ,IAAI,IAA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