jsflow-sdk 1.2.0-rc.1 → 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 CHANGED
@@ -6,6 +6,39 @@ 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
+ ## [Unreleased] (1.2.0)
10
+
11
+ Notes from the rc.1 receipts (2026-11-27), for the 1.2.0 entry:
12
+
13
+ - A closed tab shows `'left'` within about a round trip while its browser stays open. Closing the last tab or
14
+ window can lose the beacon, and the others then see `'lost'` after about 3.3 s. Closing a browser's last window
15
+ is such a loss: it is reported about 2 s later than with 1.1, as `'lost'`.
16
+ - Large transfers on clean links reach 2–3× 1.1's rate, not yet the full window.
17
+
18
+ ## [1.2.0-rc.2] - 2026-11-27
19
+
20
+ The second release candidate of 1.2.0, under the npm tag `next`. The API
21
+ gains two members (`@since 1.2.0`) and nothing in it changes meaning: the
22
+ hold before `'peer'` down is the app's to set, and a down says how long the
23
+ peer had been out of reach. With the defaults, rc.2 reports the same events
24
+ at the same times as rc.1. The wire
25
+ version is still 2, and peers on 1.1.0 to 1.2.0-rc.2 share a room.
26
+
27
+ ### Added
28
+
29
+ - **`join({ holdMs })`: how long `'peer'` down waits for a peer that went
30
+ quiet,** 0–30000 ms, default 2000 (rc.1's fixed hold). Keep 2000 for most
31
+ apps. Lower suits presence on direct links, at the cost of a down/up flap
32
+ on a stalled relay; higher suits a game on mobile networks. A peer that
33
+ leaves or says goodbye is reported at once whatever the value.
34
+ - **`PeerInfo.unreachableMs`:** on a down for a peer that went quiet, how
35
+ long it had been out of reach (after the hold, `holdMs` to about 500 ms
36
+ more; after a keepalive timeout, however long it was); `null` on up and on
37
+ a down for a known reason (BYE, leave, a closed link, an eviction). An
38
+ app's own grace period can count from the loss: y-jsflow 0.2.0-rc.2
39
+ removes a lost peer's cursor `leaveHoldMs` after the loss rather than
40
+ after the event.
41
+
9
42
  ## [1.2.0-rc.1] - 2026-11-26
10
43
 
11
44
  The first release candidate of 1.2.0, under the npm tag `next`. The API grows
package/jsflow.d.ts CHANGED
@@ -162,6 +162,15 @@ export interface JoinOptions {
162
162
  * when the app requires tickets; its user id becomes room.user(peerId) for everyone.
163
163
  */
164
164
  ticket?: string | (() => Promise<string> | string);
165
+ /**
166
+ * How long a peer that went quiet is held before 'peer' down, in ms (0–30000, default 2000); a peer that leaves
167
+ * or says BYE is reported at once. Keep 2000 for most apps: a lower value lets a stalled relay link show as a
168
+ * down/up flap (without the hold, 25 in 4 minutes in our relay test). Use about 500 for a presence app on direct
169
+ * links that shows departures at once, or 5000 for a game on mobile networks that shouldn't drop a player over a
170
+ * brief stall.
171
+ * @since 1.2.0
172
+ */
173
+ holdMs?: number;
165
174
  }
166
175
 
167
176
  /**
@@ -196,9 +205,9 @@ export interface Room {
196
205
 
197
206
  /**
198
207
  * A peer became reachable (`up` true) or unreachable (`up` false). peers() lists the reachable ones.
199
- * From 1.2, down waits 2 s for a peer that went quiet (a route lost for less is not reported), and comes at once
200
- * after its BYE, a link the far end closed, its eviction or a keepalive timeout. `info.why` says whether it left
201
- * (since 1.2.0).
208
+ * From 1.2, down waits `holdMs` (default 2 s) for a peer that went quiet (a route lost for less is not reported),
209
+ * and comes at once after its BYE, a link the far end closed, its eviction or a keepalive timeout. `info.why` says
210
+ * whether it left, `info.unreachableMs` how long it had been out of reach (since 1.2.0).
202
211
  */
203
212
  on(ev: 'peer', f: (id: PeerId, up: boolean, info: PeerInfo) => void): this;
204
213
  /**
@@ -361,17 +370,28 @@ export class JoinError extends Error {
361
370
 
362
371
  /**
363
372
  * Why a 'peer' event happened: on down, 'left' if the peer left the room (its leave() or closed tab, told by the
364
- * service before or within the 2 s hold), else 'lost' (quiet, hidden, or a tab that closed without reaching the
373
+ * service before or within the hold), else 'lost' (quiet, hidden, or a tab that closed without reaching the
365
374
  * service); null on up.
366
375
  * @since 1.2.0
367
376
  * @example
368
377
  * room.on('peer', (id, up, info) => {
369
378
  * if (up) return;
370
379
  * show(info.why === 'left' ? `${id} left` : `${id} was lost`);
380
+ * // a grace period of 3 s, counted from the loss
381
+ * const wait = 3000 - (info.unreachableMs ?? 0);
382
+ * setTimeout(() => removeCursor(id), Math.max(0, wait));
371
383
  * });
372
384
  */
373
385
  export interface PeerInfo {
386
+ /** On down, 'left' (the peer left the room) or 'lost' (it went quiet or out of reach); null on up. */
374
387
  readonly why: 'left' | 'lost' | null;
388
+ /**
389
+ * On a down for a peer that went quiet: how long it had been out of reach, in ms (after the hold, `holdMs` to
390
+ * about 500 ms more; after a keepalive timeout, however long it was; it went quiet about 0.5 s before that). null
391
+ * on up, and on a down reported at once for a known reason (its BYE or leave, a closed link, an eviction).
392
+ * @since 1.2.0
393
+ */
394
+ readonly unreachableMs: number | null;
375
395
  }
376
396
 
377
397
  /**
@@ -457,7 +477,9 @@ export interface OutgoingStream extends WritableStream<Uint8Array> {
457
477
  * });
458
478
  */
459
479
  export interface StreamInfo {
480
+ /** The opener's number for this stream, counting up from 1; unique among the streams from that peer. */
460
481
  readonly id: number;
482
+ /** The app channel the opener gave openStream() (default 0). */
461
483
  readonly channel: Channel;
462
484
  /** The opener's meta, as JSON gave it; undefined if none. */
463
485
  readonly meta: unknown;
package/jsflow.js CHANGED
@@ -1,4 +1,4 @@
1
- // jsflow-sdk 1.2.0-rc.1. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
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
4
  function selected(stats) {
@@ -318,7 +318,7 @@ function ticketRegion(ticket) {
318
318
  return void 0;
319
319
  }
320
320
  }
321
- var SDK_VERSION = "1.2.0-rc.1";
321
+ var SDK_VERSION = "1.2.0-rc.2";
322
322
  var WIRE = 2;
323
323
  var MAX_DATAGRAM = 1052;
324
324
  var MAX_RELIABLE = 65536;
@@ -390,6 +390,7 @@ function homeAllowed(server, home) {
390
390
  }
391
391
  async function join(opts) {
392
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");
393
394
  let server = String(opts.server ?? SERVER).replace(/\/$/, "");
394
395
  const name = opts.room;
395
396
  if (!name) throw new TypeError("join: room is required");
@@ -506,7 +507,7 @@ async function join(opts) {
506
507
  });
507
508
  const ua = navigator.userAgent, webkitDesktop = /Safari/.test(ua) && !/Chrome|Chromium|Edg|Firefox/.test(ua);
508
509
  const bye = /iPhone|iPad|Android/.test(ua) || webkitDesktop;
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 } });
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 } });
510
511
  let lastId = null, lastEventAt = Date.now();
511
512
  const ended = () => state === "left" || state === "superseded" || state === "away" || state === "evicted";
512
513
  const openSse = () => {
@@ -595,9 +596,9 @@ async function join(opts) {
595
596
  case "peer":
596
597
  if (m.up) peers.add(m.id);
597
598
  else peers.delete(m.id);
598
- emit("peer", m.id, m.up, { why: m.up ? null : m.why ?? "lost" });
599
+ emit("peer", m.id, m.up, { why: m.up ? null : m.why ?? "lost", unreachableMs: m.up ? null : m.unreachableMs ?? null });
599
600
  return repath(m.id);
600
- // 1.2: info.why
601
+ // 1.2: info.why, info.unreachableMs
601
602
  case "message":
602
603
  return emit("message", m.from, m.data, { channel: m.chan, broadcast: !!m.broadcast, reliable: !!m.reliable });
603
604
  case "state":
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsflow-sdk",
3
- "version": "1.2.0-rc.1",
3
+ "version": "1.2.0-rc.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.2.0-rc.1",
3
- "jsflow.js": "sha384-nOSP3w3zIVHP8n3U108NEDxZMEQuZeUl7V22Oap93w2x7KsLRlFTd9t8IPxM8yWx",
4
- "worker.js": "sha384-BJ2DsQbUpmicAb8QKY9rMYsqp2TmdFQao9SQ08ovpkPsyBKDU17iExyqilo5Sxk/",
5
- "jsflow.d.ts": "sha384-MvNOfWH25DatQq04hCHJVJs+GnoT3T7up7Fy8FwqDMXvdPTPAzG6omyuKRcT/SG8"
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
  }
package/worker.js CHANGED
@@ -1,4 +1,4 @@
1
- // jsflow-sdk 1.2.0-rc.1. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
1
+ // jsflow-sdk 1.2.0-rc.2. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
2
2
 
3
3
  // core/header.js
4
4
  var VERSION = 2;
@@ -2504,13 +2504,15 @@ function reliable({
2504
2504
  // core/peer-hold.js
2505
2505
  function peerHold({ holdMs = 2e3 } = {}) {
2506
2506
  if (!(holdMs >= 0)) throw new RangeError("peerHold: holdMs must be ≥ 0");
2507
- const known = /* @__PURE__ */ new Set(), since = /* @__PURE__ */ new Map(), gone = /* @__PURE__ */ new Map();
2507
+ const known = /* @__PURE__ */ new Set(), since = /* @__PURE__ */ new Map(), gone = /* @__PURE__ */ new Map(), silentGone = /* @__PURE__ */ new Set();
2508
2508
  return {
2509
- gone(peer, now) {
2509
+ gone(peer, now, silent = false) {
2510
2510
  gone.set(peer, now);
2511
+ if (silent) silentGone.add(peer);
2512
+ else silentGone.delete(peer);
2511
2513
  },
2512
2514
  tick(reachable, now) {
2513
- const up = [], down = [];
2515
+ const up = [], down = [], outMs = /* @__PURE__ */ new Map();
2514
2516
  for (const p of reachable) {
2515
2517
  since.delete(p);
2516
2518
  if (!known.has(p)) {
@@ -2521,15 +2523,25 @@ function peerHold({ holdMs = 2e3 } = {}) {
2521
2523
  for (const p of known) {
2522
2524
  if (reachable.has(p)) continue;
2523
2525
  if (!since.has(p)) since.set(p, now);
2524
- if (gone.has(p) && now - gone.get(p) < holdMs || now - since.get(p) >= holdMs) down.push(p);
2526
+ if (gone.has(p) && now - gone.get(p) < holdMs) {
2527
+ down.push(p);
2528
+ if (silentGone.has(p)) outMs.set(p, now - since.get(p));
2529
+ } else if (now - since.get(p) >= holdMs) {
2530
+ down.push(p);
2531
+ outMs.set(p, now - since.get(p));
2532
+ }
2525
2533
  }
2526
2534
  for (const p of down) {
2527
2535
  known.delete(p);
2528
2536
  since.delete(p);
2529
2537
  gone.delete(p);
2538
+ silentGone.delete(p);
2539
+ }
2540
+ for (const [p, t] of gone) if (now - t >= holdMs) {
2541
+ gone.delete(p);
2542
+ silentGone.delete(p);
2530
2543
  }
2531
- for (const [p, t] of gone) if (now - t >= holdMs) gone.delete(p);
2532
- return { up, down };
2544
+ return { up, down, outMs };
2533
2545
  },
2534
2546
  known: () => new Set(known)
2535
2547
  };
@@ -2916,7 +2928,7 @@ var leftAt = /* @__PURE__ */ new Map();
2916
2928
  var hold = peerHold({ holdMs: 2e3 });
2917
2929
  var GONE = /* @__PURE__ */ new Set(["bye", "keepaliveDead", "evictedPeer"]);
2918
2930
  var metric = (name, value) => {
2919
- if (GONE.has(name) && Number.isInteger(value)) hold.gone(value, Date.now());
2931
+ if (GONE.has(name) && Number.isInteger(value)) hold.gone(value, Date.now(), name === "keepaliveDead");
2920
2932
  out({ op: "metric", name, value });
2921
2933
  };
2922
2934
  self.onmessage = (e) => {
@@ -2946,6 +2958,7 @@ var near;
2946
2958
  async function init({ id, inc, key, room, server, near: nr, token, tokenKeys, refreshAt, bye, opts = {} }) {
2947
2959
  home = server;
2948
2960
  near = nr;
2961
+ hold = peerHold({ holdMs: opts.holdMs ?? 2e3 });
2949
2962
  const post = (p, b) => fetch(home + "/v2" + p, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(b), signal: AbortSignal.timeout(5e3) }).then((r) => r.json());
2950
2963
  const sup = (r) => {
2951
2964
  if (r?.superseded) h?.onSuperseded(r.superseded);
@@ -3133,7 +3146,7 @@ function peerWatch() {
3133
3146
  const t = Date.now(), table = h.api.table().dist;
3134
3147
  for (const [p, l] of leftAt) if (t - l.at > 1e4 || !table.has(p) && l.reported || (h.api.lsa(p)?.inc ?? 0) > l.inc) leftAt.delete(p);
3135
3148
  const now = new Set([...table.keys()].filter((x) => x !== env.id && !leftAt.has(x)));
3136
- const { up, down } = hold.tick(now, t);
3149
+ const { up, down, outMs } = hold.tick(now, t);
3137
3150
  if (st) {
3138
3151
  const live = st.peers();
3139
3152
  for (const p of [...goneSince.keys()]) if (!live.has(p)) goneSince.delete(p);
@@ -3153,7 +3166,7 @@ function peerWatch() {
3153
3166
  for (const p of down) {
3154
3167
  const l = leftAt.get(p);
3155
3168
  if (l) l.reported = true;
3156
- out({ op: "peer", id: p, up: false, why: l ? "left" : "lost" });
3169
+ out({ op: "peer", id: p, up: false, why: l ? "left" : "lost", unreachableMs: l ? null : outMs.get(p) ?? null });
3157
3170
  }
3158
3171
  const c = h.api.live().length > 0;
3159
3172
  if (c !== connected && !byeSent) {