livekit-client 2.20.1 → 2.21.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/dist/livekit-client.e2ee.worker.js +1 -1
- package/dist/livekit-client.e2ee.worker.js.map +1 -1
- package/dist/livekit-client.e2ee.worker.mjs +37 -7
- package/dist/livekit-client.e2ee.worker.mjs.map +1 -1
- package/dist/livekit-client.esm.mjs +2279 -651
- package/dist/livekit-client.esm.mjs.map +1 -1
- package/dist/livekit-client.fm.worker.js +1 -1
- package/dist/livekit-client.fm.worker.js.map +1 -1
- package/dist/livekit-client.fm.worker.mjs +37 -7
- package/dist/livekit-client.fm.worker.mjs.map +1 -1
- package/dist/livekit-client.umd.js +1 -1
- package/dist/livekit-client.umd.js.map +1 -1
- package/dist/src/api/SignalClient.d.ts.map +1 -1
- package/dist/src/index.d.ts +1 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/logger.d.ts +3 -1
- package/dist/src/logger.d.ts.map +1 -1
- package/dist/src/room/PCTransport.d.ts +2 -0
- package/dist/src/room/PCTransport.d.ts.map +1 -1
- package/dist/src/room/PCTransportManager.d.ts +1 -0
- package/dist/src/room/PCTransportManager.d.ts.map +1 -1
- package/dist/src/room/RTCEngine.d.ts +31 -26
- package/dist/src/room/RTCEngine.d.ts.map +1 -1
- package/dist/src/room/RegionUrlProvider.d.ts.map +1 -1
- package/dist/src/room/Room.d.ts +4 -0
- package/dist/src/room/Room.d.ts.map +1 -1
- package/dist/src/room/data-channel/DataChannelManager.d.ts +62 -0
- package/dist/src/room/data-channel/DataChannelManager.d.ts.map +1 -0
- package/dist/src/room/data-channel/FlowControlledDataChannel.d.ts +95 -0
- package/dist/src/room/data-channel/FlowControlledDataChannel.d.ts.map +1 -0
- package/dist/src/room/data-channel/LossyDataChannel.d.ts +40 -0
- package/dist/src/room/data-channel/LossyDataChannel.d.ts.map +1 -0
- package/dist/src/room/data-channel/ReliableDataChannel.d.ts +56 -0
- package/dist/src/room/data-channel/ReliableDataChannel.d.ts.map +1 -0
- package/dist/src/room/data-channel/types.d.ts +12 -0
- package/dist/src/room/data-channel/types.d.ts.map +1 -0
- package/dist/src/room/data-stream/compression.d.ts +49 -0
- package/dist/src/room/data-stream/compression.d.ts.map +1 -0
- package/dist/src/room/data-stream/constants.d.ts +18 -0
- package/dist/src/room/data-stream/constants.d.ts.map +1 -0
- package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts +4 -0
- package/dist/src/room/data-stream/incoming/IncomingDataStreamManager.d.ts.map +1 -1
- package/dist/src/room/data-stream/incoming/StreamReader.d.ts.map +1 -1
- package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +49 -2
- package/dist/src/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts.map +1 -1
- package/dist/src/room/data-stream/outgoing/header-utils.d.ts +18 -0
- package/dist/src/room/data-stream/outgoing/header-utils.d.ts.map +1 -0
- package/dist/src/room/data-track/incoming/IncomingDataTrackManager.d.ts +7 -0
- package/dist/src/room/data-track/incoming/IncomingDataTrackManager.d.ts.map +1 -1
- package/dist/src/room/data-track/outgoing/OutgoingDataTrackManager.d.ts.map +1 -1
- package/dist/src/room/errors.d.ts +3 -1
- package/dist/src/room/errors.d.ts.map +1 -1
- package/dist/src/room/participant/LocalParticipant.d.ts +9 -1
- package/dist/src/room/participant/LocalParticipant.d.ts.map +1 -1
- package/dist/src/room/participant/Participant.d.ts.map +1 -1
- package/dist/src/room/participant/RemoteParticipant.d.ts +11 -2
- package/dist/src/room/participant/RemoteParticipant.d.ts.map +1 -1
- package/dist/src/room/participant/publishUtils.d.ts +7 -0
- package/dist/src/room/participant/publishUtils.d.ts.map +1 -1
- package/dist/src/room/rpc/client/RpcClientManager.d.ts.map +1 -1
- package/dist/src/room/rpc/server/RpcServerManager.d.ts.map +1 -1
- package/dist/src/room/track/LocalAudioTrack.d.ts +1 -0
- package/dist/src/room/track/LocalAudioTrack.d.ts.map +1 -1
- package/dist/src/room/track/Track.d.ts.map +1 -1
- package/dist/src/room/track/options.d.ts +11 -1
- package/dist/src/room/track/options.d.ts.map +1 -1
- package/dist/src/room/types.d.ts +17 -0
- package/dist/src/room/types.d.ts.map +1 -1
- package/dist/src/room/utils.d.ts +13 -0
- package/dist/src/room/utils.d.ts.map +1 -1
- package/dist/src/test/signalToken.d.ts +26 -0
- package/dist/src/test/signalToken.d.ts.map +1 -0
- package/dist/src/utils/dataPacketBuffer.d.ts +18 -0
- package/dist/src/utils/dataPacketBuffer.d.ts.map +1 -1
- package/dist/src/version.d.ts +5 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/ts4.2/index.d.ts +1 -1
- package/dist/ts4.2/logger.d.ts +3 -1
- package/dist/ts4.2/room/PCTransport.d.ts +2 -0
- package/dist/ts4.2/room/PCTransportManager.d.ts +1 -0
- package/dist/ts4.2/room/RTCEngine.d.ts +31 -26
- package/dist/ts4.2/room/Room.d.ts +4 -0
- package/dist/ts4.2/room/data-channel/DataChannelManager.d.ts +62 -0
- package/dist/ts4.2/room/data-channel/FlowControlledDataChannel.d.ts +95 -0
- package/dist/ts4.2/room/data-channel/LossyDataChannel.d.ts +41 -0
- package/dist/ts4.2/room/data-channel/ReliableDataChannel.d.ts +57 -0
- package/dist/ts4.2/room/data-channel/types.d.ts +12 -0
- package/dist/ts4.2/room/data-stream/compression.d.ts +49 -0
- package/dist/ts4.2/room/data-stream/constants.d.ts +18 -0
- package/dist/ts4.2/room/data-stream/incoming/IncomingDataStreamManager.d.ts +4 -0
- package/dist/ts4.2/room/data-stream/outgoing/OutgoingDataStreamManager.d.ts +49 -2
- package/dist/ts4.2/room/data-stream/outgoing/header-utils.d.ts +18 -0
- package/dist/ts4.2/room/data-track/incoming/IncomingDataTrackManager.d.ts +7 -0
- package/dist/ts4.2/room/errors.d.ts +3 -1
- package/dist/ts4.2/room/participant/LocalParticipant.d.ts +9 -1
- package/dist/ts4.2/room/participant/RemoteParticipant.d.ts +11 -1
- package/dist/ts4.2/room/participant/publishUtils.d.ts +7 -0
- package/dist/ts4.2/room/track/LocalAudioTrack.d.ts +1 -0
- package/dist/ts4.2/room/track/options.d.ts +11 -1
- package/dist/ts4.2/room/types.d.ts +17 -0
- package/dist/ts4.2/room/utils.d.ts +13 -0
- package/dist/ts4.2/test/signalToken.d.ts +26 -0
- package/dist/ts4.2/utils/dataPacketBuffer.d.ts +18 -0
- package/dist/ts4.2/version.d.ts +5 -1
- package/package.json +8 -4
- package/src/api/SignalClient.e2e.test.ts +264 -0
- package/src/api/SignalClient.ts +34 -2
- package/src/index.ts +2 -0
- package/src/logger.ts +2 -0
- package/src/room/PCTransport.ts +66 -31
- package/src/room/PCTransportManager.ts +10 -6
- package/src/room/RTCEngine.test.ts +238 -6
- package/src/room/RTCEngine.ts +101 -338
- package/src/room/RegionUrlProvider.ts +4 -2
- package/src/room/Room.test.ts +4 -1
- package/src/room/Room.ts +47 -9
- package/src/room/data-channel/DataChannelManager.test.ts +152 -0
- package/src/room/data-channel/DataChannelManager.ts +237 -0
- package/src/room/data-channel/FlowControlledDataChannel.test.ts +154 -0
- package/src/room/data-channel/FlowControlledDataChannel.ts +216 -0
- package/src/room/data-channel/LossyDataChannel.test.ts +118 -0
- package/src/room/data-channel/LossyDataChannel.ts +125 -0
- package/src/room/data-channel/ReliableDataChannel.test.ts +158 -0
- package/src/room/data-channel/ReliableDataChannel.ts +153 -0
- package/src/room/data-channel/types.ts +30 -0
- package/src/room/data-stream/compression.ts +117 -0
- package/src/room/data-stream/constants.ts +18 -0
- package/src/room/data-stream/incoming/IncomingDataStreamManager.test.ts +1601 -0
- package/src/room/data-stream/incoming/IncomingDataStreamManager.ts +438 -86
- package/src/room/data-stream/incoming/StreamReader.ts +8 -2
- package/src/room/data-stream/outgoing/OutgoingDataStreamManager.test.ts +1292 -0
- package/src/room/data-stream/outgoing/OutgoingDataStreamManager.ts +489 -116
- package/src/room/data-stream/outgoing/header-utils.ts +87 -0
- package/src/room/data-track/incoming/IncomingDataTrackManager.test.ts +303 -0
- package/src/room/data-track/incoming/IncomingDataTrackManager.ts +70 -8
- package/src/room/data-track/outgoing/OutgoingDataTrackManager.ts +2 -0
- package/src/room/errors.ts +10 -4
- package/src/room/participant/LocalParticipant.ts +29 -6
- package/src/room/participant/Participant.ts +5 -0
- package/src/room/participant/RemoteParticipant.ts +19 -5
- package/src/room/participant/publishUtils.ts +14 -11
- package/src/room/rpc/client/RpcClientManager.test.ts +20 -27
- package/src/room/rpc/client/RpcClientManager.ts +1 -4
- package/src/room/rpc/server/RpcServerManager.test.ts +24 -23
- package/src/room/rpc/server/RpcServerManager.ts +1 -3
- package/src/room/track/LocalAudioTrack.ts +16 -0
- package/src/room/track/Track.ts +10 -6
- package/src/room/track/options.ts +11 -1
- package/src/room/types.ts +18 -0
- package/src/room/utils.ts +67 -0
- package/src/test/signalServerSetup.ts +151 -0
- package/src/test/signalToken.ts +59 -0
- package/src/test/vitest-context.d.ts +10 -0
- package/src/utils/dataPacketBuffer.test.ts +100 -0
- package/src/utils/dataPacketBuffer.ts +46 -1
- package/src/version.ts +5 -1
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { type ChildProcess, execFileSync, spawn } from 'node:child_process';
|
|
2
|
+
import { existsSync } from 'node:fs';
|
|
3
|
+
import { Socket, createServer } from 'node:net';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { createToken } from './signalToken';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* vitest globalSetup for the browser signal e2e suite. It provides the tests a
|
|
9
|
+
* ws:// base URL for the LiveKit mock test-server, via one of two paths:
|
|
10
|
+
*
|
|
11
|
+
* - LK_TEST_SERVER_URL — connect to an already-running server (CI runs the
|
|
12
|
+
* published test-server Docker image as a service; see .github/workflows/e2e.yaml).
|
|
13
|
+
* - LK_SERVER_DIR — a local livekit-server checkout: build cmd/test-server
|
|
14
|
+
* and spawn it on an ephemeral port (dev convenience).
|
|
15
|
+
*
|
|
16
|
+
* If neither is set (or the toolchain is unavailable) the suite is skipped.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const HOST = '127.0.0.1';
|
|
20
|
+
|
|
21
|
+
let child: ChildProcess | undefined;
|
|
22
|
+
|
|
23
|
+
function getFreePort(): Promise<number> {
|
|
24
|
+
return new Promise((resolve, reject) => {
|
|
25
|
+
const srv = createServer();
|
|
26
|
+
srv.unref();
|
|
27
|
+
srv.on('error', reject);
|
|
28
|
+
srv.listen(0, HOST, () => {
|
|
29
|
+
const addr = srv.address();
|
|
30
|
+
const port = typeof addr === 'object' && addr ? addr.port : 0;
|
|
31
|
+
srv.close(() => resolve(port));
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Poll the validate endpoint (from Node — no CORS here) until it answers 200.
|
|
37
|
+
async function waitReady(serverUrl: string): Promise<boolean> {
|
|
38
|
+
const token = await createToken({ signal: 'happy' });
|
|
39
|
+
const httpBase = serverUrl.replace(/^ws/, 'http');
|
|
40
|
+
const validateUrl = `${httpBase}/rtc/validate?access_token=${encodeURIComponent(token)}`;
|
|
41
|
+
const deadline = Date.now() + 20_000;
|
|
42
|
+
while (Date.now() < deadline) {
|
|
43
|
+
try {
|
|
44
|
+
if ((await fetch(validateUrl)).status === 200) {
|
|
45
|
+
return true;
|
|
46
|
+
}
|
|
47
|
+
} catch {
|
|
48
|
+
// not up yet
|
|
49
|
+
}
|
|
50
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
51
|
+
}
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export default async function setup(project: { provide: (name: string, value: unknown) => void }) {
|
|
56
|
+
const provide = (serverUrl: string) => {
|
|
57
|
+
project.provide('serverUrl', serverUrl);
|
|
58
|
+
project.provide('e2eUnavailable', '');
|
|
59
|
+
};
|
|
60
|
+
const skip = (reason: string) => {
|
|
61
|
+
// eslint-disable-next-line no-console
|
|
62
|
+
console.warn(`\n[e2e] signal-connection e2e suite SKIPPED: ${reason}\n`);
|
|
63
|
+
project.provide('serverUrl', '');
|
|
64
|
+
project.provide('e2eUnavailable', reason);
|
|
65
|
+
return () => {};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// CI / Docker: connect to an externally-managed server.
|
|
69
|
+
const externalUrl = process.env.LK_TEST_SERVER_URL;
|
|
70
|
+
if (externalUrl) {
|
|
71
|
+
if (!(await waitReady(externalUrl))) {
|
|
72
|
+
return skip(`test-server at ${externalUrl} did not become ready`);
|
|
73
|
+
}
|
|
74
|
+
provide(externalUrl);
|
|
75
|
+
return () => {}; // container lifecycle is owned by CI
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Local dev: build + spawn cmd/test-server from a livekit-server checkout.
|
|
79
|
+
const serverDir = process.env.LK_SERVER_DIR;
|
|
80
|
+
if (!serverDir || !existsSync(serverDir)) {
|
|
81
|
+
return skip(
|
|
82
|
+
'set LK_TEST_SERVER_URL (a running server) or LK_SERVER_DIR (a livekit-server checkout)',
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const binary = join(serverDir, 'bin', 'test-server');
|
|
87
|
+
if (!process.env.LK_E2E_SKIP_BUILD) {
|
|
88
|
+
try {
|
|
89
|
+
execFileSync('go', ['build', '-o', 'bin/test-server', './cmd/test-server'], {
|
|
90
|
+
cwd: serverDir,
|
|
91
|
+
stdio: 'pipe',
|
|
92
|
+
});
|
|
93
|
+
} catch (e) {
|
|
94
|
+
return skip(`failed to build test-server: ${(e as Error).message}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (!existsSync(binary)) {
|
|
98
|
+
return skip(`test-server binary missing at ${binary}`);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Fresh ephemeral port each run so a leftover server from a crashed run can
|
|
102
|
+
// never be mistaken for ours (vitest browser-mode teardown is unreliable, so
|
|
103
|
+
// a spawned server may outlive the run; the ephemeral port makes that
|
|
104
|
+
// harmless, at the cost of leaving at most one idle process per run).
|
|
105
|
+
const port = Number(process.env.LK_TEST_SERVER_PORT) || (await getFreePort());
|
|
106
|
+
const serverUrl = `ws://${HOST}:${port}`;
|
|
107
|
+
|
|
108
|
+
child = spawn(binary, ['--ports', String(port), '--bind', HOST], {
|
|
109
|
+
cwd: serverDir,
|
|
110
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
111
|
+
});
|
|
112
|
+
let serverLog = '';
|
|
113
|
+
child.stdout?.on('data', (d: unknown) => {
|
|
114
|
+
serverLog += String(d);
|
|
115
|
+
});
|
|
116
|
+
child.stderr?.on('data', (d: unknown) => {
|
|
117
|
+
serverLog += String(d);
|
|
118
|
+
});
|
|
119
|
+
// With stdio: 'pipe' these are net.Sockets (typed as Readable, which lacks unref);
|
|
120
|
+
// unref the pipe handles so their 'data' listeners don't keep the parent alive.
|
|
121
|
+
if (child.stdout instanceof Socket) child.stdout.unref();
|
|
122
|
+
if (child.stderr instanceof Socket) child.stderr.unref();
|
|
123
|
+
child.unref();
|
|
124
|
+
|
|
125
|
+
const kill = () => {
|
|
126
|
+
try {
|
|
127
|
+
child?.kill('SIGKILL');
|
|
128
|
+
} catch {
|
|
129
|
+
// already gone
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
process.once('exit', kill);
|
|
133
|
+
process.once('SIGINT', () => {
|
|
134
|
+
kill();
|
|
135
|
+
process.exit(130);
|
|
136
|
+
});
|
|
137
|
+
process.once('SIGTERM', () => {
|
|
138
|
+
kill();
|
|
139
|
+
process.exit(143);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
if (!(await waitReady(serverUrl))) {
|
|
143
|
+
kill();
|
|
144
|
+
return skip(
|
|
145
|
+
`test-server did not become ready on ${serverUrl}\n--- server output ---\n${serverLog}`,
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
provide(serverUrl);
|
|
150
|
+
return kill;
|
|
151
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { SignJWT } from 'jose';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mint a LiveKit access token for the mock test-server (HS256, dev secret).
|
|
5
|
+
*
|
|
6
|
+
* The mock's signal behavior mode is selected via a participant attribute
|
|
7
|
+
* under the reserved key `lk.mock` (dot notation, matching LiveKit's internal
|
|
8
|
+
* attribute convention), whose value is the JSON control object
|
|
9
|
+
* `{"signal":"<mode>"}` — mirroring the X-Lk-Mock header protocol. Attributes
|
|
10
|
+
* are a standard AccessToken field (a string map), so a dedicated `lk-mock`
|
|
11
|
+
* key coexists with any real metadata/attributes and needs no bespoke token
|
|
12
|
+
* construction. The room name is just a room; it no longer encodes behavior.
|
|
13
|
+
*/
|
|
14
|
+
export interface TokenOptions {
|
|
15
|
+
/** Mock behavior mode (e.g. 'no_pong'); omitted → the mock defaults to 'happy'. */
|
|
16
|
+
signal?: string;
|
|
17
|
+
/** LeaveRequest action the leave modes should send (0=DISCONNECT,1=RESUME,2=RECONNECT). */
|
|
18
|
+
leaveAction?: number;
|
|
19
|
+
room?: string;
|
|
20
|
+
identity?: string;
|
|
21
|
+
apiKey?: string;
|
|
22
|
+
secret?: string;
|
|
23
|
+
ttlSeconds?: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function createToken(opts: TokenOptions = {}): Promise<string> {
|
|
27
|
+
const {
|
|
28
|
+
signal,
|
|
29
|
+
leaveAction,
|
|
30
|
+
room = 'e2e-room',
|
|
31
|
+
identity = `test-${Math.random().toString(36).slice(2, 8)}`,
|
|
32
|
+
apiKey = 'devkey',
|
|
33
|
+
secret = 'secret',
|
|
34
|
+
ttlSeconds = 600,
|
|
35
|
+
} = opts;
|
|
36
|
+
const key = new TextEncoder().encode(secret);
|
|
37
|
+
const payload: Record<string, unknown> = {
|
|
38
|
+
video: { room, roomJoin: true, canPublish: true, canSubscribe: true, canPublishData: true },
|
|
39
|
+
};
|
|
40
|
+
if (signal) {
|
|
41
|
+
const control: Record<string, unknown> = { signal };
|
|
42
|
+
if (leaveAction !== undefined) {
|
|
43
|
+
control.leaveAction = leaveAction;
|
|
44
|
+
}
|
|
45
|
+
payload.attributes = { 'lk.mock': JSON.stringify(control) };
|
|
46
|
+
}
|
|
47
|
+
return new SignJWT(payload)
|
|
48
|
+
.setProtectedHeader({ alg: 'HS256' })
|
|
49
|
+
.setIssuer(apiKey)
|
|
50
|
+
.setSubject(identity)
|
|
51
|
+
.setIssuedAt()
|
|
52
|
+
.setExpirationTime(`${ttlSeconds}s`)
|
|
53
|
+
.sign(key);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** A syntactically-valid token signed with the WRONG secret — for 401 tests. */
|
|
57
|
+
export async function createInvalidToken(): Promise<string> {
|
|
58
|
+
return createToken({ secret: 'not-the-secret' });
|
|
59
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import 'vitest';
|
|
2
|
+
|
|
3
|
+
declare module 'vitest' {
|
|
4
|
+
interface ProvidedContext {
|
|
5
|
+
/** ws:// base URL of the spawned mock test-server, or '' when unavailable. */
|
|
6
|
+
serverUrl: string;
|
|
7
|
+
/** Non-empty reason string when the e2e suite must be skipped. */
|
|
8
|
+
e2eUnavailable: string;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { DataPacketBuffer } from './dataPacketBuffer';
|
|
3
|
+
|
|
4
|
+
const item = (sequence: number, size: number, sent: boolean) => ({
|
|
5
|
+
data: new Uint8Array(size),
|
|
6
|
+
sequence,
|
|
7
|
+
sent,
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
describe('DataPacketBuffer', () => {
|
|
11
|
+
it('trims sent packets down to the buffered amount', () => {
|
|
12
|
+
const buffer = new DataPacketBuffer();
|
|
13
|
+
buffer.push(item(1, 100, true));
|
|
14
|
+
buffer.push(item(2, 100, true));
|
|
15
|
+
buffer.push(item(3, 100, true));
|
|
16
|
+
|
|
17
|
+
// 150 buffered bytes cover the last two packets; the first is fully delivered.
|
|
18
|
+
buffer.alignBufferedAmount(150);
|
|
19
|
+
|
|
20
|
+
expect(buffer.getAll().map((i) => i.sequence)).toEqual([2, 3]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('never trims unsent packets, even when the buffered amount is zero', () => {
|
|
24
|
+
const buffer = new DataPacketBuffer();
|
|
25
|
+
buffer.push(item(1, 100, false));
|
|
26
|
+
buffer.push(item(2, 100, false));
|
|
27
|
+
|
|
28
|
+
buffer.alignBufferedAmount(0);
|
|
29
|
+
|
|
30
|
+
expect(buffer.length).toBe(2);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it('does not let unsent tail bytes cause over-trimming of the sent prefix', () => {
|
|
34
|
+
const buffer = new DataPacketBuffer();
|
|
35
|
+
buffer.push(item(1, 100, true));
|
|
36
|
+
buffer.push(item(2, 100, true));
|
|
37
|
+
// Queued during a reconnect window — not part of the channel's bufferedAmount.
|
|
38
|
+
buffer.push(item(3, 100, false));
|
|
39
|
+
buffer.push(item(4, 100, false));
|
|
40
|
+
|
|
41
|
+
// Both sent packets are still in flight; nothing may be trimmed. With size accounting based
|
|
42
|
+
// on the total (400) instead of sent bytes (200), both sent packets would be popped here.
|
|
43
|
+
buffer.alignBufferedAmount(200);
|
|
44
|
+
|
|
45
|
+
expect(buffer.getAll().map((i) => i.sequence)).toEqual([1, 2, 3, 4]);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('markSent makes a single packet eligible for trimming and tracks sent size', () => {
|
|
49
|
+
const buffer = new DataPacketBuffer();
|
|
50
|
+
const first = item(1, 100, false);
|
|
51
|
+
const second = item(2, 100, false);
|
|
52
|
+
buffer.push(first);
|
|
53
|
+
buffer.push(second);
|
|
54
|
+
|
|
55
|
+
// Only the marked packet can be trimmed; the still-unsent one blocks the front.
|
|
56
|
+
buffer.markSent(first);
|
|
57
|
+
buffer.alignBufferedAmount(0);
|
|
58
|
+
expect(buffer.getAll().map((i) => i.sequence)).toEqual([1, 2]);
|
|
59
|
+
|
|
60
|
+
buffer.markSent(second);
|
|
61
|
+
buffer.alignBufferedAmount(50);
|
|
62
|
+
expect(buffer.getAll().map((i) => i.sequence)).toEqual([2]);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('getUnsent returns only unsent packets, in order', () => {
|
|
66
|
+
const buffer = new DataPacketBuffer();
|
|
67
|
+
const a = item(1, 100, false);
|
|
68
|
+
const b = item(2, 100, false);
|
|
69
|
+
buffer.push(a);
|
|
70
|
+
buffer.push(b);
|
|
71
|
+
buffer.push(item(3, 100, false));
|
|
72
|
+
buffer.markSent(b);
|
|
73
|
+
|
|
74
|
+
expect(buffer.getUnsent().map((i) => i.sequence)).toEqual([1, 3]);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('markAllUnsent flags every packet for re-send and resets sent size', () => {
|
|
78
|
+
const buffer = new DataPacketBuffer();
|
|
79
|
+
buffer.push(item(1, 100, true));
|
|
80
|
+
buffer.push(item(2, 100, true));
|
|
81
|
+
|
|
82
|
+
buffer.markAllUnsent();
|
|
83
|
+
expect(buffer.getUnsent().map((i) => i.sequence)).toEqual([1, 2]);
|
|
84
|
+
|
|
85
|
+
// With everything unsent, nothing can be trimmed even at a zero buffered amount.
|
|
86
|
+
buffer.alignBufferedAmount(0);
|
|
87
|
+
expect(buffer.getAll().map((i) => i.sequence)).toEqual([1, 2]);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('popToSequence drops acked packets regardless of sent state', () => {
|
|
91
|
+
const buffer = new DataPacketBuffer();
|
|
92
|
+
buffer.push(item(1, 100, true));
|
|
93
|
+
buffer.push(item(2, 100, false));
|
|
94
|
+
buffer.push(item(3, 100, false));
|
|
95
|
+
|
|
96
|
+
buffer.popToSequence(2);
|
|
97
|
+
|
|
98
|
+
expect(buffer.getAll().map((i) => i.sequence)).toEqual([3]);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -3,6 +3,13 @@ import type { NonSharedUint8Array } from '../type-polyfills/non-shared-typed-arr
|
|
|
3
3
|
export interface DataPacketItem {
|
|
4
4
|
data: NonSharedUint8Array;
|
|
5
5
|
sequence: number;
|
|
6
|
+
/**
|
|
7
|
+
* Whether the packet has been handed to the data channel. Unsent packets are queued for the
|
|
8
|
+
* resume replay (e.g. sends that landed in a reconnect window) and must never be trimmed by
|
|
9
|
+
* {@link DataPacketBuffer.alignBufferedAmount}, which reasons about the channel's buffered
|
|
10
|
+
* bytes — those only ever contain sent packets.
|
|
11
|
+
*/
|
|
12
|
+
sent: boolean;
|
|
6
13
|
}
|
|
7
14
|
|
|
8
15
|
export class DataPacketBuffer {
|
|
@@ -10,15 +17,23 @@ export class DataPacketBuffer {
|
|
|
10
17
|
|
|
11
18
|
private _totalSize = 0;
|
|
12
19
|
|
|
20
|
+
private _sentSize = 0;
|
|
21
|
+
|
|
13
22
|
push(item: DataPacketItem) {
|
|
14
23
|
this.buffer.push(item);
|
|
15
24
|
this._totalSize += item.data.byteLength;
|
|
25
|
+
if (item.sent) {
|
|
26
|
+
this._sentSize += item.data.byteLength;
|
|
27
|
+
}
|
|
16
28
|
}
|
|
17
29
|
|
|
18
30
|
pop(): DataPacketItem | undefined {
|
|
19
31
|
const item = this.buffer.shift();
|
|
20
32
|
if (item) {
|
|
21
33
|
this._totalSize -= item.data.byteLength;
|
|
34
|
+
if (item.sent) {
|
|
35
|
+
this._sentSize -= item.data.byteLength;
|
|
36
|
+
}
|
|
22
37
|
}
|
|
23
38
|
return item;
|
|
24
39
|
}
|
|
@@ -27,6 +42,31 @@ export class DataPacketBuffer {
|
|
|
27
42
|
return this.buffer.slice();
|
|
28
43
|
}
|
|
29
44
|
|
|
45
|
+
/** Every queued packet not yet handed to the channel, in sequence order. */
|
|
46
|
+
getUnsent(): DataPacketItem[] {
|
|
47
|
+
return this.buffer.filter((item) => !item.sent);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Marks a single queued packet as handed to the channel. */
|
|
51
|
+
markSent(item: DataPacketItem) {
|
|
52
|
+
if (!item.sent) {
|
|
53
|
+
item.sent = true;
|
|
54
|
+
this._sentSize += item.data.byteLength;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Marks every queued packet as not-yet-sent. Used at the start of a resume replay: whatever is
|
|
60
|
+
* still buffered was sent on the previous channel (or deferred) and must be re-handed to the
|
|
61
|
+
* current one, so none of it counts as sent until the replay actually transmits it.
|
|
62
|
+
*/
|
|
63
|
+
markAllUnsent() {
|
|
64
|
+
for (const item of this.buffer) {
|
|
65
|
+
item.sent = false;
|
|
66
|
+
}
|
|
67
|
+
this._sentSize = 0;
|
|
68
|
+
}
|
|
69
|
+
|
|
30
70
|
popToSequence(sequence: number) {
|
|
31
71
|
while (this.buffer.length > 0) {
|
|
32
72
|
const first = this.buffer[0];
|
|
@@ -41,7 +81,12 @@ export class DataPacketBuffer {
|
|
|
41
81
|
alignBufferedAmount(bufferedAmount: number) {
|
|
42
82
|
while (this.buffer.length > 0) {
|
|
43
83
|
const first = this.buffer[0];
|
|
44
|
-
|
|
84
|
+
// Unsent packets aren't part of the channel's bufferedAmount and are still awaiting the
|
|
85
|
+
// resume replay — trimming them would silently lose them.
|
|
86
|
+
if (!first.sent) {
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
if (this._sentSize - first.data.byteLength <= bufferedAmount) {
|
|
45
90
|
break;
|
|
46
91
|
}
|
|
47
92
|
this.pop();
|
package/src/version.ts
CHANGED
|
@@ -8,7 +8,11 @@ export const CLIENT_PROTOCOL_DEFAULT = 0;
|
|
|
8
8
|
/** Replaces RPC v1 protocol with a v2 data streams based one to support unlimited request /
|
|
9
9
|
* response payload length. */
|
|
10
10
|
export const CLIENT_PROTOCOL_DATA_STREAM_RPC = 1;
|
|
11
|
+
/** "Data streams v2": the client knows how to receive a single-packet data stream (a stream whose
|
|
12
|
+
* entire payload is smuggled into the header packet, with no chunk/trailer packets). Senders only
|
|
13
|
+
* use the single-packet optimization when the recipient advertises at least this protocol. */
|
|
14
|
+
export const CLIENT_PROTOCOL_DATA_STREAM_V2 = 2;
|
|
11
15
|
|
|
12
16
|
/** The client protocol version indicates what level of support that the client has for
|
|
13
17
|
* client <-> client api interactions. */
|
|
14
|
-
export const clientProtocol =
|
|
18
|
+
export const clientProtocol = CLIENT_PROTOCOL_DATA_STREAM_V2;
|