@johnhenry/browsermesh-core 0.2.2 → 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 +1 -1
- package/src/handshake.mjs +10 -0
- package/src/hardening.mjs +34 -4
package/package.json
CHANGED
package/src/handshake.mjs
CHANGED
|
@@ -816,6 +816,16 @@ export class HandshakeCoordinator {
|
|
|
816
816
|
])
|
|
817
817
|
clearTimeout(timeoutHandle)
|
|
818
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
|
+
|
|
819
829
|
const sessionInfo = {
|
|
820
830
|
localPodId: this.#localPodId,
|
|
821
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
|
|
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 =>
|
|
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 {
|