jsflow-sdk 1.1.2 → 1.2.0-rc.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/CHANGELOG.md +109 -0
- package/README.md +1 -1
- package/jsflow.d.ts +138 -4
- package/jsflow.js +283 -24
- package/package.json +1 -1
- package/sri.json +4 -4
- package/worker.js +682 -53
package/jsflow.js
CHANGED
|
@@ -1,17 +1,32 @@
|
|
|
1
|
-
// jsflow-sdk 1.1.
|
|
1
|
+
// jsflow-sdk 1.2.0-rc.1. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
|
|
2
2
|
|
|
3
3
|
// core/link-main.js
|
|
4
|
-
function
|
|
4
|
+
function selected(stats) {
|
|
5
5
|
const st = [...stats?.values?.() ?? stats ?? []], by = new Map(st.map((x) => [x.id, x]));
|
|
6
6
|
const tr = st.find((x) => x.type === "transport" && x.selectedCandidatePairId);
|
|
7
7
|
const pair = tr ? by.get(tr.selectedCandidatePairId) : st.find((x) => x.type === "candidate-pair" && (x.selected || x.nominated && x.state === "succeeded"));
|
|
8
|
+
return { by, pair };
|
|
9
|
+
}
|
|
10
|
+
function linkKind(stats) {
|
|
11
|
+
const { by, pair } = selected(stats);
|
|
8
12
|
if (!pair) return void 0;
|
|
9
13
|
const t = [by.get(pair.localCandidateId)?.candidateType, by.get(pair.remoteCandidateId)?.candidateType];
|
|
10
14
|
if (t.includes("relay")) return "relay";
|
|
11
15
|
if (t.includes("srflx") || t.includes("prflx")) return "reflexive";
|
|
12
16
|
return t[0] || t[1] ? "host" : void 0;
|
|
13
17
|
}
|
|
14
|
-
function
|
|
18
|
+
function pacedLink(stats, ours, farRelay = false) {
|
|
19
|
+
const { by, pair } = selected(stats);
|
|
20
|
+
if (!pair) return void 0;
|
|
21
|
+
const l = by.get(pair.localCandidateId), r = by.get(pair.remoteCandidateId);
|
|
22
|
+
if (l?.candidateType === "relay") return !!ours?.(l.url);
|
|
23
|
+
return r?.candidateType === "relay" && !!farRelay;
|
|
24
|
+
}
|
|
25
|
+
var turnHost = (url) => {
|
|
26
|
+
const a = String(url).replace(/^turns?:/i, "").replace(/[?].*$/, "").toLowerCase(), v6 = /^\[([^\]]*)\]/.exec(a);
|
|
27
|
+
return v6 ? v6[1] : a.replace(/:\d+$/, "");
|
|
28
|
+
};
|
|
29
|
+
function linkHost(worker, { iceServers = [], iceTransportPolicy, gatherCapMs = 1500, wire: sampleWire = true, onKinds = null, paceTurn = null, paceFar = false } = {}) {
|
|
15
30
|
const pcs = /* @__PURE__ */ new Map();
|
|
16
31
|
const post = (m, t) => worker.postMessage(m, t ?? []);
|
|
17
32
|
let relayWanted = iceServers.some((s) => [].concat(s.urls).some((u) => /^turns?:/.test(u)));
|
|
@@ -209,8 +224,10 @@ function linkHost(worker, { iceServers = [], iceTransportPolicy, gatherCapMs = 1
|
|
|
209
224
|
}, 1e4);
|
|
210
225
|
let onPath = null, pathTimer = null, reading = false, again = false;
|
|
211
226
|
const own = /* @__PURE__ */ new Map();
|
|
227
|
+
const paced = /* @__PURE__ */ new Map();
|
|
228
|
+
const reads = () => !!onPath || !!paceTurn;
|
|
212
229
|
async function readPaths() {
|
|
213
|
-
if (!
|
|
230
|
+
if (!reads()) return;
|
|
214
231
|
if (reading) {
|
|
215
232
|
again = true;
|
|
216
233
|
return;
|
|
@@ -226,20 +243,35 @@ function linkHost(worker, { iceServers = [], iceTransportPolicy, gatherCapMs = 1
|
|
|
226
243
|
}
|
|
227
244
|
if (pc.connectionState !== "connected") continue;
|
|
228
245
|
live.add(peer);
|
|
229
|
-
let
|
|
246
|
+
let st;
|
|
230
247
|
try {
|
|
231
|
-
|
|
248
|
+
st = await pc.getStats();
|
|
232
249
|
} catch {
|
|
250
|
+
continue;
|
|
233
251
|
}
|
|
234
|
-
|
|
235
|
-
if (
|
|
236
|
-
|
|
237
|
-
|
|
252
|
+
if (pcs.get(peer)?.pc !== pc) continue;
|
|
253
|
+
if (onPath) {
|
|
254
|
+
const k = linkKind(st), p = k === "relay" ? "relay" : k ? "direct" : void 0;
|
|
255
|
+
if (p && own.get(peer) !== p) {
|
|
256
|
+
own.set(peer, p);
|
|
257
|
+
onPath(peer, p);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
const on = pacedLink(st, paceTurn, paceFar);
|
|
261
|
+
if (on !== void 0 && paced.get(peer) !== on) {
|
|
262
|
+
paced.set(peer, on);
|
|
263
|
+
post({ op: "pace", peer, on });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
if (onPath) {
|
|
267
|
+
for (const peer of [...own.keys()]) if (!live.has(peer)) {
|
|
268
|
+
own.delete(peer);
|
|
269
|
+
onPath(peer, null);
|
|
238
270
|
}
|
|
239
271
|
}
|
|
240
|
-
for (const peer of [...
|
|
241
|
-
|
|
242
|
-
|
|
272
|
+
for (const peer of [...paced.keys()]) if (!live.has(peer)) {
|
|
273
|
+
paced.delete(peer);
|
|
274
|
+
post({ op: "pace", peer, on: false });
|
|
243
275
|
}
|
|
244
276
|
} finally {
|
|
245
277
|
reading = false;
|
|
@@ -250,7 +282,7 @@ function linkHost(worker, { iceServers = [], iceTransportPolicy, gatherCapMs = 1
|
|
|
250
282
|
}
|
|
251
283
|
}
|
|
252
284
|
function pathsSoon(connected = false) {
|
|
253
|
-
if (!
|
|
285
|
+
if (!reads()) return;
|
|
254
286
|
setTimeout(readPaths, 0);
|
|
255
287
|
if (connected) {
|
|
256
288
|
setTimeout(readPaths, 1e3);
|
|
@@ -261,8 +293,9 @@ function linkHost(worker, { iceServers = [], iceTransportPolicy, gatherCapMs = 1
|
|
|
261
293
|
if (onPath) return;
|
|
262
294
|
onPath = f;
|
|
263
295
|
readPaths();
|
|
264
|
-
pathTimer
|
|
296
|
+
pathTimer ??= setInterval(readPaths, 1e4);
|
|
265
297
|
}
|
|
298
|
+
if (paceTurn) pathTimer = setInterval(readPaths, 1e4);
|
|
266
299
|
const ownPath = (peer) => own.get(peer) ?? (pcs.get(peer)?.pc.connectionState === "connected" ? "pending" : void 0);
|
|
267
300
|
return { pcs, setIceServers, watchPaths, ownPath, pathsSoon, closeAll() {
|
|
268
301
|
clearInterval(kindsTimer);
|
|
@@ -285,7 +318,7 @@ function ticketRegion(ticket) {
|
|
|
285
318
|
return void 0;
|
|
286
319
|
}
|
|
287
320
|
}
|
|
288
|
-
var SDK_VERSION = "1.1
|
|
321
|
+
var SDK_VERSION = "1.2.0-rc.1";
|
|
289
322
|
var WIRE = 2;
|
|
290
323
|
var MAX_DATAGRAM = 1052;
|
|
291
324
|
var MAX_RELIABLE = 65536;
|
|
@@ -320,6 +353,24 @@ var JoinError = class extends Error {
|
|
|
320
353
|
this.docs = `${ERRORS_PAGE}#${this.code}`;
|
|
321
354
|
}
|
|
322
355
|
};
|
|
356
|
+
var StreamError = class extends Error {
|
|
357
|
+
constructor(code, { reason, delivered = 0 } = {}) {
|
|
358
|
+
super(`stream: ${code}${reason ? `: ${reason}` : ""}`);
|
|
359
|
+
this.name = "StreamError";
|
|
360
|
+
this.code = code;
|
|
361
|
+
this.delivered = delivered;
|
|
362
|
+
if (reason !== void 0) this.reason = reason;
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
var OutgoingStream = class extends WritableStream {
|
|
366
|
+
constructor(sink, id) {
|
|
367
|
+
super(sink, { highWaterMark: 1 });
|
|
368
|
+
this.id = id;
|
|
369
|
+
this.delivered = 0;
|
|
370
|
+
this.onprogress = null;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
var reasonOf = (r) => r === void 0 || r === null ? "" : String(r?.message ?? r);
|
|
323
374
|
var REOPEN = Object.freeze({ lo: 500, hi: 5e3, cap: 15e3 });
|
|
324
375
|
var reopenDelay = (n, r = Math.random(), { lo, hi, cap } = REOPEN) => n === 0 ? lo + r * (hi - lo) : r * Math.min(cap, hi * 2 ** (n - 1));
|
|
325
376
|
function homeAllowed(server, home) {
|
|
@@ -412,17 +463,24 @@ async function join(opts) {
|
|
|
412
463
|
const region = known ?? opts.region;
|
|
413
464
|
if (region !== void 0) server = REGION_ORIGINS[region];
|
|
414
465
|
}
|
|
415
|
-
const body = { app: opts.app, room: name, pub: pub ?? void 0, spub: spub ?? void 0, ticket: ticket ?? void 0, wire: WIRE, sdk: SDK_VERSION, platform: "web" };
|
|
466
|
+
const body = { app: opts.app, room: name, pub: pub ?? void 0, spub: spub ?? void 0, ticket: ticket ?? void 0, wire: WIRE, sdk: SDK_VERSION, platform: "web", hop: true };
|
|
416
467
|
const labOpts = {};
|
|
417
468
|
let base = server, r, hops = 0;
|
|
418
469
|
for (; ; ) {
|
|
419
470
|
r = await fetch(base + "/v2/join", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
|
|
420
|
-
if (r.status !== 409) break;
|
|
421
|
-
const b = await r.clone().json().catch(() => null);
|
|
422
|
-
if (
|
|
471
|
+
if (r.status !== 409 && !r.ok) break;
|
|
472
|
+
const b = await r.clone().json().catch(() => null), to = r.ok ? b?.hop : b?.err === "wrong_home" ? b : null;
|
|
473
|
+
if (!to) break;
|
|
474
|
+
if (hops >= 2 || !homeAllowed(server, to.home)) {
|
|
475
|
+
if (r.ok) {
|
|
476
|
+
worker.terminate();
|
|
477
|
+
throw new JoinError(409, { err: "wrong_home", home: to.home, ...to.near ? { near: to.near } : {} });
|
|
478
|
+
}
|
|
479
|
+
break;
|
|
480
|
+
}
|
|
423
481
|
hops++;
|
|
424
|
-
base =
|
|
425
|
-
if (
|
|
482
|
+
base = to.home;
|
|
483
|
+
if (to.near && !body.near) body.near = to.near;
|
|
426
484
|
}
|
|
427
485
|
if (!r.ok) {
|
|
428
486
|
worker.terminate();
|
|
@@ -436,7 +494,16 @@ async function join(opts) {
|
|
|
436
494
|
if (!ended()) fetch(home + "/v2/link", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ room, id, inc, key, links: c }) }).catch(() => {
|
|
437
495
|
});
|
|
438
496
|
};
|
|
439
|
-
const
|
|
497
|
+
const turnHosts = (list) => new Set((list ?? []).flatMap((s) => [].concat(s.urls)).filter((u) => /^turns?:/i.test(u)).map(turnHost));
|
|
498
|
+
let ourTurn = turnHosts(iceServers);
|
|
499
|
+
const host = linkHost(worker, {
|
|
500
|
+
iceServers: opts.iceServers ?? iceServers ?? [],
|
|
501
|
+
iceTransportPolicy: opts.iceTransportPolicy,
|
|
502
|
+
wire: false,
|
|
503
|
+
onKinds: beacon,
|
|
504
|
+
paceTurn: (url) => url ? ourTurn.has(turnHost(url)) : !opts.iceServers,
|
|
505
|
+
paceFar: !opts.iceServers
|
|
506
|
+
});
|
|
440
507
|
const ua = navigator.userAgent, webkitDesktop = /Safari/.test(ua) && !/Chrome|Chromium|Edg|Firefox/.test(ua);
|
|
441
508
|
const bye = /iPhone|iPad|Android/.test(ua) || webkitDesktop;
|
|
442
509
|
worker.postMessage({ op: "init", id, inc, key, room, server: home, near, token, tokenKeys, refreshAt, bye: opts.bye ?? bye, opts: { ua, relayOnly: opts.iceTransportPolicy === "relay", ...labOpts } });
|
|
@@ -528,8 +595,9 @@ async function join(opts) {
|
|
|
528
595
|
case "peer":
|
|
529
596
|
if (m.up) peers.add(m.id);
|
|
530
597
|
else peers.delete(m.id);
|
|
531
|
-
emit("peer", m.id, m.up);
|
|
598
|
+
emit("peer", m.id, m.up, { why: m.up ? null : m.why ?? "lost" });
|
|
532
599
|
return repath(m.id);
|
|
600
|
+
// 1.2: info.why
|
|
533
601
|
case "message":
|
|
534
602
|
return emit("message", m.from, m.data, { channel: m.chan, broadcast: !!m.broadcast, reliable: !!m.reliable });
|
|
535
603
|
case "state":
|
|
@@ -555,6 +623,36 @@ async function join(opts) {
|
|
|
555
623
|
waiting.delete(m.mid);
|
|
556
624
|
return w?.ok(m.op === "pong" ? m.r : m.ok);
|
|
557
625
|
}
|
|
626
|
+
// 1.2 streams
|
|
627
|
+
case "sopened":
|
|
628
|
+
case "swrote": {
|
|
629
|
+
const w = waiting.get(m.mid);
|
|
630
|
+
waiting.delete(m.mid);
|
|
631
|
+
return w?.ok(m);
|
|
632
|
+
}
|
|
633
|
+
case "sacc":
|
|
634
|
+
return outS.get(`${m.peer}:${m.id}`)?.accepted();
|
|
635
|
+
case "scredit":
|
|
636
|
+
return outS.get(`${m.peer}:${m.id}`)?.progress(m.read);
|
|
637
|
+
case "sdone":
|
|
638
|
+
return outS.get(`${m.peer}:${m.id}`)?.done();
|
|
639
|
+
case "sopen":
|
|
640
|
+
return incoming(m.peer, m.id, m.channel, m.meta);
|
|
641
|
+
case "sdata":
|
|
642
|
+
return inS.get(`${m.peer}:${m.id}`)?.push(m.data);
|
|
643
|
+
case "sfin":
|
|
644
|
+
return inS.get(`${m.peer}:${m.id}`)?.end();
|
|
645
|
+
case "serr": {
|
|
646
|
+
const e2 = new StreamError(m.code, { reason: m.reason || void 0, delivered: m.delivered ?? 0 }), k2 = `${m.peer}:${m.id}`;
|
|
647
|
+
if (m.mid !== void 0) {
|
|
648
|
+
const w = waiting.get(m.mid);
|
|
649
|
+
waiting.delete(m.mid);
|
|
650
|
+
w?.no(e2);
|
|
651
|
+
}
|
|
652
|
+
if (m.side === "out") outS.get(k2)?.fail(e2);
|
|
653
|
+
else inS.get(k2)?.fail(e2);
|
|
654
|
+
return;
|
|
655
|
+
}
|
|
558
656
|
case "undelivered":
|
|
559
657
|
return emit("undelivered", m.peer, m.n, m.why);
|
|
560
658
|
case "user":
|
|
@@ -564,6 +662,7 @@ async function join(opts) {
|
|
|
564
662
|
if (state === "away") state = "joining";
|
|
565
663
|
return openSse();
|
|
566
664
|
case "ice":
|
|
665
|
+
ourTurn = turnHosts(m.iceServers);
|
|
567
666
|
if (!opts.iceServers) host.setIceServers(m.iceServers);
|
|
568
667
|
return;
|
|
569
668
|
// P3: fresh TURN credentials (an app's own list stays)
|
|
@@ -581,6 +680,144 @@ async function join(opts) {
|
|
|
581
680
|
}
|
|
582
681
|
}
|
|
583
682
|
});
|
|
683
|
+
const outS = /* @__PURE__ */ new Map(), inS = /* @__PURE__ */ new Map();
|
|
684
|
+
function outgoing(to, id2, opened, refused) {
|
|
685
|
+
const k2 = `${to}:${id2}`;
|
|
686
|
+
let ctrl = null, isOpen = false, closing = null, progAt = 0, progT = null;
|
|
687
|
+
const stream = new OutgoingStream({
|
|
688
|
+
start(c) {
|
|
689
|
+
ctrl = c;
|
|
690
|
+
},
|
|
691
|
+
write(chunk) {
|
|
692
|
+
if (!(chunk instanceof Uint8Array)) throw new TypeError("openStream: write Uint8Array chunks");
|
|
693
|
+
const d = chunk.slice(), w = ++mid, sig = ctrl.signal;
|
|
694
|
+
return new Promise((ok, no) => {
|
|
695
|
+
const onAbort = () => {
|
|
696
|
+
waiting.delete(w);
|
|
697
|
+
outS.delete(k2);
|
|
698
|
+
clearTimeout(progT);
|
|
699
|
+
worker.postMessage({ op: "sabort", peer: to, id: id2, reason: reasonOf(sig.reason) });
|
|
700
|
+
no(sig.reason);
|
|
701
|
+
};
|
|
702
|
+
if (sig?.aborted) return onAbort();
|
|
703
|
+
sig?.addEventListener("abort", onAbort, { once: true });
|
|
704
|
+
const off = () => sig?.removeEventListener("abort", onAbort);
|
|
705
|
+
waiting.set(w, { ok: (r2) => {
|
|
706
|
+
off();
|
|
707
|
+
r2?.gone ? no(new StreamError("aborted")) : ok();
|
|
708
|
+
}, no: (e) => {
|
|
709
|
+
off();
|
|
710
|
+
no(e);
|
|
711
|
+
} });
|
|
712
|
+
worker.postMessage({ op: "swrite", peer: to, id: id2, data: d, mid: w }, [d.buffer]);
|
|
713
|
+
});
|
|
714
|
+
},
|
|
715
|
+
close() {
|
|
716
|
+
return new Promise((ok, no) => {
|
|
717
|
+
closing = { ok, no };
|
|
718
|
+
worker.postMessage({ op: "sclose", peer: to, id: id2 });
|
|
719
|
+
});
|
|
720
|
+
},
|
|
721
|
+
abort(reason) {
|
|
722
|
+
outS.delete(k2);
|
|
723
|
+
clearTimeout(progT);
|
|
724
|
+
worker.postMessage({ op: "sabort", peer: to, id: id2, reason: reasonOf(reason) });
|
|
725
|
+
}
|
|
726
|
+
}, id2);
|
|
727
|
+
const tell = () => {
|
|
728
|
+
progT = null;
|
|
729
|
+
progAt = Date.now();
|
|
730
|
+
try {
|
|
731
|
+
stream.onprogress?.(stream.delivered);
|
|
732
|
+
} catch {
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
outS.set(k2, {
|
|
736
|
+
accepted() {
|
|
737
|
+
isOpen = true;
|
|
738
|
+
opened(stream);
|
|
739
|
+
},
|
|
740
|
+
progress(read) {
|
|
741
|
+
stream.delivered = read;
|
|
742
|
+
if (!progT) progT = setTimeout(tell, Math.max(0, 100 - (Date.now() - progAt)));
|
|
743
|
+
},
|
|
744
|
+
done() {
|
|
745
|
+
outS.delete(k2);
|
|
746
|
+
clearTimeout(progT);
|
|
747
|
+
tell();
|
|
748
|
+
closing?.ok();
|
|
749
|
+
},
|
|
750
|
+
fail(e) {
|
|
751
|
+
outS.delete(k2);
|
|
752
|
+
clearTimeout(progT);
|
|
753
|
+
if (!isOpen) return refused(e);
|
|
754
|
+
closing ? closing.no(e) : ctrl?.error(e);
|
|
755
|
+
}
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
function incoming(peer, id2, channel, meta) {
|
|
759
|
+
const k2 = `${peer}:${id2}`;
|
|
760
|
+
if (!handlers.get("stream")?.size) return worker.postMessage({ op: "scancel", peer, id: id2, reason: "no listener" });
|
|
761
|
+
const q = [];
|
|
762
|
+
let ctrl = null, waiter = null, ended2 = false;
|
|
763
|
+
const give = () => {
|
|
764
|
+
const d = q.shift();
|
|
765
|
+
ctrl.enqueue(d);
|
|
766
|
+
worker.postMessage({ op: "sread", peer, id: id2, n: d.length });
|
|
767
|
+
};
|
|
768
|
+
const finish = () => {
|
|
769
|
+
inS.delete(k2);
|
|
770
|
+
ctrl.close();
|
|
771
|
+
};
|
|
772
|
+
const readable = new ReadableStream({
|
|
773
|
+
start(c) {
|
|
774
|
+
ctrl = c;
|
|
775
|
+
},
|
|
776
|
+
pull() {
|
|
777
|
+
if (q.length) {
|
|
778
|
+
give();
|
|
779
|
+
if (!q.length && ended2) finish();
|
|
780
|
+
return;
|
|
781
|
+
}
|
|
782
|
+
if (ended2) return finish();
|
|
783
|
+
return new Promise((ok) => {
|
|
784
|
+
waiter = ok;
|
|
785
|
+
});
|
|
786
|
+
},
|
|
787
|
+
cancel(reason) {
|
|
788
|
+
inS.delete(k2);
|
|
789
|
+
worker.postMessage({ op: "scancel", peer, id: id2, reason: reasonOf(reason) });
|
|
790
|
+
}
|
|
791
|
+
}, { highWaterMark: 0 });
|
|
792
|
+
const wake = () => {
|
|
793
|
+
const w = waiter;
|
|
794
|
+
waiter = null;
|
|
795
|
+
w?.();
|
|
796
|
+
};
|
|
797
|
+
inS.set(k2, {
|
|
798
|
+
push(d) {
|
|
799
|
+
q.push(d);
|
|
800
|
+
if (waiter) {
|
|
801
|
+
give();
|
|
802
|
+
if (!q.length && ended2) finish();
|
|
803
|
+
wake();
|
|
804
|
+
}
|
|
805
|
+
},
|
|
806
|
+
end() {
|
|
807
|
+
ended2 = true;
|
|
808
|
+
if (waiter && !q.length) {
|
|
809
|
+
finish();
|
|
810
|
+
wake();
|
|
811
|
+
}
|
|
812
|
+
},
|
|
813
|
+
fail(e) {
|
|
814
|
+
inS.delete(k2);
|
|
815
|
+
ctrl.error(e);
|
|
816
|
+
wake();
|
|
817
|
+
}
|
|
818
|
+
});
|
|
819
|
+
emit("stream", peer, readable, { id: id2, channel, meta });
|
|
820
|
+
}
|
|
584
821
|
const tellLeave = () => {
|
|
585
822
|
const body2 = JSON.stringify({ room, id, inc, key });
|
|
586
823
|
try {
|
|
@@ -646,6 +883,7 @@ async function join(opts) {
|
|
|
646
883
|
if (!handlers.has(ev)) handlers.set(ev, /* @__PURE__ */ new Set());
|
|
647
884
|
handlers.get(ev).add(f);
|
|
648
885
|
if (ev === "path") watchPaths();
|
|
886
|
+
if (ev === "stream" && handlers.get(ev).size === 1) worker.postMessage({ op: "slisten", on: true });
|
|
649
887
|
return this;
|
|
650
888
|
},
|
|
651
889
|
/** (1.1.0) How this peer reaches that one: 'direct' or 'relay' over its own link, 'mesh' through other peers, null if not reachable. */
|
|
@@ -655,8 +893,28 @@ async function join(opts) {
|
|
|
655
893
|
},
|
|
656
894
|
off(ev, f) {
|
|
657
895
|
handlers.get(ev)?.delete(f);
|
|
896
|
+
if (ev === "stream" && !handlers.get(ev)?.size) worker.postMessage({ op: "slisten", on: false });
|
|
658
897
|
return this;
|
|
659
898
|
},
|
|
899
|
+
/**
|
|
900
|
+
* (1.2) Open a stream to a peer: resolves with a WritableStream (plus id, delivered, onprogress) once the peer
|
|
901
|
+
* accepted it; rejects with a StreamError ('unsupported', 'not_direct', 'refused', 'unreachable').
|
|
902
|
+
*/
|
|
903
|
+
openStream(to, o = {}) {
|
|
904
|
+
live();
|
|
905
|
+
const channel = chanOf(o);
|
|
906
|
+
if (o.path !== void 0 && o.path !== "direct") throw new TypeError("openStream: path is 'direct' or not given");
|
|
907
|
+
if (o.meta !== void 0) {
|
|
908
|
+
const j2 = JSON.stringify(o.meta);
|
|
909
|
+
if (j2 === void 0 || new TextEncoder().encode(j2).length > 4096) throw new RangeError("openStream: meta must be JSON of at most 4096 bytes");
|
|
910
|
+
}
|
|
911
|
+
if (o.path === "direct" && host.ownPath(to) !== "direct") return Promise.reject(new StreamError("not_direct"));
|
|
912
|
+
const m = ++mid;
|
|
913
|
+
return new Promise((ok, no) => {
|
|
914
|
+
waiting.set(m, { ok: (r2) => r2.err ? no(r2.err === "bad" ? new TypeError(r2.message) : new StreamError(r2.err)) : outgoing(to, r2.id, ok, no), no });
|
|
915
|
+
worker.postMessage({ op: "sopen", to, chan: channel, meta: o.meta, mid: m });
|
|
916
|
+
});
|
|
917
|
+
},
|
|
660
918
|
/** A datagram to one peer: fast, may be lost, ≤ MAX_DATAGRAM bytes. */
|
|
661
919
|
send(to, data, o) {
|
|
662
920
|
live();
|
|
@@ -721,6 +979,7 @@ export {
|
|
|
721
979
|
MAX_RELIABLE,
|
|
722
980
|
SDK_VERSION,
|
|
723
981
|
SERVER,
|
|
982
|
+
StreamError,
|
|
724
983
|
WIRE,
|
|
725
984
|
join,
|
|
726
985
|
text
|
package/package.json
CHANGED
package/sri.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.1
|
|
3
|
-
"jsflow.js": "sha384-
|
|
4
|
-
"worker.js": "sha384-
|
|
5
|
-
"jsflow.d.ts": "sha384-
|
|
2
|
+
"version": "1.2.0-rc.1",
|
|
3
|
+
"jsflow.js": "sha384-nOSP3w3zIVHP8n3U108NEDxZMEQuZeUl7V22Oap93w2x7KsLRlFTd9t8IPxM8yWx",
|
|
4
|
+
"worker.js": "sha384-BJ2DsQbUpmicAb8QKY9rMYsqp2TmdFQao9SQ08ovpkPsyBKDU17iExyqilo5Sxk/",
|
|
5
|
+
"jsflow.d.ts": "sha384-MvNOfWH25DatQq04hCHJVJs+GnoT3T7up7Fy8FwqDMXvdPTPAzG6omyuKRcT/SG8"
|
|
6
6
|
}
|