@johnhenry/browsermesh-transport 0.5.1 → 0.6.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/README.md +181 -2
- package/package.json +13 -4
- package/src/channel-relay.mjs +2 -2
- package/src/cross-origin.mjs +3 -3
- package/src/gateway.mjs +3 -3
- package/src/identity-binding.mjs +181 -0
- package/src/index.mjs +8 -0
- package/src/relay.mjs +2 -2
- package/src/silent-catch.mjs +38 -9
- package/src/streams.mjs +3 -3
- package/src/transport.mjs +48 -6
- package/src/webrtc.mjs +236 -25
- package/src/websocket.mjs +23 -16
- package/src/webtransport.mjs +5 -5
- package/src/wisp-client.mjs +5 -5
- package/src/wisp.mjs +4 -4
- package/src/wsh-bridge.mjs +9 -14
- package/src/wsh-frames.mjs +180 -0
- package/src/wsh-in-process.mjs +82 -0
- package/src/wsh-links.mjs +1005 -0
- package/src/wsh-pod-transport.mjs +159 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* wsh-pod-transport.mjs -- a `Pod` TransportAdapter over wsh links.
|
|
3
|
+
*
|
|
4
|
+
* Implements the contract in `@johnhenry/browsermesh-pod`'s `transport.mjs`
|
|
5
|
+
* (`send(msg)`, `onMessage(handler)`, `open()`, `close()`, `ready`) on top of
|
|
6
|
+
* `WshPeerLinks`, so a `Pod` uses it with no change:
|
|
7
|
+
*
|
|
8
|
+
* const identity = await PodIdentity.generate()
|
|
9
|
+
* const transport = new WshPodTransport({ identity, relays: ['wss://relay.example/'],
|
|
10
|
+
* authorize: (peer) => registry.knows(peer.podId), peers: [otherPodId] })
|
|
11
|
+
* await pod.boot({ identity, transport })
|
|
12
|
+
*
|
|
13
|
+
* The sender of an inbound message is the authenticated key of the link it
|
|
14
|
+
* arrived on, never a field of the message: `from` is set to it, and a message
|
|
15
|
+
* whose own `from` or `podId` names another pod is dropped (`foreign-sender`).
|
|
16
|
+
* The handler is called as `handler(msg, { peerId })` with that same key, which
|
|
17
|
+
* a `Pod` takes as a link-authenticated sender (no per-message signature needed;
|
|
18
|
+
* signed messages from pods that sign by default are accepted as well).
|
|
19
|
+
*
|
|
20
|
+
* `send(msg)` goes to the link named by `msg.to`, or to every link for a
|
|
21
|
+
* broadcast (`to` absent or `'*'`). It never throws and never returns a
|
|
22
|
+
* rejecting promise; a message it cannot send (no link, queue full) is dropped
|
|
23
|
+
* and reported as a `dropped` event.
|
|
24
|
+
*
|
|
25
|
+
* No static import of `@johnhenry/browsermesh-pod`: the contract is duck-typed.
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { WshPeerLinks } from './wsh-links.mjs';
|
|
29
|
+
|
|
30
|
+
const POD_GOODBYE = 'pod:goodbye';
|
|
31
|
+
const EVENTS = ['peer-up', 'peer-down', 'refused', 'error', 'dropped'];
|
|
32
|
+
|
|
33
|
+
export class WshPodTransport {
|
|
34
|
+
#links;
|
|
35
|
+
#owns;
|
|
36
|
+
#handler = null;
|
|
37
|
+
#ready = false;
|
|
38
|
+
#closed = false;
|
|
39
|
+
#peers;
|
|
40
|
+
#awaitPeers;
|
|
41
|
+
#goodbyeOnDown;
|
|
42
|
+
#cbs = Object.fromEntries(EVENTS.map((e) => [e, []]));
|
|
43
|
+
#unsubs = [];
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* @param {object} opts - `WshPeerLinks` options (`identity`, `relays`, `authorize`, ...), plus:
|
|
47
|
+
* @param {WshPeerLinks} [opts.links] - Use an existing hub (it is not closed by `close()`).
|
|
48
|
+
* @param {string[]} [opts.peers] - Pod ids to link to on `open()`; they are redialled when the link is lost.
|
|
49
|
+
* @param {boolean} [opts.awaitPeers=false] - `open()` waits for the first attempt at each of `peers` to finish.
|
|
50
|
+
* @param {boolean} [opts.goodbyeOnDown=true] - Hand the pod a `pod:goodbye` when a peer's link goes away, so it forgets the peer.
|
|
51
|
+
*/
|
|
52
|
+
constructor({ links, peers = [], awaitPeers = false, goodbyeOnDown = true, ...hubOpts } = {}) {
|
|
53
|
+
this.#owns = !links;
|
|
54
|
+
this.#links = links ?? new WshPeerLinks(hubOpts);
|
|
55
|
+
this.#peers = [...peers];
|
|
56
|
+
this.#awaitPeers = awaitPeers;
|
|
57
|
+
this.#goodbyeOnDown = goodbyeOnDown;
|
|
58
|
+
const l = this.#links;
|
|
59
|
+
this.#unsubs.push(
|
|
60
|
+
l.on('message', (m) => this.#onData(m)),
|
|
61
|
+
l.on('peer-up', (e) => this.#fire('peer-up', e)),
|
|
62
|
+
l.on('peer-down', (e) => {
|
|
63
|
+
this.#fire('peer-down', e);
|
|
64
|
+
if (this.#goodbyeOnDown && this.#handler && !this.#closed) this.#deliver({ type: POD_GOODBYE, podId: e.podId, from: e.podId, ts: Date.now(), synthetic: true });
|
|
65
|
+
}),
|
|
66
|
+
l.on('refused', (e) => this.#fire('refused', e)),
|
|
67
|
+
l.on('error', (e) => this.#fire('error', e)),
|
|
68
|
+
l.on('dropped', (e) => this.#fire('dropped', e)),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The hub, for `connect(podId)`, `peers()` and the like. */
|
|
73
|
+
get links() { return this.#links; }
|
|
74
|
+
|
|
75
|
+
get ready() { return this.#ready; }
|
|
76
|
+
|
|
77
|
+
/** @param {(msg: object) => void} handler */
|
|
78
|
+
onMessage(handler) { this.#handler = handler; }
|
|
79
|
+
|
|
80
|
+
/** Events: `peer-up`, `peer-down`, `refused`, `error`, `dropped` (`{ from, reason }`). */
|
|
81
|
+
on(event, cb) {
|
|
82
|
+
if (!EVENTS.includes(event)) throw new Error(`Unknown event: ${event}`);
|
|
83
|
+
this.#cbs[event].push(cb);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
async open() {
|
|
87
|
+
if (this.#ready) return;
|
|
88
|
+
if (this.#closed) throw new Error('WshPodTransport is closed');
|
|
89
|
+
await this.#links.listen();
|
|
90
|
+
this.#ready = true;
|
|
91
|
+
const attempts = this.#peers.map((p) => this.#links.connect(p).catch((error) => { this.#fire('error', { error, podId: p }); }));
|
|
92
|
+
if (this.#awaitPeers) await Promise.allSettled(attempts);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** @param {object} msg */
|
|
96
|
+
send(msg) {
|
|
97
|
+
if (!this.#ready || !msg || typeof msg !== 'object') return;
|
|
98
|
+
const to = msg.to;
|
|
99
|
+
if (to && to !== '*') {
|
|
100
|
+
if (!this.#links.isConnected(to)) { this.#fire('dropped', { to, reason: 'no-link' }); return; }
|
|
101
|
+
this.#push(to, msg);
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
for (const peer of this.#links.peers()) this.#push(peer, msg);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async close() {
|
|
108
|
+
if (this.#closed) return;
|
|
109
|
+
this.#closed = true;
|
|
110
|
+
this.#ready = false;
|
|
111
|
+
for (const off of this.#unsubs) off();
|
|
112
|
+
this.#unsubs = [];
|
|
113
|
+
if (this.#owns) await this.#links.close();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// -- internals ----------------------------------------------------------
|
|
117
|
+
|
|
118
|
+
#push(peer, msg) {
|
|
119
|
+
this.#links.send(peer, msg).catch((err) => this.#fire('dropped', { to: peer, reason: err?.code ?? 'send-failed' }));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
#fire(event, data) {
|
|
123
|
+
for (const cb of [...this.#cbs[event]]) {
|
|
124
|
+
try { cb(data); } catch { /* listener errors do not propagate */ }
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#onData({ from, data }) {
|
|
129
|
+
if (this.#closed) return;
|
|
130
|
+
let msg;
|
|
131
|
+
try {
|
|
132
|
+
msg = JSON.parse(typeof data === 'string' ? data : new TextDecoder().decode(data));
|
|
133
|
+
} catch {
|
|
134
|
+
this.#fire('dropped', { from, reason: 'not-json' });
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (!msg || typeof msg !== 'object' || Array.isArray(msg)) {
|
|
138
|
+
this.#fire('dropped', { from, reason: 'not-a-message' });
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// The sender is the key the session authenticated. A message that claims
|
|
142
|
+
// otherwise is not delivered under either name.
|
|
143
|
+
for (const field of ['from', 'podId']) {
|
|
144
|
+
if (field in msg && msg[field] !== from) {
|
|
145
|
+
this.#fire('dropped', { from, reason: 'foreign-sender', field });
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
// The second argument tells the pod that this link authenticated the peer:
|
|
150
|
+
// the pod then takes `from` as the link's identity without asking for a
|
|
151
|
+
// per-message signature (a mismatching `from` would be dropped there too).
|
|
152
|
+
this.#deliver({ ...msg, from }, { peerId: from });
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
#deliver(msg, meta) {
|
|
156
|
+
if (!this.#handler) return;
|
|
157
|
+
try { this.#handler(msg, meta); } catch { /* a throwing handler does not break the link */ }
|
|
158
|
+
}
|
|
159
|
+
}
|