@johnhenry/browsermesh-transport 0.4.0 → 0.4.1
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 +2 -2
- package/src/websocket.mjs +63 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnhenry/browsermesh-transport",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "WebSocket, WebRTC, WebTransport, and relay adapters for BrowserMesh",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"directory": "packages/browsermesh-transport"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"@johnhenry/browsermesh-primitives": ">=0.
|
|
22
|
+
"@johnhenry/browsermesh-primitives": ">=0.3.0 <1.0.0"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"node-datachannel": "^0.33.4"
|
package/src/websocket.mjs
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { silentCatch } from './silent-catch.mjs'
|
|
2
2
|
import { encodeWireData } from './wire-data.mjs'
|
|
3
3
|
import { resolveIceTransportPolicy } from './webrtc.mjs'
|
|
4
|
-
|
|
5
|
-
// but this package's peer range still admits 0.2.x, and a missing named export
|
|
6
|
-
// would fail to link (breaking the whole transport package) instead of only `padding`.
|
|
7
|
-
import * as primitives from '@johnhenry/browsermesh-primitives'
|
|
4
|
+
import { padTo, unpad } from '@johnhenry/browsermesh-primitives';
|
|
8
5
|
/**
|
|
9
6
|
// STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
|
|
10
7
|
* clawser-mesh-websocket.js -- WebSocket, WebRTC & WebTransport Adapters.
|
|
@@ -49,14 +46,6 @@ function byteLength(data) {
|
|
|
49
46
|
return 0;
|
|
50
47
|
}
|
|
51
48
|
|
|
52
|
-
/** The padding functions, or a clear error when primitives predates 0.3.0. */
|
|
53
|
-
function paddingFns() {
|
|
54
|
-
if (typeof primitives.padTo !== 'function' || typeof primitives.unpad !== 'function') {
|
|
55
|
-
throw new Error('padding requires @johnhenry/browsermesh-primitives >= 0.3.0');
|
|
56
|
-
}
|
|
57
|
-
return primitives;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
49
|
/** First byte of a padded frame: the payload is raw bytes. */
|
|
61
50
|
const PAD_FRAME_BINARY = 0;
|
|
62
51
|
/** First byte of a padded frame: the payload is UTF-8 text. */
|
|
@@ -87,7 +76,7 @@ function padFrame(wire, padding) {
|
|
|
87
76
|
const framed = new Uint8Array(payload.length + 1);
|
|
88
77
|
framed[0] = kind;
|
|
89
78
|
framed.set(payload, 1);
|
|
90
|
-
return
|
|
79
|
+
return padTo(framed, padding);
|
|
91
80
|
}
|
|
92
81
|
|
|
93
82
|
/**
|
|
@@ -102,7 +91,7 @@ function unpadFrame(data) {
|
|
|
102
91
|
if (data instanceof ArrayBuffer) bytes = new Uint8Array(data);
|
|
103
92
|
else if (ArrayBuffer.isView(data)) bytes = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
104
93
|
else throw new Error('padding is enabled but a non-binary frame arrived (is the peer padding too?)');
|
|
105
|
-
const framed =
|
|
94
|
+
const framed = unpad(bytes);
|
|
106
95
|
if (framed.length < 1) throw new Error('padded frame is empty');
|
|
107
96
|
const body = framed.subarray(1);
|
|
108
97
|
if (framed[0] === PAD_FRAME_TEXT) return new TextDecoder().decode(body);
|
|
@@ -213,7 +202,6 @@ export class WebSocketTransport {
|
|
|
213
202
|
throw new TypeError('padding must be a boolean or { buckets }');
|
|
214
203
|
}
|
|
215
204
|
this.#padding = opts.padding ? (opts.padding === true ? {} : { buckets: opts.padding.buckets }) : false;
|
|
216
|
-
if (this.#padding) paddingFns();
|
|
217
205
|
this.#jitterMs = opts.jitterMs ?? 0;
|
|
218
206
|
this.#url = opts.url;
|
|
219
207
|
this.#protocols = opts.protocols || [];
|
|
@@ -603,6 +591,15 @@ export class WebRTCTransport {
|
|
|
603
591
|
/** @type {{ open: Function[], message: Function[], close: Function[], error: Function[], 'ice-candidate': Function[] }} */
|
|
604
592
|
#callbacks = { open: [], message: [], close: [], error: [], 'ice-candidate': [] };
|
|
605
593
|
|
|
594
|
+
/**
|
|
595
|
+
* Unsubscribe functions for the listeners this transport put on the shared
|
|
596
|
+
* signaler (when the signaler returns them), so a finished or closed
|
|
597
|
+
* transport stops receiving -- and acting on -- every later answer and ICE
|
|
598
|
+
* candidate on that signaler.
|
|
599
|
+
* @type {Function[]}
|
|
600
|
+
*/
|
|
601
|
+
#signalerOffs = [];
|
|
602
|
+
|
|
606
603
|
/** @type {{ messagesSent: number, messagesReceived: number, bytesIn: number, bytesOut: number, iceState: string }} */
|
|
607
604
|
#stats = { messagesSent: 0, messagesReceived: 0, bytesIn: 0, bytesOut: 0, iceState: 'new' };
|
|
608
605
|
|
|
@@ -652,6 +649,40 @@ export class WebRTCTransport {
|
|
|
652
649
|
/** Remote pod identifier. */
|
|
653
650
|
get remotePodId() { return this.#remotePodId; }
|
|
654
651
|
|
|
652
|
+
|
|
653
|
+
/**
|
|
654
|
+
* True when a signaling message came from this transport's own remote peer.
|
|
655
|
+
*
|
|
656
|
+
* A signaler is shared: one SignalingClient serves every negotiation a pod
|
|
657
|
+
* has in flight, and it hands EVERY answer and ICE candidate to EVERY
|
|
658
|
+
* listener along with the sender's podId. A transport that ignores that
|
|
659
|
+
* second argument applies a third peer's answer to its own
|
|
660
|
+
* RTCPeerConnection (and the right answer, arriving later, then fails with
|
|
661
|
+
* "wrong state: stable") -- with three pods, one link comes up one-way and
|
|
662
|
+
* a pod can end with no sessions. A signaler that does not say who a
|
|
663
|
+
* message is from (a bare two-party channel) is trusted as before.
|
|
664
|
+
*
|
|
665
|
+
* @param {string|undefined|null} fromPodId
|
|
666
|
+
* @returns {boolean}
|
|
667
|
+
*/
|
|
668
|
+
#isFromRemote(fromPodId) {
|
|
669
|
+
return fromPodId === undefined || fromPodId === null || fromPodId === this.#remotePodId;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
/** Remember a signaler subscription's unsubscribe function, if it returned one. */
|
|
673
|
+
#track(off) {
|
|
674
|
+
if (typeof off === 'function') this.#signalerOffs.push(off);
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/** Stop listening on the shared signaler. Idempotent. */
|
|
678
|
+
#detachSignaler() {
|
|
679
|
+
const offs = this.#signalerOffs;
|
|
680
|
+
this.#signalerOffs = [];
|
|
681
|
+
for (const off of offs) {
|
|
682
|
+
try { off(); } catch (e) { silentCatch('clawser-mesh-websocket', 'signaler-unsubscribe', e) }
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
|
|
655
686
|
// -- Public API ------------------------------------------------------------
|
|
656
687
|
|
|
657
688
|
/**
|
|
@@ -681,13 +712,15 @@ export class WebRTCTransport {
|
|
|
681
712
|
if (this.#pc.connectionState === 'failed' || this.#pc.connectionState === 'closed') {
|
|
682
713
|
if (this.#state !== 'closed' && this.#state !== 'closing') {
|
|
683
714
|
this.#state = 'closed';
|
|
715
|
+
this.#detachSignaler();
|
|
684
716
|
this._fireEvent('close');
|
|
685
717
|
}
|
|
686
718
|
}
|
|
687
719
|
});
|
|
688
720
|
|
|
689
721
|
// Set up signaler listeners for remote ICE candidates
|
|
690
|
-
this.#signaler.onIceCandidate((data) => {
|
|
722
|
+
this.#track(this.#signaler.onIceCandidate((data, fromPodId) => {
|
|
723
|
+
if (!this.#isFromRemote(fromPodId)) return;
|
|
691
724
|
if (this.#pc) {
|
|
692
725
|
// The promise must not be dropped. addIceCandidate rejects on a
|
|
693
726
|
// malformed candidate and on one that arrives before the remote
|
|
@@ -699,7 +732,7 @@ export class WebRTCTransport {
|
|
|
699
732
|
silentCatch('clawser-mesh-websocket', 'ignore-rejected-ice-candidate', e);
|
|
700
733
|
});
|
|
701
734
|
}
|
|
702
|
-
});
|
|
735
|
+
}));
|
|
703
736
|
|
|
704
737
|
// Create data channels and offer. A second createDataChannel() call on
|
|
705
738
|
// the same RTCPeerConnection is a second SCTP stream, not a second
|
|
@@ -722,7 +755,13 @@ export class WebRTCTransport {
|
|
|
722
755
|
reject(new Error('WebRTC answer timeout'));
|
|
723
756
|
}, 30000);
|
|
724
757
|
|
|
725
|
-
|
|
758
|
+
// eslint-disable-next-line prefer-const
|
|
759
|
+
let offAnswer;
|
|
760
|
+
offAnswer = this.#signaler.onAnswer(async (data, fromPodId) => {
|
|
761
|
+
// Not ours: another negotiation's answer on the shared signaler.
|
|
762
|
+
if (!this.#isFromRemote(fromPodId)) return;
|
|
763
|
+
// An offer gets one answer; a repeat must not re-apply to a stable pc.
|
|
764
|
+
if (typeof offAnswer === 'function') offAnswer();
|
|
726
765
|
clearTimeout(timeout);
|
|
727
766
|
try {
|
|
728
767
|
await this.#pc.setRemoteDescription(data.answer);
|
|
@@ -746,6 +785,7 @@ export class WebRTCTransport {
|
|
|
746
785
|
this.#dataChannel.addEventListener('open', onDCOpen);
|
|
747
786
|
}
|
|
748
787
|
});
|
|
788
|
+
this.#track(offAnswer);
|
|
749
789
|
});
|
|
750
790
|
}
|
|
751
791
|
|
|
@@ -780,12 +820,14 @@ export class WebRTCTransport {
|
|
|
780
820
|
if (this.#pc.connectionState === 'failed' || this.#pc.connectionState === 'closed') {
|
|
781
821
|
if (this.#state !== 'closed' && this.#state !== 'closing') {
|
|
782
822
|
this.#state = 'closed';
|
|
823
|
+
this.#detachSignaler();
|
|
783
824
|
this._fireEvent('close');
|
|
784
825
|
}
|
|
785
826
|
}
|
|
786
827
|
});
|
|
787
828
|
|
|
788
|
-
this.#signaler.onIceCandidate((data) => {
|
|
829
|
+
this.#track(this.#signaler.onIceCandidate((data, fromPodId) => {
|
|
830
|
+
if (!this.#isFromRemote(fromPodId)) return;
|
|
789
831
|
if (this.#pc) {
|
|
790
832
|
// See connect()'s identical handler: the promise must not be
|
|
791
833
|
// dropped, for the same reason.
|
|
@@ -793,7 +835,7 @@ export class WebRTCTransport {
|
|
|
793
835
|
silentCatch('clawser-mesh-websocket', 'ignore-rejected-ice-candidate', e);
|
|
794
836
|
});
|
|
795
837
|
}
|
|
796
|
-
});
|
|
838
|
+
}));
|
|
797
839
|
}
|
|
798
840
|
this.#state = 'connecting';
|
|
799
841
|
|
|
@@ -891,6 +933,7 @@ export class WebRTCTransport {
|
|
|
891
933
|
this.#pc.close();
|
|
892
934
|
}
|
|
893
935
|
this.#state = 'closed';
|
|
936
|
+
this.#detachSignaler();
|
|
894
937
|
this._fireEvent('close');
|
|
895
938
|
}
|
|
896
939
|
|