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