@dimchansky/meshpost 0.5.0 → 0.7.0

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 (49) hide show
  1. package/README.md +66 -544
  2. package/dist/bip340.d.ts +1 -0
  3. package/dist/bip340.js +346 -0
  4. package/dist/blob.d.ts +1 -1
  5. package/dist/blob.js +44 -45
  6. package/dist/channel.d.ts +4 -0
  7. package/dist/channel.js +19 -44
  8. package/dist/combine.d.ts +9 -2
  9. package/dist/combine.js +40 -57
  10. package/dist/crypto.js +10 -9
  11. package/dist/identity.d.ts +2 -0
  12. package/dist/identity.js +39 -71
  13. package/dist/index.d.ts +3 -2
  14. package/dist/index.js +1 -1
  15. package/dist/listeners.js +18 -3
  16. package/dist/mqtt.js +211 -476
  17. package/dist/nostr.js +194 -836
  18. package/dist/peer-link.d.ts +1 -0
  19. package/dist/peer-link.js +983 -0
  20. package/dist/presence.d.ts +1 -0
  21. package/dist/presence.js +74 -0
  22. package/dist/public.d.ts +1 -1
  23. package/dist/public.js +25 -77
  24. package/dist/relay-core.d.ts +1 -0
  25. package/dist/relay-core.js +399 -0
  26. package/dist/room-reclaim.d.ts +1 -0
  27. package/dist/room-reclaim.js +115 -0
  28. package/dist/room-stats.d.ts +1 -0
  29. package/dist/room-stats.js +75 -0
  30. package/dist/room.js +245 -1218
  31. package/dist/rpc.js +25 -28
  32. package/dist/signaling.d.ts +8 -0
  33. package/dist/signaling.js +55 -0
  34. package/dist/state.js +23 -33
  35. package/dist/tabs.js +6 -50
  36. package/dist/voice-mic.d.ts +1 -0
  37. package/dist/voice-mic.js +496 -0
  38. package/dist/voice-remote.d.ts +1 -0
  39. package/dist/voice-remote.js +568 -0
  40. package/dist/voice.d.ts +19 -1
  41. package/dist/voice.js +95 -1036
  42. package/dist/ws.js +56 -197
  43. package/docs/adr/0001-room-internals-and-channel-budget.md +31 -0
  44. package/docs/api.md +219 -0
  45. package/docs/behavior.md +14 -8
  46. package/docs/guide.md +404 -0
  47. package/docs/signaling-and-production.md +312 -0
  48. package/docs/voice-and-media.md +193 -0
  49. package/package.json +6 -3
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,74 @@
1
+ import { isSignal } from './signaling.js';
2
+ const maxSeenPeers = 256;
3
+ const leaseTtlMs = 30_000;
4
+ const defaultErrorCooldownMs = 15_000;
5
+ /** @internal */
6
+ export function createErrorThrottle(emit, cooldownMs = defaultErrorCooldownMs) {
7
+ const lastAt = new Map();
8
+ return (code, message) => {
9
+ const now = Date.now();
10
+ const prev = lastAt.get(code) ?? -Infinity;
11
+ if (now - prev >= cooldownMs) {
12
+ lastAt.set(code, now);
13
+ emit(code, message);
14
+ }
15
+ };
16
+ }
17
+ /** @internal */
18
+ export function createPresenceTracker(discovery, sendPresent) {
19
+ const seen = new Map();
20
+ const roster = () => discovery.roster([...seen.keys()]);
21
+ const touch = (id) => {
22
+ const known = seen.has(id);
23
+ if (!known && seen.size >= maxSeenPeers) {
24
+ const oldest = seen.keys().next().value;
25
+ if (oldest !== undefined)
26
+ seen.delete(oldest);
27
+ }
28
+ seen.delete(id);
29
+ seen.set(id, Date.now());
30
+ if (!known)
31
+ roster();
32
+ return known;
33
+ };
34
+ return {
35
+ touch,
36
+ dispatch(data) {
37
+ const from = data.from;
38
+ switch (data.type) {
39
+ case 'hello':
40
+ touch(from);
41
+ sendPresent(from);
42
+ break;
43
+ case 'heartbeat':
44
+ if (!touch(from))
45
+ sendPresent(from);
46
+ break;
47
+ case 'present':
48
+ touch(from);
49
+ break;
50
+ case 'bye':
51
+ if (seen.delete(from))
52
+ roster();
53
+ break;
54
+ case 'signal':
55
+ if (isSignal(data.signal)) {
56
+ touch(from);
57
+ discovery.receive(from, data.signal);
58
+ }
59
+ break;
60
+ }
61
+ },
62
+ prune(now = Date.now()) {
63
+ let changed = false;
64
+ for (const [id, last] of seen) {
65
+ if (now - last > leaseTtlMs) {
66
+ seen.delete(id);
67
+ changed = true;
68
+ }
69
+ }
70
+ if (changed)
71
+ roster();
72
+ }
73
+ };
74
+ }
package/dist/public.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Signaling, SignalingState, WebSocketConstructor } from './signaling.js';
1
+ import { type Signaling, type SignalingState, type WebSocketConstructor } from './signaling.js';
2
2
  export type PublicTransportKind = 'ws' | 'mqtt' | 'nostr' | 'tabs';
3
3
  export interface PublicTransportEvent {
4
4
  readonly transport: PublicTransportKind;
package/dist/public.js CHANGED
@@ -1,71 +1,22 @@
1
- import { combineSignaling, withLocalTabsInternal } from './combine.js';
1
+ import { combineSignaling, withLocalTabs } from './combine.js';
2
2
  import { mqttSignaling } from './mqtt.js';
3
3
  import { nostrSignaling } from './nostr.js';
4
+ import { normalizeWsRelayUrl, validatePositiveTimeout } from './relay-core.js';
5
+ import { observeSignaling } from './signaling.js';
4
6
  import { optionalWsSignaling } from './ws.js';
5
7
  const maxOptionalWsUrls = 8;
6
8
  const maxRawWsUrlItems = 64;
7
9
  const defaultWsHandshakeTimeoutMs = 4_000;
8
10
  const defaultPublicHandshakeTimeoutMs = 10_000;
9
- function validateTimeoutMs(value, label) {
10
- if (!Number.isSafeInteger(value) || value <= 0 || value > 2_147_483_647) {
11
- throw new RangeError(`Invalid ${label}`);
12
- }
13
- }
14
- function notifyTransport(callback, event) {
15
- try {
16
- callback(event);
17
- }
18
- catch (error) {
19
- if (typeof globalThis.reportError === 'function') {
20
- globalThis.reportError(error);
21
- }
22
- else {
23
- queueMicrotask(() => {
24
- throw error;
25
- });
26
- }
27
- }
28
- }
29
11
  function observeTransport(adapter, transport, onTransportChange, url) {
30
- return (discovery) => {
31
- let lastState;
32
- let closed = false;
33
- const emit = (state, reason) => {
34
- if (state === lastState)
35
- return;
36
- lastState = state;
37
- notifyTransport(onTransportChange, Object.freeze({
38
- transport,
39
- ...(url !== undefined ? { url } : {}),
40
- state,
41
- ...(reason !== undefined ? { reason } : {})
42
- }));
43
- };
44
- const session = adapter({
45
- ...discovery,
46
- status(state, reason) {
47
- if (closed)
48
- return;
49
- emit(state, reason);
50
- discovery.status(state, reason);
51
- }
52
- });
53
- if (!closed && lastState === undefined) {
54
- emit('connecting');
55
- }
56
- return {
57
- ...(session.trickle !== undefined ? { trickle: session.trickle } : {}),
58
- ...(session.canReach ? { canReach: (peerId) => session.canReach(peerId) } : {}),
59
- send: (to, signal) => session.send(to, signal),
60
- close() {
61
- if (closed)
62
- return;
63
- closed = true;
64
- session.close();
65
- emit('closed');
66
- }
67
- };
68
- };
12
+ return observeSignaling(adapter, (state, reason) => {
13
+ onTransportChange(Object.freeze({
14
+ transport,
15
+ ...(url !== undefined ? { url } : {}),
16
+ state,
17
+ ...(reason !== undefined ? { reason } : {})
18
+ }));
19
+ });
69
20
  }
70
21
  function normalizeWsUrls(raw) {
71
22
  if (raw === undefined)
@@ -83,20 +34,7 @@ function normalizeWsUrls(raw) {
83
34
  }
84
35
  const deduped = new Set();
85
36
  for (const item of raw) {
86
- if (typeof item !== 'string' || item.length === 0) {
87
- throw new TypeError('Invalid WebSocket signaling URL');
88
- }
89
- let parsed;
90
- try {
91
- parsed = new URL(item);
92
- }
93
- catch {
94
- throw new TypeError('Invalid WebSocket signaling URL');
95
- }
96
- if (parsed.protocol !== 'ws:' && parsed.protocol !== 'wss:') {
97
- throw new TypeError('WebSocket signaling URL must use ws: or wss:');
98
- }
99
- deduped.add(parsed.toString());
37
+ deduped.add(normalizeWsRelayUrl(item, 'Invalid WebSocket signaling URL', 'WebSocket signaling URL must use ws: or wss:'));
100
38
  if (deduped.size > maxOptionalWsUrls) {
101
39
  throw new RangeError(`Too many wsUrls (max ${maxOptionalWsUrls} unique URLs)`);
102
40
  }
@@ -137,8 +75,8 @@ export function publicSignaling(options = {}) {
137
75
  if (mqtt === false && nostr === false) {
138
76
  throw new RangeError('publicSignaling requires at least one public relay transport (mqtt or nostr)');
139
77
  }
140
- validateTimeoutMs(wsHandshakeTimeoutMs, 'wsHandshakeTimeoutMs');
141
- validateTimeoutMs(handshakeTimeoutMs, 'handshakeTimeoutMs');
78
+ validatePositiveTimeout(wsHandshakeTimeoutMs, 'Invalid wsHandshakeTimeoutMs');
79
+ validatePositiveTimeout(handshakeTimeoutMs, 'Invalid handshakeTimeoutMs');
142
80
  const normalizedWsUrls = normalizeWsUrls(wsUrls);
143
81
  const adapters = [];
144
82
  const maybeObserve = (adapter, kind, url) => onTransportChange ? observeTransport(adapter, kind, onTransportChange, url) : adapter;
@@ -190,6 +128,16 @@ export function publicSignaling(options = {}) {
190
128
  });
191
129
  };
192
130
  return localTabs
193
- ? withLocalTabsInternal(pooledNetwork, onTransportChange ? (tabs) => observeTransport(tabs, 'tabs', onTransportChange) : undefined)
131
+ ? withLocalTabs(pooledNetwork, onTransportChange
132
+ ? {
133
+ onTabsState(state, reason) {
134
+ onTransportChange(Object.freeze({
135
+ transport: 'tabs',
136
+ state,
137
+ ...(reason !== undefined ? { reason } : {})
138
+ }));
139
+ }
140
+ }
141
+ : undefined)
194
142
  : pooledNetwork;
195
143
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,399 @@
1
+ import { createReplayGuard, deriveSignalingKeys, maxMidLength, openPayload, sealPayload } from './crypto.js';
2
+ import { createErrorThrottle } from './presence.js';
3
+ import { isUuid } from './signaling.js';
4
+ const leaseSweepIntervalMs = 5_000;
5
+ const byeFlushTimeoutMs = 250;
6
+ const encoder = new TextEncoder();
7
+ const decoder = new TextDecoder();
8
+ /** @internal */
9
+ export const maxDecryptQueueDepth = 64;
10
+ /** @internal */
11
+ export function safeCloseSocket(ws) {
12
+ if (!ws)
13
+ return;
14
+ try {
15
+ ws.close();
16
+ }
17
+ catch {
18
+ // Ignore close errors.
19
+ }
20
+ }
21
+ /** @internal */
22
+ export function validatePositiveTimeout(value, errorMessage) {
23
+ if (!Number.isSafeInteger(value) || value <= 0 || value > 2_147_483_647) {
24
+ throw new RangeError(errorMessage);
25
+ }
26
+ }
27
+ /** @internal */
28
+ export function normalizeWsRelayUrl(raw, invalidMessage, protocolMessage) {
29
+ if (typeof raw !== 'string' || raw.length === 0)
30
+ throw new TypeError(invalidMessage);
31
+ let parsed;
32
+ try {
33
+ parsed = new URL(raw);
34
+ }
35
+ catch {
36
+ throw new TypeError(invalidMessage);
37
+ }
38
+ if (parsed.protocol !== 'ws:' && parsed.protocol !== 'wss:') {
39
+ throw new TypeError(protocolMessage);
40
+ }
41
+ return parsed.toString();
42
+ }
43
+ /** @internal */
44
+ export function validateCommonSignalingOptions(options) {
45
+ if (options.password !== undefined &&
46
+ (typeof options.password !== 'string' || options.password.length === 0)) {
47
+ throw new TypeError('Invalid password');
48
+ }
49
+ if (options.privateTopic !== undefined && typeof options.privateTopic !== 'boolean') {
50
+ throw new TypeError('Invalid privateTopic option');
51
+ }
52
+ if (options.WebSocket !== undefined && typeof options.WebSocket !== 'function') {
53
+ throw new TypeError('Invalid WebSocket constructor');
54
+ }
55
+ }
56
+ /** @internal */
57
+ export function resolveSessionWebSocket(custom) {
58
+ const WsCtor = custom ?? globalThis.WebSocket;
59
+ if (typeof WsCtor !== 'function') {
60
+ throw new Error('WebSocket is not available in this runtime; pass options.WebSocket');
61
+ }
62
+ return WsCtor;
63
+ }
64
+ /** @internal */
65
+ export function resolveSessionPassword(password, privateTopic, encrypt = true, insecureMessage = 'Password encryption requires a secure context (crypto.subtle)') {
66
+ if (privateTopic && password === undefined) {
67
+ throw new TypeError('privateTopic requires a room password');
68
+ }
69
+ if (privateTopic && !encrypt) {
70
+ throw new Error('privateTopic requires encrypt: true');
71
+ }
72
+ const useEncryption = password !== undefined && encrypt;
73
+ if (password !== undefined) {
74
+ if (typeof password !== 'string' || password.length === 0) {
75
+ throw new TypeError('Invalid password');
76
+ }
77
+ if (useEncryption && !globalThis.crypto?.subtle) {
78
+ throw new Error(insecureMessage);
79
+ }
80
+ }
81
+ return { password, useEncryption };
82
+ }
83
+ /** @internal */
84
+ export function startKeyDerivation(options) {
85
+ const { keysPromise, privateTopic, isClosed, onResolvedKey, onPrivateKeys, onConnect, onError } = options;
86
+ if (keysPromise && privateTopic) {
87
+ void keysPromise
88
+ .then((keys) => {
89
+ if (isClosed())
90
+ return;
91
+ onResolvedKey(keys.aeadKey);
92
+ onPrivateKeys?.(keys);
93
+ onConnect();
94
+ })
95
+ .catch(() => {
96
+ if (isClosed())
97
+ return;
98
+ onError();
99
+ });
100
+ return;
101
+ }
102
+ if (keysPromise) {
103
+ void keysPromise
104
+ .then((keys) => {
105
+ onResolvedKey(keys.aeadKey);
106
+ })
107
+ .catch(() => {
108
+ if (isClosed())
109
+ return;
110
+ onError();
111
+ });
112
+ }
113
+ // Defer initial connect so synchronous errors never fire before callers attach listeners.
114
+ queueMicrotask(() => {
115
+ if (!isClosed())
116
+ onConnect();
117
+ });
118
+ }
119
+ /** @internal */
120
+ export function createRelayEnvelope(options) {
121
+ const { discovery, password, baseScope, transportLabel } = options;
122
+ const replayGuard = createReplayGuard();
123
+ const emitError = createErrorThrottle((code, message) => discovery.error?.(code, message));
124
+ const keysPromise = password !== undefined ? deriveSignalingKeys(password, baseScope) : undefined;
125
+ let resolvedKey;
126
+ let msgSeq = 0;
127
+ let pendingDecryptCount = 0;
128
+ const emitDecryptFailed = () => emitError('decrypt-failed', `Failed to decrypt ${transportLabel} signaling message`);
129
+ const emitClockSkew = () => emitError('clock-skew', `${transportLabel} signaling message rejected due to system clock skew (>60s)`);
130
+ const nextMid = () => discovery.self.slice(0, 8) + ':' + ++msgSeq;
131
+ const buildJson = (body, fixedMid) => JSON.stringify({ ...body, from: discovery.self, mid: fixedMid ?? nextMid(), ts: Date.now() });
132
+ const getKey = async () => resolvedKey ?? (keysPromise ? (await keysPromise).aeadKey : undefined);
133
+ const parseAndVerifyMessage = (raw, authenticated, now = Date.now()) => {
134
+ let data = null;
135
+ try {
136
+ const text = typeof raw === 'string' ? raw : decoder.decode(raw);
137
+ data = JSON.parse(text);
138
+ }
139
+ catch {
140
+ return null;
141
+ }
142
+ if (!data || !isUuid(data.from) || data.from === discovery.self)
143
+ return null;
144
+ if (data.to !== undefined && data.to !== discovery.self)
145
+ return null;
146
+ if (authenticated) {
147
+ const check = replayGuard.checkAuthenticated(data.from, data, now);
148
+ if (check === 'clock-skew') {
149
+ emitClockSkew();
150
+ return null;
151
+ }
152
+ if (check !== 'ok')
153
+ return null;
154
+ return data;
155
+ }
156
+ if (data.mid !== undefined &&
157
+ (typeof data.mid !== 'string' || data.mid.length === 0 || data.mid.length > maxMidLength)) {
158
+ return null;
159
+ }
160
+ if (typeof data.mid === 'string' && !replayGuard.recordMid(data.from + ':' + data.mid, now)) {
161
+ return null;
162
+ }
163
+ return data;
164
+ };
165
+ return {
166
+ keysPromise,
167
+ isEncrypted: keysPromise !== undefined,
168
+ setResolvedKey(key) {
169
+ resolvedKey = key;
170
+ },
171
+ getKey,
172
+ nextMid,
173
+ buildJson,
174
+ async sealJson(body, aad, fixedMid) {
175
+ const plainJson = buildJson(body, fixedMid);
176
+ const plainBytes = encoder.encode(plainJson);
177
+ try {
178
+ const key = await getKey();
179
+ const bytes = key ? await sealPayload(key, plainBytes, aad) : plainBytes;
180
+ return { plainJson, bytes };
181
+ }
182
+ catch {
183
+ return null;
184
+ }
185
+ },
186
+ async openEncrypted(boxed, aad, opts) {
187
+ if (pendingDecryptCount >= maxDecryptQueueDepth) {
188
+ opts?.onFinally?.();
189
+ return null;
190
+ }
191
+ pendingDecryptCount++;
192
+ try {
193
+ const key = await getKey();
194
+ if (!key || opts?.isClosed?.())
195
+ return null;
196
+ const plain = await openPayload(key, boxed, aad);
197
+ if (opts?.isClosed?.())
198
+ return null;
199
+ if (!plain) {
200
+ emitDecryptFailed();
201
+ return null;
202
+ }
203
+ if (opts?.afterDecrypt && !opts.afterDecrypt())
204
+ return null;
205
+ return parseAndVerifyMessage(plain, true);
206
+ }
207
+ catch {
208
+ return null;
209
+ }
210
+ finally {
211
+ pendingDecryptCount--;
212
+ opts?.onFinally?.();
213
+ }
214
+ },
215
+ parsePlaintext(raw, now) {
216
+ return parseAndVerifyMessage(raw, false, now);
217
+ },
218
+ flushBye({ aad, hasReadyTargets, onPlain, onEncrypted, onDoneNoBye }) {
219
+ const byeJson = buildJson({ type: 'bye' });
220
+ if (!keysPromise) {
221
+ onPlain(byeJson);
222
+ return;
223
+ }
224
+ if (!resolvedKey || !aad || !hasReadyTargets) {
225
+ onDoneNoBye();
226
+ return;
227
+ }
228
+ let settled = false;
229
+ const finishNoBye = () => {
230
+ if (settled)
231
+ return;
232
+ settled = true;
233
+ onDoneNoBye();
234
+ };
235
+ const fallbackTimer = setTimeout(finishNoBye, byeFlushTimeoutMs);
236
+ void sealPayload(resolvedKey, encoder.encode(byeJson), aad)
237
+ .then((boxed) => {
238
+ clearTimeout(fallbackTimer);
239
+ if (settled)
240
+ return;
241
+ settled = true;
242
+ onEncrypted(boxed);
243
+ })
244
+ .catch(() => {
245
+ clearTimeout(fallbackTimer);
246
+ finishNoBye();
247
+ });
248
+ },
249
+ emitDecryptFailed,
250
+ prune(now) {
251
+ replayGuard.prune(now);
252
+ }
253
+ };
254
+ }
255
+ /** @internal */
256
+ export function createRelayPool(config) {
257
+ const { discovery, urls, handshakeTimeoutMs, offlineReason, envelope, privateTopic } = config;
258
+ const readySockets = new Set();
259
+ const allSockets = new Set();
260
+ const reconnectTimers = new Set();
261
+ let closed = false;
262
+ let reportedOnline = false;
263
+ let reportedOffline = false;
264
+ let initialPending = urls.length;
265
+ const updateStatus = () => {
266
+ if (closed)
267
+ return;
268
+ const online = readySockets.size > 0;
269
+ if (online && !reportedOnline) {
270
+ reportedOnline = true;
271
+ reportedOffline = false;
272
+ discovery.status('online');
273
+ }
274
+ else if (!online && (reportedOnline || (initialPending === 0 && !reportedOffline))) {
275
+ reportedOnline = false;
276
+ reportedOffline = true;
277
+ discovery.status('offline', offlineReason);
278
+ discovery.error?.('signaling-error', offlineReason);
279
+ }
280
+ };
281
+ const connectUrl = (url, attempt = 0, isInitial = true) => {
282
+ if (closed)
283
+ return;
284
+ let settledInitial = !isInitial;
285
+ const settleInitial = () => {
286
+ if (!settledInitial) {
287
+ settledInitial = true;
288
+ initialPending = Math.max(0, initialPending - 1);
289
+ }
290
+ };
291
+ let ws;
292
+ try {
293
+ ws = config.createSocket(url);
294
+ }
295
+ catch {
296
+ settleInitial();
297
+ updateStatus();
298
+ return;
299
+ }
300
+ allSockets.add(ws);
301
+ let ready = false;
302
+ let cleaned = false;
303
+ const abort = () => safeCloseSocket(ws);
304
+ let handshakeTimer = setTimeout(() => {
305
+ if (!ready)
306
+ abort();
307
+ }, handshakeTimeoutMs);
308
+ const detach = config.attachSocket(ws, {
309
+ isInitial,
310
+ abort,
311
+ markReady() {
312
+ if (closed || ready)
313
+ return;
314
+ ready = true;
315
+ clearTimeout(handshakeTimer);
316
+ handshakeTimer = undefined;
317
+ attempt = 0;
318
+ settleInitial();
319
+ readySockets.add(ws);
320
+ updateStatus();
321
+ config.onReady(ws, isInitial);
322
+ }
323
+ });
324
+ const cleanup = () => {
325
+ if (cleaned)
326
+ return;
327
+ cleaned = true;
328
+ clearTimeout(handshakeTimer);
329
+ handshakeTimer = undefined;
330
+ detach();
331
+ allSockets.delete(ws);
332
+ const wasReady = readySockets.delete(ws);
333
+ settleInitial();
334
+ if (wasReady || initialPending === 0)
335
+ updateStatus();
336
+ if (!closed) {
337
+ const base = Math.min(15_000, 1_000 * 2 ** Math.min(attempt, 4));
338
+ const delay = Math.round(base * (0.75 + Math.random() * 0.5));
339
+ const timer = setTimeout(() => {
340
+ reconnectTimers.delete(timer);
341
+ connectUrl(url, attempt + 1, false);
342
+ }, delay);
343
+ reconnectTimers.add(timer);
344
+ }
345
+ };
346
+ ws.onerror = abort;
347
+ ws.onclose = cleanup;
348
+ };
349
+ const leaseTimer = setInterval(() => {
350
+ if (closed)
351
+ return;
352
+ config.onSweep?.(Date.now(), readySockets.size > 0);
353
+ }, leaseSweepIntervalMs);
354
+ startKeyDerivation({
355
+ keysPromise: envelope.keysPromise,
356
+ privateTopic,
357
+ isClosed: () => closed,
358
+ onResolvedKey: envelope.setResolvedKey,
359
+ ...(config.onPrivateKeys ? { onPrivateKeys: config.onPrivateKeys } : {}),
360
+ onConnect() {
361
+ for (const url of urls)
362
+ connectUrl(url);
363
+ },
364
+ onError() {
365
+ initialPending = 0;
366
+ for (const ws of [...allSockets])
367
+ safeCloseSocket(ws);
368
+ updateStatus();
369
+ }
370
+ });
371
+ const session = {
372
+ trickle: true,
373
+ send(to, signal) {
374
+ if (closed || readySockets.size === 0)
375
+ return false;
376
+ config.sendSignal(to, signal);
377
+ return true;
378
+ },
379
+ close() {
380
+ if (closed)
381
+ return;
382
+ closed = true;
383
+ clearInterval(leaseTimer);
384
+ for (const timer of reconnectTimers)
385
+ clearTimeout(timer);
386
+ reconnectTimers.clear();
387
+ const socketsToClose = [...allSockets];
388
+ const readyToNotify = [...readySockets];
389
+ allSockets.clear();
390
+ readySockets.clear();
391
+ config.onClose(socketsToClose, readyToNotify);
392
+ }
393
+ };
394
+ return {
395
+ isClosed: () => closed,
396
+ readySockets,
397
+ session
398
+ };
399
+ }
@@ -0,0 +1 @@
1
+ export {};