@codingfrees/uap-device-control 0.1.22 → 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 +216 -64
- package/package.json +5 -2
|
@@ -620,11 +620,12 @@ function secureEqual(left, right) {
|
|
|
620
620
|
// src/remote.ts
|
|
621
621
|
import os3 from "node:os";
|
|
622
622
|
import path4 from "node:path";
|
|
623
|
+
import WebSocket from "ws";
|
|
623
624
|
|
|
624
625
|
// package.json
|
|
625
626
|
var package_default = {
|
|
626
627
|
name: "@codingfrees/uap-device-control",
|
|
627
|
-
version: "0.1
|
|
628
|
+
version: "0.2.1",
|
|
628
629
|
description: "Cross-platform outbound UAP Device Control client",
|
|
629
630
|
type: "module",
|
|
630
631
|
bin: {
|
|
@@ -636,19 +637,23 @@ var package_default = {
|
|
|
636
637
|
engines: {
|
|
637
638
|
node: ">=26.8.1 <27"
|
|
638
639
|
},
|
|
640
|
+
dependencies: {
|
|
641
|
+
ws: "8.22.0"
|
|
642
|
+
},
|
|
639
643
|
publishConfig: {
|
|
640
644
|
access: "public"
|
|
641
645
|
},
|
|
642
646
|
scripts: {
|
|
643
|
-
build: 'pnpm exec esbuild src/bin.ts --bundle --platform=node --format=esm --target=node26 --outfile=dist/uap-device-control.mjs --banner:js="#!/usr/bin/env node"',
|
|
647
|
+
build: 'pnpm exec esbuild src/bin.ts --bundle --packages=external --platform=node --format=esm --target=node26 --outfile=dist/uap-device-control.mjs --banner:js="#!/usr/bin/env node"',
|
|
644
648
|
prepack: "pnpm run build",
|
|
645
649
|
"publish:public": "node scripts/publish.mjs"
|
|
646
650
|
}
|
|
647
651
|
};
|
|
648
652
|
|
|
649
653
|
// ../device-agent/src/engine.ts
|
|
650
|
-
import { spawn } from "node:child_process";
|
|
651
|
-
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";
|
|
652
657
|
import { realpathSync } from "node:fs";
|
|
653
658
|
import os2 from "node:os";
|
|
654
659
|
import path3 from "node:path";
|
|
@@ -882,9 +887,29 @@ var DeviceAgentEngine = class {
|
|
|
882
887
|
return { path: relative, kind: value.isDirectory() ? "directory" : "file", size: value.size, modifiedAt: value.mtimeMs, createdAt: value.birthtimeMs };
|
|
883
888
|
}
|
|
884
889
|
if (capability === "filesystem.read" || capability === "filesystem.download" || capability === "filesystem.preview") {
|
|
885
|
-
const
|
|
886
|
-
|
|
887
|
-
|
|
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
|
+
};
|
|
888
913
|
}
|
|
889
914
|
if (root2.readOnly) throw new TypeError("device root is read only");
|
|
890
915
|
if (capability === "filesystem.write" || capability === "filesystem.upload") {
|
|
@@ -935,7 +960,13 @@ var DeviceAgentEngine = class {
|
|
|
935
960
|
}
|
|
936
961
|
async #process(capability, input, workspaceId) {
|
|
937
962
|
this.#prune();
|
|
938
|
-
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
|
+
}
|
|
939
970
|
if (capability === "process.start") {
|
|
940
971
|
if (this.#running(this.#processes) >= this.#maxProcesses) throw new TypeError("process limit reached");
|
|
941
972
|
const value2 = this.#spawn(input, false, workspaceId);
|
|
@@ -1356,13 +1387,33 @@ function environment(value) {
|
|
|
1356
1387
|
}
|
|
1357
1388
|
return result;
|
|
1358
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
|
+
}
|
|
1359
1410
|
var defaultShell = () => process.platform === "win32" ? "powershell.exe" : "/bin/sh";
|
|
1360
1411
|
var defaultExecutables = () => process.platform === "win32" ? ["powershell.exe", "cmd.exe"] : ["/bin/sh", "/bin/bash"];
|
|
1361
1412
|
|
|
1362
1413
|
// ../device-agent/src/native-desktop.ts
|
|
1363
|
-
import { execFile } from "node:child_process";
|
|
1364
|
-
import { promisify } from "node:util";
|
|
1365
|
-
var execute =
|
|
1414
|
+
import { execFile as execFile2 } from "node:child_process";
|
|
1415
|
+
import { promisify as promisify2 } from "node:util";
|
|
1416
|
+
var execute = promisify2(execFile2);
|
|
1366
1417
|
async function createNativeDesktopAdapter(platform = process.platform, run = execute) {
|
|
1367
1418
|
if (platform === "win32") return windowsAdapter(run);
|
|
1368
1419
|
if (platform === "linux") {
|
|
@@ -1618,11 +1669,11 @@ var WINDOWS_CLIPBOARD_READ = "Add-Type -AssemblyName System.Windows.Forms;[Conso
|
|
|
1618
1669
|
var WINDOWS_CLIPBOARD_WRITE = "param($text) Add-Type -AssemblyName System.Windows.Forms;[System.Windows.Forms.Clipboard]::SetText($text)";
|
|
1619
1670
|
|
|
1620
1671
|
// ../device-agent/src/native-services.ts
|
|
1621
|
-
import { execFile as
|
|
1672
|
+
import { execFile as execFile3 } from "node:child_process";
|
|
1622
1673
|
import { realpath as realpath2, stat as stat2 } from "node:fs/promises";
|
|
1623
1674
|
import { win32 } from "node:path";
|
|
1624
|
-
import { promisify as
|
|
1625
|
-
var execute2 =
|
|
1675
|
+
import { promisify as promisify3 } from "node:util";
|
|
1676
|
+
var execute2 = promisify3(execFile3);
|
|
1626
1677
|
async function createNativeServiceAdapter(options = {}) {
|
|
1627
1678
|
const platform = options.platform ?? process.platform;
|
|
1628
1679
|
const environment2 = options.environment ?? process.env;
|
|
@@ -1751,7 +1802,7 @@ async function runRemote(options) {
|
|
|
1751
1802
|
options.signal?.addEventListener("abort", abort, { once: true });
|
|
1752
1803
|
refreshSession = abort;
|
|
1753
1804
|
try {
|
|
1754
|
-
await remoteSession(options.origin, options.stateDirectory, engine, fetcher, sessionController.signal, options.log);
|
|
1805
|
+
await remoteSession(options.origin, options.stateDirectory, engine, fetcher, sessionController.signal, options.log, options.socketFactory);
|
|
1755
1806
|
delay = 500;
|
|
1756
1807
|
} catch (error) {
|
|
1757
1808
|
if (options.signal?.aborted) break;
|
|
@@ -1760,8 +1811,8 @@ async function runRemote(options) {
|
|
|
1760
1811
|
continue;
|
|
1761
1812
|
}
|
|
1762
1813
|
if (error instanceof RemoteAuthorizationError) throw new TypeError("Device Control authorization was rejected after refresh; run login again");
|
|
1763
|
-
if (!(error instanceof RemoteUnavailableError
|
|
1764
|
-
const httpStatus = error
|
|
1814
|
+
if (!(error instanceof RemoteUnavailableError)) throw error;
|
|
1815
|
+
const httpStatus = error.httpStatus;
|
|
1765
1816
|
options.log?.({
|
|
1766
1817
|
event: "remote.session",
|
|
1767
1818
|
status: "reconnecting",
|
|
@@ -1786,14 +1837,12 @@ function optionalBrowserBridgeFailure(error) {
|
|
|
1786
1837
|
if (!error || typeof error !== "object" || !("code" in error)) return false;
|
|
1787
1838
|
return ["EADDRINUSE", "EACCES", "EAFNOSUPPORT", "EPROTONOSUPPORT"].includes(String(error.code));
|
|
1788
1839
|
}
|
|
1789
|
-
async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, signal, log) {
|
|
1840
|
+
async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, signal, log, socketFactory = defaultSocketFactory, healthIntervalMs = 45e3) {
|
|
1790
1841
|
const deviceId = engine.options.deviceId;
|
|
1791
|
-
const
|
|
1792
|
-
const connected = await invoke2("/device-control/remote/connect", { device: engine.info() });
|
|
1793
|
-
const sessionId = bounded2(connected.sessionId, "remote session id");
|
|
1794
|
-
log?.({ event: "remote.session", status: "connected", deviceId, agentVersion: UAP_DEVICE_CONTROL_AGENT_VERSION });
|
|
1842
|
+
const socket = await authorizedSocket(origin, stateDirectory, fetcher, socketFactory, signal, log, deviceId);
|
|
1795
1843
|
const inFlight = /* @__PURE__ */ new Set();
|
|
1796
1844
|
const dispatched = /* @__PURE__ */ new Set();
|
|
1845
|
+
const acknowledgements = /* @__PURE__ */ new Map();
|
|
1797
1846
|
let browserExecution = Promise.resolve(void 0);
|
|
1798
1847
|
let resultFailure;
|
|
1799
1848
|
const execute4 = (command, workspaceId) => {
|
|
@@ -1811,38 +1860,155 @@ async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, si
|
|
|
1811
1860
|
} catch (error) {
|
|
1812
1861
|
outcome = { ok: false, error: safeError(error) };
|
|
1813
1862
|
}
|
|
1814
|
-
|
|
1815
|
-
await invoke2("/device-control/remote/result", { sessionId, deviceId, requestId: command.requestId, ...outcome, executionMs: elapsed(startedAt) });
|
|
1816
|
-
} catch (error) {
|
|
1817
|
-
if (!(error instanceof RemoteResultExpiredError)) throw error;
|
|
1818
|
-
log?.({ event: "remote.result", status: "expired", deviceId, requestId: command.requestId });
|
|
1819
|
-
}
|
|
1863
|
+
await sendResult(socket, acknowledgements, { type: "result", requestId: command.requestId, ...outcome, executionMs: elapsed(startedAt) });
|
|
1820
1864
|
};
|
|
1821
1865
|
try {
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1866
|
+
await new Promise((resolve, reject) => {
|
|
1867
|
+
let connected = false;
|
|
1868
|
+
let probeInProgress = false;
|
|
1869
|
+
let probeFailures = 0;
|
|
1870
|
+
let healthTimer;
|
|
1871
|
+
const fail = (error) => {
|
|
1872
|
+
for (const pending of acknowledgements.values()) pending.reject(error);
|
|
1873
|
+
acknowledgements.clear();
|
|
1874
|
+
reject(error);
|
|
1875
|
+
};
|
|
1876
|
+
const onMessage = (data) => {
|
|
1877
|
+
try {
|
|
1878
|
+
const value = socketMessage(data);
|
|
1879
|
+
if (value.type === "connected") {
|
|
1880
|
+
connected = true;
|
|
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
|
+
}
|
|
1902
|
+
return;
|
|
1903
|
+
}
|
|
1904
|
+
if (!connected) throw new TypeError("remote socket sent data before connection acknowledgement");
|
|
1905
|
+
if (value.type === "result-ack") {
|
|
1906
|
+
const requestId = bounded2(value.requestId, "request id");
|
|
1907
|
+
const pending = acknowledgements.get(requestId);
|
|
1908
|
+
if (pending) {
|
|
1909
|
+
acknowledgements.delete(requestId);
|
|
1910
|
+
value.accepted === true ? pending.resolve() : pending.reject(new RemoteResultExpiredError("/device-control/remote/socket"));
|
|
1911
|
+
}
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
if (value.type === "notice") {
|
|
1915
|
+
log?.({ event: "plugin.tool", ...noticeValue(value.notice) });
|
|
1916
|
+
return;
|
|
1917
|
+
}
|
|
1918
|
+
if (value.type !== "command") throw new TypeError("remote socket message type is invalid");
|
|
1919
|
+
if (inFlight.size >= 4) throw new TypeError("remote socket exceeded command concurrency");
|
|
1920
|
+
const command = commandValue(value.command);
|
|
1921
|
+
if (dispatched.has(command.requestId)) return;
|
|
1922
|
+
dispatched.add(command.requestId);
|
|
1923
|
+
if (dispatched.size > 2048) dispatched.delete(dispatched.values().next().value);
|
|
1924
|
+
const job = complete(command, bounded2(value.workspaceId, "workspace id")).catch((error) => {
|
|
1925
|
+
resultFailure ??= error;
|
|
1926
|
+
socket.close(1011, "command result failed");
|
|
1927
|
+
});
|
|
1928
|
+
inFlight.add(job);
|
|
1929
|
+
void job.then(() => inFlight.delete(job));
|
|
1930
|
+
} catch (error) {
|
|
1931
|
+
fail(error instanceof Error ? error : new TypeError("remote socket message is invalid"));
|
|
1932
|
+
socket.close(1002, "invalid message");
|
|
1933
|
+
}
|
|
1934
|
+
};
|
|
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
|
+
};
|
|
1939
|
+
const onError = () => fail(new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1940
|
+
const onAbort = () => {
|
|
1941
|
+
if (healthTimer) clearInterval(healthTimer);
|
|
1942
|
+
socket.close(1e3, "client stopped");
|
|
1943
|
+
resolve();
|
|
1944
|
+
};
|
|
1945
|
+
socket.on("message", onMessage).on("close", onClose).on("error", onError);
|
|
1946
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1947
|
+
socket.send(JSON.stringify({ type: "hello", device: engine.info() }));
|
|
1948
|
+
});
|
|
1840
1949
|
} finally {
|
|
1841
1950
|
await Promise.allSettled(inFlight);
|
|
1842
|
-
|
|
1951
|
+
socket.close(1e3, "session ended");
|
|
1843
1952
|
log?.({ event: "remote.session", status: "disconnected", deviceId });
|
|
1844
1953
|
}
|
|
1845
1954
|
}
|
|
1955
|
+
function defaultSocketFactory(url, token) {
|
|
1956
|
+
return new WebSocket(url, { headers: { authorization: "Bearer " + token } });
|
|
1957
|
+
}
|
|
1958
|
+
async function authorizedSocket(origin, stateDirectory, fetcher, factory, signal, log, deviceId) {
|
|
1959
|
+
const open2 = async (refresh = false) => {
|
|
1960
|
+
const current = await accessToken(stateDirectory, fetcher, refresh);
|
|
1961
|
+
const url = new URL("/device-control/remote/socket", canonicalOrigin2(origin));
|
|
1962
|
+
url.searchParams.set("deviceId", deviceId);
|
|
1963
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
1964
|
+
return await socketHandshake(factory(url.href, current.token), signal);
|
|
1965
|
+
};
|
|
1966
|
+
try {
|
|
1967
|
+
return await open2();
|
|
1968
|
+
} catch (error) {
|
|
1969
|
+
if (!(error instanceof RemoteAuthorizationError)) throw error;
|
|
1970
|
+
log?.({ event: "remote.session", status: "reauthorizing", deviceId, httpStatus: error.httpStatus, pathname: error.pathname });
|
|
1971
|
+
return open2(true);
|
|
1972
|
+
}
|
|
1973
|
+
}
|
|
1974
|
+
function socketHandshake(socket, signal) {
|
|
1975
|
+
return new Promise((resolve, reject) => {
|
|
1976
|
+
const done = (error) => {
|
|
1977
|
+
socket.off("open", opened).off("error", failed).off("unexpected-response", rejected);
|
|
1978
|
+
signal?.removeEventListener("abort", aborted);
|
|
1979
|
+
error ? reject(error) : resolve(socket);
|
|
1980
|
+
};
|
|
1981
|
+
const opened = () => done();
|
|
1982
|
+
const failed = () => done(new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1983
|
+
const rejected = (_request, response) => done(
|
|
1984
|
+
response.statusCode === 401 || response.statusCode === 403 ? new RemoteAuthorizationError(response.statusCode, "/device-control/remote/socket") : new RemoteUnavailableError(response.statusCode, "/device-control/remote/socket")
|
|
1985
|
+
);
|
|
1986
|
+
const aborted = () => {
|
|
1987
|
+
socket.close(1e3, "client stopped");
|
|
1988
|
+
done(new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1989
|
+
};
|
|
1990
|
+
socket.on("open", opened).on("error", failed).on("unexpected-response", rejected);
|
|
1991
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
1992
|
+
});
|
|
1993
|
+
}
|
|
1994
|
+
function sendResult(socket, acknowledgements, value) {
|
|
1995
|
+
const requestId = bounded2(value.requestId, "request id");
|
|
1996
|
+
return new Promise((resolve, reject) => {
|
|
1997
|
+
acknowledgements.set(requestId, { resolve, reject });
|
|
1998
|
+
socket.send(JSON.stringify(value), (error) => {
|
|
1999
|
+
if (!error) return;
|
|
2000
|
+
acknowledgements.delete(requestId);
|
|
2001
|
+
reject(error);
|
|
2002
|
+
});
|
|
2003
|
+
});
|
|
2004
|
+
}
|
|
2005
|
+
function socketMessage(value) {
|
|
2006
|
+
const bytes = typeof value === "string" ? Buffer.from(value) : Buffer.isBuffer(value) ? value : Array.isArray(value) ? Buffer.concat(value) : Buffer.from(value);
|
|
2007
|
+
if (bytes.byteLength > 2 * 1024 * 1024) throw new TypeError("remote socket message is too large");
|
|
2008
|
+
const parsed = JSON.parse(bytes.toString("utf8"));
|
|
2009
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new TypeError("remote socket message is invalid");
|
|
2010
|
+
return parsed;
|
|
2011
|
+
}
|
|
1846
2012
|
async function authorizedRequest(fetcher, origin, stateDirectory, pathname, body, signal, timeoutMs = 15e3, log, deviceId) {
|
|
1847
2013
|
const current = await accessToken(stateDirectory, fetcher);
|
|
1848
2014
|
try {
|
|
@@ -1874,13 +2040,6 @@ async function request(fetcher, origin, pathname, token, body, signal, timeoutMs
|
|
|
1874
2040
|
throw new RemoteUnavailableError(void 0, pathname, controller.signal.aborted ? "timeout" : "transport");
|
|
1875
2041
|
}
|
|
1876
2042
|
if (response.status === 401 || response.status === 403) throw new RemoteAuthorizationError(response.status, pathname);
|
|
1877
|
-
if (response.status === 409 && (pathname === "/device-control/remote/poll" || pathname === "/device-control/remote/result")) {
|
|
1878
|
-
const body2 = await response.json().catch(() => void 0);
|
|
1879
|
-
if (body2 && typeof body2 === "object" && !Array.isArray(body2) && "error" in body2) {
|
|
1880
|
-
if (body2.error === "session_expired") throw new RemoteSessionExpiredError(pathname);
|
|
1881
|
-
if (pathname.endsWith("/result") && body2.error === "request_not_pending") throw new RemoteResultExpiredError(pathname);
|
|
1882
|
-
}
|
|
1883
|
-
}
|
|
1884
2043
|
if (!response.ok) throw new RemoteUnavailableError(response.status, pathname);
|
|
1885
2044
|
const raw = await response.json();
|
|
1886
2045
|
if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new TypeError("remote response is invalid");
|
|
@@ -1900,13 +2059,6 @@ async function remoteStatus(origin, stateDirectory, deviceId, fetcher = fetch) {
|
|
|
1900
2059
|
function defaultRemoteRoots(cwd = process.cwd()) {
|
|
1901
2060
|
return [{ id: "cwd", label: "Current directory", path: path4.resolve(cwd), readOnly: false }];
|
|
1902
2061
|
}
|
|
1903
|
-
var RemoteSessionExpiredError = class extends Error {
|
|
1904
|
-
constructor(pathname) {
|
|
1905
|
-
super("remote broker session expired");
|
|
1906
|
-
this.pathname = pathname;
|
|
1907
|
-
}
|
|
1908
|
-
pathname;
|
|
1909
|
-
};
|
|
1910
2062
|
var RemoteResultExpiredError = class extends Error {
|
|
1911
2063
|
constructor(pathname) {
|
|
1912
2064
|
super("remote command waiter expired before result acceptance");
|
|
@@ -1991,9 +2143,9 @@ import os4 from "node:os";
|
|
|
1991
2143
|
import path5 from "node:path";
|
|
1992
2144
|
import { chmod, copyFile, mkdir as mkdir4, rename as rename3, rm as rm3, writeFile as writeFile4 } from "node:fs/promises";
|
|
1993
2145
|
import { existsSync } from "node:fs";
|
|
1994
|
-
import { execFile as
|
|
1995
|
-
import { promisify as
|
|
1996
|
-
var execute3 =
|
|
2146
|
+
import { execFile as execFile4 } from "node:child_process";
|
|
2147
|
+
import { promisify as promisify4 } from "node:util";
|
|
2148
|
+
var execute3 = promisify4(execFile4);
|
|
1997
2149
|
async function installRemoteService(origin, stateDirectory) {
|
|
1998
2150
|
if (await runnerV2Installed()) throw new TypeError("Runner V2 is installed; use its Device Control provider instead of installing a duplicate Remote service");
|
|
1999
2151
|
const command = process.execPath;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codingfrees/uap-device-control",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "Cross-platform outbound UAP Device Control client",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -12,11 +12,14 @@
|
|
|
12
12
|
"engines": {
|
|
13
13
|
"node": ">=26.8.1 <27"
|
|
14
14
|
},
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"ws": "8.22.0"
|
|
17
|
+
},
|
|
15
18
|
"publishConfig": {
|
|
16
19
|
"access": "public"
|
|
17
20
|
},
|
|
18
21
|
"scripts": {
|
|
19
|
-
"build": "pnpm exec esbuild src/bin.ts --bundle --platform=node --format=esm --target=node26 --outfile=dist/uap-device-control.mjs --banner:js=\"#!/usr/bin/env node\"",
|
|
22
|
+
"build": "pnpm exec esbuild src/bin.ts --bundle --packages=external --platform=node --format=esm --target=node26 --outfile=dist/uap-device-control.mjs --banner:js=\"#!/usr/bin/env node\"",
|
|
20
23
|
"prepack": "pnpm run build",
|
|
21
24
|
"publish:public": "node scripts/publish.mjs"
|
|
22
25
|
}
|