@codingfrees/uap-device-control 0.1.6 → 0.1.8

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.
@@ -60,7 +60,7 @@ async function createOAuthPkce() {
60
60
  // src/state.ts
61
61
  import os from "node:os";
62
62
  import path from "node:path";
63
- import { mkdir, readFile, rm, rename, writeFile } from "node:fs/promises";
63
+ import { link, mkdir, readFile, rm, rename, writeFile } from "node:fs/promises";
64
64
  import { randomUUID } from "node:crypto";
65
65
  function defaultOwnerBootstrapPath(home = os.homedir(), platform = process.platform) {
66
66
  if (platform === "win32") return path.join(process.env.LOCALAPPDATA || path.join(home, "AppData", "Local"), "UAP", "DeviceControl", "owner-bootstrap.json");
@@ -142,17 +142,21 @@ async function withRemoteAuthRefreshLock(stateDirectory, task) {
142
142
  async function acquireStateLock(stateDirectory, name, waitMs, busyMessage) {
143
143
  await mkdir(stateDirectory, { recursive: true, mode: 448 });
144
144
  const file = path.join(stateDirectory, name);
145
- const owner = { pid: process.pid, token: randomUUID(), createdAt: Date.now() };
145
+ const owner = { pid: process.pid, token: randomUUID() };
146
+ const temporary = file + ".tmp-" + owner.token;
146
147
  const deadline = Date.now() + waitMs;
147
148
  for (; ; ) {
148
149
  try {
149
- await writeFile(file, JSON.stringify(owner), { encoding: "utf8", flag: "wx", mode: 384 });
150
+ await writeFile(temporary, JSON.stringify(owner), { encoding: "utf8", flag: "wx", mode: 384 });
151
+ await link(temporary, file);
150
152
  return { release: () => removeOwnedLock(file, owner.token) };
151
153
  } catch (error) {
152
154
  if (!exists(error)) throw error;
155
+ } finally {
156
+ await rm(temporary, { force: true });
153
157
  }
154
158
  const current = await readLock(file);
155
- if (!current) throw new TypeError("Device Control state lock is invalid: " + file);
159
+ if (!current) continue;
156
160
  if (!processAlive(current.pid)) {
157
161
  await removeOwnedLock(file, current.token);
158
162
  continue;
@@ -164,13 +168,13 @@ async function acquireStateLock(stateDirectory, name, waitMs, busyMessage) {
164
168
  async function readLock(file) {
165
169
  try {
166
170
  const raw = JSON.parse(await readFile(file, "utf8"));
167
- if (!raw || typeof raw !== "object" || Array.isArray(raw)) return void 0;
171
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) throw new TypeError("Device Control state lock is invalid: " + file);
168
172
  const value = raw;
169
- if (!Number.isSafeInteger(value.pid) || Number(value.pid) < 1 || typeof value.token !== "string" || !/^[0-9a-f-]{36}$/.test(value.token) || !Number.isSafeInteger(value.createdAt)) return void 0;
170
- return { pid: Number(value.pid), token: value.token, createdAt: Number(value.createdAt) };
173
+ if (!Number.isSafeInteger(value.pid) || Number(value.pid) < 1 || typeof value.token !== "string" || !/^[0-9a-f-]{36}$/.test(value.token)) throw new TypeError("Device Control state lock is invalid: " + file);
174
+ return { pid: Number(value.pid), token: value.token };
171
175
  } catch (error) {
172
176
  if (missing(error)) return void 0;
173
- return void 0;
177
+ throw error;
174
178
  }
175
179
  }
176
180
  async function removeOwnedLock(file, token) {
@@ -400,6 +404,31 @@ function integer(value, label) {
400
404
  import os3 from "node:os";
401
405
  import path4 from "node:path";
402
406
 
407
+ // package.json
408
+ var package_default = {
409
+ name: "@codingfrees/uap-device-control",
410
+ version: "0.1.8",
411
+ description: "Cross-platform outbound UAP Device Control client",
412
+ type: "module",
413
+ bin: {
414
+ "uap-device-control": "dist/uap-device-control.mjs"
415
+ },
416
+ files: [
417
+ "dist/"
418
+ ],
419
+ engines: {
420
+ node: ">=26.8.1 <27"
421
+ },
422
+ publishConfig: {
423
+ access: "public"
424
+ },
425
+ scripts: {
426
+ 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"',
427
+ prepack: "pnpm run build",
428
+ "publish:public": "node scripts/publish.mjs"
429
+ }
430
+ };
431
+
403
432
  // ../device-agent/src/engine.ts
404
433
  import { spawn } from "node:child_process";
405
434
  import { cp, mkdir as mkdir2, readdir, readFile as readFile2, realpath, rename as rename2, rm as rm2, stat, writeFile as writeFile2 } from "node:fs/promises";
@@ -411,6 +440,7 @@ var DeviceAgentEngine = class {
411
440
  constructor(options) {
412
441
  this.options = options;
413
442
  if (!/^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/.test(options.deviceId)) throw new TypeError("device id is invalid");
443
+ if (options.agentVersion !== void 0 && !/^[0-9A-Za-z][0-9A-Za-z._+-]{0,119}$/.test(options.agentVersion)) throw new TypeError("device agent version is invalid");
414
444
  this.#maxTransfer = bounded(options.maxTransferBytes ?? 8 * 1024 * 1024, 1024, 64 * 1024 * 1024, "transfer limit");
415
445
  this.#maxOutput = bounded(options.maxOutputBytes ?? 1024 * 1024, 1024, 8 * 1024 * 1024, "output limit");
416
446
  this.#maxProcesses = bounded(options.maxProcesses ?? 8, 1, 64, "process limit");
@@ -488,7 +518,7 @@ var DeviceAgentEngine = class {
488
518
  status: "online",
489
519
  os: os2.platform(),
490
520
  architecture: os2.arch(),
491
- agentVersion: "0.1.0",
521
+ agentVersion: this.options.agentVersion ?? "0.1.0",
492
522
  lastSeen: Date.now(),
493
523
  capabilities: [...this.capabilities],
494
524
  activeSessions: this.#sessions.size
@@ -1674,6 +1704,7 @@ function secureEqual(left, right) {
1674
1704
  }
1675
1705
 
1676
1706
  // src/remote.ts
1707
+ var UAP_DEVICE_CONTROL_AGENT_VERSION = package_default.version;
1677
1708
  async function runRemote(options) {
1678
1709
  const fetcher = options.fetcher ?? fetch;
1679
1710
  const deviceId = await loadRemoteDeviceId(options.stateDirectory);
@@ -1694,6 +1725,7 @@ async function runRemote(options) {
1694
1725
  const engine = new DeviceAgentEngine({
1695
1726
  deviceId,
1696
1727
  name: os3.hostname(),
1728
+ agentVersion: UAP_DEVICE_CONTROL_AGENT_VERSION,
1697
1729
  roots: options.roots,
1698
1730
  ...options.allowedExecutables.length ? { allowedExecutables: options.allowedExecutables } : {},
1699
1731
  ...services ? { services, allowedServices: options.allowedServices } : {},
@@ -2020,11 +2052,11 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2020
2052
  return;
2021
2053
  }
2022
2054
  if (config.command === "status") {
2023
- const [auth, deviceId] = await Promise.all([Promise.resolve(existingAuth), loadRemoteDeviceId(config.stateDirectory)]);
2024
- const deviceAuthorized = Boolean(auth?.scope.split(" ").includes("device-control:device"));
2055
+ const [auth, deviceId] = await Promise.all([existingAuth, loadRemoteDeviceId(config.stateDirectory)]);
2056
+ const deviceAuthorized = !!auth?.scope.split(" ").includes("device-control:device");
2025
2057
  let remote;
2026
2058
  if (auth && deviceAuthorized) remote = await remoteStatus(auth.origin, config.stateDirectory, deviceId).catch(() => ({ online: false }));
2027
- output({ deviceId, origin: auth?.origin ?? resolvedOrigin, authenticated: Boolean(auth), deviceAuthorized, remote: remote ?? null, serviceMode: config.service, platform: process.platform });
2059
+ output({ deviceId, origin: auth?.origin ?? resolvedOrigin, authenticated: !!auth, deviceAuthorized, remote: remote ?? null, serviceMode: config.service, platform: process.platform });
2028
2060
  return;
2029
2061
  }
2030
2062
  if (config.command === "install-service") {
@@ -2040,10 +2072,9 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2040
2072
  }
2041
2073
  const controller = new AbortController();
2042
2074
  for (const signal of ["SIGINT", "SIGTERM"]) process.once(signal, () => controller.abort());
2043
- const remoteOrigin = resolvedOrigin;
2044
- output({ status: "connecting", origin: remoteOrigin, platform: process.platform });
2075
+ output({ status: "connecting", origin: resolvedOrigin, platform: process.platform });
2045
2076
  await runRemote({
2046
- origin: remoteOrigin,
2077
+ origin: resolvedOrigin,
2047
2078
  stateDirectory: config.stateDirectory,
2048
2079
  roots: config.roots,
2049
2080
  allowedExecutables: config.allowedExecutables,
@@ -2059,12 +2090,11 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2059
2090
  }
2060
2091
  function formatCliEvent(event, json2 = false) {
2061
2092
  if (json2) return JSON.stringify(event);
2062
- const status = String(event.status ?? "");
2093
+ const status = event.status;
2063
2094
  if (event.event === "plugin.tool") {
2064
2095
  const tool = String(event.tool ?? "tool");
2065
- const phase = String(event.phase ?? "");
2066
- if (phase === "invoking") return "\u{1F527} " + tool + " \xB7 running";
2067
- if (phase === "failed") return "\u274C " + tool + " \xB7 failed";
2096
+ if (event.phase === "invoking") return "\u{1F527} " + tool + " \xB7 running" + (event.deviceId ? " \xB7 " + JSON.stringify(event, ["providerId", "deviceId"]) : "");
2097
+ if (event.phase === "failed") return "\u274C " + tool + " \xB7 failed";
2068
2098
  const summary2 = event.summary && typeof event.summary === "object" ? " \xB7 " + JSON.stringify(event.summary) : "";
2069
2099
  return "\u2705 " + tool + " \xB7 completed" + summary2;
2070
2100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codingfrees/uap-device-control",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "Cross-platform outbound UAP Device Control client",
5
5
  "type": "module",
6
6
  "bin": {