jsflow-sdk 1.1.3 → 1.2.0-rc.2
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 +121 -0
- package/jsflow.d.ts +160 -4
- package/jsflow.js +285 -25
- package/package.json +1 -1
- package/sri.json +4 -4
- package/worker.js +681 -48
package/jsflow.js
CHANGED
|
@@ -1,17 +1,32 @@
|
|
|
1
|
-
// jsflow-sdk 1.
|
|
1
|
+
// jsflow-sdk 1.2.0-rc.2. 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.
|
|
321
|
+
var SDK_VERSION = "1.2.0-rc.2";
|
|
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) {
|
|
@@ -339,6 +390,7 @@ function homeAllowed(server, home) {
|
|
|
339
390
|
}
|
|
340
391
|
async function join(opts) {
|
|
341
392
|
if (opts.region !== void 0 && !Object.hasOwn(REGION_ORIGINS, opts.region)) throw new TypeError(`join: region must be one of ${Object.keys(REGION_ORIGINS).join(", ")}`);
|
|
393
|
+
if (opts.holdMs !== void 0 && !(typeof opts.holdMs === "number" && opts.holdMs >= 0 && opts.holdMs <= 3e4)) throw new RangeError("join: holdMs must be a number from 0 to 30000");
|
|
342
394
|
let server = String(opts.server ?? SERVER).replace(/\/$/, "");
|
|
343
395
|
const name = opts.room;
|
|
344
396
|
if (!name) throw new TypeError("join: room is required");
|
|
@@ -412,17 +464,24 @@ async function join(opts) {
|
|
|
412
464
|
const region = known ?? opts.region;
|
|
413
465
|
if (region !== void 0) server = REGION_ORIGINS[region];
|
|
414
466
|
}
|
|
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" };
|
|
467
|
+
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
468
|
const labOpts = {};
|
|
417
469
|
let base = server, r, hops = 0;
|
|
418
470
|
for (; ; ) {
|
|
419
471
|
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 (
|
|
472
|
+
if (r.status !== 409 && !r.ok) break;
|
|
473
|
+
const b = await r.clone().json().catch(() => null), to = r.ok ? b?.hop : b?.err === "wrong_home" ? b : null;
|
|
474
|
+
if (!to) break;
|
|
475
|
+
if (hops >= 2 || !homeAllowed(server, to.home)) {
|
|
476
|
+
if (r.ok) {
|
|
477
|
+
worker.terminate();
|
|
478
|
+
throw new JoinError(409, { err: "wrong_home", home: to.home, ...to.near ? { near: to.near } : {} });
|
|
479
|
+
}
|
|
480
|
+
break;
|
|
481
|
+
}
|
|
423
482
|
hops++;
|
|
424
|
-
base =
|
|
425
|
-
if (
|
|
483
|
+
base = to.home;
|
|
484
|
+
if (to.near && !body.near) body.near = to.near;
|
|
426
485
|
}
|
|
427
486
|
if (!r.ok) {
|
|
428
487
|
worker.terminate();
|
|
@@ -436,10 +495,19 @@ async function join(opts) {
|
|
|
436
495
|
if (!ended()) fetch(home + "/v2/link", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ room, id, inc, key, links: c }) }).catch(() => {
|
|
437
496
|
});
|
|
438
497
|
};
|
|
439
|
-
const
|
|
498
|
+
const turnHosts = (list) => new Set((list ?? []).flatMap((s) => [].concat(s.urls)).filter((u) => /^turns?:/i.test(u)).map(turnHost));
|
|
499
|
+
let ourTurn = turnHosts(iceServers);
|
|
500
|
+
const host = linkHost(worker, {
|
|
501
|
+
iceServers: opts.iceServers ?? iceServers ?? [],
|
|
502
|
+
iceTransportPolicy: opts.iceTransportPolicy,
|
|
503
|
+
wire: false,
|
|
504
|
+
onKinds: beacon,
|
|
505
|
+
paceTurn: (url) => url ? ourTurn.has(turnHost(url)) : !opts.iceServers,
|
|
506
|
+
paceFar: !opts.iceServers
|
|
507
|
+
});
|
|
440
508
|
const ua = navigator.userAgent, webkitDesktop = /Safari/.test(ua) && !/Chrome|Chromium|Edg|Firefox/.test(ua);
|
|
441
509
|
const bye = /iPhone|iPad|Android/.test(ua) || webkitDesktop;
|
|
442
|
-
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 } });
|
|
510
|
+
worker.postMessage({ op: "init", id, inc, key, room, server: home, near, token, tokenKeys, refreshAt, bye: opts.bye ?? bye, opts: { ua, relayOnly: opts.iceTransportPolicy === "relay", holdMs: opts.holdMs, ...labOpts } });
|
|
443
511
|
let lastId = null, lastEventAt = Date.now();
|
|
444
512
|
const ended = () => state === "left" || state === "superseded" || state === "away" || state === "evicted";
|
|
445
513
|
const openSse = () => {
|
|
@@ -528,8 +596,9 @@ async function join(opts) {
|
|
|
528
596
|
case "peer":
|
|
529
597
|
if (m.up) peers.add(m.id);
|
|
530
598
|
else peers.delete(m.id);
|
|
531
|
-
emit("peer", m.id, m.up);
|
|
599
|
+
emit("peer", m.id, m.up, { why: m.up ? null : m.why ?? "lost", unreachableMs: m.up ? null : m.unreachableMs ?? null });
|
|
532
600
|
return repath(m.id);
|
|
601
|
+
// 1.2: info.why, info.unreachableMs
|
|
533
602
|
case "message":
|
|
534
603
|
return emit("message", m.from, m.data, { channel: m.chan, broadcast: !!m.broadcast, reliable: !!m.reliable });
|
|
535
604
|
case "state":
|
|
@@ -555,6 +624,36 @@ async function join(opts) {
|
|
|
555
624
|
waiting.delete(m.mid);
|
|
556
625
|
return w?.ok(m.op === "pong" ? m.r : m.ok);
|
|
557
626
|
}
|
|
627
|
+
// 1.2 streams
|
|
628
|
+
case "sopened":
|
|
629
|
+
case "swrote": {
|
|
630
|
+
const w = waiting.get(m.mid);
|
|
631
|
+
waiting.delete(m.mid);
|
|
632
|
+
return w?.ok(m);
|
|
633
|
+
}
|
|
634
|
+
case "sacc":
|
|
635
|
+
return outS.get(`${m.peer}:${m.id}`)?.accepted();
|
|
636
|
+
case "scredit":
|
|
637
|
+
return outS.get(`${m.peer}:${m.id}`)?.progress(m.read);
|
|
638
|
+
case "sdone":
|
|
639
|
+
return outS.get(`${m.peer}:${m.id}`)?.done();
|
|
640
|
+
case "sopen":
|
|
641
|
+
return incoming(m.peer, m.id, m.channel, m.meta);
|
|
642
|
+
case "sdata":
|
|
643
|
+
return inS.get(`${m.peer}:${m.id}`)?.push(m.data);
|
|
644
|
+
case "sfin":
|
|
645
|
+
return inS.get(`${m.peer}:${m.id}`)?.end();
|
|
646
|
+
case "serr": {
|
|
647
|
+
const e2 = new StreamError(m.code, { reason: m.reason || void 0, delivered: m.delivered ?? 0 }), k2 = `${m.peer}:${m.id}`;
|
|
648
|
+
if (m.mid !== void 0) {
|
|
649
|
+
const w = waiting.get(m.mid);
|
|
650
|
+
waiting.delete(m.mid);
|
|
651
|
+
w?.no(e2);
|
|
652
|
+
}
|
|
653
|
+
if (m.side === "out") outS.get(k2)?.fail(e2);
|
|
654
|
+
else inS.get(k2)?.fail(e2);
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
558
657
|
case "undelivered":
|
|
559
658
|
return emit("undelivered", m.peer, m.n, m.why);
|
|
560
659
|
case "user":
|
|
@@ -564,6 +663,7 @@ async function join(opts) {
|
|
|
564
663
|
if (state === "away") state = "joining";
|
|
565
664
|
return openSse();
|
|
566
665
|
case "ice":
|
|
666
|
+
ourTurn = turnHosts(m.iceServers);
|
|
567
667
|
if (!opts.iceServers) host.setIceServers(m.iceServers);
|
|
568
668
|
return;
|
|
569
669
|
// P3: fresh TURN credentials (an app's own list stays)
|
|
@@ -581,6 +681,144 @@ async function join(opts) {
|
|
|
581
681
|
}
|
|
582
682
|
}
|
|
583
683
|
});
|
|
684
|
+
const outS = /* @__PURE__ */ new Map(), inS = /* @__PURE__ */ new Map();
|
|
685
|
+
function outgoing(to, id2, opened, refused) {
|
|
686
|
+
const k2 = `${to}:${id2}`;
|
|
687
|
+
let ctrl = null, isOpen = false, closing = null, progAt = 0, progT = null;
|
|
688
|
+
const stream = new OutgoingStream({
|
|
689
|
+
start(c) {
|
|
690
|
+
ctrl = c;
|
|
691
|
+
},
|
|
692
|
+
write(chunk) {
|
|
693
|
+
if (!(chunk instanceof Uint8Array)) throw new TypeError("openStream: write Uint8Array chunks");
|
|
694
|
+
const d = chunk.slice(), w = ++mid, sig = ctrl.signal;
|
|
695
|
+
return new Promise((ok, no) => {
|
|
696
|
+
const onAbort = () => {
|
|
697
|
+
waiting.delete(w);
|
|
698
|
+
outS.delete(k2);
|
|
699
|
+
clearTimeout(progT);
|
|
700
|
+
worker.postMessage({ op: "sabort", peer: to, id: id2, reason: reasonOf(sig.reason) });
|
|
701
|
+
no(sig.reason);
|
|
702
|
+
};
|
|
703
|
+
if (sig?.aborted) return onAbort();
|
|
704
|
+
sig?.addEventListener("abort", onAbort, { once: true });
|
|
705
|
+
const off = () => sig?.removeEventListener("abort", onAbort);
|
|
706
|
+
waiting.set(w, { ok: (r2) => {
|
|
707
|
+
off();
|
|
708
|
+
r2?.gone ? no(new StreamError("aborted")) : ok();
|
|
709
|
+
}, no: (e) => {
|
|
710
|
+
off();
|
|
711
|
+
no(e);
|
|
712
|
+
} });
|
|
713
|
+
worker.postMessage({ op: "swrite", peer: to, id: id2, data: d, mid: w }, [d.buffer]);
|
|
714
|
+
});
|
|
715
|
+
},
|
|
716
|
+
close() {
|
|
717
|
+
return new Promise((ok, no) => {
|
|
718
|
+
closing = { ok, no };
|
|
719
|
+
worker.postMessage({ op: "sclose", peer: to, id: id2 });
|
|
720
|
+
});
|
|
721
|
+
},
|
|
722
|
+
abort(reason) {
|
|
723
|
+
outS.delete(k2);
|
|
724
|
+
clearTimeout(progT);
|
|
725
|
+
worker.postMessage({ op: "sabort", peer: to, id: id2, reason: reasonOf(reason) });
|
|
726
|
+
}
|
|
727
|
+
}, id2);
|
|
728
|
+
const tell = () => {
|
|
729
|
+
progT = null;
|
|
730
|
+
progAt = Date.now();
|
|
731
|
+
try {
|
|
732
|
+
stream.onprogress?.(stream.delivered);
|
|
733
|
+
} catch {
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
outS.set(k2, {
|
|
737
|
+
accepted() {
|
|
738
|
+
isOpen = true;
|
|
739
|
+
opened(stream);
|
|
740
|
+
},
|
|
741
|
+
progress(read) {
|
|
742
|
+
stream.delivered = read;
|
|
743
|
+
if (!progT) progT = setTimeout(tell, Math.max(0, 100 - (Date.now() - progAt)));
|
|
744
|
+
},
|
|
745
|
+
done() {
|
|
746
|
+
outS.delete(k2);
|
|
747
|
+
clearTimeout(progT);
|
|
748
|
+
tell();
|
|
749
|
+
closing?.ok();
|
|
750
|
+
},
|
|
751
|
+
fail(e) {
|
|
752
|
+
outS.delete(k2);
|
|
753
|
+
clearTimeout(progT);
|
|
754
|
+
if (!isOpen) return refused(e);
|
|
755
|
+
closing ? closing.no(e) : ctrl?.error(e);
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
function incoming(peer, id2, channel, meta) {
|
|
760
|
+
const k2 = `${peer}:${id2}`;
|
|
761
|
+
if (!handlers.get("stream")?.size) return worker.postMessage({ op: "scancel", peer, id: id2, reason: "no listener" });
|
|
762
|
+
const q = [];
|
|
763
|
+
let ctrl = null, waiter = null, ended2 = false;
|
|
764
|
+
const give = () => {
|
|
765
|
+
const d = q.shift();
|
|
766
|
+
ctrl.enqueue(d);
|
|
767
|
+
worker.postMessage({ op: "sread", peer, id: id2, n: d.length });
|
|
768
|
+
};
|
|
769
|
+
const finish = () => {
|
|
770
|
+
inS.delete(k2);
|
|
771
|
+
ctrl.close();
|
|
772
|
+
};
|
|
773
|
+
const readable = new ReadableStream({
|
|
774
|
+
start(c) {
|
|
775
|
+
ctrl = c;
|
|
776
|
+
},
|
|
777
|
+
pull() {
|
|
778
|
+
if (q.length) {
|
|
779
|
+
give();
|
|
780
|
+
if (!q.length && ended2) finish();
|
|
781
|
+
return;
|
|
782
|
+
}
|
|
783
|
+
if (ended2) return finish();
|
|
784
|
+
return new Promise((ok) => {
|
|
785
|
+
waiter = ok;
|
|
786
|
+
});
|
|
787
|
+
},
|
|
788
|
+
cancel(reason) {
|
|
789
|
+
inS.delete(k2);
|
|
790
|
+
worker.postMessage({ op: "scancel", peer, id: id2, reason: reasonOf(reason) });
|
|
791
|
+
}
|
|
792
|
+
}, { highWaterMark: 0 });
|
|
793
|
+
const wake = () => {
|
|
794
|
+
const w = waiter;
|
|
795
|
+
waiter = null;
|
|
796
|
+
w?.();
|
|
797
|
+
};
|
|
798
|
+
inS.set(k2, {
|
|
799
|
+
push(d) {
|
|
800
|
+
q.push(d);
|
|
801
|
+
if (waiter) {
|
|
802
|
+
give();
|
|
803
|
+
if (!q.length && ended2) finish();
|
|
804
|
+
wake();
|
|
805
|
+
}
|
|
806
|
+
},
|
|
807
|
+
end() {
|
|
808
|
+
ended2 = true;
|
|
809
|
+
if (waiter && !q.length) {
|
|
810
|
+
finish();
|
|
811
|
+
wake();
|
|
812
|
+
}
|
|
813
|
+
},
|
|
814
|
+
fail(e) {
|
|
815
|
+
inS.delete(k2);
|
|
816
|
+
ctrl.error(e);
|
|
817
|
+
wake();
|
|
818
|
+
}
|
|
819
|
+
});
|
|
820
|
+
emit("stream", peer, readable, { id: id2, channel, meta });
|
|
821
|
+
}
|
|
584
822
|
const tellLeave = () => {
|
|
585
823
|
const body2 = JSON.stringify({ room, id, inc, key });
|
|
586
824
|
try {
|
|
@@ -646,6 +884,7 @@ async function join(opts) {
|
|
|
646
884
|
if (!handlers.has(ev)) handlers.set(ev, /* @__PURE__ */ new Set());
|
|
647
885
|
handlers.get(ev).add(f);
|
|
648
886
|
if (ev === "path") watchPaths();
|
|
887
|
+
if (ev === "stream" && handlers.get(ev).size === 1) worker.postMessage({ op: "slisten", on: true });
|
|
649
888
|
return this;
|
|
650
889
|
},
|
|
651
890
|
/** (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 +894,28 @@ async function join(opts) {
|
|
|
655
894
|
},
|
|
656
895
|
off(ev, f) {
|
|
657
896
|
handlers.get(ev)?.delete(f);
|
|
897
|
+
if (ev === "stream" && !handlers.get(ev)?.size) worker.postMessage({ op: "slisten", on: false });
|
|
658
898
|
return this;
|
|
659
899
|
},
|
|
900
|
+
/**
|
|
901
|
+
* (1.2) Open a stream to a peer: resolves with a WritableStream (plus id, delivered, onprogress) once the peer
|
|
902
|
+
* accepted it; rejects with a StreamError ('unsupported', 'not_direct', 'refused', 'unreachable').
|
|
903
|
+
*/
|
|
904
|
+
openStream(to, o = {}) {
|
|
905
|
+
live();
|
|
906
|
+
const channel = chanOf(o);
|
|
907
|
+
if (o.path !== void 0 && o.path !== "direct") throw new TypeError("openStream: path is 'direct' or not given");
|
|
908
|
+
if (o.meta !== void 0) {
|
|
909
|
+
const j2 = JSON.stringify(o.meta);
|
|
910
|
+
if (j2 === void 0 || new TextEncoder().encode(j2).length > 4096) throw new RangeError("openStream: meta must be JSON of at most 4096 bytes");
|
|
911
|
+
}
|
|
912
|
+
if (o.path === "direct" && host.ownPath(to) !== "direct") return Promise.reject(new StreamError("not_direct"));
|
|
913
|
+
const m = ++mid;
|
|
914
|
+
return new Promise((ok, no) => {
|
|
915
|
+
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 });
|
|
916
|
+
worker.postMessage({ op: "sopen", to, chan: channel, meta: o.meta, mid: m });
|
|
917
|
+
});
|
|
918
|
+
},
|
|
660
919
|
/** A datagram to one peer: fast, may be lost, ≤ MAX_DATAGRAM bytes. */
|
|
661
920
|
send(to, data, o) {
|
|
662
921
|
live();
|
|
@@ -721,6 +980,7 @@ export {
|
|
|
721
980
|
MAX_RELIABLE,
|
|
722
981
|
SDK_VERSION,
|
|
723
982
|
SERVER,
|
|
983
|
+
StreamError,
|
|
724
984
|
WIRE,
|
|
725
985
|
join,
|
|
726
986
|
text
|
package/package.json
CHANGED
package/sri.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.
|
|
3
|
-
"jsflow.js": "sha384-
|
|
4
|
-
"worker.js": "sha384-
|
|
5
|
-
"jsflow.d.ts": "sha384-
|
|
2
|
+
"version": "1.2.0-rc.2",
|
|
3
|
+
"jsflow.js": "sha384-Cvx3Gg4mp6lcPVirCvKsCs/gZjTWj/KkR5VDH56cWxoByFbw2rTKivoiDQbatLjd",
|
|
4
|
+
"worker.js": "sha384-NlKZwa2oIOzDciQiR2pcIjLgzSqGAYOA6xlcnVE9MmHJpNpKP0fojOZONcoqhXtM",
|
|
5
|
+
"jsflow.d.ts": "sha384-IAfJESeuU0XaKsCqYO0KsvDkgozW4AFhWfvAWGnOsGpzdIBFqulaL8uBYBbRraTW"
|
|
6
6
|
}
|