@codingfrees/uap-device-control 0.2.12 → 0.2.13
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/README.md +1 -1
- package/dist/uap-device-control.mjs +66 -43
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Cross-platform outbound Device Control client for Universal Agent Platform.
|
|
|
4
4
|
|
|
5
5
|
## Requirements
|
|
6
6
|
|
|
7
|
-
- Node.js 26.x (`>=
|
|
7
|
+
- Node.js 24.18.1 or later, 25.x, or 26.x (`>=24.18.1 <27`). Prefer supported LTS majors for ongoing installations.
|
|
8
8
|
- a public UAP Device Control origin for first login
|
|
9
9
|
- Windows, Linux or macOS
|
|
10
10
|
|
|
@@ -234,19 +234,19 @@ async function loginDeviceControl(origin, stateDirectory, options = {}) {
|
|
|
234
234
|
if (options.pairingCode !== void 0 && !/^[A-Za-z0-9_-]{32}$/.test(options.pairingCode)) throw new TypeError("invalid single-use pairing code");
|
|
235
235
|
const fetcher = options.fetcher ?? fetch;
|
|
236
236
|
const issuer = canonicalOrigin(origin);
|
|
237
|
-
const
|
|
237
|
+
const requestTimeoutMs = options.requestTimeoutMs ?? 15e3;
|
|
238
|
+
const requestJson = (url, init = {}) => timedJson(fetcher, url, init, requestTimeoutMs);
|
|
239
|
+
const metadata = await requestJson(new URL("/.well-known/oauth-authorization-server", issuer), { redirect: "error", cache: "no-store" });
|
|
238
240
|
const authorizationEndpoint = exactHttps(metadata.authorization_endpoint, issuer);
|
|
239
241
|
const tokenEndpoint = exactHttps(metadata.token_endpoint, issuer);
|
|
240
242
|
const registrationEndpoint = exactHttps(metadata.registration_endpoint, issuer);
|
|
241
|
-
const callback = await callbackServer(
|
|
243
|
+
const callback = await callbackServer();
|
|
242
244
|
try {
|
|
243
|
-
const registration = await
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
})
|
|
249
|
-
);
|
|
245
|
+
const registration = await requestJson(registrationEndpoint, {
|
|
246
|
+
method: "POST",
|
|
247
|
+
headers: { "content-type": "application/json" },
|
|
248
|
+
body: JSON.stringify({ redirect_uris: [callback.redirectUri], token_endpoint_auth_method: "none", grant_types: ["authorization_code", "refresh_token"], response_types: ["code"] })
|
|
249
|
+
});
|
|
250
250
|
const clientId = text(registration.client_id, "OAuth client id");
|
|
251
251
|
const pkce = await createOAuthPkce();
|
|
252
252
|
const authorize2 = new URL(authorizationEndpoint);
|
|
@@ -258,15 +258,13 @@ async function loginDeviceControl(origin, stateDirectory, options = {}) {
|
|
|
258
258
|
authorize2.searchParams.set("state", pkce.state);
|
|
259
259
|
authorize2.searchParams.set("code_challenge", pkce.challenge);
|
|
260
260
|
authorize2.searchParams.set("code_challenge_method", "S256");
|
|
261
|
-
const result = options.pairingCode ? await automaticPairing(
|
|
261
|
+
const result = options.pairingCode ? await automaticPairing(requestJson, issuer, authorize2, options.pairingCode, pkce.state) : await browserAuthorization(authorize2, callback.result, options.onAuthorizationUrl, options.timeoutMs ?? 8 * 6e4);
|
|
262
262
|
if (result.state !== pkce.state || !result.code) throw new TypeError("OAuth callback is invalid");
|
|
263
|
-
const token = await
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
})
|
|
269
|
-
);
|
|
263
|
+
const token = await requestJson(tokenEndpoint, {
|
|
264
|
+
method: "POST",
|
|
265
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
266
|
+
body: new URLSearchParams({ grant_type: "authorization_code", client_id: clientId, code: result.code, redirect_uri: callback.redirectUri, code_verifier: pkce.verifier, resource: issuer + "/mcp" })
|
|
267
|
+
});
|
|
270
268
|
const refreshToken = text(token.refresh_token, "OAuth refresh token");
|
|
271
269
|
const accessToken2 = text(token.access_token, "OAuth access token");
|
|
272
270
|
const expiresIn = integer(token.expires_in, "OAuth expiry");
|
|
@@ -329,37 +327,65 @@ async function logoutDeviceControl(stateDirectory, fetcher = fetch) {
|
|
|
329
327
|
await clearRemoteAuth(stateDirectory);
|
|
330
328
|
return true;
|
|
331
329
|
}
|
|
332
|
-
async function browserAuthorization(authorize2, result, onAuthorizationUrl) {
|
|
330
|
+
async function browserAuthorization(authorize2, result, onAuthorizationUrl, timeoutMs) {
|
|
333
331
|
await onAuthorizationUrl?.(authorize2.href);
|
|
334
|
-
return result;
|
|
332
|
+
return withTimeout(result, timeoutMs, "OAuth browser authorization timed out; finish Google sign-in and select Authorize this computer in the same browser.");
|
|
335
333
|
}
|
|
336
|
-
async function automaticPairing(
|
|
337
|
-
const started = await
|
|
338
|
-
|
|
339
|
-
|
|
334
|
+
async function automaticPairing(requestJson, issuer, authorize2, pairingCode, expectedState) {
|
|
335
|
+
const started = await requestJson(new URL("/pairing/start", issuer), {
|
|
336
|
+
method: "POST",
|
|
337
|
+
headers: { "content-type": "application/json" },
|
|
338
|
+
body: JSON.stringify({ authorizeUrl: authorize2.href }),
|
|
339
|
+
redirect: "error",
|
|
340
|
+
cache: "no-store"
|
|
341
|
+
});
|
|
340
342
|
const pairingId = text(started.pairingId, "pairing id");
|
|
341
343
|
const pollToken = text(started.pollToken, "pairing poll token");
|
|
342
|
-
await
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
344
|
+
await requestJson(new URL("/pairing/approve", issuer), {
|
|
345
|
+
method: "POST",
|
|
346
|
+
headers: { "content-type": "application/json", "x-uap-pairing-code": pairingCode },
|
|
347
|
+
body: JSON.stringify({ pairingId }),
|
|
348
|
+
redirect: "error",
|
|
349
|
+
cache: "no-store"
|
|
350
|
+
});
|
|
351
|
+
const status = await requestJson(new URL("/pairing/status", issuer), {
|
|
352
|
+
method: "POST",
|
|
353
|
+
headers: { "content-type": "application/json" },
|
|
354
|
+
body: JSON.stringify({ pairingId, pollToken }),
|
|
355
|
+
redirect: "error",
|
|
356
|
+
cache: "no-store"
|
|
357
|
+
});
|
|
354
358
|
if (status.status !== "approved" || typeof status.redirect !== "string") throw new TypeError("pairing approval failed");
|
|
355
359
|
const redirect = new URL(status.redirect);
|
|
356
360
|
if (redirect.searchParams.get("state") !== expectedState) throw new TypeError("pairing callback state is invalid");
|
|
357
361
|
return { code: redirect.searchParams.get("code") ?? void 0, state: redirect.searchParams.get("state") ?? void 0 };
|
|
358
362
|
}
|
|
359
|
-
async function
|
|
363
|
+
async function timedJson(fetcher, url, init, timeoutMs) {
|
|
364
|
+
const controller = new AbortController();
|
|
365
|
+
return withTimeout(json(fetcher(url, { ...init, signal: controller.signal })), timeoutMs, `OAuth request timed out at ${url.pathname}`, () => controller.abort());
|
|
366
|
+
}
|
|
367
|
+
async function withTimeout(pending, timeoutMs, message, onTimeout) {
|
|
368
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > 10 * 6e4) throw new TypeError("OAuth timeout setting is invalid");
|
|
369
|
+
let timer;
|
|
370
|
+
try {
|
|
371
|
+
return await Promise.race([
|
|
372
|
+
pending,
|
|
373
|
+
new Promise((_, reject) => {
|
|
374
|
+
timer = setTimeout(() => {
|
|
375
|
+
reject(new TypeError(message));
|
|
376
|
+
onTimeout?.();
|
|
377
|
+
}, timeoutMs);
|
|
378
|
+
})
|
|
379
|
+
]);
|
|
380
|
+
} finally {
|
|
381
|
+
clearTimeout(timer);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
async function callbackServer() {
|
|
360
385
|
let settle;
|
|
361
386
|
let reject;
|
|
362
387
|
const result = new Promise((resolve, rejectResult) => (settle = resolve, reject = rejectResult));
|
|
388
|
+
void result.catch(() => void 0);
|
|
363
389
|
const server = createServer((request, response) => {
|
|
364
390
|
const url = new URL(request.url ?? "/", "http://127.0.0.1");
|
|
365
391
|
if (url.pathname !== "/callback") {
|
|
@@ -372,7 +398,7 @@ async function callbackServer(timeoutMs) {
|
|
|
372
398
|
reject(new TypeError("OAuth browser authorization was declined"));
|
|
373
399
|
return;
|
|
374
400
|
}
|
|
375
|
-
response.end("UAP Device Control
|
|
401
|
+
response.end("UAP Device Control callback received. Return to the terminal to confirm login.");
|
|
376
402
|
settle({ code: url.searchParams.get("code") ?? void 0, state: url.searchParams.get("state") ?? void 0 });
|
|
377
403
|
});
|
|
378
404
|
await new Promise((resolve, rejectListen) => {
|
|
@@ -381,13 +407,10 @@ async function callbackServer(timeoutMs) {
|
|
|
381
407
|
});
|
|
382
408
|
const address = server.address();
|
|
383
409
|
if (!address || typeof address === "string") throw new TypeError("OAuth callback address is unavailable");
|
|
384
|
-
const timer = setTimeout(() => reject(new TypeError("OAuth login timed out")), timeoutMs);
|
|
385
|
-
timer.unref();
|
|
386
410
|
return {
|
|
387
411
|
redirectUri: "http://127.0.0.1:" + address.port + "/callback",
|
|
388
412
|
result,
|
|
389
413
|
close: async () => {
|
|
390
|
-
clearTimeout(timer);
|
|
391
414
|
await new Promise((resolve) => server.close(() => resolve()));
|
|
392
415
|
}
|
|
393
416
|
};
|
|
@@ -802,7 +825,7 @@ import WebSocket from "ws";
|
|
|
802
825
|
// package.json
|
|
803
826
|
var package_default = {
|
|
804
827
|
name: "@codingfrees/uap-device-control",
|
|
805
|
-
version: "0.2.
|
|
828
|
+
version: "0.2.13",
|
|
806
829
|
description: "Cross-platform outbound UAP Device Control client",
|
|
807
830
|
contentPolicy: {
|
|
808
831
|
class: "dual-use"
|
|
@@ -821,7 +844,7 @@ var package_default = {
|
|
|
821
844
|
"DISCLOSURE"
|
|
822
845
|
],
|
|
823
846
|
engines: {
|
|
824
|
-
node: ">=
|
|
847
|
+
node: ">=24.18.1 <27"
|
|
825
848
|
},
|
|
826
849
|
dependencies: {
|
|
827
850
|
ws: "8.22.0"
|
|
@@ -830,7 +853,7 @@ var package_default = {
|
|
|
830
853
|
access: "public"
|
|
831
854
|
},
|
|
832
855
|
scripts: {
|
|
833
|
-
build: 'pnpm exec esbuild src/bin.ts --bundle --packages=external --platform=node --format=esm --target=
|
|
856
|
+
build: 'pnpm exec esbuild src/bin.ts --bundle --packages=external --platform=node --format=esm --target=node24 --outfile=dist/uap-device-control.mjs --banner:js="#!/usr/bin/env node"',
|
|
834
857
|
prepack: "pnpm run build",
|
|
835
858
|
"publish:public": "node scripts/publish.mjs"
|
|
836
859
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codingfrees/uap-device-control",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.13",
|
|
4
4
|
"description": "Cross-platform outbound UAP Device Control client",
|
|
5
5
|
"contentPolicy": {
|
|
6
6
|
"class": "dual-use"
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"DISCLOSURE"
|
|
20
20
|
],
|
|
21
21
|
"engines": {
|
|
22
|
-
"node": ">=
|
|
22
|
+
"node": ">=24.18.1 <27"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"ws": "8.22.0"
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
"access": "public"
|
|
29
29
|
},
|
|
30
30
|
"scripts": {
|
|
31
|
-
"build": "pnpm exec esbuild src/bin.ts --bundle --packages=external --platform=node --format=esm --target=
|
|
31
|
+
"build": "pnpm exec esbuild src/bin.ts --bundle --packages=external --platform=node --format=esm --target=node24 --outfile=dist/uap-device-control.mjs --banner:js=\"#!/usr/bin/env node\"",
|
|
32
32
|
"prepack": "pnpm run build",
|
|
33
33
|
"publish:public": "node scripts/publish.mjs"
|
|
34
34
|
}
|