@vielzeug/mesh 26.11.16 → 26.11.17
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/dist/_node.cjs +1 -1
- package/dist/_node.cjs.map +1 -1
- package/dist/_node.d.ts +7 -5
- package/dist/_node.d.ts.map +1 -1
- package/dist/_node.js +73 -78
- package/dist/_node.js.map +1 -1
- package/dist/_wire.cjs +1 -1
- package/dist/_wire.cjs.map +1 -1
- package/dist/_wire.d.ts +8 -13
- package/dist/_wire.d.ts.map +1 -1
- package/dist/_wire.js +2 -13
- package/dist/_wire.js.map +1 -1
- package/dist/guest.cjs +1 -1
- package/dist/guest.cjs.map +1 -1
- package/dist/guest.d.ts.map +1 -1
- package/dist/guest.js +69 -69
- package/dist/guest.js.map +1 -1
- package/dist/host.cjs +1 -1
- package/dist/host.cjs.map +1 -1
- package/dist/host.d.ts.map +1 -1
- package/dist/host.js +106 -89
- package/dist/host.js.map +1 -1
- package/dist/mesh.cjs +3 -3
- package/dist/mesh.cjs.map +1 -1
- package/dist/mesh.iife.js +3 -3
- package/dist/mesh.iife.js.map +1 -1
- package/dist/mesh.js +63 -61
- package/dist/mesh.js.map +1 -1
- package/dist/testing/fake-rtc.cjs +1 -1
- package/dist/testing/fake-rtc.cjs.map +1 -1
- package/dist/testing/fake-rtc.d.ts +6 -0
- package/dist/testing/fake-rtc.d.ts.map +1 -1
- package/dist/testing/fake-rtc.js +9 -0
- package/dist/testing/fake-rtc.js.map +1 -1
- package/dist/types.d.ts +6 -3
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/mesh.js
CHANGED
|
@@ -43,67 +43,48 @@ var s = {
|
|
|
43
43
|
return i(JSON.stringify(e));
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
|
-
// src/_random.ts
|
|
47
|
-
function randomBytes(e, t) {
|
|
48
|
-
let r = new Uint8Array(e);
|
|
49
|
-
if (t) for (let i = 0; i < e; i++) r[i] = Math.floor(n(t) * 256);
|
|
50
|
-
else crypto.getRandomValues(r);
|
|
51
|
-
return r;
|
|
52
|
-
}
|
|
53
|
-
function randomId(e) {
|
|
54
|
-
return t(randomBytes(16, e));
|
|
55
|
-
}
|
|
56
46
|
// src/_wire.ts
|
|
57
47
|
function toEnvelope(e) {
|
|
58
48
|
if (typeof e != "object" || !e) return null;
|
|
59
49
|
let t = e;
|
|
60
|
-
return t.v !==
|
|
50
|
+
return t.v !== 2 || typeof t.t != "string" || typeof t.ts != "number" ? null : e;
|
|
61
51
|
}
|
|
62
|
-
var MessageDedupe = class {
|
|
63
|
-
seen = /* @__PURE__ */ new Set();
|
|
64
|
-
order = [];
|
|
65
|
-
capacity;
|
|
66
|
-
constructor(e = 256) {
|
|
67
|
-
this.capacity = e;
|
|
68
|
-
}
|
|
69
|
-
check(e) {
|
|
70
|
-
return !this.seen.has(e) && (this.seen.add(e), this.order.push(e), this.order.length > this.capacity && this.seen.delete(this.order.shift()), !0);
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
52
|
// src/_node.ts
|
|
74
|
-
function createNodeCore(
|
|
75
|
-
let
|
|
76
|
-
disposalCtrl:
|
|
53
|
+
function createNodeCore() {
|
|
54
|
+
let e = r(), t = new AbortController(), n = {
|
|
55
|
+
disposalCtrl: t,
|
|
77
56
|
disposed: !1,
|
|
78
|
-
emitTap(
|
|
79
|
-
|
|
57
|
+
emitTap(t) {
|
|
58
|
+
e.emit(t);
|
|
80
59
|
},
|
|
81
60
|
ensureLive() {
|
|
82
|
-
if (
|
|
61
|
+
if (n.disposed) throw new MeshDisposedError();
|
|
83
62
|
},
|
|
84
63
|
markDisposed() {
|
|
85
|
-
|
|
64
|
+
n.disposed || (n.disposed = !0, n.status = "disposed", n.emitTap({
|
|
86
65
|
status: "disposed",
|
|
87
66
|
type: "status-change"
|
|
88
|
-
}),
|
|
67
|
+
}), n.emitTap({ type: "dispose" }), e.clear(), t.abort());
|
|
89
68
|
},
|
|
90
69
|
setStatus(e) {
|
|
91
|
-
|
|
70
|
+
n.disposed || n.status === e || (n.status = e, n.emitTap({
|
|
92
71
|
status: e,
|
|
93
72
|
type: "status-change"
|
|
94
73
|
}));
|
|
95
74
|
},
|
|
96
75
|
status: "idle",
|
|
97
|
-
tap(
|
|
98
|
-
return
|
|
76
|
+
tap(t, r) {
|
|
77
|
+
return n.disposed ? () => {} : e.tap(t, r);
|
|
99
78
|
}
|
|
100
79
|
};
|
|
101
|
-
return
|
|
80
|
+
return n;
|
|
81
|
+
}
|
|
82
|
+
function wireDisposalSignal(e, t) {
|
|
83
|
+
e && (e.aborted ? t() : e.addEventListener("abort", t, { once: !0 }));
|
|
102
84
|
}
|
|
103
85
|
function createPeerRecord(e, t, n) {
|
|
104
86
|
let r = {
|
|
105
87
|
dc: null,
|
|
106
|
-
dedupe: new MessageDedupe(),
|
|
107
88
|
id: e,
|
|
108
89
|
name: t,
|
|
109
90
|
pc: null,
|
|
@@ -162,10 +143,6 @@ function createMessenger(e) {
|
|
|
162
143
|
reject(i, "malformed");
|
|
163
144
|
return;
|
|
164
145
|
}
|
|
165
|
-
if (!n.dedupe.check(c.id)) {
|
|
166
|
-
reject(i, "duplicate");
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
146
|
let l = t.get(c.t);
|
|
170
147
|
if (!l || l.size === 0) {
|
|
171
148
|
reject(i, "unknown-type");
|
|
@@ -178,7 +155,6 @@ function createMessenger(e) {
|
|
|
178
155
|
type: "message-received"
|
|
179
156
|
});
|
|
180
157
|
let u = {
|
|
181
|
-
messageId: c.id,
|
|
182
158
|
payload: c.p,
|
|
183
159
|
peerId: i,
|
|
184
160
|
sentAt: c.ts
|
|
@@ -202,11 +178,10 @@ function createMessenger(e) {
|
|
|
202
178
|
let i = t.dc;
|
|
203
179
|
if (!i || i.readyState !== "open" || t.status !== "connected") throw new MeshConnectionError(`Peer "${t.id}" is not connected`, t.id);
|
|
204
180
|
let a = {
|
|
205
|
-
id: randomId(e.random),
|
|
206
181
|
p: r,
|
|
207
182
|
t: n,
|
|
208
183
|
ts: e.clock(),
|
|
209
|
-
v:
|
|
184
|
+
v: 2
|
|
210
185
|
}, s;
|
|
211
186
|
try {
|
|
212
187
|
s = JSON.stringify(a);
|
|
@@ -289,6 +264,16 @@ async function createProof(e, n) {
|
|
|
289
264
|
}, !1, ["sign"]), o = await r.sign("HMAC", a, i.encode(canonicalSdp(n)));
|
|
290
265
|
return t(new Uint8Array(o));
|
|
291
266
|
}
|
|
267
|
+
// src/_random.ts
|
|
268
|
+
function randomBytes(e, t) {
|
|
269
|
+
let r = new Uint8Array(e);
|
|
270
|
+
if (t) for (let i = 0; i < e; i++) r[i] = Math.floor(n(t) * 256);
|
|
271
|
+
else crypto.getRandomValues(r);
|
|
272
|
+
return r;
|
|
273
|
+
}
|
|
274
|
+
function randomId(e) {
|
|
275
|
+
return t(randomBytes(16, e));
|
|
276
|
+
}
|
|
292
277
|
// src/_rtc.ts
|
|
293
278
|
var c = { createPeerConnection(e) {
|
|
294
279
|
let t = globalThis.RTCPeerConnection;
|
|
@@ -328,11 +313,10 @@ function waitChannelOpen(e, t, n, r, i) {
|
|
|
328
313
|
}
|
|
329
314
|
// src/guest.ts
|
|
330
315
|
function createMeshGuest(e = {}) {
|
|
331
|
-
let t = e.rtc ?? c, n = e.clock ?? (() => Date.now()), r = e.maxMessageBytes ?? 65536, i = e.channelOpenTimeoutMs ?? 6e4, o = e.iceGatheringTimeoutMs ?? 5e3, s = createNodeCore(
|
|
316
|
+
let t = e.rtc ?? c, n = e.clock ?? (() => Date.now()), r = e.maxMessageBytes ?? 65536, i = e.channelOpenTimeoutMs ?? 6e4, o = e.iceGatheringTimeoutMs ?? 5e3, s = createNodeCore(), l = createMessenger({
|
|
332
317
|
clock: n,
|
|
333
318
|
emitTap: s.emitTap,
|
|
334
|
-
maxMessageBytes: r
|
|
335
|
-
random: e.random
|
|
319
|
+
maxMessageBytes: r
|
|
336
320
|
}), u = null, d = a(() => {
|
|
337
321
|
u?.status === "connecting" && failPeer(u, openTimeoutError());
|
|
338
322
|
}), openTimeoutError = () => new MeshTimeoutError(`Timed out waiting ${i}ms for the data channel to open`);
|
|
@@ -382,10 +366,10 @@ function createMeshGuest(e = {}) {
|
|
|
382
366
|
}), t.addEventListener("iceconnectionstatechange", () => {
|
|
383
367
|
if (s.disposed) return;
|
|
384
368
|
let n = t.iceConnectionState;
|
|
385
|
-
n === "failed" || n === "closed"
|
|
369
|
+
(n === "failed" || n === "closed") && failPeer(e, new MeshConnectionError(`ice ${n}`, e.id));
|
|
386
370
|
});
|
|
387
371
|
}
|
|
388
|
-
|
|
372
|
+
let f = {
|
|
389
373
|
async acceptInvitation(r, a) {
|
|
390
374
|
if (s.ensureLive(), u && u.status !== "failed" && u.status !== "disconnected") throw new MeshPairingError("Guest is already paired");
|
|
391
375
|
if (r?.v !== 1) throw new MeshPairingError("Unsupported invitation version");
|
|
@@ -469,6 +453,7 @@ function createMeshGuest(e = {}) {
|
|
|
469
453
|
this.dispose();
|
|
470
454
|
}
|
|
471
455
|
};
|
|
456
|
+
return wireDisposalSignal(e.signal, () => f.dispose()), f;
|
|
472
457
|
}
|
|
473
458
|
// src/host.ts
|
|
474
459
|
function createMeshHost(e = {}) {
|
|
@@ -476,11 +461,10 @@ function createMeshHost(e = {}) {
|
|
|
476
461
|
label: "mesh",
|
|
477
462
|
ordered: !0,
|
|
478
463
|
...e.channel
|
|
479
|
-
}, u = createNodeCore(
|
|
464
|
+
}, u = createNodeCore(), d = createMessenger({
|
|
480
465
|
clock: r,
|
|
481
466
|
emitTap: u.emitTap,
|
|
482
|
-
maxMessageBytes: i
|
|
483
|
-
random: e.random
|
|
467
|
+
maxMessageBytes: i
|
|
484
468
|
}), f = /* @__PURE__ */ new Map(), p = /* @__PURE__ */ new Map(), m = !1, h = !1;
|
|
485
469
|
function recomputeStatus() {
|
|
486
470
|
let e = p.size > 0 ? "pairing" : m ? "disconnected" : h ? "failed" : "idle";
|
|
@@ -500,6 +484,15 @@ function createMeshHost(e = {}) {
|
|
|
500
484
|
type: "invitation-expired"
|
|
501
485
|
}), recomputeStatus());
|
|
502
486
|
}
|
|
487
|
+
function releaseInvitation(e, t) {
|
|
488
|
+
p.get(e) === t && (t.state = "open");
|
|
489
|
+
}
|
|
490
|
+
function consumeInvitation(e, t) {
|
|
491
|
+
p.get(e) === t && (p.delete(e), clearTimeout(t.expiryTimer));
|
|
492
|
+
}
|
|
493
|
+
function discardInvitation(e, t) {
|
|
494
|
+
consumeInvitation(e, t), t.pc.close();
|
|
495
|
+
}
|
|
503
496
|
function setPeerStatus(e, t) {
|
|
504
497
|
e.status !== t && (e.status = t, u.emitTap({
|
|
505
498
|
peerId: e.id,
|
|
@@ -532,7 +525,7 @@ function createMeshHost(e = {}) {
|
|
|
532
525
|
peerId: e.id,
|
|
533
526
|
state: n,
|
|
534
527
|
type: "ice-state"
|
|
535
|
-
}), n === "failed" || n === "closed"
|
|
528
|
+
}), (n === "failed" || n === "closed") && leavePeer(e, `ice ${n}`, n === "failed");
|
|
536
529
|
});
|
|
537
530
|
}
|
|
538
531
|
function markPeerConnected(e) {
|
|
@@ -541,26 +534,34 @@ function createMeshHost(e = {}) {
|
|
|
541
534
|
type: "peer-joined"
|
|
542
535
|
});
|
|
543
536
|
}
|
|
544
|
-
|
|
537
|
+
let g = {
|
|
545
538
|
async acceptAnswer(t) {
|
|
546
539
|
if (u.ensureLive(), t?.v !== 1) throw new MeshPairingError("Unsupported answer version");
|
|
547
540
|
if (!t.peer?.id) throw new MeshPairingError("Answer is missing a peer id");
|
|
548
541
|
let n = p.get(t.sessionId);
|
|
549
542
|
if (!n) throw new MeshPairingError("Unknown or expired session");
|
|
550
|
-
if (n.
|
|
543
|
+
if (n.state !== "open") throw new MeshPairingError("Invitation already answered");
|
|
551
544
|
if (f.has(t.peer.id)) throw new MeshPairingError(`Peer "${t.peer.id}" is already connected`);
|
|
552
545
|
if (r() >= n.expiresAt) throw expireInvitation(t.sessionId), new MeshPairingError("Invitation expired");
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
546
|
+
n.state = "reserved";
|
|
547
|
+
let i;
|
|
548
|
+
try {
|
|
549
|
+
let r = await createProof(n.secret, t.sdp);
|
|
550
|
+
if (t.proof !== r) throw new MeshPairingError("Proof mismatch");
|
|
551
|
+
i = await e.approvePeer?.(t.peer) ?? !0;
|
|
552
|
+
} catch (e) {
|
|
553
|
+
throw releaseInvitation(t.sessionId, n), e;
|
|
554
|
+
}
|
|
555
|
+
if (u.ensureLive(), !i) throw u.emitTap({
|
|
556
556
|
peerId: t.peer.id,
|
|
557
557
|
type: "peer-rejected"
|
|
558
|
-
}), new MeshPairingError(`Peer "${t.peer.id}" rejected by host`);
|
|
558
|
+
}), discardInvitation(t.sessionId, n), recomputeStatus(), new MeshPairingError(`Peer "${t.peer.id}" rejected by host`);
|
|
559
559
|
if (u.emitTap({
|
|
560
560
|
peerId: t.peer.id,
|
|
561
561
|
type: "peer-approved"
|
|
562
|
-
}),
|
|
563
|
-
|
|
562
|
+
}), p.get(t.sessionId) !== n) throw new MeshPairingError("Invitation expired");
|
|
563
|
+
if (f.has(t.peer.id)) throw releaseInvitation(t.sessionId, n), new MeshPairingError(`Peer "${t.peer.id}" is already connected`);
|
|
564
|
+
consumeInvitation(t.sessionId, n);
|
|
564
565
|
let a = createPeerRecord(t.peer.id, t.peer.name, "guest"), s = waitChannelOpen(n.dc, n.pc, o, a.id, u.disposalCtrl.signal);
|
|
565
566
|
wirePeerTransport(a, n.pc, n.dc), f.set(a.id, a), recomputeStatus();
|
|
566
567
|
try {
|
|
@@ -599,13 +600,13 @@ function createMeshHost(e = {}) {
|
|
|
599
600
|
if (!h) throw o.close(), new MeshConnectionError("Local description has no SDP");
|
|
600
601
|
let g = setTimeout(() => expireInvitation(d), a);
|
|
601
602
|
return p.set(d, {
|
|
602
|
-
answered: !1,
|
|
603
603
|
dc: c,
|
|
604
604
|
expiresAt: m,
|
|
605
605
|
expiryTimer: g,
|
|
606
606
|
pc: o,
|
|
607
607
|
secret: f,
|
|
608
|
-
sessionId: d
|
|
608
|
+
sessionId: d,
|
|
609
|
+
state: "open"
|
|
609
610
|
}), u.emitTap({
|
|
610
611
|
sessionId: d,
|
|
611
612
|
type: "invitation-created"
|
|
@@ -661,6 +662,7 @@ function createMeshHost(e = {}) {
|
|
|
661
662
|
this.dispose();
|
|
662
663
|
}
|
|
663
664
|
};
|
|
665
|
+
return wireDisposalSignal(e.signal, () => g.dispose()), g;
|
|
664
666
|
}
|
|
665
667
|
// src/_base45.ts
|
|
666
668
|
var l = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:", u = new Map([...l].map((e, t) => [e, t]));
|