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 CHANGED
@@ -6,6 +6,127 @@ 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
+
42
+ ## [1.2.0-rc.1] - 2026-11-26
43
+
44
+ The first release candidate of 1.2.0, under the npm tag `next`. The API grows
45
+ and nothing in it changes meaning: `openStream()`, the `'stream'` event,
46
+ `StreamError` and the `'peer'` event's third argument are new (`@since 1.2.0`),
47
+ and everything else in `jsflow.d.ts` is as in 1.1.3, apart from its comments.
48
+ The wire version is still 2. Peers on 1.1.0 to 1.2.0-rc.1 share a room: a 1.2
49
+ peer sends a 1.1 peer what it understands (the 64-packet window, no streams),
50
+ and the new behaviour (faster large messages, the wide window, streams) runs
51
+ between 1.2 peers. 'peer' down with why 'left' and the join without a failed
52
+ request need the service's matching change, which ships separately.
53
+
54
+ ### Added
55
+
56
+ - **`room.openStream(peer, { channel?, meta?, path? })` and the `'stream'`
57
+ event: ordered byte streams of any length between two peers,** as
58
+ standard WHATWG streams. `file.stream().pipeTo(await room.openStream(peer))`
59
+ sends a file; the receiver gets a `ReadableStream`. Backpressure works both
60
+ ways: a slow reader holds at most 1 MiB, and the sender's writes wait.
61
+ `delivered` and `onprogress` give the receiver's progress, and `close()`
62
+ resolves once it has read everything. A stream can be refused, aborted
63
+ or cancelled, and ends with a `StreamError` if the peer is lost. That
64
+ error carries `delivered`, so a seekable source can resume. Stream data
65
+ shares the peer's reliable queue fairly: a `sendReliable()` sent during a
66
+ transfer is not stuck behind it. Streams work only between 1.2 peers;
67
+ anything older answers `'unsupported'`.
68
+ - **`'peer'` down says why:** a third argument, `{ why: 'left' | 'lost' | null }`.
69
+ It is `'left'` when the service saw the peer leave (its `leave()` or a
70
+ closed tab), at once. Handlers with two parameters work as before.
71
+
72
+ ### Changed
73
+
74
+ - **The page reads its links' candidate pairs from the start,** once per link
75
+ per 10 s and around link-up, for relay pacing. Before, they were read only
76
+ once `room.path()` or a `'path'` listener asked.
77
+
78
+ - **Large reliable messages are no longer sent one at a time.** A 64 KiB
79
+ `sendReliable` message fills the reliable layer's whole window, and the
80
+ next could start only once the previous one was fully acknowledged. The
81
+ receiver now acknowledges every fragment it has, so the window keeps
82
+ moving: 100 MB as 64 KiB messages over a 53 ms link went from 0.66 to
83
+ 1.27 MB/s in the lab. No wire change: a 1.1 sender gets the faster rate
84
+ from a 1.2 receiver, and a 1.2 sender to a 1.1 receiver sends as before.
85
+ - **Between two 1.2 peers on a clean link, up to 255 packets in flight**
86
+ instead of 64. That is about 4 times the rate on a long round trip: 4.35
87
+ MB/s at 53 ms in the lab, against 1.27. The window drops back to 64
88
+ when the link shows congestion: a round that loses a tenth of its
89
+ packets, a retransmission timeout, or a round trip grown half again
90
+ above its least, which means the extra packets only wait in a queue. It
91
+ opens again after 5 s without any of these. Single losses do not narrow
92
+ it. A peer says it
93
+ takes the wide window in its link-state announcements (a new capability
94
+ bit), so 1.1 peers and the Swift SDK are sent 64 as before.
95
+ - **`'peer'` down waits 2 s for a peer that went quiet.** A route that
96
+ disappears for less than 2 s no longer shows as the peer leaving and
97
+ coming back. Through a capped relay, a bulk transfer showed the receiving
98
+ app 25 such flaps in 4 minutes. A peer that says goodbye (it hides or
99
+ closes on a phone or Safari), whose link the far end closes, that is
100
+ evicted or that is declared dead by keepalive is still reported at once.
101
+ A peer lost without a word (a crash, a network drop) is reported about 2 s
102
+ later than before.
103
+
104
+ - **Reliable data to a peer relayed through the service's TURN is paced at
105
+ 425 KB/s on the wire.** The service's relays pass 500,000 bytes a second in
106
+ each direction and drop everything past that until the next second, so a
107
+ large transfer used to stall the link for about half a second every two
108
+ seconds. Other messages to that peer stalled with it, and ICE consent
109
+ checks were dropped too. Paced below the budget nothing is dropped: in the
110
+ lab a relayed 100 MB transfer ran at 0.40 MB/s with no stalls, against
111
+ 0.42 MB/s with 165. Links through an app's own `iceServers` are not paced.
112
+
113
+ - **No red "failed request" in the console when a room lives on another
114
+ server.** The join asks the service to name the room's server in a normal
115
+ answer (`hop`), not a 409, and the SDK follows it as before: at most two
116
+ hops, only to an allowed origin. A service before this answers 409 as
117
+ always, and that is still followed.
118
+
119
+ - **The 1 MiB `sendReliable()` queue counts at least 32 bytes a message**
120
+ (each part of a larger one), so a flood of tiny messages is bounded too.
121
+ Anything of 32 bytes or more fits as before (16 × 64 KiB, about 10,000 ×
122
+ 100 B); 10-byte messages now fit about 32,000 deep, not 100,000.
123
+
124
+ ### Fixed
125
+
126
+ - **A neighbour comes back with its first packet after a stall**, not with
127
+ its next hello. Its route stayed withdrawn while its data arrived, and the
128
+ acknowledgements back to it were dropped.
129
+
9
130
  ## [1.1.3] - 2026-11-24
10
131
 
11
132
  A patch under the npm tag `latest`. Nothing in the API changes (`jsflow.d.ts`
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
  /**
@@ -187,15 +196,26 @@ export interface Room {
187
196
  * How I reach a peer now: 'direct' over my own link to it, peer to peer; 'relay' over my own link
188
197
  * through a TURN relay (either end relayed); 'mesh' through other peers (no link of my own to it,
189
198
  * said once that has held for a second, so a peer leaving does not flash 'mesh'); null if it is not
190
- * reachable. From the link's selected candidate pair, read only once path() or a
191
- * 'path' listener asks: at once, about 1 s and 5 s after a link comes up, then every 10 s. Changes are
199
+ * reachable. From the link's selected candidate pair, read at link-up, about 1 s and 5 s after, then
200
+ * every 10 s (from 1.2; before, only once path() or a 'path' listener asked). Changes are
192
201
  * reported by the 'path' event.
193
202
  * @since 1.1.0
194
203
  */
195
204
  path(peer: PeerId): 'direct' | 'relay' | 'mesh' | null;
196
205
 
197
- /** A peer became reachable (`up` true) or unreachable (`up` false). peers() lists the reachable ones. */
198
- on(ev: 'peer', f: (id: PeerId, up: boolean) => void): this;
206
+ /**
207
+ * A peer became reachable (`up` true) or unreachable (`up` false). peers() lists the reachable ones.
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).
211
+ */
212
+ on(ev: 'peer', f: (id: PeerId, up: boolean, info: PeerInfo) => void): this;
213
+ /**
214
+ * A peer opened a stream to me (openStream()). Read `readable` at your own pace: the sender waits while 1 MiB
215
+ * is unread. Without a 'stream' listener every stream is refused.
216
+ * @since 1.2.0
217
+ */
218
+ on(ev: 'stream', f: (from: PeerId, readable: ReadableStream<Uint8Array>, info: StreamInfo) => void): this;
199
219
  /**
200
220
  * How I reach `peer` changed (see path()): for example 'relay' to 'direct' once ICE settles, 'mesh' to
201
221
  * 'direct' when a link of my own comes up, null when it is no longer reachable. Listening starts the
@@ -234,6 +254,25 @@ export interface Room {
234
254
  * Resolves true when queued, false if that peer's queue (1 MiB) is full.
235
255
  */
236
256
  sendReliable(to: PeerId, data: Data, o?: { channel?: Channel }): Promise<boolean>;
257
+ /**
258
+ * Open a stream to a peer: an ordered byte stream of any length, with backpressure both ways. Resolves once the
259
+ * peer accepted it. Rejects with a StreamError: 'unsupported' (a peer before 1.2), 'not_direct' (o.path
260
+ * 'direct' and the link is not), 'refused' (the peer has no 'stream' listener, or already 8 streams open from
261
+ * me; this is not ErrorCode's 'too_many_streams', which is about the service's event streams) or
262
+ * 'unreachable'. Later failures error the writer with a StreamError: 'cancelled' (the reader cancelled),
263
+ * 'timeout' (unreachable for 30 s), 'peer_restarted' or 'left'. close() resolves once the receiving app has
264
+ * read everything. A change of path during the stream comes through on('path'). writer.abort() ends it at once;
265
+ * pipeTo()'s signal waits, as the streams spec says, for a write in flight (until the reader makes room or the
266
+ * stream ends).
267
+ * @since 1.2.0
268
+ * @example
269
+ * const file = input.files[0];
270
+ * const meta = { name: file.name, size: file.size };
271
+ * const out = await room.openStream(peer, { meta });
272
+ * out.onprogress = (n) => bar.value = n / file.size;
273
+ * await file.stream().pipeTo(out);
274
+ */
275
+ openStream(to: PeerId, o?: StreamOptions): Promise<OutgoingStream>;
237
276
  /**
238
277
  * Round trip to a peer through the mesh (a sealed echo): its RTT in ms, the
239
278
  * hops it took, and the peer's wall clock in ms; null if no answer in 5 s.
@@ -329,6 +368,123 @@ export class JoinError extends Error {
329
368
  readonly docs: string;
330
369
  }
331
370
 
371
+ /**
372
+ * Why a 'peer' event happened: on down, 'left' if the peer left the room (its leave() or closed tab, told by the
373
+ * service before or within the hold), else 'lost' (quiet, hidden, or a tab that closed without reaching the
374
+ * service); null on up.
375
+ * @since 1.2.0
376
+ * @example
377
+ * room.on('peer', (id, up, info) => {
378
+ * if (up) return;
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));
383
+ * });
384
+ */
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. */
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;
395
+ }
396
+
397
+ /**
398
+ * Why a stream was refused or ended early (StreamError's `code`). Separate from ErrorCode, which is the
399
+ * service's: 'refused' covers a peer's limit of 8 open streams from one peer.
400
+ * @since 1.2.0
401
+ * @example
402
+ * const retry: StreamErrorCode[] = ['timeout', 'peer_restarted'];
403
+ */
404
+ export type StreamErrorCode = 'unsupported' | 'not_direct' | 'refused' | 'unreachable' | 'aborted' | 'cancelled' | 'timeout' | 'peer_restarted' | 'left';
405
+
406
+ /**
407
+ * What openStream() rejects with, and what a stream's writer or reader errors with.
408
+ * @since 1.2.0
409
+ * @example
410
+ * try {
411
+ * const meta = { name: file.name, offset: 0 };
412
+ * const out = await room.openStream(peer, { meta });
413
+ * await file.stream().pipeTo(out);
414
+ * } catch (e) {
415
+ * if (e instanceof StreamError && e.code === 'timeout') {
416
+ * // resume later from e.delivered with a new stream
417
+ * }
418
+ * throw e;
419
+ * }
420
+ */
421
+ export class StreamError extends Error {
422
+ /** Always 'StreamError', so it survives a structured clone or a log line where instanceof does not. */
423
+ readonly name: 'StreamError';
424
+ /** Why: one of StreamErrorCode. */
425
+ readonly code: StreamErrorCode;
426
+ /** Bytes the receiving app had read when the stream ended: a sender with a seekable source can open a new stream from there. */
427
+ readonly delivered: number;
428
+ /** The other side's reason, for 'aborted' and 'cancelled': at most 200 bytes. */
429
+ readonly reason?: string;
430
+ }
431
+
432
+ /**
433
+ * Options for openStream().
434
+ * @since 1.2.0
435
+ * @example
436
+ * const o: StreamOptions = {
437
+ * channel: 2,
438
+ * meta: { name: 'photo.jpg', size: 3_400_000 },
439
+ * path: 'direct',
440
+ * };
441
+ */
442
+ export interface StreamOptions {
443
+ /** A label the receiver sees, 0–13 (default 0). The stream keeps its own order whatever the channel. */
444
+ channel?: Channel;
445
+ /** Anything JSON can carry, at most 4 KB as JSON: a file name, a size, an offset to resume from. */
446
+ meta?: unknown;
447
+ /** 'direct': reject with 'not_direct' unless path(to) is 'direct' when the stream opens. */
448
+ path?: 'direct';
449
+ }
450
+
451
+ /**
452
+ * The sending end of a stream: a standard WritableStream (pipeTo(), getWriter(), writer.ready, abort()) with
453
+ * progress. Write Uint8Array chunks.
454
+ * @since 1.2.0
455
+ * @example
456
+ * const out = await room.openStream(peer);
457
+ * const w = out.getWriter();
458
+ * for (const chunk of chunks) { await w.ready; await w.write(chunk); }
459
+ * await w.close(); // once the peer has read it all
460
+ */
461
+ export interface OutgoingStream extends WritableStream<Uint8Array> {
462
+ /** The stream's id, as the receiver's StreamInfo has it. */
463
+ readonly id: number;
464
+ /** Bytes the receiving app has read so far. */
465
+ readonly delivered: number;
466
+ /** Called at most every 100 ms while `delivered` grows, and once at the end. A path change comes through room.on('path'). */
467
+ onprogress: ((delivered: number) => void) | null;
468
+ }
469
+
470
+ /**
471
+ * What a 'stream' listener is told about an incoming stream.
472
+ * @since 1.2.0
473
+ * @example
474
+ * room.on('stream', async (from, readable, info) => {
475
+ * const { name } = info.meta as { name: string };
476
+ * save(name, await new Response(readable).blob());
477
+ * });
478
+ */
479
+ export interface StreamInfo {
480
+ /** The opener's number for this stream, counting up from 1; unique among the streams from that peer. */
481
+ readonly id: number;
482
+ /** The app channel the opener gave openStream() (default 0). */
483
+ readonly channel: Channel;
484
+ /** The opener's meta, as JSON gave it; undefined if none. */
485
+ readonly meta: unknown;
486
+ }
487
+
332
488
  /**
333
489
  * Join a room. Rejects with a JoinError if the service (or the SDK itself) refuses.
334
490
  * @since 1.0.0