@johnhenry/browsermesh-transport 0.3.2 → 0.3.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/README.md +15 -0
- package/package.json +1 -1
- package/src/index.mjs +1 -0
- package/src/webrtc.mjs +3 -2
- package/src/websocket.mjs +20 -10
- package/src/wire-data.mjs +48 -0
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ Extracted from the private `clawser` monorepo (previously `packages/browsermesh-
|
|
|
24
24
|
|--------|-------------|
|
|
25
25
|
| transport | `MeshTransport`, `MockMeshTransport`, `MeshTransportNegotiator` |
|
|
26
26
|
| websocket | `WebSocketTransport`, `WebRTCTransport`, `WebTransportTransport`, `NATTraversal`, `TransportFactory` |
|
|
27
|
+
| wire-data | `encodeWireData`, `isWireNative` |
|
|
27
28
|
| webrtc | `WebRTCPeerConnection`, `WebRTCMeshManager`, `WebRTCTransportAdapter` |
|
|
28
29
|
| webtransport | `WebTransportBridge`, `WebTransportAdapterFactory` |
|
|
29
30
|
| relay | `MeshRelayClient`, `MockRelayServer` |
|
|
@@ -46,6 +47,20 @@ npm install @johnhenry/browsermesh-transport @johnhenry/browsermesh-primitives
|
|
|
46
47
|
import { MeshTransport, WebSocketTransport, StreamMultiplexer } from '@johnhenry/browsermesh-transport';
|
|
47
48
|
```
|
|
48
49
|
|
|
50
|
+
## What `send()` puts on the wire
|
|
51
|
+
|
|
52
|
+
`RTCDataChannel.send()` and `WebSocket.send()` accept only a string or binary
|
|
53
|
+
and quietly turn anything else into the text `"[object Object]"`. Every
|
|
54
|
+
transport here therefore sends strings and binary (`ArrayBuffer`, typed arrays,
|
|
55
|
+
`Blob`) unchanged and any other value as its JSON text (`encodeWireData()`; it
|
|
56
|
+
throws a `TypeError` for `undefined` or a function rather than send the text
|
|
57
|
+
`"undefined"`). The receiving side gets a string; a consumer that wants objects
|
|
58
|
+
parses it (`@johnhenry/browsermesh-apps`' `ctx.onIncomingData()` does). A custom
|
|
59
|
+
`MeshTransport` should follow the same rule.
|
|
60
|
+
|
|
61
|
+
`WebRTCTransport.send(data, { channel })` takes `'control'` (default) or
|
|
62
|
+
`'bulk'` and falls back to control when the bulk channel is not open.
|
|
63
|
+
|
|
49
64
|
## License
|
|
50
65
|
|
|
51
66
|
MIT
|
package/package.json
CHANGED
package/src/index.mjs
CHANGED
package/src/webrtc.mjs
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
|
|
15
15
|
import { MeshTransport } from './transport.mjs'
|
|
16
16
|
import { silentCatch } from './silent-catch.mjs'
|
|
17
|
+
import { encodeWireData } from './wire-data.mjs'
|
|
17
18
|
|
|
18
19
|
// ---------------------------------------------------------------------------
|
|
19
20
|
// Feature detection
|
|
@@ -544,9 +545,9 @@ export class WebRTCPeerConnection {
|
|
|
544
545
|
if (dc.readyState !== 'open') {
|
|
545
546
|
throw new Error('Data channel not open')
|
|
546
547
|
}
|
|
547
|
-
const str =
|
|
548
|
+
const str = encodeWireData(data)
|
|
548
549
|
dc.send(str)
|
|
549
|
-
this.#stats.bytesSent += str.length
|
|
550
|
+
this.#stats.bytesSent += str.length ?? str.byteLength ?? str.size ?? 0
|
|
550
551
|
this.#stats.messagesOut += 1
|
|
551
552
|
}
|
|
552
553
|
|
package/src/websocket.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { silentCatch } from './silent-catch.mjs'
|
|
2
|
+
import { encodeWireData } from './wire-data.mjs'
|
|
2
3
|
/**
|
|
3
4
|
// STATUS: INTEGRATED — wired into ClawserPod lifecycle, proven via E2E testing
|
|
4
5
|
* clawser-mesh-websocket.js -- WebSocket, WebRTC & WebTransport Adapters.
|
|
@@ -201,14 +202,18 @@ export class WebSocketTransport {
|
|
|
201
202
|
}
|
|
202
203
|
|
|
203
204
|
/**
|
|
204
|
-
* Send data over the WebSocket.
|
|
205
|
+
* Send data over the WebSocket. Strings and binary go out verbatim; any
|
|
206
|
+
* other value (an envelope object) is sent as its JSON text -- a raw
|
|
207
|
+
* `WebSocket.send(object)` would transmit the string `"[object Object]"`.
|
|
208
|
+
* See `wire-data.mjs`.
|
|
205
209
|
* @param {*} data
|
|
206
210
|
*/
|
|
207
211
|
send(data) {
|
|
208
212
|
if (!this.connected) throw new Error('Not connected');
|
|
209
|
-
|
|
213
|
+
const wire = encodeWireData(data);
|
|
214
|
+
this.#ws.send(wire);
|
|
210
215
|
this.#stats.messagesSent++;
|
|
211
|
-
this.#stats.bytesOut += byteLength(
|
|
216
|
+
this.#stats.bytesOut += byteLength(wire);
|
|
212
217
|
}
|
|
213
218
|
|
|
214
219
|
/**
|
|
@@ -691,7 +696,10 @@ export class WebRTCTransport {
|
|
|
691
696
|
}
|
|
692
697
|
|
|
693
698
|
/**
|
|
694
|
-
* Send data over a data channel.
|
|
699
|
+
* Send data over a data channel. Strings and binary go out verbatim; any
|
|
700
|
+
* other value (an envelope object) is sent as its JSON text -- a raw
|
|
701
|
+
* `RTCDataChannel.send(object)` would transmit the string
|
|
702
|
+
* `"[object Object]"`. See `wire-data.mjs`.
|
|
695
703
|
* @param {*} data
|
|
696
704
|
* @param {object} [opts]
|
|
697
705
|
* @param {'control'|'bulk'} [opts.channel='control'] - `'control'` is the
|
|
@@ -710,9 +718,10 @@ export class WebRTCTransport {
|
|
|
710
718
|
? this.#bulkChannel
|
|
711
719
|
: this.#dataChannel;
|
|
712
720
|
if (!dc) throw new Error('Not connected');
|
|
713
|
-
|
|
721
|
+
const wire = encodeWireData(data);
|
|
722
|
+
dc.send(wire);
|
|
714
723
|
this.#stats.messagesSent++;
|
|
715
|
-
this.#stats.bytesOut += byteLength(
|
|
724
|
+
this.#stats.bytesOut += byteLength(wire);
|
|
716
725
|
}
|
|
717
726
|
|
|
718
727
|
/**
|
|
@@ -943,12 +952,13 @@ export class WebTransportTransport {
|
|
|
943
952
|
*/
|
|
944
953
|
async send(data) {
|
|
945
954
|
if (!this.connected) throw new Error('Not connected');
|
|
946
|
-
const
|
|
947
|
-
|
|
948
|
-
|
|
955
|
+
const wire = encodeWireData(data);
|
|
956
|
+
const encoded = typeof wire === 'string'
|
|
957
|
+
? new TextEncoder().encode(wire)
|
|
958
|
+
: wire;
|
|
949
959
|
await this.#writer.write(encoded);
|
|
950
960
|
this.#stats.messagesSent++;
|
|
951
|
-
this.#stats.bytesOut += byteLength(
|
|
961
|
+
this.#stats.bytesOut += byteLength(wire);
|
|
952
962
|
}
|
|
953
963
|
|
|
954
964
|
/**
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wire-data.mjs -- the one canonical encoding for what a mesh transport
|
|
3
|
+
* hands to a real wire.
|
|
4
|
+
*
|
|
5
|
+
* `RTCDataChannel.send()` and `WebSocket.send()` accept only a string or
|
|
6
|
+
* binary data (`ArrayBuffer`, a typed-array view, a `Blob`). Anything else
|
|
7
|
+
* is coerced with `String(value)`, so a plain object arrives as the
|
|
8
|
+
* literal text `"[object Object]"` -- no error, no warning. PeerNode and
|
|
9
|
+
* every MeshService above it send envelope *objects* (`{ type, ...payload }`),
|
|
10
|
+
* so a transport that forwards its argument untouched silently destroys
|
|
11
|
+
* every message.
|
|
12
|
+
*
|
|
13
|
+
* Rule: strings and binary pass through byte-for-byte; every other value is
|
|
14
|
+
* sent as its JSON text. The receiving side's counterpart (parse a JSON
|
|
15
|
+
* object/array string back into an object) lives with the consumer that
|
|
16
|
+
* wants objects -- see `@johnhenry/browsermesh-apps`'s `ctx.onIncomingData()`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Whether `data` is something a data channel / WebSocket accepts verbatim.
|
|
21
|
+
* @param {*} data
|
|
22
|
+
* @returns {boolean}
|
|
23
|
+
*/
|
|
24
|
+
export function isWireNative(data) {
|
|
25
|
+
if (typeof data === 'string') return true;
|
|
26
|
+
if (data instanceof ArrayBuffer) return true;
|
|
27
|
+
if (ArrayBuffer.isView(data)) return true;
|
|
28
|
+
if (typeof Blob !== 'undefined' && data instanceof Blob) return true;
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Encode a value for `RTCDataChannel.send()` / `WebSocket.send()`.
|
|
34
|
+
*
|
|
35
|
+
* @param {*} data
|
|
36
|
+
* @returns {string|ArrayBuffer|ArrayBufferView|Blob} `data` itself when it is
|
|
37
|
+
* already a string or binary; otherwise its JSON text.
|
|
38
|
+
* @throws {TypeError} When `data` is `undefined` or a function/symbol (no JSON
|
|
39
|
+
* representation) -- failing loudly beats sending the text "undefined".
|
|
40
|
+
*/
|
|
41
|
+
export function encodeWireData(data) {
|
|
42
|
+
if (isWireNative(data)) return data;
|
|
43
|
+
const json = JSON.stringify(data);
|
|
44
|
+
if (typeof json !== 'string') {
|
|
45
|
+
throw new TypeError(`Cannot send a ${typeof data} over a transport (no JSON representation)`);
|
|
46
|
+
}
|
|
47
|
+
return json;
|
|
48
|
+
}
|