jsflow-sdk 0.0.2 → 1.0.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 +111 -0
- package/LICENSE +101 -0
- package/README.md +29 -4
- package/jsflow.d.ts +297 -0
- package/jsflow.js +610 -0
- package/package.json +30 -36
- package/sri.json +6 -0
- package/worker.js +2567 -0
- package/.npmignore +0 -3
- package/index.html +0 -7
- package/lib/auth.js +0 -32
- package/lib/config.js +0 -4
- package/lib/connect.js +0 -13
- package/lib/log.js +0 -43
- package/lib/main.js +0 -49
- package/lib/token.js +0 -34
- package/logTest.js +0 -49
- package/plan.txt +0 -60
- package/src/main.js +0 -43
- package/src/main_ws.js +0 -81
- package/ssl/GandiStandardSSLCA.pem +0 -27
- package/test.js +0 -18
- package/web.js +0 -8295
- package/web2.js +0 -1297
- package/web3.js +0 -6984
- package/web4.js +0 -11488
- package/web5.js +0 -28222
- package/web_min.js.gz +0 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `jsflow-sdk`. The format follows
|
|
4
|
+
[Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and versions follow
|
|
5
|
+
[Semantic Versioning](https://semver.org/): the API in `jsflow.d.ts` is the
|
|
6
|
+
contract; metric names passed to `onMetric` are diagnostic and may change in
|
|
7
|
+
any minor release.
|
|
8
|
+
|
|
9
|
+
## [1.0.0-rc.1] - 2026-11-18
|
|
10
|
+
|
|
11
|
+
The release candidate for 1.0.0: everything listed under 1.0.0 below. Published under the npm tag `next`
|
|
12
|
+
(`npm install jsflow-sdk@next`); `npm install jsflow-sdk` keeps the stable release.
|
|
13
|
+
|
|
14
|
+
## [1.0.0] - unreleased
|
|
15
|
+
|
|
16
|
+
The first stable API, on wire version 2.
|
|
17
|
+
|
|
18
|
+
### Migrating from 0.1.x
|
|
19
|
+
|
|
20
|
+
1. **One wire version per room.** 1.0 speaks wire 2 only; 0.1.x speaks
|
|
21
|
+
wire 1. Both work against the service, but a room runs the version of
|
|
22
|
+
its first joiner, and a peer of the other version is refused
|
|
23
|
+
(`JoinError` code `wire`). Ship 1.0 to all your clients at once, or
|
|
24
|
+
give 1.0 rooms new names (for example a `-w2` suffix, `WIRE` is
|
|
25
|
+
exported for it) while old pages are still open.
|
|
26
|
+
2. **`MAX_DATAGRAM` is 1052 bytes** (was 1099): `send()` and `broadcast()`
|
|
27
|
+
throw a `RangeError` above it, because every broadcast now carries a
|
|
28
|
+
signature. If you split data by size, split at 1052 (or a little under);
|
|
29
|
+
use `sendReliable()` (up to `MAX_RELIABLE`, 65536) for anything larger.
|
|
30
|
+
3. **`join()` rejects with a `JoinError`** (was a plain `Error` whose
|
|
31
|
+
message held the HTTP status and body). Branch on `e.code`, one of
|
|
32
|
+
`ErrorCode` (the same words as the account API's):
|
|
33
|
+
- `unknown_app` (was `403 app`), `origin` (was `403 origin`),
|
|
34
|
+
`room_full` (was a bare `409`), `rate` (was `429`);
|
|
35
|
+
- new: `ticket_required`, `ticket_invalid`, `ticket_expired`
|
|
36
|
+
(tickets, below), `blocked` (your backend evicted or blocked this
|
|
37
|
+
user), `quota` (your cost cap is reached), `wire` (above),
|
|
38
|
+
`sdk_unsupported` (an SDK older than the service accepts);
|
|
39
|
+
- raised by the SDK itself, `status` 0: `unsupported_browser` (no Web
|
|
40
|
+
Crypto X25519 or Ed25519, below) and `worker_failed` (the SDK's
|
|
41
|
+
worker did not start, twice, on a busy device: joining again usually
|
|
42
|
+
works);
|
|
43
|
+
- `wrong_home` never reaches you: the SDK follows it to the room's home
|
|
44
|
+
instance by itself.
|
|
45
|
+
A network failure still rejects with a `TypeError`.
|
|
46
|
+
4. **Two new states**, with events: `evicted` (your backend removed this
|
|
47
|
+
peer; joining again is refused until the block ends; final) and
|
|
48
|
+
`suspended` (your cost cap is reached: links stay up, new joins to the
|
|
49
|
+
app are refused until the cap is raised; it returns to `connected` by
|
|
50
|
+
itself). An exhaustive `switch` on `State` needs both cases.
|
|
51
|
+
5. **Minimum browsers: Chrome and Edge 137, Safari 17, Firefox 130.**
|
|
52
|
+
Every peer seals and signs with Web Crypto X25519 and Ed25519; an older
|
|
53
|
+
browser gets `unsupported_browser` before any request (0.1.x joined
|
|
54
|
+
it and ran its links unsealed).
|
|
55
|
+
6. **Tickets (optional, recommended).** Your backend signs a short-lived
|
|
56
|
+
ticket with the app's secret key (`mintTicket` in the docs, no call to
|
|
57
|
+
us) and the page passes it as `join({ ticket })`, a string or a
|
|
58
|
+
function the SDK calls again at every rejoin. With `requireTickets` on
|
|
59
|
+
for the app, a join without one is refused. `room.user(peerId)` then
|
|
60
|
+
gives each peer's user id.
|
|
61
|
+
7. **CDN URLs.** The version directory now holds two files: `jsflow.js`
|
|
62
|
+
and `worker.js` (0.1.x had a tree under `client/` and `core/`). Import
|
|
63
|
+
`https://cdn.jsflow.com/v1.0.0/jsflow.js` as before; `worker.js` must
|
|
64
|
+
stay next to it. Each version directory has `sri.json`, the
|
|
65
|
+
`integrity` values for `<script type="module" integrity="…">`. On npm:
|
|
66
|
+
`jsflow-sdk` 1.0.0 (the 0.0.x versions on npm are unrelated and
|
|
67
|
+
deprecated).
|
|
68
|
+
8. **`onMetric(name, value, detail?)`**: `value` is now always a number
|
|
69
|
+
(1 for an event) and the measurement's data is `detail`. Metric names
|
|
70
|
+
are diagnostic and may change in any minor release.
|
|
71
|
+
9. **Removed** lab options (`lab`, `want`, `pool`, `target`, `max`) and
|
|
72
|
+
`room.lab()`; they were never documented.
|
|
73
|
+
|
|
74
|
+
### Added
|
|
75
|
+
- Join tickets, `room.user(peerId)`, `JoinError {status, code}` with the
|
|
76
|
+
`ErrorCode` union, the `evicted` and `suspended` states and events,
|
|
77
|
+
`room.ping(peer)` (round-trip time and hops through the mesh), and the
|
|
78
|
+
`SDK_VERSION`, `WIRE` and `MAX_RELIABLE` exports.
|
|
79
|
+
- Every broadcast and link-state message is signed (Ed25519) and checked
|
|
80
|
+
by every peer; forged ones are dropped where they arrive.
|
|
81
|
+
- Tokens and relay credentials refresh, so rooms live past 6 hours; the
|
|
82
|
+
signalling stream resumes after a drop without losing signals.
|
|
83
|
+
- HTTP/2 to the signalling service; rooms move between service instances
|
|
84
|
+
(deploys, failover) without dropping links.
|
|
85
|
+
- A hidden page (phones, desktop Safari) holds no slot and accrues no time
|
|
86
|
+
while away.
|
|
87
|
+
- `join({ worker })`: the URL of `worker.js` for a self-hosted copy.
|
|
88
|
+
- `join({ region })` (`'eu-north-1'` or `'us-east-2'`): a region-pinned
|
|
89
|
+
app's first request goes straight to that region's signalling, so it
|
|
90
|
+
never reaches another region. A join ticket's `region` claim does the
|
|
91
|
+
same (and wins if the two differ); an explicit `server` wins over both.
|
|
92
|
+
|
|
93
|
+
### Changed
|
|
94
|
+
- `MAX_DATAGRAM` 1052 (was 1099); `join()` rejects with `JoinError`;
|
|
95
|
+
`onMetric(name, value: number, detail?)`; two files per release.
|
|
96
|
+
- `send()` and `broadcast()` (unreliable) drop a datagram, instead of
|
|
97
|
+
queueing it, when the link already holds 256 KB of unsent ones: data
|
|
98
|
+
that late is worthless and delays what follows. Drops are counted as the
|
|
99
|
+
metric `dgDropped` (`detail`: `{n, bytes}`, at most once a second).
|
|
100
|
+
`sendReliable()` is unaffected.
|
|
101
|
+
- `iceTransportPolicy: 'relay'` is now enforced by the SDK, not only by the
|
|
102
|
+
browser: everything it signals carries relay candidates only (no local,
|
|
103
|
+
mDNS or public-mapped address, and no related address), through every
|
|
104
|
+
reconnect, ICE restart and move. Use it for rooms of strangers, children,
|
|
105
|
+
or anyone who must not learn another's IP address.
|
|
106
|
+
- `app` (your publishable key) is required: `join()` throws a `TypeError`
|
|
107
|
+
without it.
|
|
108
|
+
|
|
109
|
+
### Removed
|
|
110
|
+
- Lab-only options and methods (`lab`, `want`, `pool`, `target`, `max`,
|
|
111
|
+
`room.lab()`).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
## jsflow SDK and CLI Licence
|
|
2
|
+
|
|
3
|
+
*Version 2, 18 November 2026. Copyright (c) 2026 Precisit AB. All rights reserved except as granted below.*
|
|
4
|
+
|
|
5
|
+
**Definitions.** "**Service**" means the jsflow signalling and relay service that Precisit AB ("Precisit") operates.
|
|
6
|
+
"**You**" means the person or organisation that accepts this licence. "**SDK**" means the jsflow client library: the
|
|
7
|
+
JavaScript and TypeScript code and type definitions, and in the same package any build of them, whether from npm, from
|
|
8
|
+
`cdn.jsflow.com` or from a source distribution. "**CLI**" means the `jsflow` command line tool (the `jsflow-cli` package):
|
|
9
|
+
its code and documentation, in whatever form distributed. "**Software**" means the SDK and the CLI. "**End user**" means a
|
|
10
|
+
person who runs your product, which may include the SDK, with or without an account of their own.
|
|
11
|
+
|
|
12
|
+
### 1. What you may do with the SDK
|
|
13
|
+
|
|
14
|
+
You may, free of charge:
|
|
15
|
+
|
|
16
|
+
1. **Use** the SDK in your own products and projects to connect them to the Service, for **development, testing** and
|
|
17
|
+
production.
|
|
18
|
+
2. **Copy and bundle** the SDK, unmodified or minified and bundled by your build tools, with your product, and
|
|
19
|
+
**distribute it in your applications and packages** (a web page's code, an app, a package that depends on it).
|
|
20
|
+
3. **Let your end users run it.** Your end users may run the SDK as part of your product, and use it to reach the
|
|
21
|
+
Service, **without an account** and without accepting anything else; this licence reaches them through your product
|
|
22
|
+
and they need nothing more. You are responsible for their use as the terms of service say.
|
|
23
|
+
4. **Read** the source, and run and modify it **for your own use** (to debug, to test a fix or to learn from it).
|
|
24
|
+
5. **Report** problems and **suggest** changes; see 5 for contributions.
|
|
25
|
+
|
|
26
|
+
### 2. What you may do with the CLI
|
|
27
|
+
|
|
28
|
+
You may, free of charge, **install and run the CLI** for administering your jsflow account, in continuous integration and
|
|
29
|
+
in testing against jsflow, on your own and your organisation's machines and your build systems, and read its source. The
|
|
30
|
+
app-bundling and distribution rights in 1 apply to the SDK, not to the CLI: you may not distribute the CLI as part of a
|
|
31
|
+
product or on its own, and the CLI may only be used with the Service. Its optional components (for example a Node or
|
|
32
|
+
WebRTC package it loads for room commands) keep their own licences, which we list in `THIRD-PARTY-NOTICES` in the package.
|
|
33
|
+
|
|
34
|
+
### 3. What you may not do
|
|
35
|
+
|
|
36
|
+
You may not:
|
|
37
|
+
|
|
38
|
+
1. **Use the Software with another service that offers what the Service offers**, or modify it to talk to one, unless
|
|
39
|
+
Precisit allows it in writing. (Self-hosting Precisit's published server software is governed by that software's own
|
|
40
|
+
licence, not this one.)
|
|
41
|
+
2. **Offer the Software**, or a modified version of it, on its own to others as a library, package or service, or
|
|
42
|
+
**publish a fork** of it.
|
|
43
|
+
3. **Build a competing service** using the SDK's wire protocol or code, or use the source to make one.
|
|
44
|
+
4. **Remove or change** the copyright, licence and third-party notices.
|
|
45
|
+
5. **Use Precisit's names, logos and marks** except to say truthfully that your product uses jsflow.
|
|
46
|
+
6. **Use the Software where sanctions or export controls forbid it** (see 4).
|
|
47
|
+
|
|
48
|
+
Nothing in this licence limits a right the law gives you that cannot be excluded by contract, including the rights of a
|
|
49
|
+
lawful user of software to observe, test and (under the conditions the law sets) decompile it to achieve interoperability.
|
|
50
|
+
|
|
51
|
+
### 4. Sanctions and export
|
|
52
|
+
|
|
53
|
+
You will not use, export or give access to the Software in breach of applicable sanctions or export-control law, and you
|
|
54
|
+
confirm that you are not a restricted party or in a restricted territory for it. You will take the steps that are within your
|
|
55
|
+
reach to comply for your end users (for example, not knowingly providing the Software to a restricted party or territory, and
|
|
56
|
+
acting on notice from us or from a competent authority); this is an obligation of compliance you can perform, not a warranty
|
|
57
|
+
about every person who runs your product. Precisit may refuse or
|
|
58
|
+
end access to the Service and the Software where it must, or where it cannot establish that the law allows it.
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
### 5. Conditions and contributions
|
|
62
|
+
|
|
63
|
+
5.1 **The Service terms.** Using the SDK or the CLI **with the Service** is under the terms of service for the Service,
|
|
64
|
+
including the acceptable use policy, which apply as soon as you create an account and key. The licence for the Software
|
|
65
|
+
itself does not depend on an end user having an account (1.3).
|
|
66
|
+
|
|
67
|
+
5.2 **Updates.** Precisit may release new versions under this licence or a later one. A published version stays available at
|
|
68
|
+
its URL, and the licence it was published with stays valid for it, including the permission published beside each 0.1
|
|
69
|
+
build.
|
|
70
|
+
|
|
71
|
+
5.3 **Contributions.** If you send Precisit a suggestion, patch or other contribution, you give Precisit a worldwide,
|
|
72
|
+
perpetual, irrevocable, royalty-free right to use, change and distribute it as part of the Software and the Service, and
|
|
73
|
+
you confirm you have the right to give it. Precisit is not obliged to use it.
|
|
74
|
+
|
|
75
|
+
### 6. Termination
|
|
76
|
+
|
|
77
|
+
This licence ends for you automatically if you break it. When it ends you stop using and distributing the Software.
|
|
78
|
+
**Rights already granted downstream stay**: an end user who received a copy of the SDK lawfully, in a product they received
|
|
79
|
+
before the licence ended for you, has a continuing permission to run that copy under 1.3, and ending your licence does not
|
|
80
|
+
take away what we have granted them. Suspension or closure of an account can still affect whether that copy can connect to the Service. Sections 3, 4, 7, 8 and 9 continue after the end.
|
|
81
|
+
|
|
82
|
+
### 7. No warranty
|
|
83
|
+
|
|
84
|
+
The Software is provided **as is**, without warranties of any kind, express or implied, including merchantability,
|
|
85
|
+
fitness for a particular purpose and non-infringement, to the extent the law permits.
|
|
86
|
+
|
|
87
|
+
### 8. Limit of liability
|
|
88
|
+
|
|
89
|
+
To the extent the law permits, Precisit is not liable for any loss arising from the Software's use, except as the terms of
|
|
90
|
+
service for the Service say for the Service. The limits in the terms apply.
|
|
91
|
+
|
|
92
|
+
### 9. General
|
|
93
|
+
|
|
94
|
+
This licence is governed by Swedish law, with the forum in the terms of service. If a part is invalid, the rest stands.
|
|
95
|
+
This licence and the terms of service are the whole agreement about the Software; if they conflict about use with the
|
|
96
|
+
Service, the terms of service prevail.
|
|
97
|
+
|
|
98
|
+
### Third-party code
|
|
99
|
+
|
|
100
|
+
The SDK has **no runtime dependencies**, and the CLI none by default.
|
|
101
|
+
Each published tarball (`npm pack`) includes `LICENSE` and matching package metadata.
|
package/README.md
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
==========
|
|
1
|
+
# jsflow-sdk
|
|
3
2
|
|
|
4
|
-
|
|
3
|
+
Peer-to-peer rooms for the browser: every peer in a room can reach every
|
|
4
|
+
other over a WebRTC mesh, with signalling and relays run as a service
|
|
5
|
+
([jsflow.com](https://jsflow.com)). No runtime dependencies.
|
|
5
6
|
|
|
6
|
-
|
|
7
|
+
```sh
|
|
8
|
+
npm install jsflow-sdk
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
import { join, text } from 'jsflow-sdk';
|
|
13
|
+
|
|
14
|
+
const room = await join({ app: 'pk_…', room: 'lobby' });
|
|
15
|
+
room.on('message', (from, data) => console.log(from, text(data)));
|
|
16
|
+
room.broadcast('hello');
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or from the CDN, at an exact version with its integrity hash (`sri.json` in
|
|
20
|
+
each version directory):
|
|
21
|
+
|
|
22
|
+
```html
|
|
23
|
+
<script type="module" integrity="sha384-…" crossorigin src="https://cdn.jsflow.com/v1.0.0/jsflow.js"></script>
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The SDK starts a worker (`worker.js`, next to `jsflow.js`). Vite and webpack
|
|
27
|
+
bundle it automatically; elsewhere, serve `worker.js` yourself and pass its
|
|
28
|
+
URL as `join({ worker })`.
|
|
29
|
+
|
|
30
|
+
Documentation: [jsflow.com/docs](https://jsflow.com/docs). The types are in
|
|
31
|
+
`jsflow.d.ts`. Licence: see `LICENSE` (free to use with the jsflow service).
|
package/jsflow.d.ts
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
// jsflow-sdk: the types of its public API (stable from 1.0). Documentation: https://jsflow.com/docs
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The signalling service join() uses unless given another.
|
|
5
|
+
* @since 1.0.0
|
|
6
|
+
* @example
|
|
7
|
+
* const room = await join({ app: 'pk_…', room: 'lobby' }); // uses SERVER
|
|
8
|
+
*/
|
|
9
|
+
export const SERVER: string;
|
|
10
|
+
/**
|
|
11
|
+
* This SDK's version, sent in every join; the service may refuse an old one (JoinError code 'sdk_unsupported').
|
|
12
|
+
* @since 1.0.0
|
|
13
|
+
* @example
|
|
14
|
+
* console.log(`jsflow ${SDK_VERSION}`);
|
|
15
|
+
*/
|
|
16
|
+
export const SDK_VERSION: string;
|
|
17
|
+
/**
|
|
18
|
+
* The wire version this SDK speaks. A room runs one: a peer of the other gets JoinError code 'wire'.
|
|
19
|
+
* @since 1.0.0
|
|
20
|
+
* @example
|
|
21
|
+
* const roomName = `game-${id}-w${WIRE}`; // a room per wire version during an upgrade
|
|
22
|
+
*/
|
|
23
|
+
export const WIRE: 2;
|
|
24
|
+
/**
|
|
25
|
+
* Bytes of app data per send() / broadcast() (1099 in 0.1.x).
|
|
26
|
+
* @since 1.0.0
|
|
27
|
+
* @example
|
|
28
|
+
* if (bytes.length > MAX_DATAGRAM) await room.sendReliable(to, bytes); else room.send(to, bytes);
|
|
29
|
+
*/
|
|
30
|
+
export const MAX_DATAGRAM: 1052;
|
|
31
|
+
/**
|
|
32
|
+
* Bytes per sendReliable() message.
|
|
33
|
+
* @since 1.0.0
|
|
34
|
+
* @example
|
|
35
|
+
* if (file.byteLength > MAX_RELIABLE) throw new Error('send it in parts');
|
|
36
|
+
*/
|
|
37
|
+
export const MAX_RELIABLE: 65536;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Data in: bytes, or a string sent as UTF-8. Data out is always a Uint8Array.
|
|
41
|
+
* @since 1.0.0
|
|
42
|
+
* @example
|
|
43
|
+
* const d: Data = JSON.stringify({ x: 1, y: 2 });
|
|
44
|
+
*/
|
|
45
|
+
export type Data = Uint8Array | ArrayBuffer | string;
|
|
46
|
+
/**
|
|
47
|
+
* A peer's id in the room: an integer the service assigns at each join.
|
|
48
|
+
* @since 1.0.0
|
|
49
|
+
* @example
|
|
50
|
+
* room.on('peer', (id: PeerId, up) => console.log(id, up ? 'joined' : 'left'));
|
|
51
|
+
*/
|
|
52
|
+
export type PeerId = number;
|
|
53
|
+
/**
|
|
54
|
+
* Channels 0–13 are the app's (default 0); a message arrives with its channel.
|
|
55
|
+
* @since 1.0.0
|
|
56
|
+
* @example
|
|
57
|
+
* room.broadcast(stroke, { channel: 1 });
|
|
58
|
+
*/
|
|
59
|
+
export type Channel = number;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Where this peer is. 'away': its page is hidden (phones, desktop Safari) and it holds no slot;
|
|
63
|
+
* '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.
|
|
65
|
+
* @since 1.0.0
|
|
66
|
+
* @example
|
|
67
|
+
* room.on('state', (s: State) => { if (s === 'suspended') showBanner('Paused: the app is over its plan'); });
|
|
68
|
+
*/
|
|
69
|
+
export type State =
|
|
70
|
+
/** join() is connecting: the service admitted this peer, its first links are forming. */
|
|
71
|
+
| 'joining'
|
|
72
|
+
/** In the room: linked to the mesh and reachable by the other peers. */
|
|
73
|
+
| 'connected'
|
|
74
|
+
/** The page is hidden (phones, desktop Safari): this peer said goodbye, holds no slot and accrues no time; it rejoins when the page is visible again. */
|
|
75
|
+
| 'away'
|
|
76
|
+
/** A newer tab or page with the same id took over; this one has stopped for good. */
|
|
77
|
+
| 'superseded'
|
|
78
|
+
/** The app's backend removed this peer (see the 'evicted' event); it has stopped for good. */
|
|
79
|
+
| '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). */
|
|
81
|
+
| 'suspended'
|
|
82
|
+
/** leave() was called. */
|
|
83
|
+
| 'left';
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* How a message was sent.
|
|
87
|
+
* @since 1.0.0
|
|
88
|
+
* @example
|
|
89
|
+
* room.on('message', (from, data, info: MessageInfo) => { if (info.reliable) apply(text(data)); });
|
|
90
|
+
*/
|
|
91
|
+
export interface MessageInfo {
|
|
92
|
+
/** The channel it was sent on (0–13; 0 unless the sender chose another). */
|
|
93
|
+
channel: Channel;
|
|
94
|
+
/** true: sent with broadcast(); false: send() or sendReliable(). */
|
|
95
|
+
broadcast: boolean;
|
|
96
|
+
/** true: sent with sendReliable() (in order, retransmitted). */
|
|
97
|
+
reliable: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A region jsflow runs signalling and relays in: Stockholm (`eu-north-1`) or Ohio (`us-east-2`).
|
|
102
|
+
* @since 1.0.0
|
|
103
|
+
* @example
|
|
104
|
+
* const room = await join({ app: 'pk_…', room: 'round-42', region: 'eu-north-1' }); // an EU-pinned app
|
|
105
|
+
*/
|
|
106
|
+
export type Region = 'eu-north-1' | 'us-east-2';
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* What join() needs.
|
|
110
|
+
* @since 1.0.0
|
|
111
|
+
* @example
|
|
112
|
+
* const opts: JoinOptions = { app: 'pk_…', room: 'round-42', ticket: () => fetch('/ticket', { method: 'POST' }).then((r) => r.text()) };
|
|
113
|
+
*/
|
|
114
|
+
export interface JoinOptions {
|
|
115
|
+
/** The app's publishable key ("pk_…"), from the dashboard. The page's origin must be one the app allows. */
|
|
116
|
+
app: string;
|
|
117
|
+
/** Room name, [A-Za-z0-9_-], 1–64 characters. Rooms are the app's own. */
|
|
118
|
+
room: string;
|
|
119
|
+
/** Base URL of the signalling service (default SERVER). Given, it wins over `region` and a ticket's region. */
|
|
120
|
+
server?: string;
|
|
121
|
+
/**
|
|
122
|
+
* The region an app is pinned to: the first request goes straight to that region's signalling, so it never
|
|
123
|
+
* reaches another region (for a region-pinned app, set in the dashboard). A join ticket's `region` claim does
|
|
124
|
+
* the same and wins if the two differ. Not needed without a pin: the service picks a nearby region.
|
|
125
|
+
* @since 1.0.0
|
|
126
|
+
*/
|
|
127
|
+
region?: Region;
|
|
128
|
+
/** Overrides the ICE servers (STUN/TURN) the service hands out. */
|
|
129
|
+
iceServers?: RTCIceServer[];
|
|
130
|
+
/**
|
|
131
|
+
* Passed to every RTCPeerConnection. 'relay': all traffic goes through the
|
|
132
|
+
* relay (TURN), so peers never see each other's IP addresses, at the cost of
|
|
133
|
+
* a little latency and relay bandwidth. Default 'all' (direct when possible).
|
|
134
|
+
*/
|
|
135
|
+
iceTransportPolicy?: RTCIceTransportPolicy;
|
|
136
|
+
/** Diagnostic measurements for your own telemetry: `value` a number (1 for an event), `detail` its data. Metric names are diagnostic and may change in any minor release. */
|
|
137
|
+
onMetric?: (name: string, value: number, detail?: Readonly<Record<string, unknown>>) => void;
|
|
138
|
+
/** Send BYE when the page is hidden (default: phones and desktop Safari, whose pages freeze). */
|
|
139
|
+
bye?: boolean;
|
|
140
|
+
/** The URL of the SDK's worker script, for a self-hosted copy of the SDK (default: worker.js next to this module). */
|
|
141
|
+
worker?: string;
|
|
142
|
+
/**
|
|
143
|
+
* A join ticket from your backend (an HS256 JWT signed with the app's secret key):
|
|
144
|
+
* a string, or a function the SDK calls for a fresh one at every join. Required
|
|
145
|
+
* when the app requires tickets; its user id becomes room.user(peerId) for everyone.
|
|
146
|
+
*/
|
|
147
|
+
ticket?: string | (() => Promise<string> | string);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A joined room: the peers this page can reach, and how to talk to them.
|
|
152
|
+
* @since 1.0.0
|
|
153
|
+
* @example
|
|
154
|
+
* const room = await join({ app: 'pk_…', room: 'lobby' });
|
|
155
|
+
* room.on('message', (from, data) => console.log(from, text(data)));
|
|
156
|
+
* room.broadcast('hello');
|
|
157
|
+
*/
|
|
158
|
+
export interface Room {
|
|
159
|
+
/** My peer id. A page reload or a new join() gets a new one. */
|
|
160
|
+
readonly id: PeerId;
|
|
161
|
+
/** The room name given to join(). */
|
|
162
|
+
readonly room: string;
|
|
163
|
+
/** Where this peer is now; changes are reported by the 'state' event. */
|
|
164
|
+
readonly state: State;
|
|
165
|
+
/** The peers I can reach now. */
|
|
166
|
+
peers(): PeerId[];
|
|
167
|
+
/** A peer's user id from its join ticket, vouched for by the service; '' if it joined without one; null if not known yet. */
|
|
168
|
+
user(peerId: PeerId): string | null;
|
|
169
|
+
|
|
170
|
+
/** A peer became reachable (`up` true) or unreachable (`up` false). peers() lists the reachable ones. */
|
|
171
|
+
on(ev: 'peer', f: (id: PeerId, up: boolean) => void): this;
|
|
172
|
+
/** A message from `from`: its bytes (text() decodes UTF-8) and how it was sent. */
|
|
173
|
+
on(ev: 'message', f: (from: PeerId, data: Uint8Array, info: MessageInfo) => void): this;
|
|
174
|
+
/** This peer's state changed (the new value is also in room.state). */
|
|
175
|
+
on(ev: 'state', f: (s: State) => void): this;
|
|
176
|
+
/** Reliable messages to `peer` that may not have arrived: it stayed unreachable for 30 s, or restarted. */
|
|
177
|
+
on(ev: 'undelivered', f: (peer: PeerId, n: number, why: string) => void): this;
|
|
178
|
+
/**
|
|
179
|
+
* The SDK's worker could not set up the room after join() resolved: `e` is an Error whose message says
|
|
180
|
+
* what failed (nothing more is promised). The room will not connect: call leave(), then join() again.
|
|
181
|
+
*/
|
|
182
|
+
on(ev: 'error', f: (e: Error) => void): this;
|
|
183
|
+
/** 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
|
+
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. */
|
|
186
|
+
on(ev: 'suspended', f: (info: { reason: 'quota' }) => void): this;
|
|
187
|
+
/** Remove a listener added with on(): the same event name and the same function. */
|
|
188
|
+
off(ev: string, f: (...a: any[]) => void): this;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* A datagram to one peer: fast, may be lost, ≤ MAX_DATAGRAM bytes. If a link already holds 256 KB of
|
|
192
|
+
* unsent datagrams (a burst far above a normal rate), new ones on it are dropped rather than sent late,
|
|
193
|
+
* counted as the metric 'dgDropped'. sendReliable() is never dropped.
|
|
194
|
+
*/
|
|
195
|
+
send(to: PeerId, data: Data, o?: { channel?: Channel }): void;
|
|
196
|
+
/** A datagram to every peer in the room: may be lost, ≤ MAX_DATAGRAM bytes; dropped on a link that already holds 256 KB of unsent datagrams, as send(). */
|
|
197
|
+
broadcast(data: Data, o?: { channel?: Channel }): void;
|
|
198
|
+
/**
|
|
199
|
+
* In order per peer, retransmitted until acknowledged, ≤ MAX_RELIABLE bytes.
|
|
200
|
+
* Resolves true when queued, false if that peer's queue (1 MiB) is full.
|
|
201
|
+
*/
|
|
202
|
+
sendReliable(to: PeerId, data: Data, o?: { channel?: Channel }): Promise<boolean>;
|
|
203
|
+
/**
|
|
204
|
+
* Round trip to a peer through the mesh (a sealed echo): its RTT in ms, the
|
|
205
|
+
* hops it took, and the peer's wall clock in ms; null if no answer in 5 s.
|
|
206
|
+
*/
|
|
207
|
+
ping(to: PeerId): Promise<{ rtt: number; hops: number; clock: number } | null>;
|
|
208
|
+
/** Leave the room: neighbours route around me at once. */
|
|
209
|
+
leave(): void;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// @codes-begin (generated: do not edit)
|
|
213
|
+
/**
|
|
214
|
+
* Every code a JoinError (or a service answer the SDK passes on) can carry, the same words as the
|
|
215
|
+
* account API's. Each code and what to do about it: https://jsflow.com/docs/reference/errors/
|
|
216
|
+
* @since 1.0.0
|
|
217
|
+
* @example
|
|
218
|
+
* if (e instanceof JoinError && e.code === 'room_full') showFull();
|
|
219
|
+
*/
|
|
220
|
+
export type ErrorCode =
|
|
221
|
+
| 'bad_blob'
|
|
222
|
+
| 'bad_id'
|
|
223
|
+
| 'bad_inc'
|
|
224
|
+
| 'bad_json'
|
|
225
|
+
| 'bad_kind'
|
|
226
|
+
| 'bad_path'
|
|
227
|
+
| 'bad_pool_token'
|
|
228
|
+
| 'bad_pub'
|
|
229
|
+
| 'bad_request'
|
|
230
|
+
| 'bad_room'
|
|
231
|
+
| 'bad_session_key'
|
|
232
|
+
| 'blocked'
|
|
233
|
+
| 'draining'
|
|
234
|
+
| 'internal'
|
|
235
|
+
| 'method_not_allowed'
|
|
236
|
+
| 'not_found'
|
|
237
|
+
| 'not_yours'
|
|
238
|
+
| 'origin'
|
|
239
|
+
| 'pool_full'
|
|
240
|
+
| 'quota'
|
|
241
|
+
| 'rate'
|
|
242
|
+
| 'room_full'
|
|
243
|
+
| 'sdk_unsupported'
|
|
244
|
+
| 'server_full'
|
|
245
|
+
| 'superseded'
|
|
246
|
+
| 'suspended'
|
|
247
|
+
| 'ticket_expired'
|
|
248
|
+
| 'ticket_invalid'
|
|
249
|
+
| 'ticket_required'
|
|
250
|
+
| 'too_large'
|
|
251
|
+
| 'too_many_rooms'
|
|
252
|
+
| 'too_many_streams'
|
|
253
|
+
| 'unauthorized'
|
|
254
|
+
| 'unknown_app'
|
|
255
|
+
| 'unsupported_browser'
|
|
256
|
+
| 'want_not_allowed'
|
|
257
|
+
| 'wire'
|
|
258
|
+
| 'worker_failed'
|
|
259
|
+
| 'wrong_home';
|
|
260
|
+
// @codes-end
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* A refused join. `status` is the HTTP status (0 when the SDK refuses itself: 'unsupported_browser',
|
|
264
|
+
* no Web Crypto X25519/Ed25519, Chrome/Edge 137+, Safari 17+, Firefox 130+; 'worker_failed', the SDK's
|
|
265
|
+
* worker did not start twice). `code` is one of ErrorCode, e.g. 'unknown_app', 'origin', 'room_full',
|
|
266
|
+
* 'rate', 'wire' (the room runs another wire version), 'sdk_unsupported', 'ticket_required', 'blocked',
|
|
267
|
+
* 'quota'. A network failure rejects with a TypeError instead.
|
|
268
|
+
* @since 1.0.0
|
|
269
|
+
* @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; }
|
|
272
|
+
*/
|
|
273
|
+
export class JoinError extends Error {
|
|
274
|
+
/** Always 'JoinError', so it survives a structured clone or a log line where instanceof does not. */
|
|
275
|
+
readonly name: 'JoinError';
|
|
276
|
+
/** The HTTP status; 0 for a refusal the SDK makes itself. */
|
|
277
|
+
readonly status: number;
|
|
278
|
+
/** Why: one of ErrorCode. Each code and what to do about it: https://jsflow.com/docs/reference/errors/ */
|
|
279
|
+
readonly code: ErrorCode;
|
|
280
|
+
/** The service's answer: { err, message, ...details }, or null if it was not JSON. */
|
|
281
|
+
readonly body: { err: string; message?: string; [k: string]: unknown } | null;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Join a room. Rejects with a JoinError if the service (or the SDK itself) refuses.
|
|
286
|
+
* @since 1.0.0
|
|
287
|
+
* @example
|
|
288
|
+
* const room = await join({ app: 'pk_…', room: 'round-42', ticket: () => fetch('/ticket', { method: 'POST' }).then((r) => r.text()) });
|
|
289
|
+
*/
|
|
290
|
+
export function join(opts: JoinOptions): Promise<Room>;
|
|
291
|
+
/**
|
|
292
|
+
* Decode a message's bytes as UTF-8 text.
|
|
293
|
+
* @since 1.0.0
|
|
294
|
+
* @example
|
|
295
|
+
* room.on('message', (from, data) => chat.append(text(data)));
|
|
296
|
+
*/
|
|
297
|
+
export function text(data: Uint8Array): string;
|