@johnhenry/browsermesh-transport 0.4.0 → 0.5.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 +2 -2
- package/src/webrtc.mjs +125 -7
- 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.
|
|
3
|
+
"version": "0.5.0",
|
|
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/webrtc.mjs
CHANGED
|
@@ -195,6 +195,17 @@ const BULK_CHANNEL_LABEL = 'mesh-bulk'
|
|
|
195
195
|
*/
|
|
196
196
|
const BULK_CHANNEL_OPTIONS = Object.freeze({ ordered: false })
|
|
197
197
|
|
|
198
|
+
/**
|
|
199
|
+
* Wire-label prefix of caller-named extra channels (#115). `openChannel('x')`
|
|
200
|
+
* creates a channel labelled `mesh-x:x`. The prefix is what lets a receiver
|
|
201
|
+
* tell an extra channel from the control channel: a label it does not
|
|
202
|
+
* recognise is otherwise adopted AS the control channel (see handleOffer()).
|
|
203
|
+
* Builds that predate this feature do exactly that, so only open named
|
|
204
|
+
* channels to peers known to run a build that has `openChannel()`.
|
|
205
|
+
*/
|
|
206
|
+
const EXTRA_CHANNEL_PREFIX = 'mesh-x:'
|
|
207
|
+
const EXTRA_CHANNEL_NAME = /^[A-Za-z0-9._-]{1,64}$/
|
|
208
|
+
|
|
198
209
|
// ---------------------------------------------------------------------------
|
|
199
210
|
// WebRTCPeerConnection
|
|
200
211
|
// ---------------------------------------------------------------------------
|
|
@@ -222,6 +233,12 @@ export class WebRTCPeerConnection {
|
|
|
222
233
|
* falls back to `#dataChannel` while it is null. See `send()`.
|
|
223
234
|
*/
|
|
224
235
|
#bulkChannel = null
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Caller-named extra channels, name -> RTCDataChannel (see openChannel()).
|
|
239
|
+
* @type {Map<string, RTCDataChannel>}
|
|
240
|
+
*/
|
|
241
|
+
#extraChannels = new Map()
|
|
225
242
|
#iceServers
|
|
226
243
|
#iceTransportPolicy
|
|
227
244
|
#onLog
|
|
@@ -422,6 +439,10 @@ export class WebRTCPeerConnection {
|
|
|
422
439
|
if (chan.label === BULK_CHANNEL_LABEL) {
|
|
423
440
|
this.#bulkChannel = chan
|
|
424
441
|
this.#setupDataChannel(chan, 'bulk')
|
|
442
|
+
} else if (typeof chan.label === 'string' && chan.label.startsWith(EXTRA_CHANNEL_PREFIX)) {
|
|
443
|
+
const name = chan.label.slice(EXTRA_CHANNEL_PREFIX.length)
|
|
444
|
+
this.#extraChannels.set(name, chan)
|
|
445
|
+
this.#setupDataChannel(chan, 'extra', name)
|
|
425
446
|
} else {
|
|
426
447
|
this.#dataChannel = chan
|
|
427
448
|
this.#setupDataChannel(chan, 'control')
|
|
@@ -587,16 +608,21 @@ export class WebRTCPeerConnection {
|
|
|
587
608
|
* `handleOffer()`) -- there is no other way for it to be missing on a
|
|
588
609
|
* peer that itself created both -- so the traffic still gets there, just
|
|
589
610
|
* without its own lane, exactly as it did before this channel existed.
|
|
590
|
-
*
|
|
591
|
-
*
|
|
611
|
+
* Any other string selects a channel added with `openChannel(name)` (or
|
|
612
|
+
* opened by the remote). Unlike `'bulk'` there is NO fallback: an unknown
|
|
613
|
+
* or closed name throws, because the caller asked for a lane with its own
|
|
614
|
+
* semantics (ordering, retransmits) that the control channel can't honour.
|
|
615
|
+
* @throws {Error} If there is no data channel at all, the channel name is
|
|
616
|
+
* unknown, or the channel that was actually selected is not open.
|
|
592
617
|
*/
|
|
593
618
|
send(data, { channel = 'control' } = {}) {
|
|
594
|
-
|
|
619
|
+
const extra = (channel !== 'control' && channel !== 'bulk') ? this.#extraChannels.get(channel) : undefined
|
|
620
|
+
if (channel !== 'control' && channel !== 'bulk' && !extra) {
|
|
595
621
|
throw new Error(`Unknown channel: ${channel}`)
|
|
596
622
|
}
|
|
597
|
-
const dc = (channel === 'bulk' && this.#bulkChannel?.readyState === 'open')
|
|
623
|
+
const dc = extra ?? ((channel === 'bulk' && this.#bulkChannel?.readyState === 'open')
|
|
598
624
|
? this.#bulkChannel
|
|
599
|
-
: this.#dataChannel
|
|
625
|
+
: this.#dataChannel)
|
|
600
626
|
if (!dc) throw new Error('No data channel')
|
|
601
627
|
if (dc.readyState !== 'open') {
|
|
602
628
|
throw new Error('Data channel not open')
|
|
@@ -607,6 +633,64 @@ export class WebRTCPeerConnection {
|
|
|
607
633
|
this.#stats.messagesOut += 1
|
|
608
634
|
}
|
|
609
635
|
|
|
636
|
+
/**
|
|
637
|
+
* Open an additional named data channel on the ALREADY-NEGOTIATED
|
|
638
|
+
* RTCPeerConnection (#115): another SCTP stream, no new ICE/DTLS handshake,
|
|
639
|
+
* with its own `ordered` / `maxRetransmits` / `maxPacketLifeTime`. Use it to
|
|
640
|
+
* keep, say, latency-sensitive telemetry off the same stream as bulk
|
|
641
|
+
* transfers. Address it with `send(data, { channel: name })`; the remote end
|
|
642
|
+
* adopts it automatically (listed in `channels`, delivered to `onMessage`
|
|
643
|
+
* callbacks as their second argument).
|
|
644
|
+
*
|
|
645
|
+
* Only open named channels to a peer running a build that has this method;
|
|
646
|
+
* older builds adopt an unrecognised label as their control channel.
|
|
647
|
+
*
|
|
648
|
+
* @param {string} name - 1-64 chars of `A-Za-z0-9._-`; not 'control' or 'bulk'
|
|
649
|
+
* @param {{ ordered?: boolean, maxRetransmits?: number, maxPacketLifeTime?: number }} [opts]
|
|
650
|
+
* @returns {RTCDataChannel}
|
|
651
|
+
*/
|
|
652
|
+
openChannel(name, opts = {}) {
|
|
653
|
+
if (typeof name !== 'string' || !EXTRA_CHANNEL_NAME.test(name) || name === 'control' || name === 'bulk') {
|
|
654
|
+
throw new Error(`Invalid channel name: ${JSON.stringify(name)} (1-64 of A-Za-z0-9._-, not 'control' or 'bulk')`)
|
|
655
|
+
}
|
|
656
|
+
if (!this.#pc) throw new Error('No peer connection — call createOffer() or handleOffer() first')
|
|
657
|
+
if (this.#extraChannels.has(name)) throw new Error(`Channel ${name} is already open`)
|
|
658
|
+
const init = {}
|
|
659
|
+
for (const k of ['ordered', 'maxRetransmits', 'maxPacketLifeTime']) {
|
|
660
|
+
if (opts[k] !== undefined) init[k] = opts[k]
|
|
661
|
+
}
|
|
662
|
+
const dc = this.#pc.createDataChannel(EXTRA_CHANNEL_PREFIX + name, init)
|
|
663
|
+
this.#extraChannels.set(name, dc)
|
|
664
|
+
this.#setupDataChannel(dc, 'extra', name)
|
|
665
|
+
return dc
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Close one named channel opened with `openChannel()` (or by the remote).
|
|
670
|
+
* @param {string} name
|
|
671
|
+
* @returns {boolean} true if a channel was closed
|
|
672
|
+
*/
|
|
673
|
+
closeChannel(name) {
|
|
674
|
+
const dc = this.#extraChannels.get(name)
|
|
675
|
+
if (!dc) return false
|
|
676
|
+
this.#extraChannels.delete(name)
|
|
677
|
+
try { dc.close() } catch (e) { silentCatch('clawser-mesh-webrtc', 'close-extra', e) }
|
|
678
|
+
return true
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
/**
|
|
682
|
+
* Names of the channels currently usable with `send({ channel })`:
|
|
683
|
+
* 'control', 'bulk' (when open) and every named channel.
|
|
684
|
+
* @returns {string[]}
|
|
685
|
+
*/
|
|
686
|
+
get channels() {
|
|
687
|
+
const out = []
|
|
688
|
+
if (this.#dataChannel) out.push('control')
|
|
689
|
+
if (this.#bulkChannel) out.push('bulk')
|
|
690
|
+
for (const name of this.#extraChannels.keys()) out.push(name)
|
|
691
|
+
return out
|
|
692
|
+
}
|
|
693
|
+
|
|
610
694
|
/**
|
|
611
695
|
* Attempt to recover a failed/disconnected connection via ICE restart.
|
|
612
696
|
* Only valid once an underlying RTCPeerConnection exists (i.e. after
|
|
@@ -756,6 +840,10 @@ export class WebRTCPeerConnection {
|
|
|
756
840
|
try { this.#bulkChannel.close() } catch (e) { silentCatch('clawser-mesh-webrtc', 'this', e) }
|
|
757
841
|
this.#bulkChannel = null
|
|
758
842
|
}
|
|
843
|
+
for (const [name, dc] of [...this.#extraChannels]) {
|
|
844
|
+
this.#extraChannels.delete(name)
|
|
845
|
+
try { dc.close() } catch (e) { silentCatch('clawser-mesh-webrtc', 'close-extra', e) }
|
|
846
|
+
}
|
|
759
847
|
if (this.#pc) {
|
|
760
848
|
try { this.#pc.close() } catch (e) { silentCatch('clawser-mesh-webrtc', 'this', e) }
|
|
761
849
|
this.#pc = null
|
|
@@ -825,6 +913,12 @@ export class WebRTCPeerConnection {
|
|
|
825
913
|
const dc = this.#dataChannel
|
|
826
914
|
const bulkDc = this.#bulkChannel
|
|
827
915
|
const pc = this.#pc
|
|
916
|
+
const extras = [...this.#extraChannels.values()]
|
|
917
|
+
this.#extraChannels.clear()
|
|
918
|
+
for (const x of extras) {
|
|
919
|
+
x.onopen = null; x.onmessage = null; x.onclose = null; x.onerror = null
|
|
920
|
+
try { x.close() } catch (e) { silentCatch('clawser-mesh-webrtc', 'release-extra', e) }
|
|
921
|
+
}
|
|
828
922
|
this.#dataChannel = null
|
|
829
923
|
this.#bulkChannel = null
|
|
830
924
|
this.#pc = null
|
|
@@ -1020,7 +1114,7 @@ export class WebRTCPeerConnection {
|
|
|
1020
1114
|
* method does, and only ever overwrites which channel it treats as "the"
|
|
1021
1115
|
* one to *send* on -- so it still receives from both.
|
|
1022
1116
|
*/
|
|
1023
|
-
#setupDataChannel(dc, kind) {
|
|
1117
|
+
#setupDataChannel(dc, kind, name = kind) {
|
|
1024
1118
|
dc.onopen = () => {
|
|
1025
1119
|
if (kind === 'control') this.#setState('connected')
|
|
1026
1120
|
this.#log(`DataChannel (${kind}) open with ${this.#remotePodId}`)
|
|
@@ -1032,10 +1126,15 @@ export class WebRTCPeerConnection {
|
|
|
1032
1126
|
let parsed = event.data
|
|
1033
1127
|
try { parsed = JSON.parse(event.data) } catch { /* keep as string */ }
|
|
1034
1128
|
for (const cb of this.#messageCbs) {
|
|
1035
|
-
try { cb(parsed) } catch (e) { silentCatch('clawser-mesh-webrtc', 'swallow', e) }
|
|
1129
|
+
try { cb(parsed, name) } catch (e) { silentCatch('clawser-mesh-webrtc', 'swallow', e) }
|
|
1036
1130
|
}
|
|
1037
1131
|
}
|
|
1038
1132
|
dc.onclose = () => {
|
|
1133
|
+
if (kind === 'extra') {
|
|
1134
|
+
if (this.#extraChannels.get(name) === dc) this.#extraChannels.delete(name)
|
|
1135
|
+
this.#log(`Channel ${name} closed with ${this.#remotePodId}`)
|
|
1136
|
+
return
|
|
1137
|
+
}
|
|
1039
1138
|
if (kind === 'bulk') {
|
|
1040
1139
|
// Auxiliary: drop the reference so send({channel:'bulk'}) falls back
|
|
1041
1140
|
// to control, but don't tear down a connection that may otherwise be
|
|
@@ -1051,6 +1150,10 @@ export class WebRTCPeerConnection {
|
|
|
1051
1150
|
}
|
|
1052
1151
|
dc.onerror = (event) => {
|
|
1053
1152
|
this.#fireError(event?.error || new Error(`DataChannel (${kind}) error`))
|
|
1153
|
+
if (kind === 'extra') {
|
|
1154
|
+
if (this.#extraChannels.get(name) === dc) this.#extraChannels.delete(name)
|
|
1155
|
+
return
|
|
1156
|
+
}
|
|
1054
1157
|
if (kind === 'bulk') {
|
|
1055
1158
|
// Same reasoning as onclose above: an error on the auxiliary channel
|
|
1056
1159
|
// alone should not take down a working control channel.
|
|
@@ -1518,6 +1621,21 @@ export class WebRTCTransportAdapter extends MeshTransport {
|
|
|
1518
1621
|
super.close()
|
|
1519
1622
|
}
|
|
1520
1623
|
|
|
1624
|
+
/**
|
|
1625
|
+
* Open an additional named data channel on the same negotiated connection
|
|
1626
|
+
* (#115). See `WebRTCPeerConnection#openChannel()`. Send on it with
|
|
1627
|
+
* `send(data, { channel: name })`.
|
|
1628
|
+
* @param {string} name
|
|
1629
|
+
* @param {object} [opts]
|
|
1630
|
+
*/
|
|
1631
|
+
openChannel(name, opts) { return this.#connection.openChannel(name, opts) }
|
|
1632
|
+
|
|
1633
|
+
/** Close a named channel. @param {string} name @returns {boolean} */
|
|
1634
|
+
closeChannel(name) { return this.#connection.closeChannel(name) }
|
|
1635
|
+
|
|
1636
|
+
/** Names usable as `send(data, { channel })`: 'control', 'bulk' and named channels. */
|
|
1637
|
+
get channels() { return this.#connection.channels }
|
|
1638
|
+
|
|
1521
1639
|
/** The underlying WebRTCPeerConnection. */
|
|
1522
1640
|
get peerConnection() { return this.#connection }
|
|
1523
1641
|
}
|
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
|
|