@codingfrees/uap-device-control 0.2.13 → 0.2.14

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.
@@ -825,7 +825,7 @@ import WebSocket from "ws";
825
825
  // package.json
826
826
  var package_default = {
827
827
  name: "@codingfrees/uap-device-control",
828
- version: "0.2.13",
828
+ version: "0.2.14",
829
829
  description: "Cross-platform outbound UAP Device Control client",
830
830
  contentPolicy: {
831
831
  class: "dual-use"
@@ -861,8 +861,9 @@ var package_default = {
861
861
 
862
862
  // ../device-agent/src/engine.ts
863
863
  import { execFile as execFile2, spawn } from "node:child_process";
864
+ import { once } from "node:events";
864
865
  import { promisify as promisify2 } from "node:util";
865
- import { cp, mkdir as mkdir3, open, readdir, realpath as realpath2, rename as rename2, rm as rm2, stat, writeFile as writeFile3 } from "node:fs/promises";
866
+ import { cp, lstat, mkdir as mkdir3, open, readdir, realpath as realpath2, rename as rename2, rm as rm2, stat, writeFile as writeFile3 } from "node:fs/promises";
866
867
  import { realpathSync } from "node:fs";
867
868
  import os2 from "node:os";
868
869
  import path3 from "node:path";
@@ -1089,8 +1090,13 @@ var DeviceAgentEngine = class {
1089
1090
  return {
1090
1091
  entries: await Promise.all(
1091
1092
  entries.slice(0, 2e3).map(async (entry) => {
1092
- const value = await stat(path3.join(target, entry.name));
1093
- return { name: entry.name, path: path3.posix.join(relative.replaceAll("\\", "/"), entry.name), kind: entry.isDirectory() ? "directory" : "file", size: value.size, modifiedAt: value.mtimeMs };
1093
+ const value = await lstat(path3.join(target, entry.name)).catch(() => null);
1094
+ return {
1095
+ name: entry.name,
1096
+ path: path3.posix.join(relative.replaceAll("\\", "/"), entry.name),
1097
+ kind: entry.isDirectory() ? "directory" : "file",
1098
+ ...value ? { size: value.size, modifiedAt: value.mtimeMs } : { metadataUnavailable: true }
1099
+ };
1094
1100
  })
1095
1101
  )
1096
1102
  };
@@ -1151,7 +1157,7 @@ var DeviceAgentEngine = class {
1151
1157
  }
1152
1158
  async #oneShot(input, workspaceId) {
1153
1159
  const startedAt = Date.now();
1154
- const process2 = this.#spawn(input, false, workspaceId);
1160
+ const process2 = await this.#spawn(input, false, workspaceId);
1155
1161
  process2.capability = "terminal.exec";
1156
1162
  this.#oneShots.set(process2.id, process2);
1157
1163
  const timeoutMs = bounded(Number(input.timeoutMs ?? 3e4), 100, 3e5, "command timeout");
@@ -1182,7 +1188,7 @@ var DeviceAgentEngine = class {
1182
1188
  }
1183
1189
  if (capability === "process.start") {
1184
1190
  if (this.#running(this.#processes) >= this.#maxProcesses) throw new TypeError("process limit reached");
1185
- const value2 = this.#spawn(input, false, workspaceId);
1191
+ const value2 = await this.#spawn(input, false, workspaceId);
1186
1192
  this.#processes.set(value2.id, value2);
1187
1193
  return summary(value2);
1188
1194
  }
@@ -1197,7 +1203,7 @@ var DeviceAgentEngine = class {
1197
1203
  const operation = String(input.operation);
1198
1204
  if (operation === "start") {
1199
1205
  if (this.#running(this.#sessions) >= this.#maxSessions) throw new TypeError("terminal session limit reached");
1200
- const value2 = this.#spawn(input, true, workspaceId);
1206
+ const value2 = await this.#spawn(input, true, workspaceId);
1201
1207
  value2.lastActivityAt = Date.now();
1202
1208
  value2.expiresAt = value2.lastActivityAt + this.#sessionIdleTimeoutMs;
1203
1209
  value2.idleTimer = null;
@@ -1360,16 +1366,14 @@ var DeviceAgentEngine = class {
1360
1366
  this.#screenSessions.delete(sessionId);
1361
1367
  this.#emit("device-agent.screen.expired", "screen.stream", { sessionId, idleTimeoutMs: this.#sessionIdleTimeoutMs }, session.workspaceId);
1362
1368
  }
1363
- #spawn(input, terminal, workspaceId) {
1369
+ async #spawn(input, terminal, workspaceId) {
1364
1370
  const executable = text2(input.executable ?? defaultShell(), 260, "executable");
1365
- if (!this.#unrestricted && ![...this.#allowed].some((allowed) => executable === allowed || path3.basename(executable).toLowerCase() === path3.basename(allowed).toLowerCase())) {
1371
+ if (!this.#unrestricted && ![...this.#allowed].some((allowed) => path3.basename(executable).toLowerCase() === path3.basename(allowed).toLowerCase())) {
1366
1372
  throw new TypeError("executable is not allowed: " + executable);
1367
1373
  }
1368
1374
  const args = Array.isArray(input.args) ? input.args.map((value) => text2(value, 8192, "argument")) : terminal ? [] : [text2(input.command ?? "", 8192, "command")];
1369
1375
  if (args.length > 128 || JSON.stringify(args).length > 32768) throw new TypeError("command length limit exceeded");
1370
- const cwd = input.cwd === void 0 ? void 0 : this.#cwd(input.cwd);
1371
- const env = environment(input.environment);
1372
- const child = spawn(executable, args, { cwd, env: { ...process.env, ...env }, shell: false, windowsHide: true });
1376
+ const child = spawn(executable, args, { cwd: input.cwd === void 0 ? void 0 : this.#cwd(input.cwd), env: { ...process.env, ...environment(input.environment) }, shell: false, windowsHide: true });
1373
1377
  const record = {
1374
1378
  id: randomUUID3(),
1375
1379
  capability: terminal ? "terminal.session" : "process.output",
@@ -1385,16 +1389,14 @@ var DeviceAgentEngine = class {
1385
1389
  truncated: false,
1386
1390
  workspaceId
1387
1391
  };
1388
- child.stdout.on("data", (chunk) => this.#append(record, "stdout", chunk));
1389
- child.stderr.on("data", (chunk) => this.#append(record, "stderr", chunk));
1390
- child.once("close", (code) => {
1391
- record.exitCode = code;
1392
- });
1392
+ for (const stream of ["stdout", "stderr"]) child[stream].on("data", (chunk) => this.#append(record, stream, chunk));
1393
+ child.once("close", (code) => record.exitCode = code);
1394
+ await once(child, "spawn");
1393
1395
  return record;
1394
1396
  }
1395
1397
  #append(record, target, chunk) {
1396
- const totalKey = target === "stdout" ? "stdoutBytes" : "stderrBytes";
1397
- const baseKey = target === "stdout" ? "stdoutBaseBytes" : "stderrBaseBytes";
1398
+ const totalKey = `${target}Bytes`;
1399
+ const baseKey = `${target}BaseBytes`;
1398
1400
  record[totalKey] += chunk.byteLength;
1399
1401
  const next = record[target] + chunk.toString("utf8");
1400
1402
  if (record.capability === "terminal.session") {
@@ -1493,7 +1495,10 @@ var retainUtf8Tail = (value, maximumBytes) => {
1493
1495
  while (start < bytes.byteLength && (bytes[start] & 192) === 128) start++;
1494
1496
  return { text: bytes.subarray(start).toString("utf8"), droppedBytes: start };
1495
1497
  };
1496
- var inside = (root2, target) => target === root2 || target.startsWith(root2 + path3.sep);
1498
+ var inside = (root2, target) => {
1499
+ const relative = path3.relative(root2, target);
1500
+ return relative !== ".." && !relative.startsWith(".." + path3.sep) && !path3.isAbsolute(relative);
1501
+ };
1497
1502
  var executionWorkspaceId = (value) => {
1498
1503
  if (value === void 0) return null;
1499
1504
  if (!/^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/.test(value)) throw new TypeError("workspace id is invalid");
@@ -1549,7 +1554,7 @@ function isMissing(error) {
1549
1554
  }
1550
1555
  function safeRelative(value, empty) {
1551
1556
  const result = String(value ?? "");
1552
- if (!empty && !result || result.length > 1024 || path3.isAbsolute(result) || result.split(/[\\/]/).some((part) => part === "..") || result.includes("\0")) {
1557
+ if (!empty && !result || result.length > 1024 || path3.isAbsolute(result) || path3.win32.parse(result).root !== "" || result.split(/[\\/]/).some((part) => part === "..") || result.includes("\0")) {
1553
1558
  throw new TypeError("relative path is invalid");
1554
1559
  }
1555
1560
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codingfrees/uap-device-control",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "description": "Cross-platform outbound UAP Device Control client",
5
5
  "contentPolicy": {
6
6
  "class": "dual-use"