@johnhenry/browsermesh-transport 0.4.1 → 0.5.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 +1 -1
- package/src/webrtc.mjs +125 -7
- package/src/websocket.mjs +12 -2
package/package.json
CHANGED
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
|
@@ -165,6 +165,9 @@ export class WebSocketTransport {
|
|
|
165
165
|
/** @type {Set<*>} Outstanding jitter timers, cleared on close */
|
|
166
166
|
#jitterTimers = new Set();
|
|
167
167
|
|
|
168
|
+
/** FIFO delivery is independent of timer scheduling and clock rounding. */
|
|
169
|
+
#jitterQueue = [];
|
|
170
|
+
|
|
168
171
|
/** @type {number} Earliest time the next jittered send may fire (keeps order) */
|
|
169
172
|
#nextSendAt = 0;
|
|
170
173
|
|
|
@@ -320,10 +323,16 @@ export class WebSocketTransport {
|
|
|
320
323
|
const now = Date.now();
|
|
321
324
|
const at = Math.max(now + Math.random() * this.#jitterMs, this.#nextSendAt);
|
|
322
325
|
this.#nextSendAt = at;
|
|
326
|
+
const entry = { wire, ws, ready: false };
|
|
327
|
+
this.#jitterQueue.push(entry);
|
|
323
328
|
const timer = setTimeout(() => {
|
|
324
329
|
this.#jitterTimers.delete(timer);
|
|
325
|
-
|
|
326
|
-
|
|
330
|
+
entry.ready = true;
|
|
331
|
+
while (this.#jitterQueue[0]?.ready) {
|
|
332
|
+
const next = this.#jitterQueue.shift();
|
|
333
|
+
if (this.#ws !== next.ws || !this.connected) continue;
|
|
334
|
+
try { next.ws.send(next.wire); } catch (e) { this._fireEvent('error', e); }
|
|
335
|
+
}
|
|
327
336
|
}, Math.max(0, at - now));
|
|
328
337
|
this.#jitterTimers.add(timer);
|
|
329
338
|
}
|
|
@@ -332,6 +341,7 @@ export class WebSocketTransport {
|
|
|
332
341
|
#clearJitter() {
|
|
333
342
|
for (const t of this.#jitterTimers) clearTimeout(t);
|
|
334
343
|
this.#jitterTimers.clear();
|
|
344
|
+
this.#jitterQueue.length = 0;
|
|
335
345
|
this.#nextSendAt = 0;
|
|
336
346
|
}
|
|
337
347
|
|