jsflow-sdk 1.0.0-rc.2 → 1.0.0

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,27 +6,12 @@ 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.0.0-rc.2] - 2026-11-18
9
+ ## [1.0.0] - 2026-11-19
10
10
 
11
- The second release candidate for 1.0.0, under the npm tag `next` as before.
12
-
13
- ### Fixed
14
- - **A reliable message is never delivered twice across a page reload.** If a
15
- receiver got a `sendReliable()` message, then reloaded (or restarted)
16
- before its acknowledgement reached the sender (for example while the
17
- sender's page was hidden), the sender's retransmission could reach the
18
- receiver's new page as well. The new page now drops it, and the sender
19
- gets `undelivered` for it, as for any message whose delivery it cannot
20
- confirm. Nothing changes on the wire.
21
-
22
- ## [1.0.0-rc.1] - 2026-11-18
23
-
24
- The release candidate for 1.0.0: everything listed under 1.0.0 below. Published under the npm tag `next`
25
- (`npm install jsflow-sdk@next`); `npm install jsflow-sdk` keeps the stable release.
26
-
27
- ## [1.0.0] - unreleased
28
-
29
- The first stable API, on wire version 2.
11
+ The first stable API, on wire version 2, under the npm tag `latest`
12
+ (`npm install jsflow-sdk`). The code is 1.0.0-rc.3's: since rc.3 only the
13
+ documentation comments in `jsflow.d.ts` changed (the spend cap is the
14
+ account's, and the examples fit 72 columns).
30
15
 
31
16
  ### Migrating from 0.1.x
32
17
 
@@ -95,6 +80,8 @@ The first stable API, on wire version 2.
95
80
  - `sendReliable()` delivers to the receiver's page that was there when it
96
81
  was sent, never to a reloaded one: an unconfirmed message to a peer that
97
82
  restarted is reported as `undelivered` (fixed in rc.2).
83
+ - Two peers that reach for each other while one joins through the other
84
+ link at once, instead of stalling 10 s or more (fixed in rc.3).
98
85
  - Tokens and relay credentials refresh, so rooms live past 6 hours; the
99
86
  signalling stream resumes after a drop without losing signals.
100
87
  - HTTP/2 to the signalling service; rooms move between service instances
@@ -126,3 +113,36 @@ The first stable API, on wire version 2.
126
113
  ### Removed
127
114
  - Lab-only options and methods (`lab`, `want`, `pool`, `target`, `max`,
128
115
  `room.lab()`).
116
+
117
+ ## [1.0.0-rc.3] - 2026-11-18
118
+
119
+ The third release candidate for 1.0.0, under the npm tag `next` as before.
120
+
121
+ ### Fixed
122
+ - **Two peers no longer fail to link for 10 seconds or more when one joins
123
+ through the other while the other is also trying to reach it.** A peer
124
+ joining (or rejoining) a room through another member's waiting offer
125
+ could, if that member also reached out to it at the same moment, send its
126
+ reply to the wrong place, give up on the member's connection after 4
127
+ seconds, and hold back from it for another 10. Both then waited without an
128
+ error until their attempts timed out. It was most likely with a relay
129
+ (TURN) configured, as in production, where a connection takes up to 1.5
130
+ seconds to prepare. Nothing changes on the wire or in the API.
131
+
132
+ ## [1.0.0-rc.2] - 2026-11-18
133
+
134
+ The second release candidate for 1.0.0, under the npm tag `next` as before.
135
+
136
+ ### Fixed
137
+ - **A reliable message is never delivered twice across a page reload.** If a
138
+ receiver got a `sendReliable()` message, then reloaded (or restarted)
139
+ before its acknowledgement reached the sender (for example while the
140
+ sender's page was hidden), the sender's retransmission could reach the
141
+ receiver's new page as well. The new page now drops it, and the sender
142
+ gets `undelivered` for it, as for any message whose delivery it cannot
143
+ confirm. Nothing changes on the wire.
144
+
145
+ ## [1.0.0-rc.1] - 2026-11-18
146
+
147
+ The release candidate for 1.0.0: everything listed under 1.0.0 above. Published under the npm tag `next`
148
+ (`npm install jsflow-sdk@next`); `npm install jsflow-sdk` keeps the stable release.
package/jsflow.d.ts CHANGED
@@ -4,7 +4,8 @@
4
4
  * The signalling service join() uses unless given another.
5
5
  * @since 1.0.0
6
6
  * @example
7
- * const room = await join({ app: 'pk_…', room: 'lobby' }); // uses SERVER
7
+ * // uses SERVER
8
+ * const room = await join({ app: 'pk_…', room: 'lobby' });
8
9
  */
9
10
  export const SERVER: string;
10
11
  /**
@@ -18,14 +19,16 @@ export const SDK_VERSION: string;
18
19
  * The wire version this SDK speaks. A room runs one: a peer of the other gets JoinError code 'wire'.
19
20
  * @since 1.0.0
20
21
  * @example
21
- * const roomName = `game-${id}-w${WIRE}`; // a room per wire version during an upgrade
22
+ * // a room per wire version during an upgrade
23
+ * const roomName = `game-${id}-w${WIRE}`;
22
24
  */
23
25
  export const WIRE: 2;
24
26
  /**
25
27
  * Bytes of app data per send() / broadcast() (1099 in 0.1.x).
26
28
  * @since 1.0.0
27
29
  * @example
28
- * if (bytes.length > MAX_DATAGRAM) await room.sendReliable(to, bytes); else room.send(to, bytes);
30
+ * if (bytes.length > MAX_DATAGRAM) await room.sendReliable(to, bytes);
31
+ * else room.send(to, bytes);
29
32
  */
30
33
  export const MAX_DATAGRAM: 1052;
31
34
  /**
@@ -47,7 +50,9 @@ export type Data = Uint8Array | ArrayBuffer | string;
47
50
  * A peer's id in the room: an integer the service assigns at each join.
48
51
  * @since 1.0.0
49
52
  * @example
50
- * room.on('peer', (id: PeerId, up) => console.log(id, up ? 'joined' : 'left'));
53
+ * room.on('peer', (id: PeerId, up) => {
54
+ * console.log(id, up ? 'joined' : 'left');
55
+ * });
51
56
  */
52
57
  export type PeerId = number;
53
58
  /**
@@ -61,10 +66,12 @@ export type Channel = number;
61
66
  /**
62
67
  * Where this peer is. 'away': its page is hidden (phones, desktop Safari) and it holds no slot;
63
68
  * 'superseded': a newer tab with the same id took over; 'evicted': the app's backend removed it;
64
- * 'suspended': the app's cost cap is reached (links kept, joins refused); 'left': leave() was called.
69
+ * 'suspended': the account's spend cap is reached (links kept, joins refused); 'left': leave() was called.
65
70
  * @since 1.0.0
66
71
  * @example
67
- * room.on('state', (s: State) => { if (s === 'suspended') showBanner('Paused: the app is over its plan'); });
72
+ * room.on('state', (s: State) => {
73
+ * if (s === 'suspended') showBanner('Paused: the app is over its plan');
74
+ * });
68
75
  */
69
76
  export type State =
70
77
  /** join() is connecting: the service admitted this peer, its first links are forming. */
@@ -77,7 +84,7 @@ export type State =
77
84
  | 'superseded'
78
85
  /** The app's backend removed this peer (see the 'evicted' event); it has stopped for good. */
79
86
  | 'evicted'
80
- /** The app's cost cap is reached: links are kept, but no new peer can join the app until the cap is raised (see the 'suspended' event). */
87
+ /** The account's spend cap is reached: links stay up, but new joins to the account's apps are refused ('quota') until an owner raises the cap or the next period starts (see the 'suspended' event). */
81
88
  | 'suspended'
82
89
  /** leave() was called. */
83
90
  | 'left';
@@ -86,7 +93,9 @@ export type State =
86
93
  * How a message was sent.
87
94
  * @since 1.0.0
88
95
  * @example
89
- * room.on('message', (from, data, info: MessageInfo) => { if (info.reliable) apply(text(data)); });
96
+ * room.on('message', (from, data, info: MessageInfo) => {
97
+ * if (info.reliable) apply(text(data));
98
+ * });
90
99
  */
91
100
  export interface MessageInfo {
92
101
  /** The channel it was sent on (0–13; 0 unless the sender chose another). */
@@ -101,7 +110,10 @@ export interface MessageInfo {
101
110
  * A region jsflow runs signalling and relays in: Stockholm (`eu-north-1`) or Ohio (`us-east-2`).
102
111
  * @since 1.0.0
103
112
  * @example
104
- * const room = await join({ app: 'pk_…', room: 'round-42', region: 'eu-north-1' }); // an EU-pinned app
113
+ * // an EU-pinned app
114
+ * const room = await join({
115
+ * app: 'pk_…', room: 'round-42', region: 'eu-north-1',
116
+ * });
105
117
  */
106
118
  export type Region = 'eu-north-1' | 'us-east-2';
107
119
 
@@ -109,7 +121,12 @@ export type Region = 'eu-north-1' | 'us-east-2';
109
121
  * What join() needs.
110
122
  * @since 1.0.0
111
123
  * @example
112
- * const opts: JoinOptions = { app: 'pk_…', room: 'round-42', ticket: () => fetch('/ticket', { method: 'POST' }).then((r) => r.text()) };
124
+ * const opts: JoinOptions = {
125
+ * app: 'pk_…',
126
+ * room: 'round-42',
127
+ * ticket: () =>
128
+ * fetch('/ticket', { method: 'POST' }).then((r) => r.text()),
129
+ * };
113
130
  */
114
131
  export interface JoinOptions {
115
132
  /** The app's publishable key ("pk_…"), from the dashboard. The page's origin must be one the app allows. */
@@ -182,7 +199,7 @@ export interface Room {
182
199
  on(ev: 'error', f: (e: Error) => void): this;
183
200
  /** The app's backend removed this peer; every other peer has cut it off. Joining again is refused (code 'blocked') until `until` (unix s). */
184
201
  on(ev: 'evicted', f: (info: { until: number }) => void): this;
185
- /** The app's cost cap is reached (the service said so in a signed notice at this peer's refresh). Links are kept; new joins to the app are refused (402 'quota') until the cap is raised; the state returns to 'connected' after a refresh without the notice. */
202
+ /** The account's spend cap is reached (the service said so in a signed notice at this peer's refresh). Links stay up, but new joins to the account's apps are refused (402 'quota') until an owner raises the cap or the next period starts; the state returns to 'connected' after a refresh without the notice. */
186
203
  on(ev: 'suspended', f: (info: { reason: 'quota' }) => void): this;
187
204
  /** Remove a listener added with on(): the same event name and the same function. */
188
205
  off(ev: string, f: (...a: any[]) => void): this;
@@ -267,8 +284,14 @@ export type ErrorCode =
267
284
  * 'quota'. A network failure rejects with a TypeError instead.
268
285
  * @since 1.0.0
269
286
  * @example
270
- * try { await join(opts); }
271
- * catch (e) { if (e instanceof JoinError && e.code === 'ticket_expired') return join({ ...opts, ticket: await fresh() }); throw e; }
287
+ * try {
288
+ * await join(opts);
289
+ * } catch (e) {
290
+ * if (e instanceof JoinError && e.code === 'ticket_expired') {
291
+ * return join({ ...opts, ticket: await fresh() });
292
+ * }
293
+ * throw e;
294
+ * }
272
295
  */
273
296
  export class JoinError extends Error {
274
297
  /** Always 'JoinError', so it survives a structured clone or a log line where instanceof does not. */
@@ -285,7 +308,12 @@ export class JoinError extends Error {
285
308
  * Join a room. Rejects with a JoinError if the service (or the SDK itself) refuses.
286
309
  * @since 1.0.0
287
310
  * @example
288
- * const room = await join({ app: 'pk_…', room: 'round-42', ticket: () => fetch('/ticket', { method: 'POST' }).then((r) => r.text()) });
311
+ * const room = await join({
312
+ * app: 'pk_…',
313
+ * room: 'round-42',
314
+ * ticket: () =>
315
+ * fetch('/ticket', { method: 'POST' }).then((r) => r.text()),
316
+ * });
289
317
  */
290
318
  export function join(opts: JoinOptions): Promise<Room>;
291
319
  /**
package/jsflow.js CHANGED
@@ -1,4 +1,4 @@
1
- // jsflow-sdk 1.0.0-rc.2. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
1
+ // jsflow-sdk 1.0.0. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
2
2
 
3
3
  // core/link-main.js
4
4
  function linkKind(stats) {
@@ -215,7 +215,7 @@ function ticketRegion(ticket) {
215
215
  return void 0;
216
216
  }
217
217
  }
218
- var SDK_VERSION = "1.0.0-rc.2";
218
+ var SDK_VERSION = "1.0.0";
219
219
  var WIRE = 2;
220
220
  var MAX_DATAGRAM = 1052;
221
221
  var MAX_RELIABLE = 65536;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jsflow-sdk",
3
- "version": "1.0.0-rc.2",
3
+ "version": "1.0.0",
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.0.0-rc.2",
3
- "jsflow.js": "sha384-TWv5Y1/UHWbfvHc9hVVcidbLAzwNnLqkRvKNozohCiccy7pZjReUXYnFpH3cM5Wt",
4
- "worker.js": "sha384-FP6p48dOLeb623bV9GRoeszT6RpUwimgxAPat059QIIDg2W8i/6hVNZZc1RAFe+2",
5
- "jsflow.d.ts": "sha384-ZQeEwggqNXnSnd25+7iK7oiLLI636vR16LRNPNflzbnxx/IsTmEsct592Eiq2mEg"
2
+ "version": "1.0.0",
3
+ "jsflow.js": "sha384-9ak4UNlO3ZaMWtzVXcCR5f6A8amo2mNxe61IE8UAmHv9ASKTjyAQJ70sd19jpY28",
4
+ "worker.js": "sha384-XMuPhWF0rckfgKkgs//zn+J1BMIGfatYL2BQra7RdXd8FAaOUCfBTczVjlD3GOBv",
5
+ "jsflow.d.ts": "sha384-6O9/JrVDLxYg34iUU/B+gVUlI/WS6/gyncTRbVRdt/9xx9r1L4bVNTZlw7jfR8Bi"
6
6
  }
package/worker.js CHANGED
@@ -1,4 +1,4 @@
1
- // jsflow-sdk 1.0.0-rc.2. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
1
+ // jsflow-sdk 1.0.0. Licence: see LICENSE (https://jsflow.com). Built by client/build.js.
2
2
 
3
3
  // core/header.js
4
4
  var VERSION = 2;
@@ -786,9 +786,9 @@ function meshNode(opts = {}) {
786
786
  const sus = [...nbr.values()].filter((n) => n.sus).length;
787
787
  if (deg() - sus >= o.target) return;
788
788
  const now = env2.now();
789
- const cand = reach.filter((x) => !banned.has(x) && !nbr.has(x) && !pending2.has(x) && !((reject.get(x) ?? 0) > now) && (lsdb.get(x)?.links.size ?? 0) < o.max && natCompatible(o.nat, lsdb.get(x)?.nat));
789
+ const cand = reach.filter((x) => !banned.has(x) && !busy(x) && !((reject.get(x) ?? 0) > now) && (lsdb.get(x)?.links.size ?? 0) < o.max && natCompatible(o.nat, lsdb.get(x)?.nat));
790
790
  if (!cand.length) {
791
- const full = reach.filter((x) => !nbr.has(x) && !pending2.has(x) && !((reject.get(x) ?? 0) > now) && natCompatible(o.nat, lsdb.get(x)?.nat));
791
+ const full = reach.filter((x) => !busy(x) && !((reject.get(x) ?? 0) > now) && natCompatible(o.nat, lsdb.get(x)?.nat));
792
792
  if (!full.length || now < floorAt) return;
793
793
  floorAt = now + o.floorMs;
794
794
  env2.metric("budgetFloor", 1);
@@ -799,13 +799,16 @@ function meshNode(opts = {}) {
799
799
  else pick = cand.reduce((a, b) => t.dist.get(a) <= t.dist.get(b) ? a : b);
800
800
  attempt(pick);
801
801
  }
802
+ function busy(x) {
803
+ return nbr.has(x) || pending2.has(x) || incoming.has(x);
804
+ }
802
805
  function rescue(exclude, onDone, minAgeMs = 0) {
803
806
  if (cloudBusy) return;
804
807
  cloudBusy = true;
805
808
  env2.bootstrap((peer) => {
806
809
  cloudBusy = false;
807
810
  onDone?.(peer !== null && peer !== void 0);
808
- if (peer === null || peer === void 0 || nbr.has(peer) || pending2.has(peer)) return;
811
+ if (peer === null || peer === void 0 || busy(peer)) return;
809
812
  env2.metric("cloudLink", peer);
810
813
  attempt(peer, true);
811
814
  }, exclude, minAgeMs);
@@ -834,7 +837,7 @@ function meshNode(opts = {}) {
834
837
  comps.sort((x, y) => y.length - x.length);
835
838
  for (const c of comps.slice(1)) {
836
839
  if (Math.min(...c) !== me) continue;
837
- const big = comps[0].filter((x) => !nbr.has(x) && !pending2.has(x) && natCompatible(o.nat, lsdb.get(x)?.nat));
840
+ const big = comps[0].filter((x) => !busy(x) && natCompatible(o.nat, lsdb.get(x)?.nat));
838
841
  if (big.length) {
839
842
  env2.metric("repair", a);
840
843
  attempt(big[Math.floor(env2.rand() * big.length)]);
@@ -1207,6 +1210,7 @@ function workerEnv({ id, inc, cloud, post, pool = null, shim = null, onMetric =
1207
1210
  if (c.key && parked.get(c.key) === c) return unpark(c, why);
1208
1211
  if (conns.get(peer) !== c) return;
1209
1212
  conns.delete(peer);
1213
+ poolOid.delete(peer);
1210
1214
  post({ op: "close", peer, n: c.n });
1211
1215
  h2.onLinkDown(peer, c.up ? why : "failed");
1212
1216
  }
@@ -1313,7 +1317,7 @@ function workerEnv({ id, inc, cloud, post, pool = null, shim = null, onMetric =
1313
1317
  h2.onCloudSignal(from, 1, sigWrap(0, 0, enc.encode(r.sdp)));
1314
1318
  setTimeout(() => {
1315
1319
  const c = conns.get(from);
1316
- if (c && !c.up) {
1320
+ if (c && !c.up && c.fromPool === r.oid) {
1317
1321
  metric2("poolJoinTimeout", { from });
1318
1322
  lost(from, c, "poolTimeout");
1319
1323
  }
@@ -1365,9 +1369,11 @@ function workerEnv({ id, inc, cloud, post, pool = null, shim = null, onMetric =
1365
1369
  const old = conns.get(peer);
1366
1370
  if (old) {
1367
1371
  conns.delete(peer);
1372
+ poolOid.delete(peer);
1368
1373
  post({ op: "close", peer, n: old.n });
1369
1374
  }
1370
1375
  const c = fresh(peer, "answer");
1376
+ if (poolOid.has(peer)) c.fromPool = poolOid.get(peer);
1371
1377
  post({ op: "answer", peer, n: c.n, sdp: dec.decode(blob) });
1372
1378
  } else if (kind === 2) {
1373
1379
  const c = conns.get(peer);
@@ -1381,6 +1387,7 @@ function workerEnv({ id, inc, cloud, post, pool = null, shim = null, onMetric =
1381
1387
  const c = conns.get(peer);
1382
1388
  if (c && !c.up) {
1383
1389
  conns.delete(peer);
1390
+ poolOid.delete(peer);
1384
1391
  post({ op: "close", peer, n: c.n });
1385
1392
  }
1386
1393
  },
@@ -1395,6 +1402,7 @@ function workerEnv({ id, inc, cloud, post, pool = null, shim = null, onMetric =
1395
1402
  const c = conns.get(peer);
1396
1403
  if (c) {
1397
1404
  conns.delete(peer);
1405
+ poolOid.delete(peer);
1398
1406
  post({ op: "close", peer, n: c.n });
1399
1407
  }
1400
1408
  },