@hashtree/worker 0.2.0 → 0.2.2

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.
Files changed (54) hide show
  1. package/package.json +7 -3
  2. package/src/app-runtime.ts +393 -0
  3. package/src/capabilities/blossomBandwidthTracker.ts +74 -0
  4. package/src/capabilities/blossomTransport.ts +179 -0
  5. package/src/capabilities/connectivity.ts +54 -0
  6. package/src/capabilities/idbStorage.ts +94 -0
  7. package/src/capabilities/meshRouterStore.ts +426 -0
  8. package/src/capabilities/rootResolver.ts +497 -0
  9. package/src/client-id.ts +137 -0
  10. package/src/client.ts +501 -0
  11. package/src/entry.ts +3 -0
  12. package/src/htree-path.ts +53 -0
  13. package/src/htree-url.ts +156 -0
  14. package/src/index.ts +76 -0
  15. package/src/mediaStreaming.ts +64 -0
  16. package/src/p2p/boundedQueue.ts +168 -0
  17. package/src/p2p/errorMessage.ts +6 -0
  18. package/src/p2p/index.ts +48 -0
  19. package/src/p2p/lruCache.ts +78 -0
  20. package/src/p2p/meshQueryRouter.ts +361 -0
  21. package/src/p2p/protocol.ts +11 -0
  22. package/src/p2p/queryForwardingMachine.ts +197 -0
  23. package/src/p2p/signaling.ts +284 -0
  24. package/src/p2p/uploadRateLimiter.ts +85 -0
  25. package/src/p2p/webrtcController.ts +1168 -0
  26. package/src/p2p/webrtcProxy.ts +519 -0
  27. package/src/privacyGuards.ts +31 -0
  28. package/src/protocol.ts +124 -0
  29. package/src/relay/identity.ts +86 -0
  30. package/src/relay/mediaHandler.ts +1633 -0
  31. package/src/relay/ndk.ts +590 -0
  32. package/src/relay/nostr-wasm.ts +249 -0
  33. package/src/relay/nostr.ts +249 -0
  34. package/src/relay/protocol.ts +361 -0
  35. package/src/relay/publicAssetUrl.ts +25 -0
  36. package/src/relay/rootPathResolver.ts +50 -0
  37. package/src/relay/shims.d.ts +17 -0
  38. package/src/relay/signing.ts +332 -0
  39. package/src/relay/treeRootCache.ts +354 -0
  40. package/src/relay/treeRootSubscription.ts +577 -0
  41. package/src/relay/utils/constants.ts +139 -0
  42. package/src/relay/utils/errorMessage.ts +7 -0
  43. package/src/relay/utils/lruCache.ts +79 -0
  44. package/src/relay/webrtc.ts +5 -0
  45. package/src/relay/webrtcSignaling.ts +108 -0
  46. package/src/relay/worker.ts +1787 -0
  47. package/src/relay-client.ts +265 -0
  48. package/src/relay-entry.ts +1 -0
  49. package/src/runtime-network.ts +134 -0
  50. package/src/runtime.ts +153 -0
  51. package/src/transferableBytes.ts +5 -0
  52. package/src/tree-root.ts +851 -0
  53. package/src/types.ts +8 -0
  54. package/src/worker.ts +975 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hashtree/worker",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
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",
@@ -56,7 +56,8 @@
56
56
  }
57
57
  },
58
58
  "files": [
59
- "dist"
59
+ "dist",
60
+ "src"
60
61
  ],
61
62
  "publishConfig": {
62
63
  "access": "public"
@@ -74,7 +75,10 @@
74
75
  ],
75
76
  "author": "Martti Malmi",
76
77
  "license": "MIT",
77
- "repository": "https://git.iris.to/#/npub1xdhnr9mrv47kkrn95k6cwecearydeh8e895990n3acntwvmgk2dsdeeycm/hashtree",
78
+ "repository": {
79
+ "type": "git",
80
+ "url": "https://git.iris.to/#/npub1xdhnr9mrv47kkrn95k6cwecearydeh8e895990n3acntwvmgk2dsdeeycm/hashtree"
81
+ },
78
82
  "homepage": "https://git.iris.to/#/npub1xdhnr9mrv47kkrn95k6cwecearydeh8e895990n3acntwvmgk2dsdeeycm/hashtree/ts/packages/hashtree-worker",
79
83
  "bugs": {
80
84
  "url": "https://git.iris.to/#/npub1xdhnr9mrv47kkrn95k6cwecearydeh8e895990n3acntwvmgk2dsdeeycm/hashtree?tab=issues"
@@ -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
+ }
@@ -0,0 +1,179 @@
1
+ import {
2
+ BlossomStore,
3
+ type BlossomSigner,
4
+ type BlossomUploadCallback,
5
+ fromHex,
6
+ } from '@hashtree/core';
7
+ import { finalizeEvent, generateSecretKey } from 'nostr-tools/pure';
8
+ import type { BlossomServerConfig } from '../protocol.js';
9
+ import {
10
+ BlossomBandwidthTracker,
11
+ type BlossomBandwidthStats,
12
+ type BlossomBandwidthUpdateHandler,
13
+ } from './blossomBandwidthTracker.js';
14
+
15
+ export const DEFAULT_BLOSSOM_SERVERS: BlossomServerConfig[] = [];
16
+
17
+ const MAX_CONCURRENT_READ_FETCHES = 12;
18
+
19
+ let activeReadFetches = 0;
20
+ const pendingReadFetchWaiters: Array<() => void> = [];
21
+
22
+ export type {
23
+ BlossomBandwidthServerStats,
24
+ BlossomBandwidthStats,
25
+ BlossomBandwidthUpdateHandler,
26
+ } from './blossomBandwidthTracker.js';
27
+
28
+ function normalizeServerUrl(url: string): string {
29
+ return url.replace(/\/+$/, '');
30
+ }
31
+
32
+ function normalizeServers(servers: BlossomServerConfig[] | undefined): BlossomServerConfig[] {
33
+ const source = servers && servers.length > 0 ? servers : DEFAULT_BLOSSOM_SERVERS;
34
+ const unique = new Map<string, BlossomServerConfig>();
35
+ for (const server of source) {
36
+ const url = normalizeServerUrl(server.url.trim());
37
+ if (!url) continue;
38
+ unique.set(url, {
39
+ url,
40
+ read: server.read ?? true,
41
+ write: server.write ?? false,
42
+ });
43
+ }
44
+ return Array.from(unique.values());
45
+ }
46
+
47
+ function createEphemeralSigner(): BlossomSigner {
48
+ const secretKey = generateSecretKey();
49
+ return async (template) => {
50
+ const event = finalizeEvent({
51
+ ...template,
52
+ kind: template.kind as 24242,
53
+ created_at: template.created_at,
54
+ content: template.content,
55
+ tags: template.tags,
56
+ }, secretKey);
57
+ return {
58
+ kind: event.kind,
59
+ created_at: event.created_at,
60
+ content: event.content,
61
+ tags: event.tags,
62
+ pubkey: event.pubkey,
63
+ id: event.id,
64
+ sig: event.sig,
65
+ };
66
+ };
67
+ }
68
+
69
+ function releaseReadFetchSlot(): void {
70
+ activeReadFetches = Math.max(0, activeReadFetches - 1);
71
+ pendingReadFetchWaiters.shift()?.();
72
+ }
73
+
74
+ function withReadFetchSlot<T>(loader: () => Promise<T>): Promise<T> {
75
+ return new Promise<T>((resolve, reject) => {
76
+ const start = () => {
77
+ activeReadFetches += 1;
78
+ let pending: Promise<T>;
79
+ try {
80
+ pending = loader();
81
+ } catch (error) {
82
+ releaseReadFetchSlot();
83
+ reject(error);
84
+ return;
85
+ }
86
+
87
+ pending
88
+ .then(resolve, reject)
89
+ .finally(() => {
90
+ releaseReadFetchSlot();
91
+ });
92
+ };
93
+
94
+ if (activeReadFetches < MAX_CONCURRENT_READ_FETCHES) {
95
+ start();
96
+ return;
97
+ }
98
+
99
+ pendingReadFetchWaiters.push(start);
100
+ });
101
+ }
102
+
103
+ export class BlossomTransport {
104
+ private servers: BlossomServerConfig[];
105
+ private readonly signer: BlossomSigner;
106
+ private readonly bandwidthTracker: BlossomBandwidthTracker;
107
+ private readonly inflightFetches = new Map<string, Promise<Uint8Array | null>>();
108
+ private store: BlossomStore;
109
+
110
+ constructor(servers?: BlossomServerConfig[], onBandwidthUpdate?: BlossomBandwidthUpdateHandler) {
111
+ this.servers = normalizeServers(servers);
112
+ this.signer = createEphemeralSigner();
113
+ this.bandwidthTracker = new BlossomBandwidthTracker(onBandwidthUpdate);
114
+ this.store = this.createStore(this.servers);
115
+ }
116
+
117
+ setServers(servers: BlossomServerConfig[]): void {
118
+ this.servers = normalizeServers(servers);
119
+ this.store = this.createStore(this.servers);
120
+ }
121
+
122
+ getServers(): BlossomServerConfig[] {
123
+ return this.servers;
124
+ }
125
+
126
+ getWriteServers(): BlossomServerConfig[] {
127
+ return this.servers.filter(server => !!server.write);
128
+ }
129
+
130
+ getBandwidthStats(): BlossomBandwidthStats {
131
+ return this.bandwidthTracker.getStats();
132
+ }
133
+
134
+ private createStore(servers: BlossomServerConfig[], onUploadProgress?: BlossomUploadCallback): BlossomStore {
135
+ return new BlossomStore({
136
+ servers,
137
+ signer: this.signer,
138
+ onUploadProgress,
139
+ logger: (entry) => {
140
+ this.bandwidthTracker.apply(entry);
141
+ },
142
+ });
143
+ }
144
+
145
+ createUploadStore(onUploadProgress?: BlossomUploadCallback): BlossomStore {
146
+ return this.createStore(this.servers, onUploadProgress);
147
+ }
148
+
149
+ async upload(
150
+ hashHex: string,
151
+ data: Uint8Array,
152
+ _mimeType?: string,
153
+ onUploadProgress?: BlossomUploadCallback
154
+ ): Promise<void> {
155
+ if (!this.servers.some(server => server.write)) return;
156
+ const uploadMimeType = 'application/octet-stream';
157
+ if (onUploadProgress) {
158
+ const store = this.createStore(this.servers, onUploadProgress);
159
+ await store.put(fromHex(hashHex), data, uploadMimeType);
160
+ return;
161
+ }
162
+
163
+ await this.store.put(fromHex(hashHex), data, uploadMimeType);
164
+ }
165
+
166
+ async fetch(hashHex: string): Promise<Uint8Array | null> {
167
+ const inflight = this.inflightFetches.get(hashHex);
168
+ if (inflight) {
169
+ return inflight;
170
+ }
171
+
172
+ const pending = withReadFetchSlot(() => this.store.get(fromHex(hashHex)))
173
+ .finally(() => {
174
+ this.inflightFetches.delete(hashHex);
175
+ });
176
+ this.inflightFetches.set(hashHex, pending);
177
+ return await pending;
178
+ }
179
+ }