@livedesk/hub 0.1.70 → 0.1.72
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/package.json
CHANGED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
export const CONSOLE_MEDIA_ADMISSION = Object.freeze({
|
|
2
|
+
laneBufferedBytes: 512 * 1024,
|
|
3
|
+
peerBufferedBytes: 1024 * 1024,
|
|
4
|
+
bufferedAgeMs: 120,
|
|
5
|
+
maxStreams: 64,
|
|
6
|
+
maxMetadataBytes: 64 * 1024
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
function h264Identity(packet) {
|
|
10
|
+
if (!Buffer.isBuffer(packet) || packet.length < 5) return null;
|
|
11
|
+
const size = packet.readUInt32BE(0);
|
|
12
|
+
if (size < 2 || size > CONSOLE_MEDIA_ADMISSION.maxMetadataBytes || 4 + size >= packet.length) return null;
|
|
13
|
+
let frame;
|
|
14
|
+
try { frame = JSON.parse(packet.toString('utf8', 4, 4 + size)); } catch { return null; }
|
|
15
|
+
if (frame?.frameMode !== 'mode3-h264-hw'
|
|
16
|
+
|| !['wall', 'control'].includes(frame.streamPurpose)
|
|
17
|
+
|| !Number.isSafeInteger(frame.captureGeneration) || frame.captureGeneration <= 0
|
|
18
|
+
|| !Number.isSafeInteger(frame.monitorIndex) || frame.monitorIndex < 0
|
|
19
|
+
|| !['deviceId', 'sessionId', 'streamId', 'commandId'].every(key => (
|
|
20
|
+
typeof frame[key] === 'string' && frame[key].length > 0 && frame[key].length <= 256
|
|
21
|
+
))) return null;
|
|
22
|
+
return {
|
|
23
|
+
stream: JSON.stringify([frame.deviceId, frame.streamPurpose]),
|
|
24
|
+
owner: JSON.stringify([frame.sessionId, frame.streamId, frame.commandId, frame.captureGeneration, frame.monitorIndex]),
|
|
25
|
+
key: frame.isKeyFrame === true || frame.chunkType === 'key'
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// This is send admission, not authentication or presentation authority. Those
|
|
30
|
+
// exact binding gates still run at the Hub and browser. Retain no payload,
|
|
31
|
+
// timer, task, or outbound queue; every decision precedes wire fragmentation.
|
|
32
|
+
export function createConsoleFrameAdmission() {
|
|
33
|
+
const streams = new Map();
|
|
34
|
+
let bufferedSince = null;
|
|
35
|
+
let droppedFrames = 0;
|
|
36
|
+
let recoveredStreams = 0;
|
|
37
|
+
return {
|
|
38
|
+
admit(packet, { laneBufferedBytes, peerBufferedBytes, now }) {
|
|
39
|
+
const identity = h264Identity(packet);
|
|
40
|
+
if (!identity) return 'unmanaged';
|
|
41
|
+
let state = streams.get(identity.stream);
|
|
42
|
+
if (!state) {
|
|
43
|
+
if (streams.size >= CONSOLE_MEDIA_ADMISSION.maxStreams) return 'capacity';
|
|
44
|
+
state = { owner: identity.owner, awaitingKey: false };
|
|
45
|
+
streams.set(identity.stream, state);
|
|
46
|
+
} else if (state.owner !== identity.owner) {
|
|
47
|
+
state.owner = identity.owner;
|
|
48
|
+
state.awaitingKey = true;
|
|
49
|
+
}
|
|
50
|
+
if (laneBufferedBytes <= 0) bufferedSince = null;
|
|
51
|
+
else if (bufferedSince === null) bufferedSince = now;
|
|
52
|
+
const pressured = (laneBufferedBytes > 0 && (
|
|
53
|
+
laneBufferedBytes + packet.length > CONSOLE_MEDIA_ADMISSION.laneBufferedBytes
|
|
54
|
+
|| now - bufferedSince >= CONSOLE_MEDIA_ADMISSION.bufferedAgeMs
|
|
55
|
+
)) || (peerBufferedBytes > 0
|
|
56
|
+
&& peerBufferedBytes + packet.length > CONSOLE_MEDIA_ADMISSION.peerBufferedBytes);
|
|
57
|
+
if (pressured || (state.awaitingKey && !identity.key)) {
|
|
58
|
+
state.awaitingKey = true;
|
|
59
|
+
droppedFrames += 1;
|
|
60
|
+
return 'drop';
|
|
61
|
+
}
|
|
62
|
+
if (state.awaitingKey) recoveredStreams += 1;
|
|
63
|
+
state.awaitingKey = false;
|
|
64
|
+
return 'send';
|
|
65
|
+
},
|
|
66
|
+
inspect() {
|
|
67
|
+
return {
|
|
68
|
+
streamOwners: streams.size,
|
|
69
|
+
awaitingKeyStreams: [...streams.values()].filter(state => state.awaitingKey).length,
|
|
70
|
+
droppedFrames,
|
|
71
|
+
recoveredStreams,
|
|
72
|
+
retainedPayloadBytes: 0
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
clear() {
|
|
76
|
+
streams.clear();
|
|
77
|
+
bufferedSince = null;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
import { CONSOLE_MEDIA_ADMISSION, createConsoleFrameAdmission } from './console-direct-frame-admission.mjs';
|
|
4
|
+
import { buildImmutableRemoteFramePacket } from './frame-packet-contract.mjs';
|
|
5
|
+
|
|
6
|
+
function packet({ key = false, device = 'a', generation = 1, monitor = 0, bytes = 1024 } = {}) {
|
|
7
|
+
return buildImmutableRemoteFramePacket({
|
|
8
|
+
deviceId: device, sessionId: 'session', streamId: 'stream', commandId: 'command',
|
|
9
|
+
captureGeneration: generation, monitorIndex: monitor, streamPurpose: 'wall',
|
|
10
|
+
frameMode: 'mode3-h264-hw', isKeyFrame: key
|
|
11
|
+
}, Buffer.alloc(bytes)).packet;
|
|
12
|
+
}
|
|
13
|
+
const empty = { laneBufferedBytes: 0, peerBufferedBytes: 0, now: 0 };
|
|
14
|
+
|
|
15
|
+
test('a small persistent backlog is bounded by age without a timer or retained frame', () => {
|
|
16
|
+
const gate = createConsoleFrameAdmission();
|
|
17
|
+
assert.equal(gate.admit(packet({ key: true }), empty), 'send');
|
|
18
|
+
const pending = { laneBufferedBytes: 1024, peerBufferedBytes: 1024, now: 1 };
|
|
19
|
+
assert.equal(gate.admit(packet(), pending), 'send');
|
|
20
|
+
assert.equal(gate.admit(packet(), { ...pending, now: 1 + CONSOLE_MEDIA_ADMISSION.bufferedAgeMs }), 'drop');
|
|
21
|
+
assert.equal(gate.admit(packet(), { ...empty, now: 200 }), 'drop');
|
|
22
|
+
assert.equal(gate.admit(packet({ key: true }), { ...empty, now: 201 }), 'send');
|
|
23
|
+
assert.equal(gate.inspect().retainedPayloadBytes, 0);
|
|
24
|
+
assert.equal(gate.inspect().recoveredStreams, 1);
|
|
25
|
+
gate.clear();
|
|
26
|
+
assert.equal(gate.inspect().streamOwners, 0);
|
|
27
|
+
assert.equal(gate.inspect().awaitingKeyStreams, 0);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test('a pressured viewer cannot poison another viewer, device, or replacement binding', () => {
|
|
31
|
+
const phone = createConsoleFrameAdmission();
|
|
32
|
+
const desktop = createConsoleFrameAdmission();
|
|
33
|
+
const pressure = { ...empty, laneBufferedBytes: CONSOLE_MEDIA_ADMISSION.laneBufferedBytes, peerBufferedBytes: CONSOLE_MEDIA_ADMISSION.laneBufferedBytes };
|
|
34
|
+
assert.equal(phone.admit(packet(), pressure), 'drop');
|
|
35
|
+
assert.equal(desktop.admit(packet(), empty), 'send');
|
|
36
|
+
assert.equal(phone.admit(packet({ device: 'b', key: true }), empty), 'send');
|
|
37
|
+
assert.equal(phone.admit(packet(), empty), 'drop');
|
|
38
|
+
assert.equal(phone.admit(packet({ generation: 2, monitor: 1 }), empty), 'drop');
|
|
39
|
+
assert.equal(phone.admit(packet({ generation: 2, monitor: 1, key: true }), empty), 'send');
|
|
40
|
+
assert.equal(phone.inspect().awaitingKeyStreams, 0);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('one complete key may exceed the soft watermark on an empty peer without admitting a second backlog', () => {
|
|
44
|
+
const gate = createConsoleFrameAdmission();
|
|
45
|
+
const key = packet({ key: true, bytes: 700 * 1024 });
|
|
46
|
+
assert.equal(gate.admit(key, empty), 'send');
|
|
47
|
+
assert.equal(gate.admit(key, { ...empty, laneBufferedBytes: key.length, peerBufferedBytes: key.length }), 'drop');
|
|
48
|
+
assert.equal(gate.admit(key, empty), 'send');
|
|
49
|
+
assert.equal(gate.admit(Buffer.from('legacy-binary'), empty), 'unmanaged');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('a multiplexed lane has a fixed owner bound and releases it on close', () => {
|
|
53
|
+
const gate = createConsoleFrameAdmission();
|
|
54
|
+
for (let n = 0; n < CONSOLE_MEDIA_ADMISSION.maxStreams; n++) {
|
|
55
|
+
assert.equal(gate.admit(packet({ device: `device-${n}`, key: true }), empty), 'send');
|
|
56
|
+
}
|
|
57
|
+
assert.equal(gate.admit(packet({ device: 'overflow', key: true }), empty), 'capacity');
|
|
58
|
+
gate.clear();
|
|
59
|
+
assert.equal(gate.admit(packet({ device: 'replacement', key: true }), empty), 'send');
|
|
60
|
+
});
|
package/src/console-direct.js
CHANGED
|
@@ -9,6 +9,7 @@ import {
|
|
|
9
9
|
encodeDirectConsoleWireMessage
|
|
10
10
|
} from '../../runtime-core/src/console-direct-wire.js';
|
|
11
11
|
import { workspaceRoleCanControl, workspaceRoleCanRequest } from './auth/workspace-access.js';
|
|
12
|
+
import { createConsoleFrameAdmission } from './console-direct-frame-admission.mjs';
|
|
12
13
|
|
|
13
14
|
const DEFAULT_SIGNAL_URL = 'https://livedesk-wake.lovecrdm.workers.dev';
|
|
14
15
|
const DEFAULT_STUN_URLS = Object.freeze(['stun:stun.cloudflare.com:3478']);
|
|
@@ -87,6 +88,34 @@ function byteLength(value) {
|
|
|
87
88
|
return Buffer.byteLength(String(value || ''), 'utf8');
|
|
88
89
|
}
|
|
89
90
|
|
|
91
|
+
function optionalPeerMetric(peer, method) {
|
|
92
|
+
try {
|
|
93
|
+
const value = peer[method]?.();
|
|
94
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null;
|
|
95
|
+
} catch { return null; }
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function candidateAddressScope(value) {
|
|
99
|
+
if (typeof value !== 'string' || !value || value === '?') return 'unknown';
|
|
100
|
+
if (/^(127\.|::1$)/i.test(value)) return 'loopback';
|
|
101
|
+
if (/^(10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.|169\.254\.|f[cd][0-9a-f]{2}:|fe80:)/i.test(value)) return 'private';
|
|
102
|
+
return 'public';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function inspectPeerPath(peer) {
|
|
106
|
+
try {
|
|
107
|
+
const pair = peer.getSelectedCandidatePair?.();
|
|
108
|
+
if (!pair?.local || !pair?.remote) return null;
|
|
109
|
+
return {
|
|
110
|
+
localType: String(pair.local.type || 'unknown').slice(0, 24),
|
|
111
|
+
remoteType: String(pair.remote.type || 'unknown').slice(0, 24),
|
|
112
|
+
localScope: candidateAddressScope(pair.local.address),
|
|
113
|
+
remoteScope: candidateAddressScope(pair.remote.address),
|
|
114
|
+
transport: String(pair.remote.transportType || 'unknown').slice(0, 24)
|
|
115
|
+
};
|
|
116
|
+
} catch { return null; }
|
|
117
|
+
}
|
|
118
|
+
|
|
90
119
|
function parseJson(value) {
|
|
91
120
|
try {
|
|
92
121
|
return JSON.parse(String(value || ''));
|
|
@@ -527,6 +556,7 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
527
556
|
disposePendingLogicalControl(owner, channelState.channelId);
|
|
528
557
|
disposeAssemblerTimer(channelState);
|
|
529
558
|
channelState.assembler.dispose();
|
|
559
|
+
channelState.frameAdmission?.clear();
|
|
530
560
|
closeLocalSocket(
|
|
531
561
|
owner,
|
|
532
562
|
channelState,
|
|
@@ -667,6 +697,18 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
667
697
|
|
|
668
698
|
function sendWire(owner, channelState, kind, value, maxMessageBytes, options = {}) {
|
|
669
699
|
if (!isOwnerActive(owner) || channelState.closed || !channelIsOpen(channelState.channel)) return false;
|
|
700
|
+
if (kind === 'binary' && channelState.frameAdmission && value.byteLength <= maxMessageBytes) {
|
|
701
|
+
const admission = channelState.frameAdmission.admit(value, {
|
|
702
|
+
laneBufferedBytes: channelBufferedAmount(channelState),
|
|
703
|
+
peerBufferedBytes: ownerBufferedAmount(owner),
|
|
704
|
+
now: performance.now()
|
|
705
|
+
});
|
|
706
|
+
if (admission === 'drop') return true;
|
|
707
|
+
if (admission === 'capacity') {
|
|
708
|
+
closeLogicalChannel(owner, channelState, { code: 1009, reason: 'console-direct-frame-owner-capacity' });
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
670
712
|
let chunks;
|
|
671
713
|
try {
|
|
672
714
|
const messageId = channelState.nextSendMessageId >>> 0;
|
|
@@ -973,7 +1015,7 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
973
1015
|
socket.on('message', (data, isBinary) => {
|
|
974
1016
|
if (!isOwnerActive(owner) || owner.channels.get(channelId) !== channelState || channelState.localSocket !== socket) return;
|
|
975
1017
|
const kind = isBinary ? 'binary' : 'text';
|
|
976
|
-
const value = isBinary ?
|
|
1018
|
+
const value = isBinary ? (Buffer.isBuffer(data) ? data : Buffer.from(data)) : String(data);
|
|
977
1019
|
if (!sendWire(owner, channelState, kind, value, channelState.maxMessageBytes)) {
|
|
978
1020
|
closeLogicalChannel(owner, channelState, {
|
|
979
1021
|
code: 1013,
|
|
@@ -1182,6 +1224,7 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
1182
1224
|
nextSendMessageId: 1,
|
|
1183
1225
|
openRequested: false,
|
|
1184
1226
|
localSocket: null,
|
|
1227
|
+
frameAdmission: identity.purpose === 'frame' ? createConsoleFrameAdmission() : null,
|
|
1185
1228
|
closed: false
|
|
1186
1229
|
};
|
|
1187
1230
|
owner.channels.set(identity.channelId, channelState);
|
|
@@ -1300,7 +1343,11 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
1300
1343
|
iceTransportPolicy: 'all',
|
|
1301
1344
|
disableAutoNegotiation: false,
|
|
1302
1345
|
disableFingerprintVerification: false,
|
|
1303
|
-
|
|
1346
|
+
// Each native send is one wire chunk. libdatachannel also uses this
|
|
1347
|
+
// setting as the minimum SCTP socket buffer size, below bufferedAmount.
|
|
1348
|
+
// A whole-frame limit here hides megabytes from our admission gate.
|
|
1349
|
+
// Complete frame/HTTP limits remain enforced by the wire assemblers.
|
|
1350
|
+
maxMessageSize: DIRECT_CONSOLE_WIRE_CHUNK_BYTES
|
|
1304
1351
|
});
|
|
1305
1352
|
} catch (error) {
|
|
1306
1353
|
lastError = error instanceof Error ? error.message : String(error);
|
|
@@ -1938,6 +1985,7 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
1938
1985
|
};
|
|
1939
1986
|
|
|
1940
1987
|
const inspect = () => {
|
|
1988
|
+
const peerDiagnostics = [];
|
|
1941
1989
|
let logicalWebSocketChannels = 0;
|
|
1942
1990
|
let localWebSocketChannels = 0;
|
|
1943
1991
|
let pendingHttpRequests = 0;
|
|
@@ -1964,6 +2012,30 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
1964
2012
|
}
|
|
1965
2013
|
retainedAssemblyBytes += Number(owner.wireBudget.inspect().retainedBytes || 0);
|
|
1966
2014
|
bufferedSendBytes += ownerBufferedAmount(owner);
|
|
2015
|
+
peerDiagnostics.push({
|
|
2016
|
+
consoleId: owner.consoleId,
|
|
2017
|
+
connectionId: owner.connectionId,
|
|
2018
|
+
generation: owner.generation,
|
|
2019
|
+
peerState: owner.peerState,
|
|
2020
|
+
iceState: owner.iceState,
|
|
2021
|
+
rttMs: optionalPeerMetric(owner.peer, 'rtt'),
|
|
2022
|
+
bytesSent: optionalPeerMetric(owner.peer, 'bytesSent'),
|
|
2023
|
+
bytesReceived: optionalPeerMetric(owner.peer, 'bytesReceived'),
|
|
2024
|
+
selectedPath: inspectPeerPath(owner.peer),
|
|
2025
|
+
bufferedSendBytes: (() => {
|
|
2026
|
+
const values = [owner.control, ...owner.channels.values()]
|
|
2027
|
+
.filter(lane => lane && !lane.closed)
|
|
2028
|
+
.map(lane => optionalPeerMetric(lane.channel, 'bufferedAmount'));
|
|
2029
|
+
return values.some(value => value === null) ? null : values.reduce((sum, value) => sum + value, 0);
|
|
2030
|
+
})(),
|
|
2031
|
+
pendingHttpRequests: owner.pendingHttp.size,
|
|
2032
|
+
lanes: [...owner.channels.values()].map(lane => ({
|
|
2033
|
+
channelId: lane.channelId,
|
|
2034
|
+
purpose: lane.purpose,
|
|
2035
|
+
bufferedBytes: optionalPeerMetric(lane.channel, 'bufferedAmount'),
|
|
2036
|
+
frameAdmission: lane.frameAdmission?.inspect() ?? null
|
|
2037
|
+
}))
|
|
2038
|
+
});
|
|
1967
2039
|
}
|
|
1968
2040
|
const reconnectTimerActive = Boolean(retryTimer);
|
|
1969
2041
|
const accessTokenDeadlineActive = Boolean(accessTokenAttempt);
|
|
@@ -1982,6 +2054,7 @@ export function createHubConsoleDirect(options = {}) {
|
|
|
1982
2054
|
pendingLogicalControlRequests,
|
|
1983
2055
|
retainedAssemblyBytes,
|
|
1984
2056
|
bufferedSendBytes,
|
|
2057
|
+
peerDiagnostics,
|
|
1985
2058
|
iceConnectTimers,
|
|
1986
2059
|
peerDisconnectTimers,
|
|
1987
2060
|
assemblyDeadlineTimers,
|
|
@@ -4,11 +4,13 @@ import { readFileSync } from 'node:fs';
|
|
|
4
4
|
import test from 'node:test';
|
|
5
5
|
import nodeDataChannel from 'node-datachannel';
|
|
6
6
|
import {
|
|
7
|
+
DIRECT_CONSOLE_WIRE_CHUNK_BYTES,
|
|
7
8
|
createDirectConsoleWireAssembler,
|
|
8
9
|
encodeDirectConsoleWireMessage
|
|
9
10
|
} from '../../runtime-core/src/console-direct-wire.js';
|
|
10
11
|
import { consoleDirectContract, createHubConsoleDirect } from './console-direct.js';
|
|
11
12
|
import { workspaceRoleCanRequest } from './auth/workspace-access.js';
|
|
13
|
+
import { buildImmutableRemoteFramePacket } from './frame-packet-contract.mjs';
|
|
12
14
|
|
|
13
15
|
assert.equal(consoleDirectContract.maxDefaultRetryDelayMs, 20_000);
|
|
14
16
|
assert.equal(consoleDirectContract.peerDisconnectedTimeoutMs, 5_000);
|
|
@@ -357,6 +359,8 @@ test('STUN-only peer answers and rejects stale owner callbacks after replacement
|
|
|
357
359
|
assert.deepEqual(firstPeer.config.iceServers, ['stun:stun.example.test:3478']);
|
|
358
360
|
assert.equal(firstPeer.config.iceTransportPolicy, 'all');
|
|
359
361
|
assert.equal(firstPeer.config.disableFingerprintVerification, false);
|
|
362
|
+
assert.equal(firstPeer.config.maxMessageSize, DIRECT_CONSOLE_WIRE_CHUNK_BYTES,
|
|
363
|
+
'native SCTP accepts wire chunks, not whole frames; a frame-sized setting silently enlarges its hidden buffers');
|
|
360
364
|
assert.equal(signalingMessages(signal).some(message => (
|
|
361
365
|
message.type === 'rtc-answer' && message.connectionId === CONNECTION_ONE
|
|
362
366
|
)), true);
|
|
@@ -1336,6 +1340,64 @@ test('media pressure retires only its stale logical lane while preserving the co
|
|
|
1336
1340
|
direct.close();
|
|
1337
1341
|
});
|
|
1338
1342
|
|
|
1343
|
+
test('a pressured H.264 viewer drops whole dependent frames and resumes on a key without reconnecting', async () => {
|
|
1344
|
+
const { direct, signal } = await connectedDirect();
|
|
1345
|
+
const peer = offer(signal);
|
|
1346
|
+
const control = new FakeDataChannel('livedesk-control-v1');
|
|
1347
|
+
peer.emitDataChannel(control);
|
|
1348
|
+
control.open();
|
|
1349
|
+
const lane = new FakeDataChannel(`livedesk-ws-v1:${CHANNEL_ID}:frame`);
|
|
1350
|
+
peer.emitDataChannel(lane);
|
|
1351
|
+
lane.open();
|
|
1352
|
+
sendWire(control, {
|
|
1353
|
+
type: 'ws-open', connectionId: CONNECTION_ONE, hubEpoch: HUB_EPOCH,
|
|
1354
|
+
channelId: CHANNEL_ID, purpose: 'frame', path: '/api/remote/frames/ws'
|
|
1355
|
+
});
|
|
1356
|
+
const local = FakeLocalSocket.instances[0];
|
|
1357
|
+
local.open();
|
|
1358
|
+
const frame = (sequence, key, generation = 1) => buildImmutableRemoteFramePacket({
|
|
1359
|
+
deviceId: 'device-a', sessionId: 'session-a', streamId: 'wall-a', commandId: 'command-a',
|
|
1360
|
+
captureGeneration: generation, monitorIndex: 0, streamPurpose: 'wall',
|
|
1361
|
+
frameMode: 'mode3-h264-hw', chunkType: key ? 'key' : 'delta',
|
|
1362
|
+
isKeyFrame: key, streamFrameSeq: sequence
|
|
1363
|
+
}, Buffer.alloc(32 * 1024)).packet;
|
|
1364
|
+
local.receive(frame(1, true));
|
|
1365
|
+
const initialChunks = lane.sent.length;
|
|
1366
|
+
lane.buffered = consoleDirectContract.controlHeadroomBufferedBytes;
|
|
1367
|
+
local.receive(frame(2, false));
|
|
1368
|
+
assert.equal(lane.closed, false, 'congestion must not create another logical handshake or key-frame burst');
|
|
1369
|
+
assert.equal(lane.sent.length, initialChunks, 'no fragment of a dropped frame enters SCTP');
|
|
1370
|
+
lane.buffered = 0;
|
|
1371
|
+
local.receive(frame(3, false));
|
|
1372
|
+
assert.equal(lane.sent.length, initialChunks, 'a dependent delta cannot follow a discarded delta');
|
|
1373
|
+
local.receive(frame(4, true));
|
|
1374
|
+
assert.ok(lane.sent.length > initialChunks);
|
|
1375
|
+
const recoveredChunks = lane.sent.length;
|
|
1376
|
+
local.receive(frame(5, false));
|
|
1377
|
+
assert.ok(lane.sent.length > recoveredChunks);
|
|
1378
|
+
assert.equal(control.closed, false);
|
|
1379
|
+
assert.equal(peer.closed, false);
|
|
1380
|
+
assert.equal(direct.inspect().dataChannelBackpressureCloses, 0);
|
|
1381
|
+
const diagnostics = direct.inspect().peerDiagnostics[0];
|
|
1382
|
+
assert.equal(diagnostics.rttMs, null, 'missing native RTT is unknown');
|
|
1383
|
+
assert.equal(diagnostics.bytesSent, null, 'missing native byte counters are unknown');
|
|
1384
|
+
assert.equal(diagnostics.lanes[0].frameAdmission.droppedFrames, 2);
|
|
1385
|
+
assert.equal(diagnostics.lanes[0].frameAdmission.recoveredStreams, 1);
|
|
1386
|
+
peer.rtt = () => 12;
|
|
1387
|
+
peer.bytesSent = () => 1234;
|
|
1388
|
+
peer.selectedPair = {
|
|
1389
|
+
local: { type: 'host', address: '192.168.0.4', candidate: 'must-not-leak' },
|
|
1390
|
+
remote: { type: 'host', address: '192.168.0.8', transportType: 'Udp' }
|
|
1391
|
+
};
|
|
1392
|
+
const measured = direct.inspect().peerDiagnostics[0];
|
|
1393
|
+
assert.equal(measured.rttMs, 12);
|
|
1394
|
+
assert.equal(measured.bytesSent, 1234);
|
|
1395
|
+
assert.equal(measured.selectedPath.remoteScope, 'private');
|
|
1396
|
+
assert.doesNotMatch(JSON.stringify(measured), /192\.168\.|must-not-leak/);
|
|
1397
|
+
direct.close();
|
|
1398
|
+
assert.deepEqual(direct.inspect().peerDiagnostics, []);
|
|
1399
|
+
});
|
|
1400
|
+
|
|
1339
1401
|
test('a reliable control response drops stale media backlog without retiring the peer', async () => {
|
|
1340
1402
|
const { direct, signal } = await connectedDirect({
|
|
1341
1403
|
fetchImpl: async () => new Response('{"online":true}', {
|
|
@@ -1507,18 +1569,19 @@ test('server session refresh preserves peers while logout uses the revoking clos
|
|
|
1507
1569
|
assert.doesNotMatch(logoutBlock, /hubConsoleDirect\?\.refresh\(\)/);
|
|
1508
1570
|
});
|
|
1509
1571
|
|
|
1510
|
-
test('native
|
|
1572
|
+
test('native chunk-sized SCTP accepts a full-size fragmented frame over loopback', { timeout: 8_000 }, async t => {
|
|
1511
1573
|
let offerer = null;
|
|
1512
1574
|
let answerer = null;
|
|
1513
1575
|
let offerChannel = null;
|
|
1514
1576
|
let answerChannel = null;
|
|
1515
|
-
const assembler = createDirectConsoleWireAssembler();
|
|
1577
|
+
const assembler = createDirectConsoleWireAssembler({ maxMessageBytes: consoleDirectContract.maxFrameMessageBytes });
|
|
1516
1578
|
let openTimer = null;
|
|
1517
1579
|
let messageTimer = null;
|
|
1518
1580
|
try {
|
|
1519
1581
|
nodeDataChannel.setSctpSettings({ sendBufferSize: 64 * 1024 });
|
|
1520
|
-
|
|
1521
|
-
|
|
1582
|
+
const config = { iceServers: [], maxMessageSize: DIRECT_CONSOLE_WIRE_CHUNK_BYTES };
|
|
1583
|
+
offerer = new nodeDataChannel.PeerConnection('console-direct-native-offerer', config);
|
|
1584
|
+
answerer = new nodeDataChannel.PeerConnection('console-direct-native-answerer', config);
|
|
1522
1585
|
offerer.onLocalDescription((sdp, type) => {
|
|
1523
1586
|
answerer.setRemoteDescription(sdp, type);
|
|
1524
1587
|
});
|
|
@@ -1552,11 +1615,16 @@ test('native node-datachannel buffered sends deliver the complete fragmented wir
|
|
|
1552
1615
|
await opened;
|
|
1553
1616
|
clearTimeout(openTimer);
|
|
1554
1617
|
openTimer = null;
|
|
1555
|
-
|
|
1556
|
-
|
|
1618
|
+
assert.match(offerer.localDescription().sdp, /a=max-message-size:16384(?:\r?\n|$)/);
|
|
1619
|
+
assert.match(answerer.localDescription().sdp, /a=max-message-size:16384(?:\r?\n|$)/);
|
|
1620
|
+
assert.throws(() => offerChannel.sendMessageBinary(Buffer.alloc(DIRECT_CONSOLE_WIRE_CHUNK_BYTES + 1)),
|
|
1621
|
+
/[Mm]essage.*large|[Ss]ize/);
|
|
1622
|
+
const expected = Buffer.alloc(consoleDirectContract.maxFrameMessageBytes);
|
|
1623
|
+
for (let n = 0; n < expected.length; n += 1) expected[n] = n % 251;
|
|
1557
1624
|
const chunks = encodeDirectConsoleWireMessage(expected, {
|
|
1558
1625
|
messageId: 77,
|
|
1559
|
-
kind: '
|
|
1626
|
+
kind: 'binary',
|
|
1627
|
+
maxMessageBytes: consoleDirectContract.maxFrameMessageBytes
|
|
1560
1628
|
});
|
|
1561
1629
|
assert.ok(chunks.length > 1);
|
|
1562
1630
|
let bufferedSends = 0;
|
|
@@ -1567,8 +1635,8 @@ test('native node-datachannel buffered sends deliver the complete fragmented wir
|
|
|
1567
1635
|
const message = await received;
|
|
1568
1636
|
clearTimeout(messageTimer);
|
|
1569
1637
|
messageTimer = null;
|
|
1570
|
-
assert.equal(message.kind, '
|
|
1571
|
-
assert.
|
|
1638
|
+
assert.equal(message.kind, 'binary');
|
|
1639
|
+
assert.deepEqual(Buffer.from(message.data), expected);
|
|
1572
1640
|
t.diagnostic(`native buffered sends=${bufferedSends}; complete bytes=${message.byteLength}; chunks=${chunks.length}`);
|
|
1573
1641
|
} finally {
|
|
1574
1642
|
if (openTimer) clearTimeout(openTimer);
|
|
@@ -479,7 +479,7 @@ function parseBorrowLifecycleFramePacket(data) {
|
|
|
479
479
|
}
|
|
480
480
|
}
|
|
481
481
|
|
|
482
|
-
test('
|
|
482
|
+
test('Hub and phone Wall borrowers follow Control without stopping or retaining each other', {
|
|
483
483
|
timeout: 20_000
|
|
484
484
|
}, async () => {
|
|
485
485
|
const {
|
|
@@ -563,7 +563,7 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
563
563
|
).toString('base64')
|
|
564
564
|
});
|
|
565
565
|
|
|
566
|
-
const connectBorrower = async label => {
|
|
566
|
+
const connectBorrower = async (label, options = {}) => {
|
|
567
567
|
const socket = new WebSocket(
|
|
568
568
|
`ws://127.0.0.1:${httpPort}/api/remote/frames/ws?devices=${encodeURIComponent(deviceId)}`,
|
|
569
569
|
{ headers: { Origin: baseUrl }, perMessageDeflate: false }
|
|
@@ -603,7 +603,8 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
603
603
|
maxWidth: 960,
|
|
604
604
|
maxHeight: 540,
|
|
605
605
|
quality: 55,
|
|
606
|
-
monitorIndex: 0
|
|
606
|
+
monitorIndex: 0,
|
|
607
|
+
...options
|
|
607
608
|
}));
|
|
608
609
|
return { socket, observed, subscribeJsonIndex };
|
|
609
610
|
};
|
|
@@ -799,6 +800,20 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
799
800
|
assert.equal(firstFrame.streamPurpose, 'control');
|
|
800
801
|
assert.equal(firstFrame.monitorIndex, 0);
|
|
801
802
|
|
|
803
|
+
const hubBorrower = await connectBorrower('desktop Hub', {
|
|
804
|
+
fps: 30, maxWidth: 1920, maxHeight: 1080
|
|
805
|
+
});
|
|
806
|
+
await waitForBorrowStart(hubBorrower.observed, initialControl.commandId,
|
|
807
|
+
hubBorrower.subscribeJsonIndex, 'desktop Hub borrows the same Control owner');
|
|
808
|
+
sendControlFrame(initialControl, 4, true);
|
|
809
|
+
await waitForExactReady(hubBorrower.observed, initialControl, 0, 'desktop Hub key readiness');
|
|
810
|
+
const otherMonitor = await connectBorrower('other monitor', { monitorIndex: 1 });
|
|
811
|
+
await waitForBorrowLifecycle(() => otherMonitor.observed.json.some(message =>
|
|
812
|
+
JSON.stringify(message).includes('CONTROL_CAPTURE_OWNS_DEVICE')),
|
|
813
|
+
'different monitor reports ownership conflict');
|
|
814
|
+
assert.equal(otherMonitor.observed.frames.length, 0, 'never substitute a different monitor');
|
|
815
|
+
otherMonitor.socket.close();
|
|
816
|
+
|
|
802
817
|
const firstStopCommandIndex = agentMessages.length;
|
|
803
818
|
const stopInitialPromise = postJson(
|
|
804
819
|
`/api/remote/devices/${encodeURIComponent(deviceId)}/live/stop`,
|
|
@@ -855,6 +870,8 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
855
870
|
const replacementFrameIndex = firstBorrower.observed.frames.length;
|
|
856
871
|
sendOpen(replacementControl);
|
|
857
872
|
sendControlFrame(replacementControl, 1, true);
|
|
873
|
+
await waitForBorrowStart(hubBorrower.observed, replacementControl.commandId,
|
|
874
|
+
0, 'desktop Hub follows the replacement generation');
|
|
858
875
|
const replacementBorrowStart = await waitForBorrowStart(
|
|
859
876
|
firstBorrower.observed,
|
|
860
877
|
replacementControl.commandId,
|
|
@@ -878,6 +895,8 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
878
895
|
await new Promise(resolveWait => setTimeout(resolveWait, 60));
|
|
879
896
|
assert.equal(firstBorrower.observed.frames.length, replacementFrameIndex);
|
|
880
897
|
sendControlFrame(replacementControl, 3, true);
|
|
898
|
+
await waitForExactReady(hubBorrower.observed, replacementControl, 0,
|
|
899
|
+
'desktop Hub replacement key readiness');
|
|
881
900
|
await waitForExactReady(
|
|
882
901
|
firstBorrower.observed,
|
|
883
902
|
replacementControl,
|
|
@@ -915,6 +934,10 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
915
934
|
stopCountBeforeBorrowerClose,
|
|
916
935
|
'Closing a read-only borrower must never stop the native Control owner.'
|
|
917
936
|
);
|
|
937
|
+
sendControlFrame(replacementControl, 4, true);
|
|
938
|
+
await waitForBorrowLifecycle(() => hubBorrower.observed.frames.some(frame =>
|
|
939
|
+
frame.commandId === replacementControl.commandId && frame.frameSeq === 4),
|
|
940
|
+
'desktop Hub keeps receiving after phone borrower closes');
|
|
918
941
|
|
|
919
942
|
const fallbackBorrower = await connectBorrower('fallback borrower');
|
|
920
943
|
await waitForBorrowStart(
|
|
@@ -924,7 +947,7 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
924
947
|
'fallback borrower Control binding'
|
|
925
948
|
);
|
|
926
949
|
const fallbackReadyIndex = fallbackBorrower.observed.json.length;
|
|
927
|
-
sendControlFrame(replacementControl,
|
|
950
|
+
sendControlFrame(replacementControl, 5, true);
|
|
928
951
|
await waitForExactReady(
|
|
929
952
|
fallbackBorrower.observed,
|
|
930
953
|
replacementControl,
|
|
@@ -984,6 +1007,11 @@ test('the browser Wall borrower follows exact Control key frames without owning
|
|
|
984
1007
|
'ordinary Wall browser binding after confirmed Control stop'
|
|
985
1008
|
);
|
|
986
1009
|
assert.equal(fallbackWallStart.started[0].presentationPurpose, '');
|
|
1010
|
+
await waitForBorrowLifecycle(() => hubBorrower.observed.json.some(message =>
|
|
1011
|
+
message.type === 'RemoteFrameLiveAutoStart'
|
|
1012
|
+
&& message.started?.some(item => item.commandId === fallbackWallCommand.message.commandId
|
|
1013
|
+
&& item.streamPurpose === 'wall' && item.readOnlyControlBorrow !== true)),
|
|
1014
|
+
'desktop Hub and phone both restore the ordinary Wall owner');
|
|
987
1015
|
} catch (error) {
|
|
988
1016
|
error.message += hubOutput.length > 0
|
|
989
1017
|
? `\nIsolated Hub output:\n${hubOutput.join('\n')}`
|