@johnhenry/browsermesh-core 0.7.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/handshake.mjs +50 -9
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@johnhenry/browsermesh-core",
3
- "version": "0.7.1",
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",
package/src/handshake.mjs CHANGED
@@ -778,14 +778,27 @@ export class HandshakeCoordinator {
778
778
  */
779
779
  #glareWaiters = new Map()
780
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
+
781
787
  /**
782
788
  * @param {object} opts
783
789
  * @param {string} opts.localPodId - Local pod identifier
784
790
  * @param {SignalingClient} [opts.signalingClient] - Signaling client instance
785
791
  * @param {object} [opts.transportFactory] - TransportFactory instance
786
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.
787
800
  */
788
- constructor({ localPodId, signalingClient, transportFactory, onLog } = {}) {
801
+ constructor({ localPodId, signalingClient, transportFactory, onLog, iceServers, iceTransportPolicy } = {}) {
789
802
  if (!localPodId || typeof localPodId !== 'string') {
790
803
  throw new Error('localPodId is required and must be a non-empty string')
791
804
  }
@@ -793,6 +806,8 @@ export class HandshakeCoordinator {
793
806
  this.#signalingClient = signalingClient || null
794
807
  this.#transportFactory = transportFactory || null
795
808
  this.#onLog = onLog || (() => {})
809
+ this.#iceServers = iceServers || null
810
+ this.#iceTransportPolicy = iceTransportPolicy
796
811
 
797
812
  // Wire incoming offer events from signaling client
798
813
  if (this.#signalingClient) {
@@ -802,6 +817,27 @@ export class HandshakeCoordinator {
802
817
  }
803
818
  }
804
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
+
805
841
  /**
806
842
  * Initiate a connection to a remote peer via the signaling server.
807
843
  *
@@ -811,7 +847,8 @@ export class HandshakeCoordinator {
811
847
  * @param {string} remotePodId - Target peer identifier
812
848
  * @param {object} [opts]
813
849
  * @param {object} [opts.endpoints] - Map of transport type -> constructor opts
814
- * @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
815
852
  * @param {number} [opts.timeout] - Connection timeout in ms
816
853
  * @returns {Promise<{ transport: object, sessionInfo: object }>}
817
854
  */
@@ -849,9 +886,7 @@ export class HandshakeCoordinator {
849
886
 
850
887
  this.#onLog(2, `Initiating connection to ${remotePodId}`)
851
888
 
852
- const endpointOpts = opts.endpoints || {
853
- webrtc: { config: { iceServers: opts.iceServers || [] } },
854
- }
889
+ const endpointOpts = opts.endpoints || { webrtc: this.#iceOpts(opts) }
855
890
 
856
891
  const promise = (async () => {
857
892
  let timeoutHandle
@@ -904,9 +939,12 @@ export class HandshakeCoordinator {
904
939
  *
905
940
  * @param {string} remotePodId - Initiating peer identifier
906
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
907
945
  * @returns {Promise<{ transport: object, sessionInfo: object }>}
908
946
  */
909
- async acceptConnection(remotePodId, offer) {
947
+ async acceptConnection(remotePodId, offer, opts = {}) {
910
948
  if (!this.#transportFactory) {
911
949
  throw new Error('TransportFactory is required for acceptConnection')
912
950
  }
@@ -940,6 +978,7 @@ export class HandshakeCoordinator {
940
978
  localPodId: this.#localPodId,
941
979
  remotePodId,
942
980
  signaler: this.#signalingClient,
981
+ ...this.#iceOpts(opts),
943
982
  })
944
983
 
945
984
  if (typeof transport.handleOffer === 'function') {
@@ -979,11 +1018,14 @@ export class HandshakeCoordinator {
979
1018
  *
980
1019
  * @param {ConnectionToken} token - Decoded connection token
981
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
982
1024
  * @returns {Promise<{ transport: object, sessionInfo: object, signaler: SignalingClient|null }>}
983
1025
  * `signaler` is the client this call created and still owns, or null when
984
1026
  * the coordinator's own signaling client was used.
985
1027
  */
986
- async connectViaToken(token, transportFactory) {
1028
+ async connectViaToken(token, transportFactory, opts = {}) {
987
1029
  const factory = transportFactory || this.#transportFactory
988
1030
  if (!factory) {
989
1031
  throw new Error('TransportFactory is required for connectViaToken')
@@ -1010,9 +1052,8 @@ export class HandshakeCoordinator {
1010
1052
  throw new Error('No signaling channel available (provide signalingClient or token.signalingUrl)')
1011
1053
  }
1012
1054
 
1013
- const iceServers = token.iceServers || []
1014
1055
  const endpointOpts = {
1015
- webrtc: { config: { iceServers } },
1056
+ webrtc: this.#iceOpts(opts, token.iceServers),
1016
1057
  }
1017
1058
 
1018
1059
  try {