@codingfrees/uap-device-control 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/uap-device-control.mjs +95 -21
- package/package.json +1 -1
|
@@ -625,7 +625,7 @@ import WebSocket from "ws";
|
|
|
625
625
|
// package.json
|
|
626
626
|
var package_default = {
|
|
627
627
|
name: "@codingfrees/uap-device-control",
|
|
628
|
-
version: "0.2.
|
|
628
|
+
version: "0.2.1",
|
|
629
629
|
description: "Cross-platform outbound UAP Device Control client",
|
|
630
630
|
type: "module",
|
|
631
631
|
bin: {
|
|
@@ -651,8 +651,9 @@ var package_default = {
|
|
|
651
651
|
};
|
|
652
652
|
|
|
653
653
|
// ../device-agent/src/engine.ts
|
|
654
|
-
import { spawn } from "node:child_process";
|
|
655
|
-
import {
|
|
654
|
+
import { execFile, spawn } from "node:child_process";
|
|
655
|
+
import { promisify } from "node:util";
|
|
656
|
+
import { cp, mkdir as mkdir3, open, readdir, realpath, rename as rename2, rm as rm2, stat, writeFile as writeFile3 } from "node:fs/promises";
|
|
656
657
|
import { realpathSync } from "node:fs";
|
|
657
658
|
import os2 from "node:os";
|
|
658
659
|
import path3 from "node:path";
|
|
@@ -886,9 +887,29 @@ var DeviceAgentEngine = class {
|
|
|
886
887
|
return { path: relative, kind: value.isDirectory() ? "directory" : "file", size: value.size, modifiedAt: value.mtimeMs, createdAt: value.birthtimeMs };
|
|
887
888
|
}
|
|
888
889
|
if (capability === "filesystem.read" || capability === "filesystem.download" || capability === "filesystem.preview") {
|
|
889
|
-
const
|
|
890
|
-
|
|
891
|
-
|
|
890
|
+
const info = await stat(target);
|
|
891
|
+
const chunked = input.offsetBytes !== void 0 || input.lengthBytes !== void 0;
|
|
892
|
+
if (chunked && capability === "filesystem.preview") throw new TypeError("preview does not support byte ranges");
|
|
893
|
+
const limit = Math.min(this.#maxTransfer, 2 * 1024 * 1024);
|
|
894
|
+
if (!chunked && capability !== "filesystem.preview" && info.size > limit) throw new TypeError("file is too large for a single response; use offsetBytes and lengthBytes (up to 1 MiB per chunk)");
|
|
895
|
+
const offset = chunked ? bounded(Number(input.offsetBytes ?? 0), 0, Number.MAX_SAFE_INTEGER, "file offset") : 0;
|
|
896
|
+
const length = chunked ? bounded(Number(input.lengthBytes ?? 1024 * 1024), 1, Math.min(this.#maxTransfer, 1024 * 1024), "chunk size") : capability === "filesystem.preview" ? Math.min(info.size, this.#maxTransfer, 64 * 1024) : info.size;
|
|
897
|
+
if (offset > info.size) throw new TypeError("file offset exceeds size");
|
|
898
|
+
const handle = await open(target, "r");
|
|
899
|
+
let data;
|
|
900
|
+
try {
|
|
901
|
+
const buffer = Buffer.allocUnsafe(Math.min(length, info.size - offset));
|
|
902
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, offset);
|
|
903
|
+
data = buffer.subarray(0, bytesRead);
|
|
904
|
+
} finally {
|
|
905
|
+
await handle.close();
|
|
906
|
+
}
|
|
907
|
+
return {
|
|
908
|
+
contentBase64: data.toString("base64"),
|
|
909
|
+
size: data.byteLength,
|
|
910
|
+
...chunked ? { totalSize: info.size, offsetBytes: offset, nextOffsetBytes: offset + data.byteLength, eof: offset + data.byteLength >= info.size } : {},
|
|
911
|
+
...capability === "filesystem.preview" ? { totalSize: info.size, preview: data.toString("utf8") } : {}
|
|
912
|
+
};
|
|
892
913
|
}
|
|
893
914
|
if (root2.readOnly) throw new TypeError("device root is read only");
|
|
894
915
|
if (capability === "filesystem.write" || capability === "filesystem.upload") {
|
|
@@ -939,7 +960,13 @@ var DeviceAgentEngine = class {
|
|
|
939
960
|
}
|
|
940
961
|
async #process(capability, input, workspaceId) {
|
|
941
962
|
this.#prune();
|
|
942
|
-
if (capability === "process.list")
|
|
963
|
+
if (capability === "process.list") {
|
|
964
|
+
if (input.includeSystem === true) {
|
|
965
|
+
if (this.options.systemProcessInventory === false) throw new TypeError("system process inventory is disabled");
|
|
966
|
+
return { scope: "system", processes: await systemProcessInventory() };
|
|
967
|
+
}
|
|
968
|
+
return { scope: "workspace", processes: [...this.#processes.values()].filter((value2) => value2.workspaceId === workspaceId).map(summary) };
|
|
969
|
+
}
|
|
943
970
|
if (capability === "process.start") {
|
|
944
971
|
if (this.#running(this.#processes) >= this.#maxProcesses) throw new TypeError("process limit reached");
|
|
945
972
|
const value2 = this.#spawn(input, false, workspaceId);
|
|
@@ -1360,13 +1387,33 @@ function environment(value) {
|
|
|
1360
1387
|
}
|
|
1361
1388
|
return result;
|
|
1362
1389
|
}
|
|
1390
|
+
var runInventory = promisify(execFile);
|
|
1391
|
+
async function systemProcessInventory() {
|
|
1392
|
+
const windows = process.platform === "win32";
|
|
1393
|
+
const { stdout } = await runInventory(windows ? "tasklist.exe" : "ps", windows ? ["/FO", "CSV", "/NH"] : ["-eo", "pid=,comm=,rss=,pcpu="], { timeout: 5e3, maxBuffer: 512 * 1024, windowsHide: true });
|
|
1394
|
+
const records = [];
|
|
1395
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
1396
|
+
if (windows) {
|
|
1397
|
+
const match = /^"((?:[^"]|"")*)","(\d+)"/.exec(line);
|
|
1398
|
+
if (match) {
|
|
1399
|
+
const rawMemory = /,"([\d,.\s]+)\s*K"\s*$/.exec(line)?.[1];
|
|
1400
|
+
const memoryKb = rawMemory ? Number(rawMemory.replace(/\D/g, "")) : void 0;
|
|
1401
|
+
records.push({ pid: Number(match[2]), name: match[1].replaceAll('""', '"').slice(0, 128), ...memoryKb !== void 0 ? { memoryKb } : {} });
|
|
1402
|
+
}
|
|
1403
|
+
} else {
|
|
1404
|
+
const match = /^\s*(\d+)\s+(\S+)\s+(\d+)\s+([\d.]+)/.exec(line);
|
|
1405
|
+
if (match) records.push({ pid: Number(match[1]), name: match[2].slice(0, 128), memoryKb: Number(match[3]), cpuPercent: Number(match[4]) });
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
return records.sort((left, right) => (right.memoryKb ?? 0) - (left.memoryKb ?? 0)).slice(0, 200);
|
|
1409
|
+
}
|
|
1363
1410
|
var defaultShell = () => process.platform === "win32" ? "powershell.exe" : "/bin/sh";
|
|
1364
1411
|
var defaultExecutables = () => process.platform === "win32" ? ["powershell.exe", "cmd.exe"] : ["/bin/sh", "/bin/bash"];
|
|
1365
1412
|
|
|
1366
1413
|
// ../device-agent/src/native-desktop.ts
|
|
1367
|
-
import { execFile } from "node:child_process";
|
|
1368
|
-
import { promisify } from "node:util";
|
|
1369
|
-
var execute =
|
|
1414
|
+
import { execFile as execFile2 } from "node:child_process";
|
|
1415
|
+
import { promisify as promisify2 } from "node:util";
|
|
1416
|
+
var execute = promisify2(execFile2);
|
|
1370
1417
|
async function createNativeDesktopAdapter(platform = process.platform, run = execute) {
|
|
1371
1418
|
if (platform === "win32") return windowsAdapter(run);
|
|
1372
1419
|
if (platform === "linux") {
|
|
@@ -1622,11 +1669,11 @@ var WINDOWS_CLIPBOARD_READ = "Add-Type -AssemblyName System.Windows.Forms;[Conso
|
|
|
1622
1669
|
var WINDOWS_CLIPBOARD_WRITE = "param($text) Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Clipboard]::SetText($text)";
|
|
1623
1670
|
|
|
1624
1671
|
// ../device-agent/src/native-services.ts
|
|
1625
|
-
import { execFile as
|
|
1672
|
+
import { execFile as execFile3 } from "node:child_process";
|
|
1626
1673
|
import { realpath as realpath2, stat as stat2 } from "node:fs/promises";
|
|
1627
1674
|
import { win32 } from "node:path";
|
|
1628
|
-
import { promisify as
|
|
1629
|
-
var execute2 =
|
|
1675
|
+
import { promisify as promisify3 } from "node:util";
|
|
1676
|
+
var execute2 = promisify3(execFile3);
|
|
1630
1677
|
async function createNativeServiceAdapter(options = {}) {
|
|
1631
1678
|
const platform = options.platform ?? process.platform;
|
|
1632
1679
|
const environment2 = options.environment ?? process.env;
|
|
@@ -1790,7 +1837,7 @@ function optionalBrowserBridgeFailure(error) {
|
|
|
1790
1837
|
if (!error || typeof error !== "object" || !("code" in error)) return false;
|
|
1791
1838
|
return ["EADDRINUSE", "EACCES", "EAFNOSUPPORT", "EPROTONOSUPPORT"].includes(String(error.code));
|
|
1792
1839
|
}
|
|
1793
|
-
async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, signal, log, socketFactory = defaultSocketFactory) {
|
|
1840
|
+
async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, signal, log, socketFactory = defaultSocketFactory, healthIntervalMs = 45e3) {
|
|
1794
1841
|
const deviceId = engine.options.deviceId;
|
|
1795
1842
|
const socket = await authorizedSocket(origin, stateDirectory, fetcher, socketFactory, signal, log, deviceId);
|
|
1796
1843
|
const inFlight = /* @__PURE__ */ new Set();
|
|
@@ -1818,6 +1865,9 @@ async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, si
|
|
|
1818
1865
|
try {
|
|
1819
1866
|
await new Promise((resolve, reject) => {
|
|
1820
1867
|
let connected = false;
|
|
1868
|
+
let probeInProgress = false;
|
|
1869
|
+
let probeFailures = 0;
|
|
1870
|
+
let healthTimer;
|
|
1821
1871
|
const fail = (error) => {
|
|
1822
1872
|
for (const pending of acknowledgements.values()) pending.reject(error);
|
|
1823
1873
|
acknowledgements.clear();
|
|
@@ -1829,6 +1879,26 @@ async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, si
|
|
|
1829
1879
|
if (value.type === "connected") {
|
|
1830
1880
|
connected = true;
|
|
1831
1881
|
log?.({ event: "remote.session", status: "connected", deviceId, agentVersion: UAP_DEVICE_CONTROL_AGENT_VERSION });
|
|
1882
|
+
if (!healthTimer) {
|
|
1883
|
+
healthTimer = setInterval(() => {
|
|
1884
|
+
if (probeInProgress || signal?.aborted) return;
|
|
1885
|
+
probeInProgress = true;
|
|
1886
|
+
void remoteStatus(origin, stateDirectory, deviceId, fetcher).then((state) => {
|
|
1887
|
+
if (state.online) return void (probeFailures = 0);
|
|
1888
|
+
log?.({ event: "remote.session", status: "broker-session-lost", deviceId });
|
|
1889
|
+
fail(new RemoteUnavailableError(void 0, "/device-control/remote/status", "transport"));
|
|
1890
|
+
socket.close(1012, "remote session expired");
|
|
1891
|
+
}).catch(() => {
|
|
1892
|
+
if (++probeFailures < 2) return;
|
|
1893
|
+
log?.({ event: "remote.session", status: "health-unavailable", deviceId });
|
|
1894
|
+
fail(new RemoteUnavailableError(void 0, "/device-control/remote/status", "transport"));
|
|
1895
|
+
socket.close(1012, "remote health unavailable");
|
|
1896
|
+
}).finally(() => {
|
|
1897
|
+
probeInProgress = false;
|
|
1898
|
+
});
|
|
1899
|
+
}, healthIntervalMs);
|
|
1900
|
+
healthTimer.unref();
|
|
1901
|
+
}
|
|
1832
1902
|
return;
|
|
1833
1903
|
}
|
|
1834
1904
|
if (!connected) throw new TypeError("remote socket sent data before connection acknowledgement");
|
|
@@ -1862,9 +1932,13 @@ async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, si
|
|
|
1862
1932
|
socket.close(1002, "invalid message");
|
|
1863
1933
|
}
|
|
1864
1934
|
};
|
|
1865
|
-
const onClose = () =>
|
|
1935
|
+
const onClose = () => {
|
|
1936
|
+
if (healthTimer) clearInterval(healthTimer);
|
|
1937
|
+
signal?.aborted ? resolve() : fail(resultFailure instanceof Error ? resultFailure : new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1938
|
+
};
|
|
1866
1939
|
const onError = () => fail(new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1867
1940
|
const onAbort = () => {
|
|
1941
|
+
if (healthTimer) clearInterval(healthTimer);
|
|
1868
1942
|
socket.close(1e3, "client stopped");
|
|
1869
1943
|
resolve();
|
|
1870
1944
|
};
|
|
@@ -1882,7 +1956,7 @@ function defaultSocketFactory(url, token) {
|
|
|
1882
1956
|
return new WebSocket(url, { headers: { authorization: "Bearer " + token } });
|
|
1883
1957
|
}
|
|
1884
1958
|
async function authorizedSocket(origin, stateDirectory, fetcher, factory, signal, log, deviceId) {
|
|
1885
|
-
const
|
|
1959
|
+
const open2 = async (refresh = false) => {
|
|
1886
1960
|
const current = await accessToken(stateDirectory, fetcher, refresh);
|
|
1887
1961
|
const url = new URL("/device-control/remote/socket", canonicalOrigin2(origin));
|
|
1888
1962
|
url.searchParams.set("deviceId", deviceId);
|
|
@@ -1890,11 +1964,11 @@ async function authorizedSocket(origin, stateDirectory, fetcher, factory, signal
|
|
|
1890
1964
|
return await socketHandshake(factory(url.href, current.token), signal);
|
|
1891
1965
|
};
|
|
1892
1966
|
try {
|
|
1893
|
-
return await
|
|
1967
|
+
return await open2();
|
|
1894
1968
|
} catch (error) {
|
|
1895
1969
|
if (!(error instanceof RemoteAuthorizationError)) throw error;
|
|
1896
1970
|
log?.({ event: "remote.session", status: "reauthorizing", deviceId, httpStatus: error.httpStatus, pathname: error.pathname });
|
|
1897
|
-
return
|
|
1971
|
+
return open2(true);
|
|
1898
1972
|
}
|
|
1899
1973
|
}
|
|
1900
1974
|
function socketHandshake(socket, signal) {
|
|
@@ -2069,9 +2143,9 @@ import os4 from "node:os";
|
|
|
2069
2143
|
import path5 from "node:path";
|
|
2070
2144
|
import { chmod, copyFile, mkdir as mkdir4, rename as rename3, rm as rm3, writeFile as writeFile4 } from "node:fs/promises";
|
|
2071
2145
|
import { existsSync } from "node:fs";
|
|
2072
|
-
import { execFile as
|
|
2073
|
-
import { promisify as
|
|
2074
|
-
var execute3 =
|
|
2146
|
+
import { execFile as execFile4 } from "node:child_process";
|
|
2147
|
+
import { promisify as promisify4 } from "node:util";
|
|
2148
|
+
var execute3 = promisify4(execFile4);
|
|
2075
2149
|
async function installRemoteService(origin, stateDirectory) {
|
|
2076
2150
|
if (await runnerV2Installed()) throw new TypeError("Runner V2 is installed; use its Device Control provider instead of installing a duplicate Remote service");
|
|
2077
2151
|
const command = process.execPath;
|