@johnhenry/browsermesh-core 0.7.0 → 0.8.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-core",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Identity, crypto, peer management, and trust primitives for BrowserMesh",
5
5
  "type": "module",
6
6
  "main": "./src/index.mjs",
@@ -20,7 +20,7 @@
20
20
  "directory": "packages/browsermesh-core"
21
21
  },
22
22
  "peerDependencies": {
23
- "@johnhenry/browsermesh-primitives": ">=0.2.0 <1.0.0",
23
+ "@johnhenry/browsermesh-primitives": ">=0.3.0 <1.0.0",
24
24
  "@johnhenry/browsermesh-apps": ">=0.4.0 <1.0.0"
25
25
  },
26
26
  "peerDependenciesMeta": {
@@ -13,17 +13,7 @@
13
13
  * node --import ./web/test/_setup-globals.mjs --test web/test/clawser-mesh-group-keys.test.mjs
14
14
  */
15
15
 
16
- // Namespace import, not `import { padTo, unpad }`: padding needs primitives >= 0.3.0,
17
- // but this package's peer range still admits 0.2.x, and a missing named export
18
- // would fail to link (breaking all of core) instead of only breaking `padding`.
19
- import * as primitives from '@johnhenry/browsermesh-primitives'
20
-
21
- function paddingFns() {
22
- if (typeof primitives.padTo !== 'function' || typeof primitives.unpad !== 'function') {
23
- throw new Error('padding requires @johnhenry/browsermesh-primitives >= 0.3.0')
24
- }
25
- return primitives
26
- }
16
+ import { padTo, unpad } from '@johnhenry/browsermesh-primitives'
27
17
 
28
18
  // ---------------------------------------------------------------------------
29
19
  // Wire constants
@@ -545,7 +535,7 @@ export class GroupKeyManager {
545
535
  if (!state || !state.key) throw new Error('No active group key')
546
536
 
547
537
  const pad = padding === undefined ? this.#padding : normalizePadding(padding)
548
- const sealed = pad ? paddingFns().padTo(plaintext, pad) : plaintext
538
+ const sealed = pad ? padTo(plaintext, pad) : plaintext
549
539
 
550
540
  const iv = crypto.getRandomValues(new Uint8Array(12))
551
541
  const ciphertext = new Uint8Array(
@@ -584,7 +574,7 @@ export class GroupKeyManager {
584
574
  )
585
575
 
586
576
  const pad = padding === undefined ? this.#padding : normalizePadding(padding)
587
- return pad ? paddingFns().unpad(plaintext) : plaintext
577
+ return pad ? unpad(plaintext) : plaintext
588
578
  }
589
579
 
590
580
  /**
package/src/handshake.mjs CHANGED
@@ -282,25 +282,28 @@ export class SignalingClient {
282
282
  /**
283
283
  * Register callback for incoming SDP offers.
284
284
  * @param {Function} cb - Receives (offer, fromPodId)
285
+ * @returns {() => void} Unsubscribe
285
286
  */
286
287
  onOffer(cb) {
287
- this.on('offer', cb)
288
+ return this.on('offer', cb)
288
289
  }
289
290
 
290
291
  /**
291
292
  * Register callback for incoming SDP answers.
292
293
  * @param {Function} cb - Receives (answer, fromPodId)
294
+ * @returns {() => void} Unsubscribe
293
295
  */
294
296
  onAnswer(cb) {
295
- this.on('answer', cb)
297
+ return this.on('answer', cb)
296
298
  }
297
299
 
298
300
  /**
299
301
  * Register callback for incoming ICE candidates.
300
302
  * @param {Function} cb - Receives (candidate, fromPodId)
303
+ * @returns {() => void} Unsubscribe
301
304
  */
302
305
  onIceCandidate(cb) {
303
- this.on('ice-candidate', cb)
306
+ return this.on('ice-candidate', cb)
304
307
  }
305
308
 
306
309
  // -- Generic send / event system ------------------------------------------
@@ -339,12 +342,14 @@ export class SignalingClient {
339
342
  * Register a listener for a given event type.
340
343
  * @param {string} event
341
344
  * @param {Function} cb
345
+ * @returns {() => void} Unsubscribe (same as `off(event, cb)`)
342
346
  */
343
347
  on(event, cb) {
344
348
  if (!this.#listeners.has(event)) {
345
349
  this.#listeners.set(event, new Set())
346
350
  }
347
351
  this.#listeners.get(event).add(cb)
352
+ return () => this.off(event, cb)
348
353
  }
349
354
 
350
355
  /**
@@ -773,14 +778,27 @@ export class HandshakeCoordinator {
773
778
  */
774
779
  #glareWaiters = new Map()
775
780
 
781
+ /** @type {object[]|null} Default ICE servers for every WebRTC transport */
782
+ #iceServers
783
+
784
+ /** @type {'all'|'relay'|undefined} Default iceTransportPolicy */
785
+ #iceTransportPolicy
786
+
776
787
  /**
777
788
  * @param {object} opts
778
789
  * @param {string} opts.localPodId - Local pod identifier
779
790
  * @param {SignalingClient} [opts.signalingClient] - Signaling client instance
780
791
  * @param {object} [opts.transportFactory] - TransportFactory instance
781
792
  * @param {Function} [opts.onLog] - Logging callback (level, msg)
793
+ * @param {object[]} [opts.iceServers] - Default STUN/TURN servers for every
794
+ * WebRTC transport this coordinator creates (offer side, answer side and
795
+ * connectViaToken). A per-call `iceServers` overrides it.
796
+ * @param {'all'|'relay'} [opts.iceTransportPolicy] - Default
797
+ * `iceTransportPolicy` for those transports ('relay' = TURN-only, which
798
+ * needs a `turn:`/`turns:` entry in `iceServers`). A per-call value
799
+ * overrides it.
782
800
  */
783
- constructor({ localPodId, signalingClient, transportFactory, onLog } = {}) {
801
+ constructor({ localPodId, signalingClient, transportFactory, onLog, iceServers, iceTransportPolicy } = {}) {
784
802
  if (!localPodId || typeof localPodId !== 'string') {
785
803
  throw new Error('localPodId is required and must be a non-empty string')
786
804
  }
@@ -788,6 +806,8 @@ export class HandshakeCoordinator {
788
806
  this.#signalingClient = signalingClient || null
789
807
  this.#transportFactory = transportFactory || null
790
808
  this.#onLog = onLog || (() => {})
809
+ this.#iceServers = iceServers || null
810
+ this.#iceTransportPolicy = iceTransportPolicy
791
811
 
792
812
  // Wire incoming offer events from signaling client
793
813
  if (this.#signalingClient) {
@@ -797,6 +817,27 @@ export class HandshakeCoordinator {
797
817
  }
798
818
  }
799
819
 
820
+ /**
821
+ * Resolve the WebRTC transport options carrying ICE configuration.
822
+ * Precedence: per-call value > `fallbackIceServers` (e.g. a token's) >
823
+ * coordinator default. Keys are only present when a value exists, so a
824
+ * coordinator with no ICE config leaves the transport's own defaults
825
+ * (STUN list, 'all' policy) untouched. `config.iceServers` is kept for
826
+ * factories/proxies that read the legacy shape.
827
+ *
828
+ * @param {{ iceServers?: object[], iceTransportPolicy?: string }} [perCall]
829
+ * @param {object[]} [fallbackIceServers]
830
+ * @returns {object}
831
+ */
832
+ #iceOpts(perCall = {}, fallbackIceServers) {
833
+ const iceServers = perCall.iceServers ?? fallbackIceServers ?? this.#iceServers
834
+ const iceTransportPolicy = perCall.iceTransportPolicy ?? this.#iceTransportPolicy
835
+ const out = { config: { iceServers: iceServers || [] } }
836
+ if (iceServers) out.iceServers = iceServers
837
+ if (iceTransportPolicy) out.iceTransportPolicy = iceTransportPolicy
838
+ return out
839
+ }
840
+
800
841
  /**
801
842
  * Initiate a connection to a remote peer via the signaling server.
802
843
  *
@@ -806,7 +847,8 @@ export class HandshakeCoordinator {
806
847
  * @param {string} remotePodId - Target peer identifier
807
848
  * @param {object} [opts]
808
849
  * @param {object} [opts.endpoints] - Map of transport type -> constructor opts
809
- * @param {object[]} [opts.iceServers] - ICE server configs
850
+ * @param {object[]} [opts.iceServers] - ICE server configs (overrides the constructor default)
851
+ * @param {'all'|'relay'} [opts.iceTransportPolicy] - Overrides the constructor default
810
852
  * @param {number} [opts.timeout] - Connection timeout in ms
811
853
  * @returns {Promise<{ transport: object, sessionInfo: object }>}
812
854
  */
@@ -844,9 +886,7 @@ export class HandshakeCoordinator {
844
886
 
845
887
  this.#onLog(2, `Initiating connection to ${remotePodId}`)
846
888
 
847
- const endpointOpts = opts.endpoints || {
848
- webrtc: { config: { iceServers: opts.iceServers || [] } },
849
- }
889
+ const endpointOpts = opts.endpoints || { webrtc: this.#iceOpts(opts) }
850
890
 
851
891
  const promise = (async () => {
852
892
  let timeoutHandle
@@ -899,9 +939,12 @@ export class HandshakeCoordinator {
899
939
  *
900
940
  * @param {string} remotePodId - Initiating peer identifier
901
941
  * @param {object} offer - SDP offer
942
+ * @param {object} [opts]
943
+ * @param {object[]} [opts.iceServers] - Overrides the constructor default
944
+ * @param {'all'|'relay'} [opts.iceTransportPolicy] - Overrides the constructor default
902
945
  * @returns {Promise<{ transport: object, sessionInfo: object }>}
903
946
  */
904
- async acceptConnection(remotePodId, offer) {
947
+ async acceptConnection(remotePodId, offer, opts = {}) {
905
948
  if (!this.#transportFactory) {
906
949
  throw new Error('TransportFactory is required for acceptConnection')
907
950
  }
@@ -935,6 +978,7 @@ export class HandshakeCoordinator {
935
978
  localPodId: this.#localPodId,
936
979
  remotePodId,
937
980
  signaler: this.#signalingClient,
981
+ ...this.#iceOpts(opts),
938
982
  })
939
983
 
940
984
  if (typeof transport.handleOffer === 'function') {
@@ -974,11 +1018,14 @@ export class HandshakeCoordinator {
974
1018
  *
975
1019
  * @param {ConnectionToken} token - Decoded connection token
976
1020
  * @param {object} [transportFactory] - Override transport factory
1021
+ * @param {object} [opts]
1022
+ * @param {object[]} [opts.iceServers] - Overrides token.iceServers and the constructor default
1023
+ * @param {'all'|'relay'} [opts.iceTransportPolicy] - Overrides the constructor default
977
1024
  * @returns {Promise<{ transport: object, sessionInfo: object, signaler: SignalingClient|null }>}
978
1025
  * `signaler` is the client this call created and still owns, or null when
979
1026
  * the coordinator's own signaling client was used.
980
1027
  */
981
- async connectViaToken(token, transportFactory) {
1028
+ async connectViaToken(token, transportFactory, opts = {}) {
982
1029
  const factory = transportFactory || this.#transportFactory
983
1030
  if (!factory) {
984
1031
  throw new Error('TransportFactory is required for connectViaToken')
@@ -1005,9 +1052,8 @@ export class HandshakeCoordinator {
1005
1052
  throw new Error('No signaling channel available (provide signalingClient or token.signalingUrl)')
1006
1053
  }
1007
1054
 
1008
- const iceServers = token.iceServers || []
1009
1055
  const endpointOpts = {
1010
- webrtc: { config: { iceServers } },
1056
+ webrtc: this.#iceOpts(opts, token.iceServers),
1011
1057
  }
1012
1058
 
1013
1059
  try {