@dimchansky/meshpost 0.5.0 → 0.6.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 (46) 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 +15 -16
  6. package/dist/channel.js +15 -44
  7. package/dist/combine.js +5 -24
  8. package/dist/crypto.js +10 -9
  9. package/dist/identity.d.ts +2 -0
  10. package/dist/identity.js +39 -71
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +1 -1
  13. package/dist/listeners.js +18 -3
  14. package/dist/mqtt.js +193 -408
  15. package/dist/nostr.js +163 -745
  16. package/dist/public.d.ts +1 -1
  17. package/dist/public.js +9 -54
  18. package/dist/relay-core.d.ts +1 -0
  19. package/dist/relay-core.js +323 -0
  20. package/dist/room-hello.d.ts +1 -0
  21. package/dist/room-hello.js +129 -0
  22. package/dist/room-ice.d.ts +1 -0
  23. package/dist/room-ice.js +115 -0
  24. package/dist/room-media.d.ts +1 -0
  25. package/dist/room-media.js +477 -0
  26. package/dist/room-reclaim.d.ts +1 -0
  27. package/dist/room-reclaim.js +111 -0
  28. package/dist/room-stats.d.ts +1 -0
  29. package/dist/room-stats.js +75 -0
  30. package/dist/room.js +109 -919
  31. package/dist/signaling.d.ts +8 -0
  32. package/dist/signaling.js +64 -0
  33. package/dist/state.js +1 -2
  34. package/dist/voice-helpers.d.ts +1 -0
  35. package/dist/voice-helpers.js +100 -0
  36. package/dist/voice-meter.d.ts +1 -0
  37. package/dist/voice-meter.js +79 -0
  38. package/dist/voice.d.ts +11 -1
  39. package/dist/voice.js +99 -295
  40. package/dist/ws.js +39 -142
  41. package/docs/api.md +217 -0
  42. package/docs/behavior.md +10 -4
  43. package/docs/guide.md +404 -0
  44. package/docs/signaling-and-production.md +312 -0
  45. package/docs/voice-and-media.md +193 -0
  46. package/package.json +3 -2
package/dist/identity.js CHANGED
@@ -1,6 +1,5 @@
1
- import { toBase64Url } from './crypto.js';
1
+ import { decodeBase64UrlBytes, toBase64Url } from './crypto.js';
2
2
  const encoder = new TextEncoder();
3
- const base64UrlPattern = /^[A-Za-z0-9_-]+$/;
4
3
  const userIdDomainPrefix = encoder.encode('meshpost/1/userid:');
5
4
  const dtlsFingerprintLinePattern = /^a=fingerprint:(sha-256|sha-384|sha-512)\s+([0-9A-Fa-f]{2}(?::[0-9A-Fa-f]{2})+)\s*$/i;
6
5
  const expectedFingerprintHexLength = {
@@ -11,25 +10,10 @@ const expectedFingerprintHexLength = {
11
10
  const identityInternals = new WeakMap();
12
11
  /** @internal */
13
12
  export function decodeCanonicalBase64Url(value, expectedChars, expectedBytes) {
14
- if (typeof value !== 'string' ||
15
- value.length !== expectedChars ||
16
- !base64UrlPattern.test(value)) {
13
+ if (typeof value !== 'string' || value.length !== expectedChars)
17
14
  return null;
18
- }
19
- try {
20
- const base64 = value.replace(/-/g, '+').replace(/_/g, '/');
21
- const padded = base64 + '='.repeat((4 - (base64.length % 4)) % 4);
22
- const binary = atob(padded);
23
- if (binary.length !== expectedBytes)
24
- return null;
25
- const out = new Uint8Array(expectedBytes);
26
- for (let i = 0; i < expectedBytes; i++)
27
- out[i] = binary.charCodeAt(i);
28
- return toBase64Url(out) === value ? out : null;
29
- }
30
- catch {
31
- return null;
32
- }
15
+ const out = decodeBase64UrlBytes(value);
16
+ return out && out.byteLength === expectedBytes && toBase64Url(out) === value ? out : null;
33
17
  }
34
18
  async function deriveUserIdFromRawPublicKey(subtle, rawPub) {
35
19
  const input = new Uint8Array(userIdDomainPrefix.byteLength + rawPub.byteLength);
@@ -121,15 +105,17 @@ export function isSerializedIdentity(value) {
121
105
  */
122
106
  export async function importIdentity(data, options) {
123
107
  const extractable = parseExtractableOption(options);
124
- if (!isSerializedIdentity(data)) {
108
+ if (!data || typeof data !== 'object' || Array.isArray(data)) {
125
109
  throw new TypeError('Invalid serialized identity');
126
110
  }
127
- const subtle = requireSubtleCrypto();
128
- const xBytes = decodeCanonicalBase64Url(data.x, 43, 32);
129
- const yBytes = decodeCanonicalBase64Url(data.y, 43, 32);
130
- if (!xBytes || !yBytes) {
111
+ const rec = data;
112
+ const xBytes = rec.v === 1 && rec.crv === 'P-256' ? decodeCanonicalBase64Url(rec.x, 43, 32) : null;
113
+ const yBytes = xBytes ? decodeCanonicalBase64Url(rec.y, 43, 32) : null;
114
+ const dBytes = yBytes ? decodeCanonicalBase64Url(rec.d, 43, 32) : null;
115
+ if (!xBytes || !yBytes || !dBytes) {
131
116
  throw new TypeError('Invalid serialized identity');
132
117
  }
118
+ const subtle = requireSubtleCrypto();
133
119
  const rawCandidate = new Uint8Array(65);
134
120
  rawCandidate[0] = 0x04;
135
121
  rawCandidate.set(xBytes, 1);
@@ -139,9 +125,9 @@ export async function importIdentity(data, options) {
139
125
  subtle.importKey('jwk', {
140
126
  kty: 'EC',
141
127
  crv: 'P-256',
142
- x: data.x,
143
- y: data.y,
144
- d: data.d,
128
+ x: rec.x,
129
+ y: rec.y,
130
+ d: rec.d,
145
131
  key_ops: ['sign'],
146
132
  ext: extractable
147
133
  }, { name: 'ECDSA', namedCurve: 'P-256' }, extractable, ['sign']),
@@ -340,7 +326,30 @@ export function createIdentityStore(options) {
340
326
  // Ignore restricted-storage errors.
341
327
  }
342
328
  };
329
+ const resolveStale = (fallback, selfPromise) => {
330
+ if (cached)
331
+ return cached;
332
+ if (inFlightRotate && inFlightRotate !== selfPromise)
333
+ return inFlightRotate;
334
+ return fallback;
335
+ };
336
+ const createAndPersist = async (myEpoch, storage, selfPromise) => {
337
+ const fresh = await createIdentity();
338
+ if (myEpoch !== epoch)
339
+ return resolveStale(fresh, selfPromise?.());
340
+ if (storage) {
341
+ const exported = await exportIdentity(fresh);
342
+ if (myEpoch !== epoch)
343
+ return resolveStale(fresh, selfPromise?.());
344
+ safeWrite(storage, exported);
345
+ }
346
+ cached = fresh;
347
+ return fresh;
348
+ };
343
349
  return {
350
+ get current() {
351
+ return cached;
352
+ },
344
353
  load() {
345
354
  if (cached)
346
355
  return Promise.resolve(cached);
@@ -381,27 +390,7 @@ export function createIdentityStore(options) {
381
390
  }
382
391
  safeRemove(storage);
383
392
  }
384
- const fresh = await createIdentity();
385
- if (myEpoch !== epoch) {
386
- if (cached)
387
- return cached;
388
- if (inFlightRotate)
389
- return inFlightRotate;
390
- return fresh;
391
- }
392
- if (storage) {
393
- const exported = await exportIdentity(fresh);
394
- if (myEpoch !== epoch) {
395
- if (cached)
396
- return cached;
397
- if (inFlightRotate)
398
- return inFlightRotate;
399
- return fresh;
400
- }
401
- safeWrite(storage, exported);
402
- }
403
- cached = fresh;
404
- return fresh;
393
+ return createAndPersist(myEpoch, storage);
405
394
  })().finally(() => {
406
395
  if (inFlightLoad === promise)
407
396
  inFlightLoad = null;
@@ -416,28 +405,7 @@ export function createIdentityStore(options) {
416
405
  let promise = null;
417
406
  promise = (async () => {
418
407
  requireSubtleCrypto();
419
- const storage = resolveStorage();
420
- const fresh = await createIdentity();
421
- if (myEpoch !== epoch) {
422
- if (cached)
423
- return cached;
424
- if (inFlightRotate && inFlightRotate !== promise)
425
- return inFlightRotate;
426
- return fresh;
427
- }
428
- if (storage) {
429
- const exported = await exportIdentity(fresh);
430
- if (myEpoch !== epoch) {
431
- if (cached)
432
- return cached;
433
- if (inFlightRotate && inFlightRotate !== promise)
434
- return inFlightRotate;
435
- return fresh;
436
- }
437
- safeWrite(storage, exported);
438
- }
439
- cached = fresh;
440
- return fresh;
408
+ return createAndPersist(myEpoch, resolveStorage(), () => promise);
441
409
  })().finally(() => {
442
410
  if (inFlightRotate === promise)
443
411
  inFlightRotate = null;
package/dist/index.d.ts CHANGED
@@ -11,7 +11,7 @@ export type { PeerState, PeerStateChangeReason, PeerStateOptions } from './state
11
11
  export { createBlobChannel } from './blob.js';
12
12
  export type { BlobChannel, BlobChannelOptions, BlobProgress, BlobSendOptions, BlobSendStatus } from './blob.js';
13
13
  export { createVoice, listAudioInputs, onAudioInputsChange } from './voice.js';
14
- export type { AudioInputDevice, MicError, MicErrorCode, MicStatus, Voice, VoiceOptions, VoiceSnapshot } from './voice.js';
14
+ export type { AudioInputDevice, AudioInputsChangeOptions, MicError, MicErrorCode, MicStatus, Voice, VoiceOptions, VoiceSnapshot } from './voice.js';
15
15
  export { fetchTurnCredentials } from './turn.js';
16
16
  export type { FetchTurnOptions } from './turn.js';
17
17
  export { tabSignaling } from './tabs.js';
@@ -26,5 +26,5 @@ export type { OptionalWsSignalingOptions, WsSignalingOptions } from './ws.js';
26
26
  export { publicSignaling } from './public.js';
27
27
  export type { PublicSignalingOptions, PublicTransportEvent, PublicTransportKind } from './public.js';
28
28
  export { combineSignaling, withLocalTabs } from './combine.js';
29
- export { isSignal, isUuid, withLoopbackFallback } from './signaling.js';
29
+ export { isSignal, isUuid, observeSignaling, withLoopbackFallback } from './signaling.js';
30
30
  export type { Discovery, IceCandidatePayload, Signal, Signaling, SignalingErrorCode, SignalingSession, SignalingState, WebSocketConstructor } from './signaling.js';
package/dist/index.js CHANGED
@@ -13,4 +13,4 @@ export { defaultNostrRelays, nostrSignaling } from './nostr.js';
13
13
  export { optionalWsSignaling, wsSignaling } from './ws.js';
14
14
  export { publicSignaling } from './public.js';
15
15
  export { combineSignaling, withLocalTabs } from './combine.js';
16
- export { isSignal, isUuid, withLoopbackFallback } from './signaling.js';
16
+ export { isSignal, isUuid, observeSignaling, withLoopbackFallback } from './signaling.js';
package/dist/listeners.js CHANGED
@@ -1,4 +1,15 @@
1
1
  const abortHub = new WeakMap();
2
+ /** @internal */
3
+ export function reportAsyncError(error) {
4
+ if (typeof globalThis.reportError === 'function') {
5
+ globalThis.reportError(error);
6
+ }
7
+ else {
8
+ queueMicrotask(() => {
9
+ throw error;
10
+ });
11
+ }
12
+ }
2
13
  /**
3
14
  * Subscribe `fn` to an `AbortSignal` while attaching at most ONE native `'abort'`
4
15
  * listener per `AbortSignal` across all Meshpost primitives (preventing Node.js
@@ -25,7 +36,7 @@ export function bindAbort(signal, fn) {
25
36
  cb();
26
37
  }
27
38
  catch (error) {
28
- globalThis.reportError?.(error);
39
+ reportAsyncError(error);
29
40
  }
30
41
  }
31
42
  };
@@ -50,10 +61,13 @@ export function bindAbort(signal, fn) {
50
61
  };
51
62
  }
52
63
  /** @internal */
53
- export function createListeners() {
64
+ export function createListeners(onRemove) {
54
65
  const entries = new Set();
55
66
  let closed = false;
56
67
  return {
68
+ get size() {
69
+ return entries.size;
70
+ },
57
71
  add(fn, options) {
58
72
  if (typeof fn !== 'function')
59
73
  throw new TypeError('Expected a listener function');
@@ -68,6 +82,7 @@ export function createListeners() {
68
82
  if (!entries.delete(entry))
69
83
  return;
70
84
  unbindAbort?.();
85
+ onRemove?.();
71
86
  }
72
87
  };
73
88
  entries.add(entry);
@@ -84,7 +99,7 @@ export function createListeners() {
84
99
  entry.fn(...args);
85
100
  }
86
101
  catch (error) {
87
- globalThis.reportError?.(error);
102
+ reportAsyncError(error);
88
103
  }
89
104
  }
90
105
  },