@codingfrees/uap-device-control 0.1.17 → 0.1.18
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 +23 -8
- package/package.json +1 -1
|
@@ -615,7 +615,7 @@ import path4 from "node:path";
|
|
|
615
615
|
// package.json
|
|
616
616
|
var package_default = {
|
|
617
617
|
name: "@codingfrees/uap-device-control",
|
|
618
|
-
version: "0.1.
|
|
618
|
+
version: "0.1.18",
|
|
619
619
|
description: "Cross-platform outbound UAP Device Control client",
|
|
620
620
|
type: "module",
|
|
621
621
|
bin: {
|
|
@@ -1829,7 +1829,7 @@ async function request(fetcher, origin, pathname, token, body, signal, timeoutMs
|
|
|
1829
1829
|
signal: controller.signal
|
|
1830
1830
|
});
|
|
1831
1831
|
} catch {
|
|
1832
|
-
throw new RemoteUnavailableError(void 0, pathname);
|
|
1832
|
+
throw new RemoteUnavailableError(void 0, pathname, controller.signal.aborted ? "timeout" : "transport");
|
|
1833
1833
|
}
|
|
1834
1834
|
if (response.status === 401 || response.status === 403) throw new RemoteAuthorizationError(response.status, pathname);
|
|
1835
1835
|
if (response.status === 409 && pathname === "/device-control/remote/poll") {
|
|
@@ -1846,8 +1846,7 @@ async function request(fetcher, origin, pathname, token, body, signal, timeoutMs
|
|
|
1846
1846
|
}
|
|
1847
1847
|
}
|
|
1848
1848
|
async function remoteStatus(origin, stateDirectory, deviceId, fetcher = fetch) {
|
|
1849
|
-
const
|
|
1850
|
-
const value = await request(fetcher, origin, "/device-control/remote/status", token, { deviceId }, void 0, 5e3);
|
|
1849
|
+
const value = await authorizedRequest(fetcher, origin, stateDirectory, "/device-control/remote/status", { deviceId }, void 0, 5e3);
|
|
1851
1850
|
if (typeof value.online !== "boolean") throw new TypeError("remote status response is invalid");
|
|
1852
1851
|
const lastSeen = value.lastSeen;
|
|
1853
1852
|
if (lastSeen !== void 0 && (!Number.isSafeInteger(lastSeen) || Number(lastSeen) < 0)) throw new TypeError("remote status response is invalid");
|
|
@@ -1864,13 +1863,15 @@ var RemoteSessionExpiredError = class extends Error {
|
|
|
1864
1863
|
pathname;
|
|
1865
1864
|
};
|
|
1866
1865
|
var RemoteUnavailableError = class extends Error {
|
|
1867
|
-
constructor(httpStatus, pathname) {
|
|
1866
|
+
constructor(httpStatus, pathname, reason) {
|
|
1868
1867
|
super("remote endpoint is unavailable");
|
|
1869
1868
|
this.httpStatus = httpStatus;
|
|
1870
1869
|
this.pathname = pathname;
|
|
1870
|
+
this.reason = reason;
|
|
1871
1871
|
}
|
|
1872
1872
|
httpStatus;
|
|
1873
1873
|
pathname;
|
|
1874
|
+
reason;
|
|
1874
1875
|
};
|
|
1875
1876
|
var RemoteAuthorizationError = class extends Error {
|
|
1876
1877
|
constructor(httpStatus, pathname) {
|
|
@@ -2133,9 +2134,8 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
|
|
|
2133
2134
|
if (config.command === "status") {
|
|
2134
2135
|
const [auth, deviceId] = await Promise.all([existingAuth, loadRemoteDeviceId(config.stateDirectory)]);
|
|
2135
2136
|
const deviceAuthorized = !!auth?.scope.split(" ").includes("device-control:device");
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
output({ deviceId, origin: auth?.origin ?? resolvedOrigin, authenticated: !!auth, deviceAuthorized, remote: remote ?? null, serviceMode: config.service, platform: process.platform });
|
|
2137
|
+
const remote = auth && deviceAuthorized ? await checkRemoteStatus(auth.origin, config.stateDirectory, deviceId) : null;
|
|
2138
|
+
output({ deviceId, origin: auth?.origin ?? resolvedOrigin, authenticated: !!auth, deviceAuthorized, remote, serviceMode: config.service, platform: process.platform });
|
|
2139
2139
|
return;
|
|
2140
2140
|
}
|
|
2141
2141
|
if (config.command === "install-service") {
|
|
@@ -2204,6 +2204,21 @@ function formatCliEvent(event, json2 = false) {
|
|
|
2204
2204
|
if ("authenticated" in event) return (event.authenticated ? "\u2705" : "\u26A0\uFE0F") + " Device " + String(event.deviceId) + " \xB7 " + String(event.origin) + " \xB7 remote=" + JSON.stringify(event.remote);
|
|
2205
2205
|
return "\u2139\uFE0F " + JSON.stringify(event);
|
|
2206
2206
|
}
|
|
2207
|
+
async function checkRemoteStatus(origin, stateDirectory, deviceId, probe = remoteStatus) {
|
|
2208
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
2209
|
+
try {
|
|
2210
|
+
return await probe(origin, stateDirectory, deviceId);
|
|
2211
|
+
} catch (error) {
|
|
2212
|
+
if (error instanceof RemoteAuthorizationError) return { online: null, error: "status_unauthorized", httpStatus: error.httpStatus };
|
|
2213
|
+
if (error instanceof RemoteUnavailableError) {
|
|
2214
|
+
if (attempt === 0) continue;
|
|
2215
|
+
return { online: null, error: error.reason === "timeout" ? "status_timeout" : "status_unavailable", ...error.httpStatus === void 0 ? {} : { httpStatus: error.httpStatus } };
|
|
2216
|
+
}
|
|
2217
|
+
return { online: null, error: "status_failed" };
|
|
2218
|
+
}
|
|
2219
|
+
}
|
|
2220
|
+
return { online: null, error: "status_unavailable" };
|
|
2221
|
+
}
|
|
2207
2222
|
function deviceControlHelp() {
|
|
2208
2223
|
return `UAP Device Control
|
|
2209
2224
|
|