@voltkit-rtc/client 0.6.1 → 0.7.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 +68 -2
- package/README.md +222 -11
- package/dist/cjs/chat.d.ts +0 -9
- package/dist/cjs/chat.d.ts.map +1 -1
- package/dist/cjs/chat.js +16 -14
- package/dist/cjs/chat.js.map +1 -1
- package/dist/cjs/errors.d.ts +571 -0
- package/dist/cjs/errors.d.ts.map +1 -0
- package/dist/cjs/errors.js +155 -0
- package/dist/cjs/errors.js.map +1 -0
- package/dist/cjs/index.d.ts +298 -16
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +654 -118
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/media-permissions.d.ts +101 -0
- package/dist/cjs/media-permissions.d.ts.map +1 -0
- package/dist/cjs/media-permissions.js +166 -0
- package/dist/cjs/media-permissions.js.map +1 -0
- package/dist/cjs/network.d.ts +80 -0
- package/dist/cjs/network.d.ts.map +1 -0
- package/dist/cjs/network.js +190 -0
- package/dist/cjs/network.js.map +1 -0
- package/dist/cjs/token.d.ts +17 -0
- package/dist/cjs/token.d.ts.map +1 -0
- package/dist/cjs/token.js +36 -0
- package/dist/cjs/token.js.map +1 -0
- package/dist/esm/chat.d.ts +0 -9
- package/dist/esm/chat.d.ts.map +1 -1
- package/dist/esm/chat.js +16 -14
- package/dist/esm/chat.js.map +1 -1
- package/dist/esm/errors.d.ts +571 -0
- package/dist/esm/errors.d.ts.map +1 -0
- package/dist/esm/errors.js +149 -0
- package/dist/esm/errors.js.map +1 -0
- package/dist/esm/index.d.ts +298 -16
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +633 -109
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/media-permissions.d.ts +101 -0
- package/dist/esm/media-permissions.d.ts.map +1 -0
- package/dist/esm/media-permissions.js +160 -0
- package/dist/esm/media-permissions.js.map +1 -0
- package/dist/esm/network.d.ts +80 -0
- package/dist/esm/network.d.ts.map +1 -0
- package/dist/esm/network.js +183 -0
- package/dist/esm/network.js.map +1 -0
- package/dist/esm/token.d.ts +17 -0
- package/dist/esm/token.d.ts.map +1 -0
- package/dist/esm/token.js +31 -0
- package/dist/esm/token.js.map +1 -0
- package/package.json +1 -1
- package/src/chat.ts +7 -14
- package/src/errors.ts +162 -0
- package/src/index.ts +775 -109
- package/src/media-permissions.ts +220 -0
- package/src/network.ts +212 -0
- package/src/token.ts +31 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Network quality, from the media transports' WebRTC statistics: round-trip
|
|
3
|
+
* time, packet loss in the worse direction, and the bandwidth the browser
|
|
4
|
+
* estimates it can use. A level sums them up for a UI, with documented
|
|
5
|
+
* thresholds (docs/SDK-CONTRACT.md, "Network quality").
|
|
6
|
+
*
|
|
7
|
+
* The parsers are pure, like stats.ts: they take the stats objects of a
|
|
8
|
+
* getStats() report and the previous sample's counters.
|
|
9
|
+
*/
|
|
10
|
+
import type { StatsEntry } from './stats.js';
|
|
11
|
+
export type NetworkQualityLevel = 'excellent' | 'good' | 'poor' | 'bad';
|
|
12
|
+
export interface NetworkQuality {
|
|
13
|
+
level: NetworkQualityLevel;
|
|
14
|
+
/** Round trip to the media server, in ms; null before the browser has measured one. */
|
|
15
|
+
rttMs: number | null;
|
|
16
|
+
/** Packets lost over the last interval, in percent, the worse of sending and receiving; null when nothing moved. */
|
|
17
|
+
lossPct: number | null;
|
|
18
|
+
/** The bandwidth the browser estimates it can use, in kbps: upload while publishing, else download when reported. */
|
|
19
|
+
availableKbps: number | null;
|
|
20
|
+
}
|
|
21
|
+
/** How often the transports are sampled while someone listens for `networkQuality`. */
|
|
22
|
+
export declare const NETWORK_QUALITY_INTERVAL_MS = 3000;
|
|
23
|
+
/**
|
|
24
|
+
* The thresholds: the first level whose round trip and loss are both below
|
|
25
|
+
* its limits, else `bad`. An unknown value does not lower the level.
|
|
26
|
+
*/
|
|
27
|
+
export declare const NETWORK_QUALITY_LEVELS: readonly {
|
|
28
|
+
level: Exclude<NetworkQualityLevel, 'bad'>;
|
|
29
|
+
rttMsBelow: number;
|
|
30
|
+
lossPctBelow: number;
|
|
31
|
+
}[];
|
|
32
|
+
export declare function networkLevel(rttMs: number | null, lossPct: number | null): NetworkQualityLevel;
|
|
33
|
+
/** One transport's counters at one moment; the next sample's deltas are taken against it. */
|
|
34
|
+
export interface TransportSample {
|
|
35
|
+
/** The selected candidate pair's current round trip, in ms. */
|
|
36
|
+
rttMs: number | null;
|
|
37
|
+
/** The round trip RTCP reports from the server, in ms (a fallback). */
|
|
38
|
+
remoteRttMs: number | null;
|
|
39
|
+
/** RTP packets sent, and how many of them the server reported lost. */
|
|
40
|
+
packetsSent: number;
|
|
41
|
+
remotePacketsLost: number;
|
|
42
|
+
/** RTP packets received, and how many were lost on the way. */
|
|
43
|
+
packetsReceived: number;
|
|
44
|
+
packetsLost: number;
|
|
45
|
+
/** Bits per second the browser estimates it can send / receive. */
|
|
46
|
+
availableOutgoing: number | null;
|
|
47
|
+
availableIncoming: number | null;
|
|
48
|
+
}
|
|
49
|
+
/** The counters of one transport's report. */
|
|
50
|
+
export declare function readTransportStats(report: Iterable<StatsEntry>): TransportSample;
|
|
51
|
+
/**
|
|
52
|
+
* The quality now, from each transport's sample and the one before it (by
|
|
53
|
+
* key; a transport that is new, or whose counters went backwards, gives no
|
|
54
|
+
* loss for this interval). `publishing` picks the upload estimate.
|
|
55
|
+
*/
|
|
56
|
+
export declare function networkQualityOf(current: ReadonlyMap<string, TransportSample>, previous: ReadonlyMap<string, TransportSample>, publishing: boolean): NetworkQuality;
|
|
57
|
+
/**
|
|
58
|
+
* Samples the transports on a timer while wanted and reports each interval.
|
|
59
|
+
* The first report comes as soon as a round trip is known; loss needs the
|
|
60
|
+
* previous interval's counters, so it is null until the second.
|
|
61
|
+
*/
|
|
62
|
+
export declare class NetworkQualityWatch {
|
|
63
|
+
/** Each live transport's report, by a key that changes when the transport does. */
|
|
64
|
+
private readonly sample;
|
|
65
|
+
private readonly publishing;
|
|
66
|
+
private readonly report;
|
|
67
|
+
private readonly intervalMs;
|
|
68
|
+
private timer;
|
|
69
|
+
private previous;
|
|
70
|
+
private sampling;
|
|
71
|
+
private generation;
|
|
72
|
+
constructor(
|
|
73
|
+
/** Each live transport's report, by a key that changes when the transport does. */
|
|
74
|
+
sample: () => Promise<Map<string, Iterable<StatsEntry>>>, publishing: () => boolean, report: (quality: NetworkQuality) => void, intervalMs?: number);
|
|
75
|
+
get running(): boolean;
|
|
76
|
+
/** Start or stop sampling. Safe to call as often as anything changes. */
|
|
77
|
+
set(wanted: boolean): void;
|
|
78
|
+
private tick;
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=network.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.d.ts","sourceRoot":"","sources":["../../src/network.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,MAAM,MAAM,mBAAmB,GAAG,WAAW,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,CAAC;AAExE,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,mBAAmB,CAAC;IAC3B,uFAAuF;IACvF,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,oHAAoH;IACpH,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,qHAAqH;IACrH,aAAa,EAAE,MAAM,GAAG,IAAI,CAAC;CAC9B;AAED,uFAAuF;AACvF,eAAO,MAAM,2BAA2B,OAAO,CAAC;AAEhD;;;GAGG;AACH,eAAO,MAAM,sBAAsB,EAAE,SAAS;IAAE,KAAK,EAAE,OAAO,CAAC,mBAAmB,EAAE,KAAK,CAAC,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,EAIrI,CAAC;AAEF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AQ,CAAC,UAAU,CAAC,CAAC,CAAC,EACxD,UAAU,EAAE,MAAM,OAAO,EACzB,MAAM,EAAE,CAAC,OAAO,EAAE,cAAc,KAAK,IAAI,EACzC,UAAU,SAA8B;IAG3D,IAAI,OAAO,IAAI,OAAO,CAErB;IAED,yEAAyE;IACzE,GAAG,CAAC,MAAM,EAAE,OAAO,GAAG,IAAI;YAYZ,IAAI;CAsBnB"}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/** How often the transports are sampled while someone listens for `networkQuality`. */
|
|
2
|
+
export const NETWORK_QUALITY_INTERVAL_MS = 3000;
|
|
3
|
+
/**
|
|
4
|
+
* The thresholds: the first level whose round trip and loss are both below
|
|
5
|
+
* its limits, else `bad`. An unknown value does not lower the level.
|
|
6
|
+
*/
|
|
7
|
+
export const NETWORK_QUALITY_LEVELS = [
|
|
8
|
+
{ level: 'excellent', rttMsBelow: 150, lossPctBelow: 1 },
|
|
9
|
+
{ level: 'good', rttMsBelow: 300, lossPctBelow: 3 },
|
|
10
|
+
{ level: 'poor', rttMsBelow: 500, lossPctBelow: 8 },
|
|
11
|
+
];
|
|
12
|
+
export function networkLevel(rttMs, lossPct) {
|
|
13
|
+
for (const l of NETWORK_QUALITY_LEVELS) {
|
|
14
|
+
if ((rttMs === null || rttMs < l.rttMsBelow) && (lossPct === null || lossPct < l.lossPctBelow))
|
|
15
|
+
return l.level;
|
|
16
|
+
}
|
|
17
|
+
return 'bad';
|
|
18
|
+
}
|
|
19
|
+
const num = (value) => (typeof value === 'number' && Number.isFinite(value) ? value : 0);
|
|
20
|
+
const optNum = (value) => (typeof value === 'number' && Number.isFinite(value) ? value : undefined);
|
|
21
|
+
/** The counters of one transport's report. */
|
|
22
|
+
export function readTransportStats(report) {
|
|
23
|
+
const entries = [...report].filter((e) => typeof e === 'object' && e !== null);
|
|
24
|
+
const byId = new Map();
|
|
25
|
+
for (const entry of entries)
|
|
26
|
+
if (typeof entry.id === 'string')
|
|
27
|
+
byId.set(entry.id, entry);
|
|
28
|
+
// The candidate pair in use: the transport's selected one, else a nominated
|
|
29
|
+
// succeeded pair (Firefox reports no transport entry).
|
|
30
|
+
let pair;
|
|
31
|
+
for (const transport of entries.filter((e) => e.type === 'transport')) {
|
|
32
|
+
const selected = typeof transport.selectedCandidatePairId === 'string' ? byId.get(transport.selectedCandidatePairId) : undefined;
|
|
33
|
+
if (selected) {
|
|
34
|
+
pair = selected;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (!pair) {
|
|
39
|
+
const pairs = entries.filter((e) => e.type === 'candidate-pair');
|
|
40
|
+
pair = pairs.find((p) => p.nominated === true && p.state === 'succeeded') ?? pairs.find((p) => p.selected === true);
|
|
41
|
+
}
|
|
42
|
+
const rtt = optNum(pair?.currentRoundTripTime);
|
|
43
|
+
let remoteRtt;
|
|
44
|
+
let packetsSent = 0;
|
|
45
|
+
let remotePacketsLost = 0;
|
|
46
|
+
let packetsReceived = 0;
|
|
47
|
+
let packetsLost = 0;
|
|
48
|
+
for (const e of entries) {
|
|
49
|
+
if (e.type === 'outbound-rtp')
|
|
50
|
+
packetsSent += num(e.packetsSent);
|
|
51
|
+
else if (e.type === 'remote-inbound-rtp') {
|
|
52
|
+
remotePacketsLost += Math.max(0, num(e.packetsLost));
|
|
53
|
+
const r = optNum(e.roundTripTime);
|
|
54
|
+
if (r !== undefined)
|
|
55
|
+
remoteRtt = Math.max(remoteRtt ?? 0, r);
|
|
56
|
+
}
|
|
57
|
+
else if (e.type === 'inbound-rtp') {
|
|
58
|
+
packetsReceived += num(e.packetsReceived);
|
|
59
|
+
packetsLost += Math.max(0, num(e.packetsLost));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
const out = optNum(pair?.availableOutgoingBitrate);
|
|
63
|
+
const inc = optNum(pair?.availableIncomingBitrate);
|
|
64
|
+
return {
|
|
65
|
+
rttMs: rtt === undefined ? null : Math.round(rtt * 1000),
|
|
66
|
+
remoteRttMs: remoteRtt === undefined ? null : Math.round(remoteRtt * 1000),
|
|
67
|
+
packetsSent,
|
|
68
|
+
remotePacketsLost,
|
|
69
|
+
packetsReceived,
|
|
70
|
+
packetsLost,
|
|
71
|
+
availableOutgoing: out ?? null,
|
|
72
|
+
availableIncoming: inc ?? null,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const pct = (lost, total) => (total > 0 ? Math.round((Math.min(lost, total) / total) * 1000) / 10 : null);
|
|
76
|
+
/**
|
|
77
|
+
* The quality now, from each transport's sample and the one before it (by
|
|
78
|
+
* key; a transport that is new, or whose counters went backwards, gives no
|
|
79
|
+
* loss for this interval). `publishing` picks the upload estimate.
|
|
80
|
+
*/
|
|
81
|
+
export function networkQualityOf(current, previous, publishing) {
|
|
82
|
+
let rttMs = null;
|
|
83
|
+
let lossPct = null;
|
|
84
|
+
let upload = null;
|
|
85
|
+
let download = null;
|
|
86
|
+
for (const [key, now] of current) {
|
|
87
|
+
const rtt = now.rttMs ?? now.remoteRttMs;
|
|
88
|
+
if (rtt !== null)
|
|
89
|
+
rttMs = Math.max(rttMs ?? 0, rtt);
|
|
90
|
+
if (now.availableOutgoing !== null)
|
|
91
|
+
upload = Math.max(upload ?? 0, now.availableOutgoing);
|
|
92
|
+
if (now.availableIncoming !== null)
|
|
93
|
+
download = Math.max(download ?? 0, now.availableIncoming);
|
|
94
|
+
const before = previous.get(key);
|
|
95
|
+
if (!before)
|
|
96
|
+
continue;
|
|
97
|
+
const sent = now.packetsSent - before.packetsSent;
|
|
98
|
+
const remoteLost = now.remotePacketsLost - before.remotePacketsLost;
|
|
99
|
+
const received = now.packetsReceived - before.packetsReceived;
|
|
100
|
+
const lost = now.packetsLost - before.packetsLost;
|
|
101
|
+
// Counters only go forward on one transport; backwards is a new one.
|
|
102
|
+
const up = sent >= 0 && remoteLost >= 0 ? pct(remoteLost, sent) : null;
|
|
103
|
+
const down = received >= 0 && lost >= 0 ? pct(lost, received + lost) : null;
|
|
104
|
+
for (const value of [up, down])
|
|
105
|
+
if (value !== null)
|
|
106
|
+
lossPct = Math.max(lossPct ?? 0, value);
|
|
107
|
+
}
|
|
108
|
+
const bandwidth = publishing ? upload ?? download : download ?? upload;
|
|
109
|
+
return {
|
|
110
|
+
level: networkLevel(rttMs, lossPct),
|
|
111
|
+
rttMs,
|
|
112
|
+
lossPct,
|
|
113
|
+
availableKbps: bandwidth === null ? null : Math.round(bandwidth / 1000),
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Samples the transports on a timer while wanted and reports each interval.
|
|
118
|
+
* The first report comes as soon as a round trip is known; loss needs the
|
|
119
|
+
* previous interval's counters, so it is null until the second.
|
|
120
|
+
*/
|
|
121
|
+
export class NetworkQualityWatch {
|
|
122
|
+
sample;
|
|
123
|
+
publishing;
|
|
124
|
+
report;
|
|
125
|
+
intervalMs;
|
|
126
|
+
timer = null;
|
|
127
|
+
previous = new Map();
|
|
128
|
+
sampling = false;
|
|
129
|
+
generation = 0;
|
|
130
|
+
constructor(
|
|
131
|
+
/** Each live transport's report, by a key that changes when the transport does. */
|
|
132
|
+
sample, publishing, report, intervalMs = NETWORK_QUALITY_INTERVAL_MS) {
|
|
133
|
+
this.sample = sample;
|
|
134
|
+
this.publishing = publishing;
|
|
135
|
+
this.report = report;
|
|
136
|
+
this.intervalMs = intervalMs;
|
|
137
|
+
}
|
|
138
|
+
get running() {
|
|
139
|
+
return this.timer !== null;
|
|
140
|
+
}
|
|
141
|
+
/** Start or stop sampling. Safe to call as often as anything changes. */
|
|
142
|
+
set(wanted) {
|
|
143
|
+
if (wanted && !this.timer) {
|
|
144
|
+
this.timer = setInterval(() => void this.tick(), this.intervalMs);
|
|
145
|
+
void this.tick();
|
|
146
|
+
}
|
|
147
|
+
else if (!wanted && this.timer) {
|
|
148
|
+
clearInterval(this.timer);
|
|
149
|
+
this.timer = null;
|
|
150
|
+
this.generation += 1;
|
|
151
|
+
this.previous = new Map();
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
async tick() {
|
|
155
|
+
if (this.sampling)
|
|
156
|
+
return;
|
|
157
|
+
const generation = this.generation;
|
|
158
|
+
this.sampling = true;
|
|
159
|
+
try {
|
|
160
|
+
const reports = await this.sample();
|
|
161
|
+
if (generation !== this.generation || !this.timer)
|
|
162
|
+
return;
|
|
163
|
+
const current = new Map();
|
|
164
|
+
for (const [key, report] of reports)
|
|
165
|
+
current.set(key, readTransportStats(report));
|
|
166
|
+
const first = this.previous.size === 0;
|
|
167
|
+
const quality = networkQualityOf(current, this.previous, this.publishing());
|
|
168
|
+
this.previous = current;
|
|
169
|
+
// Nothing to report without a transport, or before a second sample
|
|
170
|
+
// when not even the round trip is known yet.
|
|
171
|
+
if (current.size === 0 || (first && quality.rttMs === null))
|
|
172
|
+
return;
|
|
173
|
+
this.report(quality);
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
// Statistics are best effort; the next interval tries again.
|
|
177
|
+
}
|
|
178
|
+
finally {
|
|
179
|
+
this.sampling = false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=network.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"network.js","sourceRoot":"","sources":["../../src/network.ts"],"names":[],"mappings":"AAuBA,uFAAuF;AACvF,MAAM,CAAC,MAAM,2BAA2B,GAAG,IAAI,CAAC;AAEhD;;;GAGG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAwG;IACzI,EAAE,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC,EAAE;IACxD,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC,EAAE;IACnD,EAAE,KAAK,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC,EAAE;CACpD,CAAC;AAEF,MAAM,UAAU,YAAY,CAAC,KAAoB,EAAE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;YAClF,MAAM,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,KAAK,CAAC,CAAC;YACvC,MAAM,OAAO,GAAG,gBAAgB,CAAC,OAAO,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAC;YAC5E,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;YACxB,mEAAmE;YACnE,6CAA6C;YAC7C,IAAI,OAAO,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,IAAI,OAAO,CAAC,KAAK,KAAK,IAAI,CAAC;gBAAE,OAAO;YACpE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;QACvB,CAAC;QAAC,MAAM,CAAC;YACP,6DAA6D;QAC/D,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QACxB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When a room token expires, read from the token itself. Not verified -- the
|
|
3
|
+
* server does that -- and only used to warn before the token the SDK would
|
|
4
|
+
* reconnect with lapses (`tokenExpiring`).
|
|
5
|
+
*/
|
|
6
|
+
/** How long before the token's expiry `tokenExpiring` fires. */
|
|
7
|
+
export declare const TOKEN_EXPIRING_LEAD_MS = 60000;
|
|
8
|
+
/**
|
|
9
|
+
* The socket URL with the token as its `token` query parameter. Joined by
|
|
10
|
+
* hand rather than with `URL.searchParams`: React Native's `URL` is a partial
|
|
11
|
+
* polyfill (older releases had no `searchParams.set`), and a WebSocket URL has
|
|
12
|
+
* no fragment, so the string form is the same as the URL object's.
|
|
13
|
+
*/
|
|
14
|
+
export declare function socketUrl(wsUrl: string, token: string): string;
|
|
15
|
+
/** The `exp` claim of a JWT, in unix milliseconds; null for a token that is not a readable JWT. */
|
|
16
|
+
export declare function tokenExpiresAt(token: string): number | null;
|
|
17
|
+
//# sourceMappingURL=token.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../../src/token.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,gEAAgE;AAChE,eAAO,MAAM,sBAAsB,QAAS,CAAC;AAE7C;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAE9D;AAED,mGAAmG;AACnG,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAU3D"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When a room token expires, read from the token itself. Not verified -- the
|
|
3
|
+
* server does that -- and only used to warn before the token the SDK would
|
|
4
|
+
* reconnect with lapses (`tokenExpiring`).
|
|
5
|
+
*/
|
|
6
|
+
/** How long before the token's expiry `tokenExpiring` fires. */
|
|
7
|
+
export const TOKEN_EXPIRING_LEAD_MS = 60_000;
|
|
8
|
+
/**
|
|
9
|
+
* The socket URL with the token as its `token` query parameter. Joined by
|
|
10
|
+
* hand rather than with `URL.searchParams`: React Native's `URL` is a partial
|
|
11
|
+
* polyfill (older releases had no `searchParams.set`), and a WebSocket URL has
|
|
12
|
+
* no fragment, so the string form is the same as the URL object's.
|
|
13
|
+
*/
|
|
14
|
+
export function socketUrl(wsUrl, token) {
|
|
15
|
+
return `${wsUrl}${wsUrl.includes('?') ? '&' : '?'}token=${encodeURIComponent(token)}`;
|
|
16
|
+
}
|
|
17
|
+
/** The `exp` claim of a JWT, in unix milliseconds; null for a token that is not a readable JWT. */
|
|
18
|
+
export function tokenExpiresAt(token) {
|
|
19
|
+
const payload = typeof token === 'string' ? token.split('.')[1] : undefined;
|
|
20
|
+
if (!payload || typeof globalThis.atob !== 'function')
|
|
21
|
+
return null;
|
|
22
|
+
try {
|
|
23
|
+
const base64 = payload.replace(/-/g, '+').replace(/_/g, '/');
|
|
24
|
+
const json = JSON.parse(globalThis.atob(base64 + '='.repeat((4 - (base64.length % 4)) % 4)));
|
|
25
|
+
return typeof json.exp === 'number' && Number.isFinite(json.exp) && json.exp > 0 ? json.exp * 1000 : null;
|
|
26
|
+
}
|
|
27
|
+
catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.js","sourceRoot":"","sources":["../../src/token.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,gEAAgE;AAChE,MAAM,CAAC,MAAM,sBAAsB,GAAG,MAAM,CAAC;AAE7C;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAAC,KAAa,EAAE,KAAa;IACpD,OAAO,GAAG,KAAK,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,SAAS,kBAAkB,CAAC,KAAK,CAAC,EAAE,CAAC;AACxF,CAAC;AAED,mGAAmG;AACnG,MAAM,UAAU,cAAc,CAAC,KAAa;IAC1C,MAAM,OAAO,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC5E,IAAI,CAAC,OAAO,IAAI,OAAO,UAAU,CAAC,IAAI,KAAK,UAAU;QAAE,OAAO,IAAI,CAAC;IACnE,IAAI,CAAC;QACH,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;QAC7D,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAsB,CAAC;QAClH,OAAO,OAAO,IAAI,CAAC,GAAG,KAAK,QAAQ,IAAI,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC;IAC5G,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/package.json
CHANGED
package/src/chat.ts
CHANGED
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
* nothing else -- for an audience that is watching a broadcast over a CDN and
|
|
8
8
|
* has no reason to be on an SFU.
|
|
9
9
|
*/
|
|
10
|
+
import { isTerminalError } from './errors.js';
|
|
10
11
|
import type { Role } from './index.js';
|
|
12
|
+
import { socketUrl } from './token.js';
|
|
11
13
|
|
|
12
14
|
export interface ChatMessage {
|
|
13
15
|
/** Monotonic within a channel, so the oldest id held is a paging cursor. */
|
|
@@ -95,16 +97,6 @@ interface Pending {
|
|
|
95
97
|
|
|
96
98
|
const RPC_TIMEOUT_MS = 15_000;
|
|
97
99
|
const MAX_RECONNECT_ATTEMPTS = 8;
|
|
98
|
-
/** A token that is wrong will be wrong next time too; retrying only adds noise. */
|
|
99
|
-
const TERMINAL_JOIN_ERRORS = new Set([
|
|
100
|
-
'bad-token',
|
|
101
|
-
'no-token',
|
|
102
|
-
// The account cannot use the service until it is topped up or restored.
|
|
103
|
-
'quota-exhausted',
|
|
104
|
-
'daily-limit-reached',
|
|
105
|
-
'monthly-limit-reached',
|
|
106
|
-
'tenant-suspended',
|
|
107
|
-
]);
|
|
108
100
|
|
|
109
101
|
const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
|
|
110
102
|
|
|
@@ -179,9 +171,7 @@ export class ChatClient {
|
|
|
179
171
|
private openSocket(): Promise<void> {
|
|
180
172
|
this.detachSocket();
|
|
181
173
|
return new Promise((resolve, reject) => {
|
|
182
|
-
const
|
|
183
|
-
url.searchParams.set('token', this.token);
|
|
184
|
-
const ws = new WebSocket(url.toString());
|
|
174
|
+
const ws = new WebSocket(socketUrl(this.wsUrl, this.token));
|
|
185
175
|
this.ws = ws;
|
|
186
176
|
|
|
187
177
|
const onError = () => reject(new Error('websocket failed to open'));
|
|
@@ -246,7 +236,10 @@ export class ChatClient {
|
|
|
246
236
|
return;
|
|
247
237
|
} catch (err) {
|
|
248
238
|
this.emit('error', err as Error);
|
|
249
|
-
|
|
239
|
+
// A token that is wrong will be wrong next time too, and an account
|
|
240
|
+
// that ran out stays out until topped up: the registry's terminal
|
|
241
|
+
// codes, shared with StreamClient. Retrying only adds noise.
|
|
242
|
+
if (err instanceof ChatRpcError && isTerminalError(err.code)) {
|
|
250
243
|
this.closedByUs = true;
|
|
251
244
|
this.emit('closed', 'gave-up');
|
|
252
245
|
return;
|
package/src/errors.ts
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One error registry for every VoltKit SDK (docs/sdk-contract.json): the codes
|
|
3
|
+
* the controller and the media nodes answer with, passed through unchanged, and
|
|
4
|
+
* the ones the SDKs raise themselves. Switch on `error.code`, never on the
|
|
5
|
+
* message.
|
|
6
|
+
*
|
|
7
|
+
* Kept as a literal here, since the package cannot read the repository's docs
|
|
8
|
+
* at runtime, and checked against the contract by
|
|
9
|
+
* scripts/test/sdk-contract-browser.sh and test/contract.test.mjs. One code per
|
|
10
|
+
* line, so that check can read it.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** Who raises a code: the server (controller or media node) or the SDK itself. */
|
|
14
|
+
export type ErrorOrigin = 'server' | 'sdk';
|
|
15
|
+
|
|
16
|
+
export interface ErrorInfo {
|
|
17
|
+
origin: ErrorOrigin;
|
|
18
|
+
/** A join (or reconnect) refused with it must not be retried with the same token. */
|
|
19
|
+
terminal: boolean;
|
|
20
|
+
/** Trying the same call again later can succeed. */
|
|
21
|
+
retryable: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const ERROR_CODES = {
|
|
25
|
+
'bad-json': { origin: 'server', terminal: false, retryable: false },
|
|
26
|
+
'bad-params': { origin: 'server', terminal: false, retryable: false },
|
|
27
|
+
'unknown-method': { origin: 'server', terminal: false, retryable: false },
|
|
28
|
+
'too-many-requests': { origin: 'server', terminal: false, retryable: true },
|
|
29
|
+
'not-joined': { origin: 'server', terminal: false, retryable: false },
|
|
30
|
+
'already-joined': { origin: 'server', terminal: false, retryable: false },
|
|
31
|
+
'internal': { origin: 'server', terminal: false, retryable: true },
|
|
32
|
+
'node-error': { origin: 'server', terminal: false, retryable: true },
|
|
33
|
+
'no-token': { origin: 'server', terminal: true, retryable: false },
|
|
34
|
+
'bad-token': { origin: 'server', terminal: true, retryable: false },
|
|
35
|
+
'wrong-tenant': { origin: 'server', terminal: true, retryable: false },
|
|
36
|
+
'token-mismatch': { origin: 'server', terminal: false, retryable: false },
|
|
37
|
+
'no-room': { origin: 'server', terminal: true, retryable: false },
|
|
38
|
+
'room-ended': { origin: 'server', terminal: true, retryable: false },
|
|
39
|
+
'kicked': { origin: 'server', terminal: true, retryable: false },
|
|
40
|
+
'no-node': { origin: 'server', terminal: false, retryable: true },
|
|
41
|
+
'no-capacity': { origin: 'server', terminal: false, retryable: true },
|
|
42
|
+
'stage-full': { origin: 'server', terminal: false, retryable: true },
|
|
43
|
+
'participant-quota': { origin: 'server', terminal: false, retryable: true },
|
|
44
|
+
'viewer-quota': { origin: 'server', terminal: false, retryable: true },
|
|
45
|
+
'quota-exhausted': { origin: 'server', terminal: true, retryable: false },
|
|
46
|
+
'daily-limit-reached': { origin: 'server', terminal: true, retryable: false },
|
|
47
|
+
'monthly-limit-reached': { origin: 'server', terminal: true, retryable: false },
|
|
48
|
+
'tenant-suspended': { origin: 'server', terminal: true, retryable: false },
|
|
49
|
+
'billing-unavailable': { origin: 'server', terminal: true, retryable: false },
|
|
50
|
+
'forbidden': { origin: 'server', terminal: true, retryable: false },
|
|
51
|
+
'not-in-room': { origin: 'server', terminal: false, retryable: false },
|
|
52
|
+
'no-participant': { origin: 'server', terminal: false, retryable: false },
|
|
53
|
+
'bad-transport': { origin: 'server', terminal: false, retryable: false },
|
|
54
|
+
'no-transport': { origin: 'server', terminal: false, retryable: false },
|
|
55
|
+
'no-producer': { origin: 'server', terminal: false, retryable: false },
|
|
56
|
+
'no-consumer': { origin: 'server', terminal: false, retryable: false },
|
|
57
|
+
'cannot-consume': { origin: 'server', terminal: false, retryable: false },
|
|
58
|
+
'consumer-cap': { origin: 'server', terminal: false, retryable: true },
|
|
59
|
+
'producer-cap': { origin: 'server', terminal: false, retryable: true },
|
|
60
|
+
'room-cap': { origin: 'server', terminal: false, retryable: true },
|
|
61
|
+
'draining': { origin: 'server', terminal: false, retryable: true },
|
|
62
|
+
'bad-request': { origin: 'server', terminal: false, retryable: false },
|
|
63
|
+
'source-already-published': { origin: 'server', terminal: false, retryable: false },
|
|
64
|
+
'force-muted': { origin: 'server', terminal: false, retryable: false },
|
|
65
|
+
'media-disabled': { origin: 'server', terminal: false, retryable: false },
|
|
66
|
+
'frame-rate-not-allowed': { origin: 'server', terminal: false, retryable: false },
|
|
67
|
+
'video-quality-not-allowed': { origin: 'server', terminal: false, retryable: false },
|
|
68
|
+
'audio-quality-not-allowed': { origin: 'server', terminal: false, retryable: false },
|
|
69
|
+
'video-not-allowed': { origin: 'server', terminal: false, retryable: false },
|
|
70
|
+
'audio-not-allowed': { origin: 'server', terminal: false, retryable: false },
|
|
71
|
+
'video-subscription-limit': { origin: 'server', terminal: false, retryable: false },
|
|
72
|
+
'slot-taken': { origin: 'server', terminal: false, retryable: false },
|
|
73
|
+
'slot-blocked': { origin: 'server', terminal: false, retryable: false },
|
|
74
|
+
'slot-occupied': { origin: 'server', terminal: false, retryable: false },
|
|
75
|
+
'chat-muted': { origin: 'server', terminal: false, retryable: false },
|
|
76
|
+
'rate-limited': { origin: 'server', terminal: false, retryable: true },
|
|
77
|
+
'no-message': { origin: 'server', terminal: false, retryable: false },
|
|
78
|
+
'chat-unavailable': { origin: 'server', terminal: false, retryable: false },
|
|
79
|
+
'closed': { origin: 'server', terminal: false, retryable: true },
|
|
80
|
+
'unauthorized': { origin: 'server', terminal: false, retryable: false },
|
|
81
|
+
'bad-signature': { origin: 'server', terminal: false, retryable: false },
|
|
82
|
+
'operator-only': { origin: 'server', terminal: false, retryable: false },
|
|
83
|
+
'bad-role': { origin: 'server', terminal: false, retryable: false },
|
|
84
|
+
'room-exists': { origin: 'server', terminal: false, retryable: false },
|
|
85
|
+
'room-quota': { origin: 'server', terminal: false, retryable: true },
|
|
86
|
+
'no-nodes': { origin: 'server', terminal: false, retryable: true },
|
|
87
|
+
'capacity-exhausted': { origin: 'server', terminal: false, retryable: true },
|
|
88
|
+
'unavailable': { origin: 'server', terminal: false, retryable: true },
|
|
89
|
+
'not-found': { origin: 'server', terminal: false, retryable: false },
|
|
90
|
+
'broadcast-disabled': { origin: 'server', terminal: false, retryable: false },
|
|
91
|
+
'chat-disabled': { origin: 'server', terminal: false, retryable: false },
|
|
92
|
+
'no-stream': { origin: 'server', terminal: false, retryable: false },
|
|
93
|
+
'stream-exists': { origin: 'server', terminal: false, retryable: false },
|
|
94
|
+
'stream-quota': { origin: 'server', terminal: false, retryable: true },
|
|
95
|
+
'network': { origin: 'sdk', terminal: false, retryable: true },
|
|
96
|
+
'rates-unconfigured': { origin: 'sdk', terminal: false, retryable: false },
|
|
97
|
+
'redirected': { origin: 'sdk', terminal: false, retryable: false },
|
|
98
|
+
'not-connected': { origin: 'sdk', terminal: false, retryable: true },
|
|
99
|
+
'timeout': { origin: 'sdk', terminal: false, retryable: true },
|
|
100
|
+
'connect-failed': { origin: 'sdk', terminal: false, retryable: true },
|
|
101
|
+
'replaced': { origin: 'sdk', terminal: true, retryable: false },
|
|
102
|
+
'reconnect-gave-up': { origin: 'sdk', terminal: true, retryable: false },
|
|
103
|
+
'devices-gone': { origin: 'sdk', terminal: false, retryable: false },
|
|
104
|
+
'unpublished': { origin: 'sdk', terminal: false, retryable: false },
|
|
105
|
+
'frame-rate-fallback': { origin: 'sdk', terminal: false, retryable: false },
|
|
106
|
+
'codec-fallback': { origin: 'sdk', terminal: false, retryable: false },
|
|
107
|
+
'quality-unavailable': { origin: 'sdk', terminal: false, retryable: false },
|
|
108
|
+
'screen-share-active': { origin: 'sdk', terminal: false, retryable: false },
|
|
109
|
+
'screen-share-unsupported': { origin: 'sdk', terminal: false, retryable: false },
|
|
110
|
+
'screen-share-no-video': { origin: 'sdk', terminal: false, retryable: false },
|
|
111
|
+
'disconnected': { origin: 'sdk', terminal: false, retryable: true },
|
|
112
|
+
'produce-failed': { origin: 'sdk', terminal: false, retryable: true },
|
|
113
|
+
'consume-failed': { origin: 'sdk', terminal: false, retryable: true },
|
|
114
|
+
'media-unavailable': { origin: 'sdk', terminal: false, retryable: false },
|
|
115
|
+
'music-unavailable': { origin: 'sdk', terminal: false, retryable: false },
|
|
116
|
+
'no-camera': { origin: 'sdk', terminal: false, retryable: false },
|
|
117
|
+
'camera-failed': { origin: 'sdk', terminal: false, retryable: true },
|
|
118
|
+
'unknown-producer': { origin: 'sdk', terminal: false, retryable: false },
|
|
119
|
+
'screen-share-denied': { origin: 'sdk', terminal: false, retryable: true },
|
|
120
|
+
'screen-share-service-required': { origin: 'sdk', terminal: false, retryable: false },
|
|
121
|
+
'screen-share-source-required': { origin: 'sdk', terminal: false, retryable: false },
|
|
122
|
+
'screen-share-setup-required': { origin: 'sdk', terminal: false, retryable: false },
|
|
123
|
+
'screen-share-start-timeout': { origin: 'sdk', terminal: false, retryable: true },
|
|
124
|
+
'screen-share-ended': { origin: 'sdk', terminal: false, retryable: true },
|
|
125
|
+
'screen-share-failed': { origin: 'sdk', terminal: false, retryable: true },
|
|
126
|
+
'screen-share-size-unavailable': { origin: 'sdk', terminal: false, retryable: true },
|
|
127
|
+
'screen-audio-unavailable': { origin: 'sdk', terminal: false, retryable: false },
|
|
128
|
+
'screen-stop-failed': { origin: 'sdk', terminal: false, retryable: false },
|
|
129
|
+
'degradation-preference-failed': { origin: 'sdk', terminal: false, retryable: false },
|
|
130
|
+
} as const satisfies Record<string, ErrorInfo>;
|
|
131
|
+
|
|
132
|
+
/** Every code in the registry. A newer server may still send one this SDK predates. */
|
|
133
|
+
export type ErrorCode = keyof typeof ERROR_CODES;
|
|
134
|
+
|
|
135
|
+
/** The registry entry for a code, or undefined for one this SDK does not know. */
|
|
136
|
+
export function errorInfo(code: string): ErrorInfo | undefined {
|
|
137
|
+
return Object.prototype.hasOwnProperty.call(ERROR_CODES, code) ? ERROR_CODES[code as ErrorCode] : undefined;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Whether reconnecting cannot fix a join refused with this code: a room that
|
|
142
|
+
* has ended is not coming back, and a token that is wrong will be wrong next
|
|
143
|
+
* time too. Unknown codes are not terminal, so a newer server's code never
|
|
144
|
+
* stops a reconnect that could succeed.
|
|
145
|
+
*/
|
|
146
|
+
export function isTerminalError(code: string): boolean {
|
|
147
|
+
return errorInfo(code)?.terminal === true;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* An error with a machine-readable code: returned by the server (its code
|
|
152
|
+
* passed through unchanged, known to this SDK or not) or raised by the SDK
|
|
153
|
+
* itself with a registry code. `data` is what the server attached so the app
|
|
154
|
+
* can act on a refusal rather than merely report it: `{ slots }` for
|
|
155
|
+
* `slot-occupied`, for instance.
|
|
156
|
+
*/
|
|
157
|
+
export class RpcError extends Error {
|
|
158
|
+
constructor(readonly code: ErrorCode | (string & {}), message: string, readonly data?: unknown) {
|
|
159
|
+
super(`${code}: ${message}`);
|
|
160
|
+
this.name = 'RpcError';
|
|
161
|
+
}
|
|
162
|
+
}
|