@hashtree/worker 0.1.26 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -16
- package/package.json +23 -19
- package/src/app-runtime.ts +393 -0
- package/src/capabilities/blossomBandwidthTracker.ts +74 -0
- package/src/capabilities/blossomTransport.ts +179 -0
- package/src/capabilities/connectivity.ts +54 -0
- package/src/capabilities/idbStorage.ts +94 -0
- package/src/capabilities/meshRouterStore.ts +426 -0
- package/src/capabilities/rootResolver.ts +497 -0
- package/src/client-id.ts +137 -0
- package/src/client.ts +501 -0
- package/{dist/entry.js → src/entry.ts} +1 -1
- package/src/htree-path.ts +53 -0
- package/src/htree-url.ts +156 -0
- package/src/index.ts +76 -0
- package/src/mediaStreaming.ts +64 -0
- package/src/p2p/boundedQueue.ts +168 -0
- package/src/p2p/errorMessage.ts +6 -0
- package/src/p2p/index.ts +48 -0
- package/src/p2p/lruCache.ts +78 -0
- package/src/p2p/meshQueryRouter.ts +361 -0
- package/src/p2p/protocol.ts +11 -0
- package/src/p2p/queryForwardingMachine.ts +197 -0
- package/src/p2p/signaling.ts +284 -0
- package/src/p2p/uploadRateLimiter.ts +85 -0
- package/src/p2p/webrtcController.ts +1168 -0
- package/src/p2p/webrtcProxy.ts +519 -0
- package/src/privacyGuards.ts +31 -0
- package/src/protocol.ts +124 -0
- package/src/relay/identity.ts +86 -0
- package/src/relay/mediaHandler.ts +1633 -0
- package/src/relay/ndk.ts +590 -0
- package/{dist/iris/nostr-wasm.js → src/relay/nostr-wasm.ts} +4 -1
- package/src/relay/nostr.ts +249 -0
- package/src/relay/protocol.ts +361 -0
- package/src/relay/publicAssetUrl.ts +25 -0
- package/src/relay/rootPathResolver.ts +50 -0
- package/src/relay/shims.d.ts +17 -0
- package/src/relay/signing.ts +332 -0
- package/src/relay/treeRootCache.ts +354 -0
- package/src/relay/treeRootSubscription.ts +577 -0
- package/src/relay/utils/constants.ts +139 -0
- package/src/relay/utils/errorMessage.ts +7 -0
- package/src/relay/utils/lruCache.ts +79 -0
- package/src/relay/webrtc.ts +5 -0
- package/src/relay/webrtcSignaling.ts +108 -0
- package/src/relay/worker.ts +1787 -0
- package/src/relay-client.ts +265 -0
- package/src/relay-entry.ts +1 -0
- package/src/runtime-network.ts +134 -0
- package/src/runtime.ts +153 -0
- package/src/transferableBytes.ts +5 -0
- package/src/tree-root.ts +851 -0
- package/src/types.ts +8 -0
- package/src/worker.ts +975 -0
- package/LICENSE +0 -21
- package/dist/app-runtime.d.ts +0 -60
- package/dist/app-runtime.d.ts.map +0 -1
- package/dist/app-runtime.js +0 -271
- package/dist/app-runtime.js.map +0 -1
- package/dist/capabilities/blossomBandwidthTracker.d.ts +0 -26
- package/dist/capabilities/blossomBandwidthTracker.d.ts.map +0 -1
- package/dist/capabilities/blossomBandwidthTracker.js +0 -53
- package/dist/capabilities/blossomBandwidthTracker.js.map +0 -1
- package/dist/capabilities/blossomTransport.d.ts +0 -22
- package/dist/capabilities/blossomTransport.d.ts.map +0 -1
- package/dist/capabilities/blossomTransport.js +0 -144
- package/dist/capabilities/blossomTransport.js.map +0 -1
- package/dist/capabilities/connectivity.d.ts +0 -3
- package/dist/capabilities/connectivity.d.ts.map +0 -1
- package/dist/capabilities/connectivity.js +0 -49
- package/dist/capabilities/connectivity.js.map +0 -1
- package/dist/capabilities/idbStorage.d.ts +0 -25
- package/dist/capabilities/idbStorage.d.ts.map +0 -1
- package/dist/capabilities/idbStorage.js +0 -73
- package/dist/capabilities/idbStorage.js.map +0 -1
- package/dist/capabilities/meshRouterStore.d.ts +0 -71
- package/dist/capabilities/meshRouterStore.d.ts.map +0 -1
- package/dist/capabilities/meshRouterStore.js +0 -316
- package/dist/capabilities/meshRouterStore.js.map +0 -1
- package/dist/capabilities/rootResolver.d.ts +0 -10
- package/dist/capabilities/rootResolver.d.ts.map +0 -1
- package/dist/capabilities/rootResolver.js +0 -393
- package/dist/capabilities/rootResolver.js.map +0 -1
- package/dist/client-id.d.ts +0 -18
- package/dist/client-id.d.ts.map +0 -1
- package/dist/client-id.js +0 -98
- package/dist/client-id.js.map +0 -1
- package/dist/client.d.ts +0 -61
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -417
- package/dist/client.js.map +0 -1
- package/dist/entry.d.ts +0 -2
- package/dist/entry.d.ts.map +0 -1
- package/dist/entry.js.map +0 -1
- package/dist/htree-path.d.ts +0 -13
- package/dist/htree-path.d.ts.map +0 -1
- package/dist/htree-path.js +0 -38
- package/dist/htree-path.js.map +0 -1
- package/dist/htree-url.d.ts +0 -22
- package/dist/htree-url.d.ts.map +0 -1
- package/dist/htree-url.js +0 -118
- package/dist/htree-url.js.map +0 -1
- package/dist/index.d.ts +0 -17
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +0 -1
- package/dist/iris/identity.d.ts +0 -36
- package/dist/iris/identity.d.ts.map +0 -1
- package/dist/iris/identity.js +0 -78
- package/dist/iris/identity.js.map +0 -1
- package/dist/iris/mediaHandler.d.ts +0 -64
- package/dist/iris/mediaHandler.d.ts.map +0 -1
- package/dist/iris/mediaHandler.js +0 -1285
- package/dist/iris/mediaHandler.js.map +0 -1
- package/dist/iris/ndk.d.ts +0 -96
- package/dist/iris/ndk.d.ts.map +0 -1
- package/dist/iris/ndk.js +0 -502
- package/dist/iris/ndk.js.map +0 -1
- package/dist/iris/nostr-wasm.d.ts +0 -14
- package/dist/iris/nostr-wasm.d.ts.map +0 -1
- package/dist/iris/nostr-wasm.js.map +0 -1
- package/dist/iris/nostr.d.ts +0 -60
- package/dist/iris/nostr.d.ts.map +0 -1
- package/dist/iris/nostr.js +0 -207
- package/dist/iris/nostr.js.map +0 -1
- package/dist/iris/protocol.d.ts +0 -583
- package/dist/iris/protocol.d.ts.map +0 -1
- package/dist/iris/protocol.js +0 -16
- package/dist/iris/protocol.js.map +0 -1
- package/dist/iris/publicAssetUrl.d.ts +0 -6
- package/dist/iris/publicAssetUrl.d.ts.map +0 -1
- package/dist/iris/publicAssetUrl.js +0 -14
- package/dist/iris/publicAssetUrl.js.map +0 -1
- package/dist/iris/rootPathResolver.d.ts +0 -9
- package/dist/iris/rootPathResolver.d.ts.map +0 -1
- package/dist/iris/rootPathResolver.js +0 -32
- package/dist/iris/rootPathResolver.js.map +0 -1
- package/dist/iris/signing.d.ts +0 -50
- package/dist/iris/signing.d.ts.map +0 -1
- package/dist/iris/signing.js +0 -299
- package/dist/iris/signing.js.map +0 -1
- package/dist/iris/treeRootCache.d.ts +0 -86
- package/dist/iris/treeRootCache.d.ts.map +0 -1
- package/dist/iris/treeRootCache.js +0 -269
- package/dist/iris/treeRootCache.js.map +0 -1
- package/dist/iris/treeRootSubscription.d.ts +0 -55
- package/dist/iris/treeRootSubscription.d.ts.map +0 -1
- package/dist/iris/treeRootSubscription.js +0 -479
- package/dist/iris/treeRootSubscription.js.map +0 -1
- package/dist/iris/utils/constants.d.ts +0 -76
- package/dist/iris/utils/constants.d.ts.map +0 -1
- package/dist/iris/utils/constants.js +0 -113
- package/dist/iris/utils/constants.js.map +0 -1
- package/dist/iris/utils/errorMessage.d.ts +0 -5
- package/dist/iris/utils/errorMessage.d.ts.map +0 -1
- package/dist/iris/utils/errorMessage.js +0 -8
- package/dist/iris/utils/errorMessage.js.map +0 -1
- package/dist/iris/utils/lruCache.d.ts +0 -26
- package/dist/iris/utils/lruCache.d.ts.map +0 -1
- package/dist/iris/utils/lruCache.js +0 -66
- package/dist/iris/utils/lruCache.js.map +0 -1
- package/dist/iris/webrtc.d.ts +0 -2
- package/dist/iris/webrtc.d.ts.map +0 -1
- package/dist/iris/webrtc.js +0 -3
- package/dist/iris/webrtc.js.map +0 -1
- package/dist/iris/webrtcSignaling.d.ts +0 -37
- package/dist/iris/webrtcSignaling.d.ts.map +0 -1
- package/dist/iris/webrtcSignaling.js +0 -86
- package/dist/iris/webrtcSignaling.js.map +0 -1
- package/dist/iris/worker.d.ts +0 -12
- package/dist/iris/worker.d.ts.map +0 -1
- package/dist/iris/worker.js +0 -1529
- package/dist/iris/worker.js.map +0 -1
- package/dist/iris-client.d.ts +0 -31
- package/dist/iris-client.d.ts.map +0 -1
- package/dist/iris-client.js +0 -197
- package/dist/iris-client.js.map +0 -1
- package/dist/iris-entry.d.ts +0 -2
- package/dist/iris-entry.d.ts.map +0 -1
- package/dist/iris-entry.js +0 -2
- package/dist/iris-entry.js.map +0 -1
- package/dist/mediaStreaming.d.ts +0 -7
- package/dist/mediaStreaming.d.ts.map +0 -1
- package/dist/mediaStreaming.js +0 -48
- package/dist/mediaStreaming.js.map +0 -1
- package/dist/p2p/boundedQueue.d.ts +0 -79
- package/dist/p2p/boundedQueue.d.ts.map +0 -1
- package/dist/p2p/boundedQueue.js +0 -134
- package/dist/p2p/boundedQueue.js.map +0 -1
- package/dist/p2p/errorMessage.d.ts +0 -5
- package/dist/p2p/errorMessage.d.ts.map +0 -1
- package/dist/p2p/errorMessage.js +0 -7
- package/dist/p2p/errorMessage.js.map +0 -1
- package/dist/p2p/index.d.ts +0 -8
- package/dist/p2p/index.d.ts.map +0 -1
- package/dist/p2p/index.js +0 -6
- package/dist/p2p/index.js.map +0 -1
- package/dist/p2p/lruCache.d.ts +0 -26
- package/dist/p2p/lruCache.d.ts.map +0 -1
- package/dist/p2p/lruCache.js +0 -65
- package/dist/p2p/lruCache.js.map +0 -1
- package/dist/p2p/meshQueryRouter.d.ts +0 -44
- package/dist/p2p/meshQueryRouter.d.ts.map +0 -1
- package/dist/p2p/meshQueryRouter.js +0 -228
- package/dist/p2p/meshQueryRouter.js.map +0 -1
- package/dist/p2p/protocol.d.ts +0 -10
- package/dist/p2p/protocol.d.ts.map +0 -1
- package/dist/p2p/protocol.js +0 -2
- package/dist/p2p/protocol.js.map +0 -1
- package/dist/p2p/queryForwardingMachine.d.ts +0 -46
- package/dist/p2p/queryForwardingMachine.d.ts.map +0 -1
- package/dist/p2p/queryForwardingMachine.js +0 -144
- package/dist/p2p/queryForwardingMachine.js.map +0 -1
- package/dist/p2p/signaling.d.ts +0 -63
- package/dist/p2p/signaling.d.ts.map +0 -1
- package/dist/p2p/signaling.js +0 -185
- package/dist/p2p/signaling.js.map +0 -1
- package/dist/p2p/uploadRateLimiter.d.ts +0 -21
- package/dist/p2p/uploadRateLimiter.d.ts.map +0 -1
- package/dist/p2p/uploadRateLimiter.js +0 -62
- package/dist/p2p/uploadRateLimiter.js.map +0 -1
- package/dist/p2p/webrtcController.d.ts +0 -168
- package/dist/p2p/webrtcController.d.ts.map +0 -1
- package/dist/p2p/webrtcController.js +0 -902
- package/dist/p2p/webrtcController.js.map +0 -1
- package/dist/p2p/webrtcProxy.d.ts +0 -62
- package/dist/p2p/webrtcProxy.d.ts.map +0 -1
- package/dist/p2p/webrtcProxy.js +0 -447
- package/dist/p2p/webrtcProxy.js.map +0 -1
- package/dist/privacyGuards.d.ts +0 -14
- package/dist/privacyGuards.d.ts.map +0 -1
- package/dist/privacyGuards.js +0 -27
- package/dist/privacyGuards.js.map +0 -1
- package/dist/protocol.d.ts +0 -225
- package/dist/protocol.d.ts.map +0 -1
- package/dist/protocol.js +0 -2
- package/dist/protocol.js.map +0 -1
- package/dist/runtime-network.d.ts +0 -23
- package/dist/runtime-network.d.ts.map +0 -1
- package/dist/runtime-network.js +0 -105
- package/dist/runtime-network.js.map +0 -1
- package/dist/runtime.d.ts +0 -23
- package/dist/runtime.d.ts.map +0 -1
- package/dist/runtime.js +0 -122
- package/dist/runtime.js.map +0 -1
- package/dist/tree-root.d.ts +0 -201
- package/dist/tree-root.d.ts.map +0 -1
- package/dist/tree-root.js +0 -632
- package/dist/tree-root.js.map +0 -1
- package/dist/types.d.ts +0 -2
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/worker.d.ts +0 -9
- package/dist/worker.d.ts.map +0 -1
- package/dist/worker.js +0 -797
- package/dist/worker.js.map +0 -1
package/README.md
CHANGED
|
@@ -17,7 +17,7 @@ import { HashtreeWorkerClient } from '@hashtree/worker';
|
|
|
17
17
|
import HashtreeWorker from './workers/hashtree.worker.ts?worker';
|
|
18
18
|
|
|
19
19
|
const client = new HashtreeWorkerClient(HashtreeWorker, {
|
|
20
|
-
blossomServers: [{ url: 'https://upload.
|
|
20
|
+
blossomServers: [{ url: 'https://upload.example', read: true, write: true }],
|
|
21
21
|
});
|
|
22
22
|
await client.init();
|
|
23
23
|
|
|
@@ -26,19 +26,19 @@ const { hashHex } = await client.putBlob(data);
|
|
|
26
26
|
const { data: blob } = await client.getBlob(hashHex);
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
##
|
|
29
|
+
## Relay Worker Client
|
|
30
30
|
|
|
31
|
-
If you are using `@hashtree/worker/
|
|
32
|
-
metadata or subscription calls, use the dedicated
|
|
31
|
+
If you are using `@hashtree/worker/relay-entry?worker` and need relay-backed tree-root
|
|
32
|
+
metadata or subscription calls, use the dedicated relay wrapper:
|
|
33
33
|
|
|
34
34
|
```typescript
|
|
35
|
-
import {
|
|
36
|
-
import HashtreeWorker from '@hashtree/worker/
|
|
35
|
+
import { RelayWorkerClient } from '@hashtree/worker/relay-client';
|
|
36
|
+
import HashtreeWorker from '@hashtree/worker/relay-entry?worker';
|
|
37
37
|
|
|
38
|
-
const client = new
|
|
39
|
-
storeName: '
|
|
38
|
+
const client = new RelayWorkerClient(HashtreeWorker, {
|
|
39
|
+
storeName: 'demo-sites-worker',
|
|
40
40
|
relays: ['wss://relay.damus.io'],
|
|
41
|
-
blossomServers: [{ url: 'https://upload.
|
|
41
|
+
blossomServers: [{ url: 'https://upload.example', read: false, write: true }],
|
|
42
42
|
pubkey: '336f319763657d6b0e65a5b5876719e8c8dcdcf9396852be71ee26b73368b29b',
|
|
43
43
|
});
|
|
44
44
|
|
|
@@ -49,10 +49,10 @@ const stop = client.onTreeRootUpdate((update) => {
|
|
|
49
49
|
});
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
-
##
|
|
52
|
+
## Browser Runtime Defaults
|
|
53
53
|
|
|
54
54
|
When the app runs inside Iris or another shell that injects `window.__HTREE_SERVER_URL__`
|
|
55
|
-
(or the launch URL carries `
|
|
55
|
+
(or the launch URL carries `htree_server`), the main app-facing API should be
|
|
56
56
|
`createHtreeRuntime(...)`:
|
|
57
57
|
|
|
58
58
|
```typescript
|
|
@@ -68,8 +68,8 @@ const DEFAULT_RELAYS = [
|
|
|
68
68
|
];
|
|
69
69
|
|
|
70
70
|
const DEFAULT_BLOSSOM_SERVERS = [
|
|
71
|
-
{ url: 'https://upload.
|
|
72
|
-
{ url: 'https://cdn.
|
|
71
|
+
{ url: 'https://upload.example', read: false, write: true },
|
|
72
|
+
{ url: 'https://cdn.example', read: true, write: false },
|
|
73
73
|
];
|
|
74
74
|
|
|
75
75
|
const runtime = createHtreeRuntime({
|
|
@@ -118,7 +118,7 @@ channel.
|
|
|
118
118
|
Behavior:
|
|
119
119
|
|
|
120
120
|
- In plain web mode, `runtime.endpoints` keeps your configured public relays and Blossom servers.
|
|
121
|
-
- In
|
|
121
|
+
- In native child runtimes, `runtime.endpoints` and `runtime.getWorkerConfig()` switch transport defaults to the local daemon endpoints.
|
|
122
122
|
- `runtime.urls.media(...)` handles `/htree/...` URL generation plus the per-client `htree_c` and optional `htree_t` query params.
|
|
123
123
|
- `runtime.media.ensureReady(...)` handles the common page-side service-worker/media-port handshake.
|
|
124
124
|
|
|
@@ -130,12 +130,12 @@ If your service worker intercepts `/htree/...` media requests and forwards them
|
|
|
130
130
|
- `runtime.urls.media(..., { clientScoped: true })` appends it as the `htree_c` query param.
|
|
131
131
|
- `runtime.media.ensureReady(...)` sends the same key in `REGISTER_WORKER_PORT` and `PING_WORKER_PORT`.
|
|
132
132
|
|
|
133
|
-
That lets the service worker map fetches back to the correct worker port when multiple tabs or isolated
|
|
133
|
+
That lets the service worker map fetches back to the correct worker port when multiple tabs or isolated child webviews are active at once, without falling back to a single global port.
|
|
134
134
|
|
|
135
135
|
## Exports
|
|
136
136
|
|
|
137
137
|
- `@hashtree/worker` — `createHtreeRuntime`, `resolveRuntimeEndpoints`, and `HashtreeWorkerClient`
|
|
138
|
-
- `@hashtree/worker/
|
|
138
|
+
- `@hashtree/worker/relay-client` — `RelayWorkerClient` plus relay-backed tree-root metadata types
|
|
139
139
|
- `@hashtree/worker/worker` — `attachHashtreeWorker(...)` for embedding the worker protocol into a custom worker
|
|
140
140
|
- `@hashtree/worker/p2p` — `WebRTCController` / `WebRTCProxy` for P2P data channel management
|
|
141
141
|
- `@hashtree/worker/entry` — Worker entry point
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hashtree/worker",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Modular browser worker for hashtree blob caching, tree-root state, and Blossom connectivity",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
"types": "./dist/client.d.ts",
|
|
15
15
|
"import": "./dist/client.js"
|
|
16
16
|
},
|
|
17
|
-
"./
|
|
18
|
-
"types": "./dist/
|
|
19
|
-
"import": "./dist/
|
|
17
|
+
"./relay-client": {
|
|
18
|
+
"types": "./dist/relay-client.d.ts",
|
|
19
|
+
"import": "./dist/relay-client.js"
|
|
20
20
|
},
|
|
21
21
|
"./p2p": {
|
|
22
22
|
"types": "./dist/p2p/index.d.ts",
|
|
@@ -30,9 +30,9 @@
|
|
|
30
30
|
"types": "./dist/worker.d.ts",
|
|
31
31
|
"import": "./dist/worker.js"
|
|
32
32
|
},
|
|
33
|
-
"./
|
|
34
|
-
"types": "./dist/
|
|
35
|
-
"import": "./dist/
|
|
33
|
+
"./relay-entry": {
|
|
34
|
+
"types": "./dist/relay-entry.d.ts",
|
|
35
|
+
"import": "./dist/relay-entry.js"
|
|
36
36
|
},
|
|
37
37
|
"./protocol": {
|
|
38
38
|
"types": "./dist/protocol.d.ts",
|
|
@@ -56,11 +56,17 @@
|
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
58
|
"files": [
|
|
59
|
-
"dist"
|
|
59
|
+
"dist",
|
|
60
|
+
"src"
|
|
60
61
|
],
|
|
61
62
|
"publishConfig": {
|
|
62
63
|
"access": "public"
|
|
63
64
|
},
|
|
65
|
+
"scripts": {
|
|
66
|
+
"build": "tsc",
|
|
67
|
+
"test": "vitest run",
|
|
68
|
+
"test:watch": "vitest"
|
|
69
|
+
},
|
|
64
70
|
"keywords": [
|
|
65
71
|
"hashtree",
|
|
66
72
|
"worker",
|
|
@@ -69,7 +75,10 @@
|
|
|
69
75
|
],
|
|
70
76
|
"author": "Martti Malmi",
|
|
71
77
|
"license": "MIT",
|
|
72
|
-
"repository":
|
|
78
|
+
"repository": {
|
|
79
|
+
"type": "git",
|
|
80
|
+
"url": "https://git.iris.to/#/npub1xdhnr9mrv47kkrn95k6cwecearydeh8e895990n3acntwvmgk2dsdeeycm/hashtree"
|
|
81
|
+
},
|
|
73
82
|
"homepage": "https://git.iris.to/#/npub1xdhnr9mrv47kkrn95k6cwecearydeh8e895990n3acntwvmgk2dsdeeycm/hashtree/ts/packages/hashtree-worker",
|
|
74
83
|
"bugs": {
|
|
75
84
|
"url": "https://git.iris.to/#/npub1xdhnr9mrv47kkrn95k6cwecearydeh8e895990n3acntwvmgk2dsdeeycm/hashtree?tab=issues"
|
|
@@ -99,15 +108,10 @@
|
|
|
99
108
|
}
|
|
100
109
|
},
|
|
101
110
|
"devDependencies": {
|
|
111
|
+
"ndk": "workspace:*",
|
|
112
|
+
"ndk-cache": "workspace:*",
|
|
113
|
+
"nostr-social-graph": "workspace:*",
|
|
102
114
|
"typescript": "^5.3.0",
|
|
103
|
-
"vitest": "^2.1.9"
|
|
104
|
-
"ndk": "0.1.1",
|
|
105
|
-
"ndk-cache": "0.1.1",
|
|
106
|
-
"nostr-social-graph": "1.0.36"
|
|
107
|
-
},
|
|
108
|
-
"scripts": {
|
|
109
|
-
"build": "tsc",
|
|
110
|
-
"test": "vitest run",
|
|
111
|
-
"test:watch": "vitest"
|
|
115
|
+
"vitest": "^2.1.9"
|
|
112
116
|
}
|
|
113
|
-
}
|
|
117
|
+
}
|
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import type { WorkerConfig, BlossomServerConfig } from './protocol.js';
|
|
2
|
+
import type { HtreeRuntimeWindowLike } from './runtime.js';
|
|
3
|
+
import type { ParsedHtreeUrl, ResolveHtreeRequestUrlOptions } from './htree-url.js';
|
|
4
|
+
import type { HtreeClientIdStorageLike } from './client-id.js';
|
|
5
|
+
import {
|
|
6
|
+
appendHtreeClientId,
|
|
7
|
+
appendHtreeQueryParam,
|
|
8
|
+
getOrCreateHtreeClientId,
|
|
9
|
+
} from './client-id.js';
|
|
10
|
+
import { resolveHtreeRequestUrl } from './htree-url.js';
|
|
11
|
+
import { canUseInjectedHtreeServerUrl, canUseSameOriginHtreeProtocolStreaming } from './runtime.js';
|
|
12
|
+
import { resolveRuntimeEndpoints, type RuntimeEndpoints } from './runtime-network.js';
|
|
13
|
+
|
|
14
|
+
export type RuntimeValueSource<T> = T | (() => T);
|
|
15
|
+
|
|
16
|
+
export interface HtreeRuntimeEndpointOverrides {
|
|
17
|
+
relays?: readonly string[];
|
|
18
|
+
blossomServers?: readonly BlossomServerConfig[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface HtreeRuntimeOptions {
|
|
22
|
+
appId?: string | null;
|
|
23
|
+
fallbackBaseUrl?: string | null;
|
|
24
|
+
windowLike?: HtreeRuntimeWindowLike;
|
|
25
|
+
storage?: HtreeClientIdStorageLike | null;
|
|
26
|
+
clientIdFactory?: () => string;
|
|
27
|
+
clientIdStorageKey?: string;
|
|
28
|
+
clientIdPrefix?: string;
|
|
29
|
+
serviceWorker?: ServiceWorkerContainer | null;
|
|
30
|
+
relays?: RuntimeValueSource<readonly string[]>;
|
|
31
|
+
blossomServers?: RuntimeValueSource<readonly BlossomServerConfig[]>;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface HtreeRuntimeRequestUrlOptions extends Omit<ResolveHtreeRequestUrlOptions, 'windowLike' | 'fallbackBaseUrl'> {}
|
|
35
|
+
|
|
36
|
+
export interface HtreeRuntimeMediaUrlOptions extends HtreeRuntimeRequestUrlOptions {
|
|
37
|
+
clientScoped?: boolean;
|
|
38
|
+
mimeType?: string | null | undefined;
|
|
39
|
+
query?: Record<string, string | number | boolean | null | undefined>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface HtreeRuntimeWorkerConfigOptions extends Omit<WorkerConfig, 'relays' | 'blossomServers'> {
|
|
43
|
+
relays?: readonly string[];
|
|
44
|
+
blossomServers?: readonly BlossomServerConfig[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface HtreeRuntimeMediaPortOptions {
|
|
48
|
+
registerMediaPort: (port: MessagePort, debug?: boolean) => Promise<void> | void;
|
|
49
|
+
debug?: boolean;
|
|
50
|
+
attempts?: number;
|
|
51
|
+
delayMs?: number;
|
|
52
|
+
pingTimeoutMs?: number;
|
|
53
|
+
registrationTimeoutMs?: number;
|
|
54
|
+
controllerTimeoutMs?: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface HtreeRuntime {
|
|
58
|
+
readonly appId: string | null;
|
|
59
|
+
readonly clientId: string | null;
|
|
60
|
+
readonly endpoints: RuntimeEndpoints;
|
|
61
|
+
getEndpoints(overrides?: HtreeRuntimeEndpointOverrides): RuntimeEndpoints;
|
|
62
|
+
getWorkerConfig(options?: HtreeRuntimeWorkerConfigOptions): WorkerConfig;
|
|
63
|
+
urls: {
|
|
64
|
+
request: (input: string | ParsedHtreeUrl, options?: HtreeRuntimeRequestUrlOptions) => string;
|
|
65
|
+
media: (input: string | ParsedHtreeUrl, options?: HtreeRuntimeMediaUrlOptions) => string;
|
|
66
|
+
appendClientId: (url: string) => string;
|
|
67
|
+
};
|
|
68
|
+
media: {
|
|
69
|
+
ensureReady: (options: HtreeRuntimeMediaPortOptions) => Promise<boolean>;
|
|
70
|
+
reset: () => void;
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const DEFAULT_FALLBACK_BASE_URL = '';
|
|
75
|
+
const DEFAULT_MEDIA_PORT_ATTEMPTS = 3;
|
|
76
|
+
const DEFAULT_MEDIA_PORT_DELAY_MS = 500;
|
|
77
|
+
const DEFAULT_MEDIA_PORT_PING_TIMEOUT_MS = 1_500;
|
|
78
|
+
const DEFAULT_MEDIA_PORT_REGISTRATION_TIMEOUT_MS = 5_000;
|
|
79
|
+
const DEFAULT_MEDIA_PORT_CONTROLLER_TIMEOUT_MS = 5_000;
|
|
80
|
+
const RECONNECT_REQUEST_COOLDOWN_MS = 1_000;
|
|
81
|
+
|
|
82
|
+
function resolveRuntimeValue<T>(value: RuntimeValueSource<T> | undefined, fallback: T): T {
|
|
83
|
+
if (typeof value === 'function') {
|
|
84
|
+
return (value as () => T)();
|
|
85
|
+
}
|
|
86
|
+
return value ?? fallback;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function getServiceWorkerContainer(
|
|
90
|
+
serviceWorker?: ServiceWorkerContainer | null,
|
|
91
|
+
): ServiceWorkerContainer | null {
|
|
92
|
+
if (typeof serviceWorker !== 'undefined') {
|
|
93
|
+
return serviceWorker;
|
|
94
|
+
}
|
|
95
|
+
if (typeof navigator === 'undefined') {
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
return navigator.serviceWorker ?? null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function isDirectMediaRuntime(windowLike?: HtreeRuntimeWindowLike): boolean {
|
|
102
|
+
return canUseInjectedHtreeServerUrl(windowLike)
|
|
103
|
+
|| canUseSameOriginHtreeProtocolStreaming(windowLike);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function createMessageId(prefix: string): string {
|
|
107
|
+
return `${prefix}-${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function createHtreeRuntime(options: HtreeRuntimeOptions = {}): HtreeRuntime {
|
|
111
|
+
const appId = options.appId?.trim() || null;
|
|
112
|
+
const fallbackBaseUrl = options.fallbackBaseUrl ?? DEFAULT_FALLBACK_BASE_URL;
|
|
113
|
+
const windowLike = options.windowLike;
|
|
114
|
+
const storage = typeof options.storage === 'undefined' ? undefined : options.storage;
|
|
115
|
+
const storageKey = options.clientIdStorageKey ?? (appId ? `${appId}.mediaClientId` : 'htree.mediaClientId');
|
|
116
|
+
const clientIdPrefix = options.clientIdPrefix ?? (
|
|
117
|
+
appId ? appId.replace(/[^a-z0-9]+/gi, '').toLowerCase() || 'htree' : 'htree'
|
|
118
|
+
);
|
|
119
|
+
const serviceWorker = getServiceWorkerContainer(options.serviceWorker);
|
|
120
|
+
|
|
121
|
+
let setupPromise: Promise<boolean> | null = null;
|
|
122
|
+
let mediaReady = false;
|
|
123
|
+
let activeController: ServiceWorker | null = null;
|
|
124
|
+
let controllerListenerAttached = false;
|
|
125
|
+
let messageListenerAttached = false;
|
|
126
|
+
let reconnectPromise: Promise<void> | null = null;
|
|
127
|
+
let lastReconnectRequestAt = 0;
|
|
128
|
+
let lastEnsureOptions: HtreeRuntimeMediaPortOptions | null = null;
|
|
129
|
+
|
|
130
|
+
const getClientId = (): string | null => getOrCreateHtreeClientId({
|
|
131
|
+
storage,
|
|
132
|
+
storageKey,
|
|
133
|
+
prefix: clientIdPrefix,
|
|
134
|
+
uuidFactory: options.clientIdFactory,
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const getEndpoints = (overrides: HtreeRuntimeEndpointOverrides = {}): RuntimeEndpoints => {
|
|
138
|
+
const relays = overrides.relays ?? resolveRuntimeValue(options.relays, []);
|
|
139
|
+
const blossomServers = overrides.blossomServers ?? resolveRuntimeValue(options.blossomServers, []);
|
|
140
|
+
return resolveRuntimeEndpoints({
|
|
141
|
+
windowLike,
|
|
142
|
+
relays,
|
|
143
|
+
blossomServers,
|
|
144
|
+
});
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const appendRuntimeClientId = (url: string): string => appendHtreeClientId(url, getClientId());
|
|
148
|
+
|
|
149
|
+
const resolveRequestUrl = (
|
|
150
|
+
input: string | ParsedHtreeUrl,
|
|
151
|
+
urlOptions: HtreeRuntimeRequestUrlOptions = {},
|
|
152
|
+
): string => resolveHtreeRequestUrl(input, {
|
|
153
|
+
...urlOptions,
|
|
154
|
+
windowLike,
|
|
155
|
+
fallbackBaseUrl,
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const resolveMediaUrl = (
|
|
159
|
+
input: string | ParsedHtreeUrl,
|
|
160
|
+
mediaOptions: HtreeRuntimeMediaUrlOptions = {},
|
|
161
|
+
): string => {
|
|
162
|
+
let url = resolveRequestUrl(input, mediaOptions);
|
|
163
|
+
|
|
164
|
+
if (mediaOptions.clientScoped) {
|
|
165
|
+
url = appendRuntimeClientId(url);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
url = appendHtreeQueryParam(url, 'htree_t', mediaOptions.mimeType);
|
|
169
|
+
|
|
170
|
+
for (const [key, value] of Object.entries(mediaOptions.query ?? {})) {
|
|
171
|
+
url = appendHtreeQueryParam(url, key, value == null ? value : String(value));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return url;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const getWorkerConfig = (configOptions: HtreeRuntimeWorkerConfigOptions = {}): WorkerConfig => {
|
|
178
|
+
const { relays, blossomServers, ...rest } = configOptions;
|
|
179
|
+
const endpoints = getEndpoints({ relays, blossomServers });
|
|
180
|
+
return {
|
|
181
|
+
...rest,
|
|
182
|
+
relays: endpoints.nostrRelays,
|
|
183
|
+
blossomServers: endpoints.blossomServers,
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const resetMedia = (): void => {
|
|
188
|
+
mediaReady = false;
|
|
189
|
+
setupPromise = null;
|
|
190
|
+
activeController = null;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
const ensureControllerListener = (): void => {
|
|
194
|
+
if (controllerListenerAttached || !serviceWorker) return;
|
|
195
|
+
controllerListenerAttached = true;
|
|
196
|
+
serviceWorker.addEventListener('controllerchange', () => {
|
|
197
|
+
resetMedia();
|
|
198
|
+
});
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const requestMediaReconnect = (requestedClientKey?: string | null): void => {
|
|
202
|
+
const clientId = getClientId();
|
|
203
|
+
if (!lastEnsureOptions) return;
|
|
204
|
+
if (requestedClientKey && clientId && requestedClientKey !== clientId) {
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const now = Date.now();
|
|
209
|
+
if (reconnectPromise || now - lastReconnectRequestAt < RECONNECT_REQUEST_COOLDOWN_MS) {
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
lastReconnectRequestAt = now;
|
|
214
|
+
resetMedia();
|
|
215
|
+
reconnectPromise = ensureMediaPortReady(lastEnsureOptions)
|
|
216
|
+
.then(() => undefined)
|
|
217
|
+
.catch(() => undefined)
|
|
218
|
+
.finally(() => {
|
|
219
|
+
reconnectPromise = null;
|
|
220
|
+
});
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const ensureMessageListener = (): void => {
|
|
224
|
+
if (messageListenerAttached || !serviceWorker) return;
|
|
225
|
+
messageListenerAttached = true;
|
|
226
|
+
serviceWorker.addEventListener('message', (event: MessageEvent) => {
|
|
227
|
+
const data = event.data as { type?: string; clientKey?: string | null } | null;
|
|
228
|
+
if (data?.type !== 'REQUEST_WORKER_PORT_RECONNECT') {
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
requestMediaReconnect(data.clientKey ?? null);
|
|
232
|
+
});
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const waitForController = async (timeoutMs: number): Promise<ServiceWorker | null> => {
|
|
236
|
+
if (!serviceWorker) return null;
|
|
237
|
+
if (serviceWorker.controller) return serviceWorker.controller;
|
|
238
|
+
|
|
239
|
+
await serviceWorker.ready.catch(() => undefined);
|
|
240
|
+
if (serviceWorker.controller) return serviceWorker.controller;
|
|
241
|
+
|
|
242
|
+
return await new Promise<ServiceWorker | null>((resolve) => {
|
|
243
|
+
const timeoutId = setTimeout(() => resolve(serviceWorker.controller ?? null), timeoutMs);
|
|
244
|
+
serviceWorker.addEventListener('controllerchange', () => {
|
|
245
|
+
clearTimeout(timeoutId);
|
|
246
|
+
resolve(serviceWorker.controller ?? null);
|
|
247
|
+
}, { once: true });
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const pingMediaPort = async (
|
|
252
|
+
clientKey: string,
|
|
253
|
+
controller: ServiceWorker,
|
|
254
|
+
timeoutMs: number,
|
|
255
|
+
): Promise<boolean> => {
|
|
256
|
+
if (!serviceWorker) return false;
|
|
257
|
+
const requestId = createMessageId('media-ping');
|
|
258
|
+
const ackPromise = new Promise<boolean>((resolve) => {
|
|
259
|
+
const timeoutId = setTimeout(() => {
|
|
260
|
+
serviceWorker.removeEventListener('message', onMessage);
|
|
261
|
+
resolve(false);
|
|
262
|
+
}, timeoutMs);
|
|
263
|
+
const onMessage = (event: MessageEvent): void => {
|
|
264
|
+
const data = event.data as { type?: string; requestId?: string; ok?: boolean } | null;
|
|
265
|
+
if (data?.type === 'WORKER_PORT_PONG' && data.requestId === requestId) {
|
|
266
|
+
clearTimeout(timeoutId);
|
|
267
|
+
serviceWorker.removeEventListener('message', onMessage);
|
|
268
|
+
resolve(!!data.ok);
|
|
269
|
+
}
|
|
270
|
+
};
|
|
271
|
+
serviceWorker.addEventListener('message', onMessage);
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
controller.postMessage({ type: 'PING_WORKER_PORT', requestId, clientKey });
|
|
275
|
+
return await ackPromise;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
const setupMediaPort = async (portOptions: HtreeRuntimeMediaPortOptions): Promise<boolean> => {
|
|
279
|
+
if (!serviceWorker) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
ensureControllerListener();
|
|
284
|
+
ensureMessageListener();
|
|
285
|
+
const controller = await waitForController(portOptions.controllerTimeoutMs ?? DEFAULT_MEDIA_PORT_CONTROLLER_TIMEOUT_MS);
|
|
286
|
+
if (!controller) {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const clientKey = getClientId() ?? undefined;
|
|
291
|
+
if (mediaReady && activeController === controller) {
|
|
292
|
+
if (!clientKey) {
|
|
293
|
+
return true;
|
|
294
|
+
}
|
|
295
|
+
const alive = await pingMediaPort(
|
|
296
|
+
clientKey,
|
|
297
|
+
controller,
|
|
298
|
+
portOptions.pingTimeoutMs ?? DEFAULT_MEDIA_PORT_PING_TIMEOUT_MS,
|
|
299
|
+
);
|
|
300
|
+
if (alive) {
|
|
301
|
+
return true;
|
|
302
|
+
}
|
|
303
|
+
resetMedia();
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
const channel = new MessageChannel();
|
|
307
|
+
const requestId = createMessageId('media');
|
|
308
|
+
const ackPromise = new Promise<boolean>((resolve) => {
|
|
309
|
+
const timeoutId = setTimeout(() => {
|
|
310
|
+
serviceWorker.removeEventListener('message', onMessage);
|
|
311
|
+
resolve(false);
|
|
312
|
+
}, portOptions.registrationTimeoutMs ?? DEFAULT_MEDIA_PORT_REGISTRATION_TIMEOUT_MS);
|
|
313
|
+
const onMessage = (event: MessageEvent): void => {
|
|
314
|
+
const data = event.data as { type?: string; requestId?: string } | null;
|
|
315
|
+
if (data?.type === 'WORKER_PORT_READY' && data.requestId === requestId) {
|
|
316
|
+
clearTimeout(timeoutId);
|
|
317
|
+
serviceWorker.removeEventListener('message', onMessage);
|
|
318
|
+
resolve(true);
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
serviceWorker.addEventListener('message', onMessage);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
controller.postMessage(
|
|
325
|
+
{
|
|
326
|
+
type: 'REGISTER_WORKER_PORT',
|
|
327
|
+
port: channel.port1,
|
|
328
|
+
requestId,
|
|
329
|
+
clientKey,
|
|
330
|
+
debug: !!portOptions.debug,
|
|
331
|
+
},
|
|
332
|
+
[channel.port1],
|
|
333
|
+
);
|
|
334
|
+
await portOptions.registerMediaPort(channel.port2, !!portOptions.debug);
|
|
335
|
+
|
|
336
|
+
const acked = await ackPromise;
|
|
337
|
+
mediaReady = acked;
|
|
338
|
+
activeController = acked ? controller : null;
|
|
339
|
+
return acked;
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
const ensureMediaPortReady = async (portOptions: HtreeRuntimeMediaPortOptions): Promise<boolean> => {
|
|
343
|
+
lastEnsureOptions = portOptions;
|
|
344
|
+
if (isDirectMediaRuntime(windowLike)) {
|
|
345
|
+
return true;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const attempts = portOptions.attempts ?? DEFAULT_MEDIA_PORT_ATTEMPTS;
|
|
349
|
+
const delayMs = portOptions.delayMs ?? DEFAULT_MEDIA_PORT_DELAY_MS;
|
|
350
|
+
|
|
351
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
352
|
+
if (!setupPromise) {
|
|
353
|
+
setupPromise = setupMediaPort(portOptions).finally(() => {
|
|
354
|
+
if (!mediaReady) {
|
|
355
|
+
setupPromise = null;
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
const ready = await setupPromise.catch(() => false);
|
|
361
|
+
if (ready) {
|
|
362
|
+
return true;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (attempt < attempts - 1) {
|
|
366
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return false;
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
return {
|
|
374
|
+
appId,
|
|
375
|
+
get clientId(): string | null {
|
|
376
|
+
return getClientId();
|
|
377
|
+
},
|
|
378
|
+
get endpoints(): RuntimeEndpoints {
|
|
379
|
+
return getEndpoints();
|
|
380
|
+
},
|
|
381
|
+
getEndpoints,
|
|
382
|
+
getWorkerConfig,
|
|
383
|
+
urls: {
|
|
384
|
+
request: resolveRequestUrl,
|
|
385
|
+
media: resolveMediaUrl,
|
|
386
|
+
appendClientId: appendRuntimeClientId,
|
|
387
|
+
},
|
|
388
|
+
media: {
|
|
389
|
+
ensureReady: ensureMediaPortReady,
|
|
390
|
+
reset: resetMedia,
|
|
391
|
+
},
|
|
392
|
+
};
|
|
393
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { BlossomLogEntry } from '@hashtree/core';
|
|
2
|
+
|
|
3
|
+
export interface BlossomBandwidthServerStats {
|
|
4
|
+
url: string;
|
|
5
|
+
bytesSent: number;
|
|
6
|
+
bytesReceived: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface BlossomBandwidthStats {
|
|
10
|
+
totalBytesSent: number;
|
|
11
|
+
totalBytesReceived: number;
|
|
12
|
+
updatedAt: number;
|
|
13
|
+
servers: BlossomBandwidthServerStats[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type BlossomBandwidthUpdateHandler = (stats: BlossomBandwidthStats) => void;
|
|
17
|
+
|
|
18
|
+
export class BlossomBandwidthTracker {
|
|
19
|
+
private totalBytesSent = 0;
|
|
20
|
+
private totalBytesReceived = 0;
|
|
21
|
+
private readonly serverBandwidth = new Map<string, { bytesSent: number; bytesReceived: number }>();
|
|
22
|
+
private readonly onUpdate?: BlossomBandwidthUpdateHandler;
|
|
23
|
+
private readonly now: () => number;
|
|
24
|
+
|
|
25
|
+
constructor(onUpdate?: BlossomBandwidthUpdateHandler, now: () => number = () => Date.now()) {
|
|
26
|
+
this.onUpdate = onUpdate;
|
|
27
|
+
this.now = now;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
apply(entry: BlossomLogEntry): void {
|
|
31
|
+
const bytes = entry.bytes ?? 0;
|
|
32
|
+
if (!entry.success || bytes <= 0) return;
|
|
33
|
+
|
|
34
|
+
const serverStats = this.serverBandwidth.get(entry.server) ?? { bytesSent: 0, bytesReceived: 0 };
|
|
35
|
+
|
|
36
|
+
if (entry.operation === 'put') {
|
|
37
|
+
this.totalBytesSent += bytes;
|
|
38
|
+
serverStats.bytesSent += bytes;
|
|
39
|
+
} else if (entry.operation === 'get') {
|
|
40
|
+
this.totalBytesReceived += bytes;
|
|
41
|
+
serverStats.bytesReceived += bytes;
|
|
42
|
+
} else {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.serverBandwidth.set(entry.server, serverStats);
|
|
47
|
+
this.onUpdate?.(this.getStats());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getStats(): BlossomBandwidthStats {
|
|
51
|
+
return {
|
|
52
|
+
totalBytesSent: this.totalBytesSent,
|
|
53
|
+
totalBytesReceived: this.totalBytesReceived,
|
|
54
|
+
updatedAt: this.now(),
|
|
55
|
+
servers: this.getOrderedServerBandwidth(),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
reset(): void {
|
|
60
|
+
this.totalBytesSent = 0;
|
|
61
|
+
this.totalBytesReceived = 0;
|
|
62
|
+
this.serverBandwidth.clear();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private getOrderedServerBandwidth(): BlossomBandwidthServerStats[] {
|
|
66
|
+
return Array.from(this.serverBandwidth.entries())
|
|
67
|
+
.map(([url, stats]) => ({
|
|
68
|
+
url,
|
|
69
|
+
bytesSent: stats.bytesSent,
|
|
70
|
+
bytesReceived: stats.bytesReceived,
|
|
71
|
+
}))
|
|
72
|
+
.sort((a, b) => a.url.localeCompare(b.url));
|
|
73
|
+
}
|
|
74
|
+
}
|