@johnhenry/browsermesh-core 0.2.1 → 0.2.3

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.2.1",
3
+ "version": "0.2.3",
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
@@ -316,9 +316,19 @@ export class SignalingClient {
316
316
  if (!this.#ws) {
317
317
  throw new Error('SignalingClient not connected')
318
318
  }
319
+ // The real deployed signaling server (browsermesh-servers/signaling)
320
+ // forwards offer/answer/ice-candidate/signal messages by their
321
+ // "target" field, not "to" -- confirmed directly against that
322
+ // server's own source (`const {target, ...payload} = msg`). A `to`
323
+ // field is simply ignored server-side, so every forwarded message
324
+ // was rejected with "forwarded messages require a target field"
325
+ // before this fix. `from` is sent for backward-compatible local
326
+ // debugging/logging only -- the server doesn't read or forward it
327
+ // as-is; see #fire()'s own comment for why the receiving side no
328
+ // longer trusts it.
319
329
  const msg = JSON.stringify({
320
330
  from: this.#localPodId,
321
- to: remotePodId,
331
+ target: remotePodId,
322
332
  type,
323
333
  ...payload,
324
334
  })
@@ -372,8 +382,18 @@ export class SignalingClient {
372
382
  #fire(event, data) {
373
383
  const set = this.#listeners.get(event)
374
384
  if (!set) return
385
+ // The real signaling server stamps forwarded peer-to-peer messages
386
+ // with its own "source" field, based on which authenticated
387
+ // WebSocket connection the message actually arrived on -- that's
388
+ // the trustworthy value. `from` is whatever the ORIGINAL sender
389
+ // self-declared in their own outgoing payload and the server never
390
+ // validates it before forwarding, so preferring it here would mean
391
+ // trusting an arbitrary, spoofable claim over the server's own
392
+ // attribution. Falls back to `from` only for non-forwarded,
393
+ // direct server messages (e.g. `registered`) that have no `source`.
394
+ const fromPodId = data.source ?? data.from
375
395
  for (const cb of set) {
376
- try { cb(data, data.from) } catch (e) { silentCatch('clawser-mesh-handshake', 'swallow-listener-errors', e) }
396
+ try { cb(data, fromPodId) } catch (e) { silentCatch('clawser-mesh-handshake', 'swallow-listener-errors', e) }
377
397
  }
378
398
  }
379
399
  }
@@ -796,6 +816,16 @@ export class HandshakeCoordinator {
796
816
  ])
797
817
  clearTimeout(timeoutHandle)
798
818
 
819
+ // TransportFactory.negotiate() returns a transport that has been
820
+ // created but not yet connected (see its docstring) -- no offer,
821
+ // answer or ICE candidate has crossed the wire yet. Mirror
822
+ // acceptConnection()'s `await transport.handleOffer(offer)` on the
823
+ // answerer side by actually driving the handshake here before this
824
+ // session is adopted as 'connected'.
825
+ if (typeof transport.connect === 'function') {
826
+ await transport.connect()
827
+ }
828
+
799
829
  const sessionInfo = {
800
830
  localPodId: this.#localPodId,
801
831
  remotePodId,
package/src/hardening.mjs CHANGED
@@ -540,9 +540,16 @@ export class ConnectionPool {
540
540
  *
541
541
  * @param {string} peerId
542
542
  * @param {object} transport
543
+ * @param {object} [opts]
544
+ * @param {*} [opts.purpose] - Opaque tag (issue #116) recorded alongside
545
+ * this transport so a later `acquire(peerId, { purpose })` can request
546
+ * "the connection for purpose X" specifically, instead of getting
547
+ * whichever idle entry happens to be first. Untagged (the default) if
548
+ * omitted -- only matches an `acquire()` call that also omits `purpose`/
549
+ * `select`, same as every call before this option existed.
543
550
  * @returns {boolean} true if added successfully
544
551
  */
545
- add(peerId, transport) {
552
+ add(peerId, transport, { purpose } = {}) {
546
553
  if (!this.#pools.has(peerId)) {
547
554
  this.#pools.set(peerId, []);
548
555
  }
@@ -564,6 +571,7 @@ export class ConnectionPool {
564
571
  transport,
565
572
  lastUsed: this.#nowFn(),
566
573
  acquired: false,
574
+ purpose: purpose ?? null,
567
575
  });
568
576
 
569
577
  return true;
@@ -571,16 +579,37 @@ export class ConnectionPool {
571
579
 
572
580
  /**
573
581
  * Acquire an idle connection for a peer. Marks it as acquired.
574
- * Returns null if no idle connections exist.
582
+ * Returns null if no idle (matching, if a selector is given) connections
583
+ * exist.
584
+ *
585
+ * Without a selector, returns the first idle entry -- opaque round-robin
586
+ * reuse, exactly the pre-#116 behavior. With one, only an idle entry that
587
+ * matches is eligible, which is what makes `add()`'s `purpose` tag (or any
588
+ * property of the transport itself) actually useful for more than that:
589
+ * a caller that pooled e.g. a "bulk" and a "control" connection can now
590
+ * `acquire(peerId, { purpose: 'control' })` and reliably get that one
591
+ * back rather than whichever of the two happened to be idle first.
575
592
  *
576
593
  * @param {string} peerId
594
+ * @param {object} [opts]
595
+ * @param {*} [opts.purpose] - Only consider idle entries `add()` tagged
596
+ * with this exact `purpose` (`Object.is` comparison).
597
+ * @param {(transport: object, purpose: *) => boolean} [opts.select] -
598
+ * Only consider idle entries for which this returns true. Combined with
599
+ * `purpose` (both must pass) when both are given, so a caller can filter
600
+ * by purpose and by the transport's own state/shape at once.
577
601
  * @returns {object|null} The transport, or null
578
602
  */
579
- acquire(peerId) {
603
+ acquire(peerId, { purpose, select } = {}) {
580
604
  const entries = this.#pools.get(peerId);
581
605
  if (!entries) return null;
582
606
 
583
- const entry = entries.find(e => !e.acquired);
607
+ const entry = entries.find((e) => {
608
+ if (e.acquired) return false;
609
+ if (purpose !== undefined && !Object.is(e.purpose, purpose)) return false;
610
+ if (typeof select === 'function' && !select(e.transport, e.purpose)) return false;
611
+ return true;
612
+ });
584
613
  if (!entry) return null;
585
614
 
586
615
  entry.acquired = true;
@@ -724,6 +753,7 @@ export class ConnectionPool {
724
753
  acquired: e.acquired,
725
754
  lastUsed: e.lastUsed,
726
755
  transportType: e.transport.type ?? 'unknown',
756
+ purpose: e.purpose ?? null,
727
757
  }));
728
758
  }
729
759
  return {