@codingfrees/uap-device-control 0.1.7 → 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) {
@@ -403,7 +407,7 @@ import path4 from "node:path";
403
407
  // package.json
404
408
  var package_default = {
405
409
  name: "@codingfrees/uap-device-control",
406
- version: "0.1.7",
410
+ version: "0.1.8",
407
411
  description: "Cross-platform outbound UAP Device Control client",
408
412
  type: "module",
409
413
  bin: {
@@ -2048,11 +2052,11 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2048
2052
  return;
2049
2053
  }
2050
2054
  if (config.command === "status") {
2051
- const [auth, deviceId] = await Promise.all([Promise.resolve(existingAuth), loadRemoteDeviceId(config.stateDirectory)]);
2052
- 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");
2053
2057
  let remote;
2054
2058
  if (auth && deviceAuthorized) remote = await remoteStatus(auth.origin, config.stateDirectory, deviceId).catch(() => ({ online: false }));
2055
- 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 });
2056
2060
  return;
2057
2061
  }
2058
2062
  if (config.command === "install-service") {
@@ -2068,10 +2072,9 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2068
2072
  }
2069
2073
  const controller = new AbortController();
2070
2074
  for (const signal of ["SIGINT", "SIGTERM"]) process.once(signal, () => controller.abort());
2071
- const remoteOrigin = resolvedOrigin;
2072
- output({ status: "connecting", origin: remoteOrigin, platform: process.platform });
2075
+ output({ status: "connecting", origin: resolvedOrigin, platform: process.platform });
2073
2076
  await runRemote({
2074
- origin: remoteOrigin,
2077
+ origin: resolvedOrigin,
2075
2078
  stateDirectory: config.stateDirectory,
2076
2079
  roots: config.roots,
2077
2080
  allowedExecutables: config.allowedExecutables,
@@ -2087,12 +2090,11 @@ async function runDeviceControlCli(args = process.argv.slice(2)) {
2087
2090
  }
2088
2091
  function formatCliEvent(event, json2 = false) {
2089
2092
  if (json2) return JSON.stringify(event);
2090
- const status = String(event.status ?? "");
2093
+ const status = event.status;
2091
2094
  if (event.event === "plugin.tool") {
2092
2095
  const tool = String(event.tool ?? "tool");
2093
- const phase = String(event.phase ?? "");
2094
- if (phase === "invoking") return "\u{1F527} " + tool + " \xB7 running";
2095
- 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";
2096
2098
  const summary2 = event.summary && typeof event.summary === "object" ? " \xB7 " + JSON.stringify(event.summary) : "";
2097
2099
  return "\u2705 " + tool + " \xB7 completed" + summary2;
2098
2100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codingfrees/uap-device-control",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "Cross-platform outbound UAP Device Control client",
5
5
  "type": "module",
6
6
  "bin": {