@codingfrees/uap-device-control 0.1.22 → 0.2.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/uap-device-control.mjs +127 -49
- 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.
|
|
628
|
+
version: "0.2.0",
|
|
628
629
|
description: "Cross-platform outbound UAP Device Control client",
|
|
629
630
|
type: "module",
|
|
630
631
|
bin: {
|
|
@@ -636,11 +637,14 @@ 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
|
}
|
|
@@ -1751,7 +1755,7 @@ async function runRemote(options) {
|
|
|
1751
1755
|
options.signal?.addEventListener("abort", abort, { once: true });
|
|
1752
1756
|
refreshSession = abort;
|
|
1753
1757
|
try {
|
|
1754
|
-
await remoteSession(options.origin, options.stateDirectory, engine, fetcher, sessionController.signal, options.log);
|
|
1758
|
+
await remoteSession(options.origin, options.stateDirectory, engine, fetcher, sessionController.signal, options.log, options.socketFactory);
|
|
1755
1759
|
delay = 500;
|
|
1756
1760
|
} catch (error) {
|
|
1757
1761
|
if (options.signal?.aborted) break;
|
|
@@ -1760,8 +1764,8 @@ async function runRemote(options) {
|
|
|
1760
1764
|
continue;
|
|
1761
1765
|
}
|
|
1762
1766
|
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
|
|
1767
|
+
if (!(error instanceof RemoteUnavailableError)) throw error;
|
|
1768
|
+
const httpStatus = error.httpStatus;
|
|
1765
1769
|
options.log?.({
|
|
1766
1770
|
event: "remote.session",
|
|
1767
1771
|
status: "reconnecting",
|
|
@@ -1786,14 +1790,12 @@ function optionalBrowserBridgeFailure(error) {
|
|
|
1786
1790
|
if (!error || typeof error !== "object" || !("code" in error)) return false;
|
|
1787
1791
|
return ["EADDRINUSE", "EACCES", "EAFNOSUPPORT", "EPROTONOSUPPORT"].includes(String(error.code));
|
|
1788
1792
|
}
|
|
1789
|
-
async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, signal, log) {
|
|
1793
|
+
async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, signal, log, socketFactory = defaultSocketFactory) {
|
|
1790
1794
|
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 });
|
|
1795
|
+
const socket = await authorizedSocket(origin, stateDirectory, fetcher, socketFactory, signal, log, deviceId);
|
|
1795
1796
|
const inFlight = /* @__PURE__ */ new Set();
|
|
1796
1797
|
const dispatched = /* @__PURE__ */ new Set();
|
|
1798
|
+
const acknowledgements = /* @__PURE__ */ new Map();
|
|
1797
1799
|
let browserExecution = Promise.resolve(void 0);
|
|
1798
1800
|
let resultFailure;
|
|
1799
1801
|
const execute4 = (command, workspaceId) => {
|
|
@@ -1811,38 +1813,128 @@ async function remoteSession(origin, stateDirectory, engine, fetcher = fetch, si
|
|
|
1811
1813
|
} catch (error) {
|
|
1812
1814
|
outcome = { ok: false, error: safeError(error) };
|
|
1813
1815
|
}
|
|
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
|
-
}
|
|
1816
|
+
await sendResult(socket, acknowledgements, { type: "result", requestId: command.requestId, ...outcome, executionMs: elapsed(startedAt) });
|
|
1820
1817
|
};
|
|
1821
1818
|
try {
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
const
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1819
|
+
await new Promise((resolve, reject) => {
|
|
1820
|
+
let connected = false;
|
|
1821
|
+
const fail = (error) => {
|
|
1822
|
+
for (const pending of acknowledgements.values()) pending.reject(error);
|
|
1823
|
+
acknowledgements.clear();
|
|
1824
|
+
reject(error);
|
|
1825
|
+
};
|
|
1826
|
+
const onMessage = (data) => {
|
|
1827
|
+
try {
|
|
1828
|
+
const value = socketMessage(data);
|
|
1829
|
+
if (value.type === "connected") {
|
|
1830
|
+
connected = true;
|
|
1831
|
+
log?.({ event: "remote.session", status: "connected", deviceId, agentVersion: UAP_DEVICE_CONTROL_AGENT_VERSION });
|
|
1832
|
+
return;
|
|
1833
|
+
}
|
|
1834
|
+
if (!connected) throw new TypeError("remote socket sent data before connection acknowledgement");
|
|
1835
|
+
if (value.type === "result-ack") {
|
|
1836
|
+
const requestId = bounded2(value.requestId, "request id");
|
|
1837
|
+
const pending = acknowledgements.get(requestId);
|
|
1838
|
+
if (pending) {
|
|
1839
|
+
acknowledgements.delete(requestId);
|
|
1840
|
+
value.accepted === true ? pending.resolve() : pending.reject(new RemoteResultExpiredError("/device-control/remote/socket"));
|
|
1841
|
+
}
|
|
1842
|
+
return;
|
|
1843
|
+
}
|
|
1844
|
+
if (value.type === "notice") {
|
|
1845
|
+
log?.({ event: "plugin.tool", ...noticeValue(value.notice) });
|
|
1846
|
+
return;
|
|
1847
|
+
}
|
|
1848
|
+
if (value.type !== "command") throw new TypeError("remote socket message type is invalid");
|
|
1849
|
+
if (inFlight.size >= 4) throw new TypeError("remote socket exceeded command concurrency");
|
|
1850
|
+
const command = commandValue(value.command);
|
|
1851
|
+
if (dispatched.has(command.requestId)) return;
|
|
1852
|
+
dispatched.add(command.requestId);
|
|
1853
|
+
if (dispatched.size > 2048) dispatched.delete(dispatched.values().next().value);
|
|
1854
|
+
const job = complete(command, bounded2(value.workspaceId, "workspace id")).catch((error) => {
|
|
1855
|
+
resultFailure ??= error;
|
|
1856
|
+
socket.close(1011, "command result failed");
|
|
1857
|
+
});
|
|
1858
|
+
inFlight.add(job);
|
|
1859
|
+
void job.then(() => inFlight.delete(job));
|
|
1860
|
+
} catch (error) {
|
|
1861
|
+
fail(error instanceof Error ? error : new TypeError("remote socket message is invalid"));
|
|
1862
|
+
socket.close(1002, "invalid message");
|
|
1863
|
+
}
|
|
1864
|
+
};
|
|
1865
|
+
const onClose = () => signal?.aborted ? resolve() : fail(resultFailure instanceof Error ? resultFailure : new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1866
|
+
const onError = () => fail(new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1867
|
+
const onAbort = () => {
|
|
1868
|
+
socket.close(1e3, "client stopped");
|
|
1869
|
+
resolve();
|
|
1870
|
+
};
|
|
1871
|
+
socket.on("message", onMessage).on("close", onClose).on("error", onError);
|
|
1872
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
1873
|
+
socket.send(JSON.stringify({ type: "hello", device: engine.info() }));
|
|
1874
|
+
});
|
|
1840
1875
|
} finally {
|
|
1841
1876
|
await Promise.allSettled(inFlight);
|
|
1842
|
-
|
|
1877
|
+
socket.close(1e3, "session ended");
|
|
1843
1878
|
log?.({ event: "remote.session", status: "disconnected", deviceId });
|
|
1844
1879
|
}
|
|
1845
1880
|
}
|
|
1881
|
+
function defaultSocketFactory(url, token) {
|
|
1882
|
+
return new WebSocket(url, { headers: { authorization: "Bearer " + token } });
|
|
1883
|
+
}
|
|
1884
|
+
async function authorizedSocket(origin, stateDirectory, fetcher, factory, signal, log, deviceId) {
|
|
1885
|
+
const open = async (refresh = false) => {
|
|
1886
|
+
const current = await accessToken(stateDirectory, fetcher, refresh);
|
|
1887
|
+
const url = new URL("/device-control/remote/socket", canonicalOrigin2(origin));
|
|
1888
|
+
url.searchParams.set("deviceId", deviceId);
|
|
1889
|
+
url.protocol = url.protocol === "https:" ? "wss:" : "ws:";
|
|
1890
|
+
return await socketHandshake(factory(url.href, current.token), signal);
|
|
1891
|
+
};
|
|
1892
|
+
try {
|
|
1893
|
+
return await open();
|
|
1894
|
+
} catch (error) {
|
|
1895
|
+
if (!(error instanceof RemoteAuthorizationError)) throw error;
|
|
1896
|
+
log?.({ event: "remote.session", status: "reauthorizing", deviceId, httpStatus: error.httpStatus, pathname: error.pathname });
|
|
1897
|
+
return open(true);
|
|
1898
|
+
}
|
|
1899
|
+
}
|
|
1900
|
+
function socketHandshake(socket, signal) {
|
|
1901
|
+
return new Promise((resolve, reject) => {
|
|
1902
|
+
const done = (error) => {
|
|
1903
|
+
socket.off("open", opened).off("error", failed).off("unexpected-response", rejected);
|
|
1904
|
+
signal?.removeEventListener("abort", aborted);
|
|
1905
|
+
error ? reject(error) : resolve(socket);
|
|
1906
|
+
};
|
|
1907
|
+
const opened = () => done();
|
|
1908
|
+
const failed = () => done(new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1909
|
+
const rejected = (_request, response) => done(
|
|
1910
|
+
response.statusCode === 401 || response.statusCode === 403 ? new RemoteAuthorizationError(response.statusCode, "/device-control/remote/socket") : new RemoteUnavailableError(response.statusCode, "/device-control/remote/socket")
|
|
1911
|
+
);
|
|
1912
|
+
const aborted = () => {
|
|
1913
|
+
socket.close(1e3, "client stopped");
|
|
1914
|
+
done(new RemoteUnavailableError(void 0, "/device-control/remote/socket", "transport"));
|
|
1915
|
+
};
|
|
1916
|
+
socket.on("open", opened).on("error", failed).on("unexpected-response", rejected);
|
|
1917
|
+
signal?.addEventListener("abort", aborted, { once: true });
|
|
1918
|
+
});
|
|
1919
|
+
}
|
|
1920
|
+
function sendResult(socket, acknowledgements, value) {
|
|
1921
|
+
const requestId = bounded2(value.requestId, "request id");
|
|
1922
|
+
return new Promise((resolve, reject) => {
|
|
1923
|
+
acknowledgements.set(requestId, { resolve, reject });
|
|
1924
|
+
socket.send(JSON.stringify(value), (error) => {
|
|
1925
|
+
if (!error) return;
|
|
1926
|
+
acknowledgements.delete(requestId);
|
|
1927
|
+
reject(error);
|
|
1928
|
+
});
|
|
1929
|
+
});
|
|
1930
|
+
}
|
|
1931
|
+
function socketMessage(value) {
|
|
1932
|
+
const bytes = typeof value === "string" ? Buffer.from(value) : Buffer.isBuffer(value) ? value : Array.isArray(value) ? Buffer.concat(value) : Buffer.from(value);
|
|
1933
|
+
if (bytes.byteLength > 2 * 1024 * 1024) throw new TypeError("remote socket message is too large");
|
|
1934
|
+
const parsed = JSON.parse(bytes.toString("utf8"));
|
|
1935
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw new TypeError("remote socket message is invalid");
|
|
1936
|
+
return parsed;
|
|
1937
|
+
}
|
|
1846
1938
|
async function authorizedRequest(fetcher, origin, stateDirectory, pathname, body, signal, timeoutMs = 15e3, log, deviceId) {
|
|
1847
1939
|
const current = await accessToken(stateDirectory, fetcher);
|
|
1848
1940
|
try {
|
|
@@ -1874,13 +1966,6 @@ async function request(fetcher, origin, pathname, token, body, signal, timeoutMs
|
|
|
1874
1966
|
throw new RemoteUnavailableError(void 0, pathname, controller.signal.aborted ? "timeout" : "transport");
|
|
1875
1967
|
}
|
|
1876
1968
|
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
1969
|
if (!response.ok) throw new RemoteUnavailableError(response.status, pathname);
|
|
1885
1970
|
const raw = await response.json();
|
|
1886
1971
|
if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new TypeError("remote response is invalid");
|
|
@@ -1900,13 +1985,6 @@ async function remoteStatus(origin, stateDirectory, deviceId, fetcher = fetch) {
|
|
|
1900
1985
|
function defaultRemoteRoots(cwd = process.cwd()) {
|
|
1901
1986
|
return [{ id: "cwd", label: "Current directory", path: path4.resolve(cwd), readOnly: false }];
|
|
1902
1987
|
}
|
|
1903
|
-
var RemoteSessionExpiredError = class extends Error {
|
|
1904
|
-
constructor(pathname) {
|
|
1905
|
-
super("remote broker session expired");
|
|
1906
|
-
this.pathname = pathname;
|
|
1907
|
-
}
|
|
1908
|
-
pathname;
|
|
1909
|
-
};
|
|
1910
1988
|
var RemoteResultExpiredError = class extends Error {
|
|
1911
1989
|
constructor(pathname) {
|
|
1912
1990
|
super("remote command waiter expired before result acceptance");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codingfrees/uap-device-control",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
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
|
}
|