@livedesk/hub 0.1.23 → 0.1.25

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@livedesk/hub",
3
- "version": "0.1.23",
3
+ "version": "0.1.25",
4
4
  "description": "LiveDesk local Hub API and browser frame bridge",
5
5
  "type": "module",
6
6
  "main": "src/server.js",
@@ -0,0 +1,38 @@
1
+ export const LIVE_STREAM_MONITOR_INDEX_MIN = 0;
2
+ export const LIVE_STREAM_MONITOR_INDEX_MAX = 63;
3
+
4
+ export function parseExactLiveStreamMonitorIndex(value) {
5
+ return typeof value === 'number'
6
+ && Number.isInteger(value)
7
+ && value >= LIVE_STREAM_MONITOR_INDEX_MIN
8
+ && value <= LIVE_STREAM_MONITOR_INDEX_MAX
9
+ ? value
10
+ : null;
11
+ }
12
+
13
+ export function isReusedLiveStreamFrameReady(result, activeStream, expectedBinding) {
14
+ const expectedMonitorIndex = parseExactLiveStreamMonitorIndex(expectedBinding?.monitorIndex);
15
+ const activeMonitorIndex = parseExactLiveStreamMonitorIndex(activeStream?.monitorIndex);
16
+ const latestFrame = activeStream?.latestFrame;
17
+ const frameMonitorIndex = parseExactLiveStreamMonitorIndex(latestFrame?.monitorIndex);
18
+ const expectedStreamId = String(expectedBinding?.streamId || '');
19
+ const expectedCommandId = String(expectedBinding?.commandId || '');
20
+ const expectedCaptureGeneration = Number(expectedBinding?.captureGeneration || 0);
21
+
22
+ return result?.reused === true
23
+ && result?.ready === true
24
+ && Number(activeStream?.framesReceived || 0) > 0
25
+ && !!expectedStreamId
26
+ && String(activeStream?.streamId || '') === expectedStreamId
27
+ && !!expectedCommandId
28
+ && String(activeStream?.commandId || '') === expectedCommandId
29
+ && Number.isInteger(expectedCaptureGeneration)
30
+ && expectedCaptureGeneration > 0
31
+ && Number(activeStream?.captureGeneration || 0) === expectedCaptureGeneration
32
+ && latestFrame?.currentGenerationVerified === true
33
+ && String(latestFrame?.commandId || '') === expectedCommandId
34
+ && Number(latestFrame?.captureGeneration || 0) === expectedCaptureGeneration
35
+ && expectedMonitorIndex !== null
36
+ && activeMonitorIndex === expectedMonitorIndex
37
+ && frameMonitorIndex === expectedMonitorIndex;
38
+ }
package/src/remote-hub.js CHANGED
@@ -2,6 +2,7 @@ import net from 'net';
2
2
  import os from 'os';
3
3
  import crypto from 'crypto';
4
4
  import { createHubRelayControl } from './transport/relay-hub-control.js';
5
+ import { parseExactLiveStreamMonitorIndex } from './live-stream-monitor-contract.js';
5
6
 
6
7
  const DEFAULT_REMOTE_HUB_PORT = 5197;
7
8
  const DEFAULT_REMOTE_HUB_HOST = '0.0.0.0';
@@ -689,21 +690,39 @@ const SUPPORTED_AGENT_OPERATIONS = new Set([
689
690
  'logs.collect'
690
691
  ]);
691
692
 
692
- function normalizeAgentOperation(value) {
693
- const operation = safeString(value, 80).toLowerCase();
694
- return SUPPORTED_AGENT_OPERATIONS.has(operation) ? operation : '';
695
- }
696
-
697
- function normalizeRemoteInputEvent(value = {}) {
698
- const input = value && typeof value === 'object' ? value : {};
699
- const type = safeString(input.type || input.Type, 48);
693
+ function normalizeAgentOperation(value) {
694
+ const operation = safeString(value, 80).toLowerCase();
695
+ return SUPPORTED_AGENT_OPERATIONS.has(operation) ? operation : '';
696
+ }
697
+
698
+ const REMOTE_INPUT_MONITOR_KEYS = Object.freeze([
699
+ 'monitorIndex',
700
+ 'MonitorIndex',
701
+ 'screenIndex',
702
+ 'ScreenIndex',
703
+ 'displayIndex',
704
+ 'DisplayIndex'
705
+ ]);
706
+
707
+ function readRawRemoteInputMonitorIndex(input) {
708
+ for (const key of REMOTE_INPUT_MONITOR_KEYS) {
709
+ if (Object.prototype.hasOwnProperty.call(input, key)) {
710
+ return input[key];
711
+ }
712
+ }
713
+ return undefined;
714
+ }
715
+
716
+ function normalizeRemoteInputEvent(value = {}) {
717
+ const input = value && typeof value === 'object' ? value : {};
718
+ const type = safeString(input.type || input.Type, 48);
700
719
  const normalizedX = Number(input.normalizedX ?? input.NormalizedX);
701
720
  const normalizedY = Number(input.normalizedY ?? input.NormalizedY);
702
721
  const deltaX = Number(input.deltaX ?? input.DeltaX);
703
722
  const deltaY = Number(input.deltaY ?? input.DeltaY);
704
723
  const keyCode = Number(input.keyCode ?? input.KeyCode);
705
724
  const location = Number(input.location ?? input.Location);
706
- const monitorIndex = Number(input.monitorIndex ?? input.MonitorIndex ?? input.screenIndex ?? input.ScreenIndex ?? input.displayIndex ?? input.DisplayIndex);
725
+ const monitorIndex = parseExactLiveStreamMonitorIndex(readRawRemoteInputMonitorIndex(input));
707
726
  const inputSeq = Number(input.inputSeq ?? input.InputSeq);
708
727
  const issuedAtEpochMs = Number(input.issuedAtEpochMs ?? input.IssuedAtEpochMs);
709
728
  const hubReceivedAtEpochMs = Number(input.hubReceivedAtEpochMs ?? input.HubReceivedAtEpochMs);
@@ -719,7 +738,7 @@ function normalizeRemoteInputEvent(value = {}) {
719
738
  type,
720
739
  // A missing monitor is not equivalent to the primary display. Control
721
740
  // input must prove the exact monitor owned by its capture generation.
722
- monitorIndex: Number.isFinite(monitorIndex) ? normalizeMonitorIndex(monitorIndex) : null,
741
+ monitorIndex,
723
742
  normalizedX: Number.isFinite(normalizedX) ? Math.max(0, Math.min(1, normalizedX)) : undefined,
724
743
  normalizedY: Number.isFinite(normalizedY) ? Math.max(0, Math.min(1, normalizedY)) : undefined,
725
744
  button: safeString(input.button || input.Button, 24),
@@ -3983,6 +4002,34 @@ export function createRemoteHub(options = {}) {
3983
4002
  });
3984
4003
  return null;
3985
4004
  }
4005
+ const pendingMonitorIndex = parseExactLiveStreamMonitorIndex(pending.monitorIndex);
4006
+ const messageMonitorIndex = parseExactLiveStreamMonitorIndex(message.monitorIndex);
4007
+ if (pendingMonitorIndex === null || messageMonitorIndex === null) {
4008
+ emitRemoteEvent('RemoteLiveStreamOpenIgnored', device, {
4009
+ reason: 'invalid-pending-monitor-metadata',
4010
+ streamId: streamState.streamId,
4011
+ commandId,
4012
+ captureGeneration: messageCaptureGeneration,
4013
+ pendingCaptureGeneration,
4014
+ monitorIndex: messageMonitorIndex,
4015
+ pendingMonitorIndex,
4016
+ transport
4017
+ });
4018
+ return null;
4019
+ }
4020
+ if (messageMonitorIndex !== pendingMonitorIndex) {
4021
+ emitRemoteEvent('RemoteLiveStreamOpenIgnored', device, {
4022
+ reason: 'stale-pending-monitor',
4023
+ streamId: streamState.streamId,
4024
+ commandId,
4025
+ captureGeneration: messageCaptureGeneration,
4026
+ pendingCaptureGeneration,
4027
+ monitorIndex: messageMonitorIndex,
4028
+ pendingMonitorIndex,
4029
+ transport
4030
+ });
4031
+ return null;
4032
+ }
3986
4033
 
3987
4034
  const previousCommandId = safeString(streamState.commandId, 128);
3988
4035
  const transfer = buildRemoteFrameTransferDescriptorFromMessage(
@@ -4011,9 +4058,7 @@ export function createRemoteHub(options = {}) {
4011
4058
  height: Number.isFinite(Number(message.height)) ? Number(message.height) : Number(pending.height || 0),
4012
4059
  sourceWidth: Number.isFinite(Number(message.sourceWidth)) ? Number(message.sourceWidth) : Number(pending.sourceWidth || 0),
4013
4060
  sourceHeight: Number.isFinite(Number(message.sourceHeight)) ? Number(message.sourceHeight) : Number(pending.sourceHeight || 0),
4014
- monitorIndex: Number.isFinite(Number(message.monitorIndex))
4015
- ? normalizeMonitorIndex(message.monitorIndex)
4016
- : Number(pending.monitorIndex || 0),
4061
+ monitorIndex: pendingMonitorIndex,
4017
4062
  monitorCount: Number.isFinite(Number(message.monitorCount))
4018
4063
  ? clampNumber(message.monitorCount, 1, 64, 1)
4019
4064
  : Number(pending.monitorCount || 1),
@@ -4106,6 +4151,37 @@ export function createRemoteHub(options = {}) {
4106
4151
  return true;
4107
4152
  }
4108
4153
 
4154
+ const activeMonitorIndex = parseExactLiveStreamMonitorIndex(streamState.monitorIndex);
4155
+ const messageMonitorIndex = parseExactLiveStreamMonitorIndex(message.monitorIndex);
4156
+ if (activeMonitorIndex === null || messageMonitorIndex === null) {
4157
+ emitRemoteEvent('RemoteLiveStreamOpenIgnored', device, {
4158
+ reason: 'invalid-live-stream-monitor-metadata',
4159
+ streamId,
4160
+ commandId,
4161
+ activeCommandId,
4162
+ captureGeneration: Number(message.captureGeneration || 0),
4163
+ activeCaptureGeneration: Number(streamState.captureGeneration || 0),
4164
+ monitorIndex: messageMonitorIndex,
4165
+ activeMonitorIndex,
4166
+ transport
4167
+ });
4168
+ return false;
4169
+ }
4170
+ if (messageMonitorIndex !== activeMonitorIndex) {
4171
+ emitRemoteEvent('RemoteLiveStreamOpenIgnored', device, {
4172
+ reason: 'stale-live-stream-monitor',
4173
+ streamId,
4174
+ commandId,
4175
+ activeCommandId,
4176
+ captureGeneration: Number(message.captureGeneration || 0),
4177
+ activeCaptureGeneration: Number(streamState.captureGeneration || 0),
4178
+ monitorIndex: messageMonitorIndex,
4179
+ activeMonitorIndex,
4180
+ transport
4181
+ });
4182
+ return false;
4183
+ }
4184
+
4109
4185
  const transfer = buildRemoteFrameTransferDescriptorFromMessage(message, streamState.mode || DEFAULT_REMOTE_FRAME_MODE);
4110
4186
  const openedAt = safeString(message.openedAt, 80) || device.lastSeenAt || new Date().toISOString();
4111
4187
  const nextStreamState = {
@@ -4126,7 +4202,7 @@ export function createRemoteHub(options = {}) {
4126
4202
  height: Number.isFinite(Number(message.height)) ? Number(message.height) : Number(streamState.height || 0),
4127
4203
  sourceWidth: Number.isFinite(Number(message.sourceWidth)) ? Number(message.sourceWidth) : Number(streamState.sourceWidth || 0),
4128
4204
  sourceHeight: Number.isFinite(Number(message.sourceHeight)) ? Number(message.sourceHeight) : Number(streamState.sourceHeight || 0),
4129
- monitorIndex: Number.isFinite(Number(message.monitorIndex)) ? normalizeMonitorIndex(message.monitorIndex) : Number(streamState.monitorIndex || 0),
4205
+ monitorIndex: activeMonitorIndex,
4130
4206
  monitorCount: Number.isFinite(Number(message.monitorCount)) ? clampNumber(message.monitorCount, 1, 64, 1) : Number(streamState.monitorCount || 1),
4131
4207
  streamPurpose: safeString(message.streamPurpose || streamState.streamPurpose, 24) || 'wall',
4132
4208
  captureGeneration: Number.isSafeInteger(Number(message.captureGeneration)) && Number(message.captureGeneration) > 0
@@ -4204,8 +4280,40 @@ export function createRemoteHub(options = {}) {
4204
4280
  });
4205
4281
  return false;
4206
4282
  }
4207
-
4208
- const byteLength = normalizeFrameByteLength(framePayload, frameData);
4283
+ const activeMonitorIndex = parseExactLiveStreamMonitorIndex(streamState.monitorIndex);
4284
+ const frameMonitorIndex = parseExactLiveStreamMonitorIndex(message.monitorIndex);
4285
+ if (activeMonitorIndex === null || frameMonitorIndex === null) {
4286
+ device.counters.liveFramesDropped += 1;
4287
+ emitRemoteEvent('RemoteFrameDropped', device, {
4288
+ reason: 'invalid-live-stream-monitor-frame-metadata',
4289
+ streamId,
4290
+ commandId,
4291
+ captureGeneration: frameCaptureGeneration,
4292
+ activeCaptureGeneration,
4293
+ monitorIndex: frameMonitorIndex,
4294
+ activeMonitorIndex,
4295
+ frameSeq: Number.isFinite(frameSeq) ? frameSeq : null,
4296
+ transport
4297
+ });
4298
+ return false;
4299
+ }
4300
+ if (frameMonitorIndex !== activeMonitorIndex) {
4301
+ device.counters.liveFramesDropped += 1;
4302
+ emitRemoteEvent('RemoteFrameDropped', device, {
4303
+ reason: 'stale-live-stream-monitor-frame',
4304
+ streamId,
4305
+ commandId,
4306
+ captureGeneration: frameCaptureGeneration,
4307
+ activeCaptureGeneration,
4308
+ monitorIndex: frameMonitorIndex,
4309
+ activeMonitorIndex,
4310
+ frameSeq: Number.isFinite(frameSeq) ? frameSeq : null,
4311
+ transport
4312
+ });
4313
+ return false;
4314
+ }
4315
+
4316
+ const byteLength = normalizeFrameByteLength(framePayload, frameData);
4209
4317
  if ((!Buffer.isBuffer(framePayload) && !frameData)
4210
4318
  || byteLength > MAX_STREAM_BINARY_BYTES
4211
4319
  || !Number.isFinite(frameSeq)) {
@@ -4242,7 +4350,8 @@ export function createRemoteHub(options = {}) {
4242
4350
  const currentGenerationVerified = (!activeCommandId || commandId === activeCommandId)
4243
4351
  && (activeCaptureGeneration <= 0
4244
4352
  || (frameCaptureGeneration > 0
4245
- && frameCaptureGeneration === activeCaptureGeneration));
4353
+ && frameCaptureGeneration === activeCaptureGeneration))
4354
+ && frameMonitorIndex === activeMonitorIndex;
4246
4355
  const readyFrameReceivedBeforeFrame = streamState.readyFrameReceived === true;
4247
4356
  const captureTimingAvailable = hasFrameCaptureTiming(message);
4248
4357
  const videoTransport = transport === 'udp-p2p'
@@ -4323,7 +4432,7 @@ export function createRemoteHub(options = {}) {
4323
4432
  udpIncompleteFrames: Number.isFinite(Number(message.udpIncompleteFrames)) ? Number(message.udpIncompleteFrames) : 0,
4324
4433
  hardwareEncoder: safeString(message.hardwareEncoder, 80),
4325
4434
  platformProfile: safeString(message.platformProfile, 80),
4326
- monitorIndex: Number.isFinite(Number(message.monitorIndex)) ? normalizeMonitorIndex(message.monitorIndex) : Number(streamState.monitorIndex || 0),
4435
+ monitorIndex: activeMonitorIndex,
4327
4436
  monitorCount: Number.isFinite(Number(message.monitorCount)) ? clampNumber(message.monitorCount, 1, 64, 1) : Number(streamState.monitorCount || 1),
4328
4437
  transferProtocol: transfer.transferProtocol,
4329
4438
  transferProtocolVersion: transfer.transferProtocolVersion,
@@ -4402,7 +4511,6 @@ export function createRemoteHub(options = {}) {
4402
4511
  streamState.height = device.latestLiveFrame.height;
4403
4512
  streamState.sourceWidth = device.latestLiveFrame.sourceWidth;
4404
4513
  streamState.sourceHeight = device.latestLiveFrame.sourceHeight;
4405
- streamState.monitorIndex = device.latestLiveFrame.monitorIndex;
4406
4514
  streamState.monitorCount = device.latestLiveFrame.monitorCount;
4407
4515
  ensureDeviceLiveStreams(device).set(streamId, streamState);
4408
4516
  device.counters.liveFramesReceived += 1;
@@ -6457,8 +6565,12 @@ export function createRemoteHub(options = {}) {
6457
6565
  }
6458
6566
  const activeCaptureGeneration = Number(activeLiveStream.captureGeneration || 0);
6459
6567
  const frameCaptureGeneration = Number(latestFrame.captureGeneration || 0);
6460
- return activeCaptureGeneration <= 0
6461
- || (frameCaptureGeneration > 0 && frameCaptureGeneration === activeCaptureGeneration);
6568
+ const activeMonitorIndex = parseExactLiveStreamMonitorIndex(activeLiveStream.monitorIndex);
6569
+ const frameMonitorIndex = parseExactLiveStreamMonitorIndex(latestFrame.monitorIndex);
6570
+ return activeMonitorIndex !== null
6571
+ && frameMonitorIndex === activeMonitorIndex
6572
+ && (activeCaptureGeneration <= 0
6573
+ || (frameCaptureGeneration > 0 && frameCaptureGeneration === activeCaptureGeneration));
6462
6574
  }
6463
6575
 
6464
6576
  function getLiveStreamFreshWindowMs(activeLiveStream) {
package/src/server.js CHANGED
@@ -7,8 +7,9 @@ import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, wri
7
7
  import { dirname, resolve } from 'node:path';
8
8
  import { fileURLToPath } from 'node:url';
9
9
  import os from 'node:os';
10
- import { WebSocketServer } from 'ws';
10
+ import { WebSocketServer } from 'ws';
11
11
  import { createRemoteHub } from './remote-hub.js';
12
+ import { isReusedLiveStreamFrameReady } from './live-stream-monitor-contract.js';
12
13
  import { buildMode4AtlasSessionKey, Mode4AtlasPool, planMode4AtlasInputTransitions } from './mode4-atlas-pool.js';
13
14
  import { resolveMode4AtlasTileSize } from './mode4-atlas-sizing.js';
14
15
  import { createHubFilesystem } from './filesystem/hub-filesystem.js';
@@ -1976,12 +1977,7 @@ function startFrameSubscriptionLive(ws, reason = 'subscribe', onlyDeviceId = '',
1976
1977
  readySent: false
1977
1978
  };
1978
1979
  const activeStream = device?.activeLiveStream;
1979
- const reusedFrameReady = result.reused === true
1980
- && result.ready === true
1981
- && Number(result.framesReceived ?? activeStream?.framesReceived ?? 0) > 0
1982
- && String(activeStream?.streamId || expectedBinding.streamId) === expectedBinding.streamId
1983
- && String(activeStream?.commandId || expectedBinding.commandId) === expectedBinding.commandId
1984
- && Number(activeStream?.captureGeneration || expectedBinding.captureGeneration) === expectedBinding.captureGeneration;
1980
+ const reusedFrameReady = isReusedLiveStreamFrameReady(result, activeStream, expectedBinding);
1985
1981
  expectedBinding.readySent = reusedFrameReady;
1986
1982
  ws.liveDeskStreamIdsByDeviceId.set(deviceId, result.streamId);
1987
1983
  ws.liveDeskExpectedStreamBindingsByDeviceId.set(deviceId, expectedBinding);