jsflow-sdk 1.1.0 → 1.1.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 CHANGED
@@ -6,6 +6,74 @@ All notable changes to `jsflow-sdk`. The format follows
6
6
  contract; metric names passed to `onMetric` are diagnostic and may change in
7
7
  any minor release.
8
8
 
9
+ ## [1.1.2] - 2026-11-22
10
+
11
+ A patch under the npm tag `latest`. Nothing in the API changes (`jsflow.d.ts`
12
+ is byte-identical to 1.1.1's), the wire version is still 2, and the messages
13
+ on the wire are the same bytes; peers on 1.1.0, 1.1.1 and 1.1.2 share a room.
14
+
15
+ ### Fixed
16
+
17
+ - **A peer whose links are slow (a relay far away) no longer goes silent for
18
+ seconds after someone joins.** Each peer worked out routes from the link
19
+ delays every peer announces, except for its own links, where it used the
20
+ delay it had measured. A new link is announced at 50 ms until its first
21
+ refresh, 10 s later; on a link through a distant relay (200 ms and more) the
22
+ two ends of a route each saw the other's way as shorter and passed messages
23
+ back and forth until they expired. A peer now costs its own links from the
24
+ announced delays too, so every peer agrees on the routes.
25
+
26
+ Measured on staging, one Chrome with 8 peers per room joined through the
27
+ US entry (relay-only peers among them): before, 30 pauses of 1.5 to 5.5 s
28
+ between messages from one peer and 800 packets dropped in loops; with
29
+ 1.1.2, the same rooms: none of either, and every reliable message arrived
30
+ once. Through the EU entry there were none before either. In the
31
+ simulator, with relayed links 75 to 125 ms slower each way: before, every
32
+ seed looped (a relay-only peer silent up to 7.0 s); now none.
33
+
34
+ ### Changed
35
+
36
+ - Routes are chosen a little differently, so latencies in the simulator move
37
+ within the limits the tests hold them to: a heavy stream at 5 % loss,
38
+ median 25 to 32 ms and 99th percentile 743 to 615 ms; ordinary traffic,
39
+ 99th percentile 282 to 288 ms; under reordering, 247 to 265 ms; a transfer
40
+ whose route changes through another peer, median 647 to 137 ms. A new link
41
+ is routed at its announced 50 ms until its refresh, as every other peer
42
+ already did.
43
+
44
+ ## [1.1.1] - 2026-11-22
45
+
46
+ A patch under the npm tag `latest`. Nothing in the API changes (`jsflow.d.ts`
47
+ is byte-identical to 1.1.0's), the wire version is still 2, and peers on
48
+ 1.1.0 and 1.1.1 share a room.
49
+
50
+ ### Fixed
51
+
52
+ - **Large reliable transfers through a TURN relay complete, and the peer no
53
+ longer goes down and up while one runs.** A relay caps each allocation
54
+ (jsflow's relays at 500 KB/s). `sendReliable` kept up to 64 packets in
55
+ flight whatever the path could carry, so a bulk transfer overran the cap;
56
+ the relay's drops stalled the connection for over half a second at a time,
57
+ the peer was reported down and up every few seconds, and a 5 MB transfer
58
+ through a relay never finished. Two changes, neither on the wire:
59
+ - any packet from a neighbour now counts as a sign of life, not only its
60
+ keepalives, so a peer whose keepalives queue behind bulk data stays up;
61
+ - the sender paces reliable data to what the path delivers (congestion
62
+ control): it starts at 10 packets in flight, grows while they arrive, and
63
+ backs off when a round trip loses a tenth of them.
64
+
65
+ Measured on staging's relay with Chrome: 5 MB in 12.5 to 13.5 s and 10 MB in
66
+ 27 s (before: never), with the peer up throughout; 5 MB over a direct
67
+ connection is unchanged at 1.9 s.
68
+
69
+ ### Changed
70
+
71
+ - Under heavy random loss the slowest messages of a heavy stream arrive later:
72
+ in the simulator, at 5 % loss with 100 messages a second of up to 64 KB,
73
+ the 99th percentile goes from 353 to 743 ms. Messages at an ordinary rate
74
+ are unaffected (10 a second of 100 to 500 bytes at 5 % loss: median 20 ms,
75
+ 99th percentile 282 ms, the same as 1.1.0).
76
+
9
77
  ## [1.1.0] - 2026-11-21
10
78
 
11
79
  Under the npm tag `latest` (`npm install jsflow-sdk`). The code is
package/README.md CHANGED
@@ -20,7 +20,7 @@ Or from the CDN, at an exact version with its integrity hash (`sri.json` in
20
20
  each version directory):
21
21
 
22
22
  ```html
23
- <script type="module" integrity="sha384-…" crossorigin src="https://cdn.jsflow.com/v1.1.0/jsflow.js"></script>
23
+ <script type="module" integrity="sha384-…" crossorigin src="https://cdn.jsflow.com/v1.1.2/jsflow.js"></script>
24
24
  ```
25
25
 
26
26
  The SDK starts a worker (`worker.js`, next to `jsflow.js`). Vite and webpack
package/jsflow.js CHANGED
@@ -1,4 +1,4 @@
1
- // jsflow-sdk 1.1.0. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
1
+ // jsflow-sdk 1.1.2. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
2
2
 
3
3
  // core/link-main.js
4
4
  function linkKind(stats) {
@@ -285,7 +285,7 @@ function ticketRegion(ticket) {
285
285
  return void 0;
286
286
  }
287
287
  }
288
- var SDK_VERSION = "1.1.0";
288
+ var SDK_VERSION = "1.1.2";
289
289
  var WIRE = 2;
290
290
  var MAX_DATAGRAM = 1052;
291
291
  var MAX_RELIABLE = 65536;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsflow-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "jsflow: peer-to-peer rooms for the browser (WebRTC mesh, signalling and relays as a service).",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "type": "module",
package/sri.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
- "version": "1.1.0",
3
- "jsflow.js": "sha384-n2txdqjJFlyu0muRSD2PSmBfEChMdGlrr6bAmzd95kNflZ6iMDnsWK1SP3JMYvvw",
4
- "worker.js": "sha384-P3PVko7raQWUCMpNJbtLOpq3E6O9gd2/wv+3inhUkgsVcUgS2XSV94IfDGpTD4Mx",
2
+ "version": "1.1.2",
3
+ "jsflow.js": "sha384-6iaDEfAX53o/sELinRUcwhyTPcO+v1yX9sXNJKhf8U7yEEPXiKMRzRSRvVortkTA",
4
+ "worker.js": "sha384-59Il1fBPtlj1gK19+ZC7Ei361OucDLcAPFFKN2ozLC2RrRu5SOuHpS54+SDnQwOY",
5
5
  "jsflow.d.ts": "sha384-VM+jOFBiVG6Z3ElnDVldrvSTyha/lJh9kC8el+JL/77bYFOp6ScbTHFK2tCnFsN5"
6
6
  }
package/worker.js CHANGED
@@ -1,4 +1,4 @@
1
- // jsflow-sdk 1.1.0. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
1
+ // jsflow-sdk 1.1.2. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
2
2
 
3
3
  // core/header.js
4
4
  var VERSION = 2;
@@ -416,8 +416,8 @@ function meshNode(opts = {}) {
416
416
  if (routes) return routes;
417
417
  const dist = /* @__PURE__ */ new Map([[me, 0]]), first = /* @__PURE__ */ new Map(), done = /* @__PURE__ */ new Set();
418
418
  const cost = (a, b) => {
419
- if (a === me) return nbr.get(b)?.rtt ?? 50;
420
419
  const ra = lsdb.get(a)?.links.get(b), rb = lsdb.get(b)?.links.get(a);
420
+ if (a === me) return ra !== void 0 && rb !== void 0 ? (ra + rb) / 2 : nbr.get(b)?.rtt ?? 50;
421
421
  if (ra === void 0 || rb === void 0) return Infinity;
422
422
  const w = lsdb.get(a)?.will ?? 7;
423
423
  return (ra + rb) / 2 + 0.5 + (w === 0 ? 1e3 : (15 - w) * 2);
@@ -1023,6 +1023,8 @@ function meshNode(opts = {}) {
1023
1023
  if (!h2) return env2.metric("badVersion", from);
1024
1024
  if (h2.type === T.HELLO) return onHello(from, h2);
1025
1025
  if (h2.type === T.BYE) return onBye(from, h2);
1026
+ const n = nbr.get(from);
1027
+ if (n && n.byeTs === void 0) n.lastHeard = env2.now();
1026
1028
  route(raw, h2, from);
1027
1029
  },
1028
1030
  // ---- app API used by experiments ----
@@ -1838,6 +1840,8 @@ function reliable({
1838
1840
  minRto = 200,
1839
1841
  maxRto = 1e4,
1840
1842
  initRto = 1e3,
1843
+ initCwnd = 10,
1844
+ congestion = true,
1841
1845
  giveUpMs = 3e4,
1842
1846
  txIdleMs = 3e4,
1843
1847
  rxIdleMs = 6e4,
@@ -1850,7 +1854,7 @@ function reliable({
1850
1854
  if (!(rxIdleMs > giveUpMs && rxIdleMs > txIdleMs)) throw new RangeError("reliable: rxIdleMs must outlast giveUpMs and txIdleMs");
1851
1855
  const peers = /* @__PURE__ */ new Map();
1852
1856
  const meshInc = /* @__PURE__ */ new Map();
1853
- const stats = { sent: 0, data: 0, retx: 0, fast: 0, timeLoss: 0, rto: 0, acks: 0, piggy: 0, dup: 0, delivered: 0, giveUps: 0, resets: 0, bad: 0 };
1857
+ const stats = { sent: 0, data: 0, retx: 0, fast: 0, timeLoss: 0, rto: 0, cuts: 0, acks: 0, piggy: 0, dup: 0, delivered: 0, giveUps: 0, resets: 0, bad: 0 };
1854
1858
  let sids = 0, sweep = null;
1855
1859
  const bad = (why) => {
1856
1860
  stats.bad++;
@@ -1863,13 +1867,22 @@ function reliable({
1863
1867
  metric2("reliablePeersFull", peer);
1864
1868
  return null;
1865
1869
  }
1866
- peers.set(peer, rec = { inc: meshInc.get(peer) ?? 0, tx: null, rx: null, rtt: { srtt: null, var: 0, min: Infinity, latest: 0 }, seen: now(), noticeAt: -Infinity });
1870
+ peers.set(peer, rec = {
1871
+ inc: meshInc.get(peer) ?? 0,
1872
+ tx: null,
1873
+ rx: null,
1874
+ rtt: { srtt: null, var: 0, min: Infinity, latest: 0 },
1875
+ seen: now(),
1876
+ noticeAt: -Infinity,
1877
+ cc: { cwnd: Math.min(W, initCwnd), ssthresh: Infinity, delivered: 0, roundDel: 0, rDel0: 0, rLost: 0 }
1878
+ });
1867
1879
  if (!sweep) sweep = setTimer(sweepMs, onSweep);
1868
1880
  return rec;
1869
1881
  }
1870
1882
  function dropTx(rec) {
1871
1883
  if (rec.tx) {
1872
1884
  clearTimer(rec.tx.timer);
1885
+ clearTimer(rec.tx.paceTimer);
1873
1886
  rec.tx = null;
1874
1887
  }
1875
1888
  }
@@ -1974,7 +1987,9 @@ function reliable({
1974
1987
  rto: rtoOf(rec.rtt),
1975
1988
  rtoAt: 0,
1976
1989
  progress: now(),
1977
- timer: null
1990
+ timer: null,
1991
+ paceAt: 0,
1992
+ paceTimer: null
1978
1993
  };
1979
1994
  }
1980
1995
  function transmit(peer, rec, p) {
@@ -1983,11 +1998,46 @@ function reliable({
1983
1998
  p.sentAt = now();
1984
1999
  p.txn = ++tx.txn;
1985
2000
  p.n++;
2001
+ p.del = rec.cc.delivered;
1986
2002
  out2(peer, rec, p);
1987
2003
  }
2004
+ function pipe(tx) {
2005
+ let n = 0;
2006
+ for (const p of tx.inflight.values()) if (!p.sacked) n++;
2007
+ return n;
2008
+ }
2009
+ function delivered(rec, ps) {
2010
+ const cc = rec.cc;
2011
+ let roundEnd = false;
2012
+ for (const p of ps) {
2013
+ cc.delivered++;
2014
+ if (p.del >= cc.roundDel) roundEnd = true;
2015
+ }
2016
+ cc.cwnd = Math.min(W, cc.cwnd < cc.ssthresh ? cc.cwnd + ps.length : cc.cwnd + ps.length / cc.cwnd);
2017
+ if (!roundEnd) return;
2018
+ const rDel = cc.delivered - cc.rDel0;
2019
+ if (rDel >= 8 && cc.rLost >= 3 && cc.rLost >= 0.1 * (rDel + cc.rLost)) {
2020
+ cc.cwnd = Math.max(4, Math.min(cc.cwnd, 0.85 * rDel));
2021
+ cc.ssthresh = cc.cwnd;
2022
+ stats.cuts++;
2023
+ }
2024
+ cc.roundDel = cc.rDel0 = cc.delivered;
2025
+ cc.rLost = 0;
2026
+ }
1988
2027
  function pump(peer, rec) {
1989
- const tx = rec.tx;
1990
- while (tx.queue.length && d32(tx.next, tx.una) < W) {
2028
+ const tx = rec.tx, cc = rec.cc, t = now();
2029
+ let room = (congestion ? Math.floor(cc.cwnd) : W) - pipe(tx);
2030
+ const srtt = rec.rtt.srtt, gap = congestion && srtt ? srtt / ((cc.cwnd < cc.ssthresh ? 2 : 1.2) * cc.cwnd) : 0;
2031
+ tx.paceAt = Math.max(tx.paceAt, t - 4 * gap);
2032
+ while (tx.queue.length && d32(tx.next, tx.una) < W && room-- > 0) {
2033
+ if (tx.paceAt > t) {
2034
+ if (!tx.paceTimer) tx.paceTimer = setTimer(Math.max(1, tx.paceAt - t), () => {
2035
+ tx.paceTimer = null;
2036
+ if (rec.tx === tx && peers.get(peer) === rec && live(peer, rec)) pump(peer, rec);
2037
+ });
2038
+ break;
2039
+ }
2040
+ tx.paceAt += gap;
1991
2041
  if (!tx.inflight.size) {
1992
2042
  tx.progress = now();
1993
2043
  tx.rtoAt = now() + tx.rto;
@@ -2016,6 +2066,7 @@ function reliable({
2016
2066
  if (!live(peer, rec)) return;
2017
2067
  const lost = detectLost(rec);
2018
2068
  if (lost.length) {
2069
+ rec.cc.rLost += lost.length;
2019
2070
  for (const p of lost) transmit(peer, rec, p);
2020
2071
  return arm(peer, rec);
2021
2072
  }
@@ -2025,6 +2076,11 @@ function reliable({
2025
2076
  stats.rto++;
2026
2077
  break;
2027
2078
  }
2079
+ if (tx.rto === rtoOf(rec.rtt)) {
2080
+ const cc = rec.cc;
2081
+ cc.cwnd = cc.ssthresh = Math.max(4, cc.cwnd / 2);
2082
+ stats.cuts++;
2083
+ }
2028
2084
  tx.rto = Math.min(2 * tx.rto, maxRto);
2029
2085
  tx.rtoAt = now() + tx.rto;
2030
2086
  }
@@ -2078,13 +2134,18 @@ function reliable({
2078
2134
  const adv = d32(next, tx.una);
2079
2135
  if (adv > d32(tx.next, tx.una)) return bad("ackBeyondSent");
2080
2136
  let newest = null;
2137
+ const acked = [];
2081
2138
  const got = (p) => {
2139
+ acked.push(p);
2082
2140
  if (!newest || p.txn > newest.txn) newest = p;
2083
2141
  if (p.txn < tx.rack.txn) {
2084
2142
  if (p.n === 1) tx.reordered = true;
2085
2143
  return;
2086
2144
  }
2087
- if (p.n > 1 && rec.rtt.min < Infinity && now() - p.sentAt < rec.rtt.min) return;
2145
+ if (p.n > 1 && rec.rtt.min < Infinity && now() - p.sentAt < rec.rtt.min) {
2146
+ if (rec.cc.rLost) rec.cc.rLost--;
2147
+ return;
2148
+ }
2088
2149
  tx.rack.txn = p.txn;
2089
2150
  tx.rack.rtt = now() - p.sentAt;
2090
2151
  };
@@ -2108,7 +2169,10 @@ function reliable({
2108
2169
  tx.progress = now();
2109
2170
  tx.rtoAt = now() + tx.rto;
2110
2171
  }
2111
- for (const p of detectLost(rec)) transmit(peer, rec, p);
2172
+ if (acked.length) delivered(rec, acked);
2173
+ const lost = detectLost(rec);
2174
+ rec.cc.rLost += lost.length;
2175
+ for (const p of lost) transmit(peer, rec, p);
2112
2176
  pump(peer, rec);
2113
2177
  }
2114
2178
  function onData(peer, rec, sid, seq, una, flags, bytes) {