@nomac/cli 0.21.3 → 0.21.4
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/package.json +1 -1
- package/src/hosted-sessions.mjs +12 -0
- package/src/phone-sync.mjs +1 -1
- package/src/session-ssh.mjs +50 -14
- package/src/session-transfer.mjs +177 -10
- package/src/sessions.mjs +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomac/cli",
|
|
3
|
-
"version": "0.21.
|
|
3
|
+
"version": "0.21.4",
|
|
4
4
|
"description": "A real cloud Mac for AI agents: Xcode, the iOS Simulator, signing, TestFlight and App Store publishing, by the minute, over MCP, SSH and HTTPS.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"type": "module",
|
package/src/hosted-sessions.mjs
CHANGED
|
@@ -392,6 +392,17 @@ export function statusSummary(session, image) {
|
|
|
392
392
|
return lines.join("\n");
|
|
393
393
|
}
|
|
394
394
|
|
|
395
|
+
/**
|
|
396
|
+
* PowerShell drops a "--" on its way through npm's nomac.ps1 shim, so exec and ssh find their command
|
|
397
|
+
* without one too: it starts at the first word that is not one of nomac's own options or their values.
|
|
398
|
+
*/
|
|
399
|
+
export function withCommandSeparator(argv) {
|
|
400
|
+
if (argv.includes("--")) return argv;
|
|
401
|
+
const { tokens } = parseArgs({ args: argv, options, allowPositionals: true, strict: false, tokens: true });
|
|
402
|
+
const first = tokens.find(token => token.kind === "positional");
|
|
403
|
+
return first ? [...argv.slice(0, first.index), "--", ...argv.slice(first.index)] : argv;
|
|
404
|
+
}
|
|
405
|
+
|
|
395
406
|
/** With a cache, one access token serves a whole command until shortly before it expires. */
|
|
396
407
|
export async function hostedRequest(id, method, path, params = {}, { request = api, transport = macRequest, cache } = {}) {
|
|
397
408
|
const reusable = cache?.access && Date.parse(cache.access.expires_at) - Date.now() > 30_000;
|
|
@@ -402,6 +413,7 @@ export async function hostedRequest(id, method, path, params = {}, { request = a
|
|
|
402
413
|
export async function hostedSessionCommand(command, argv, { store = sessionStore(), request = api, transport = macRequest, run = runProcess, print = console.log, sleep = ms => new Promise(r => setTimeout(r, ms)), now = Date.now,
|
|
403
414
|
stdout = data => process.stdout.write(data), stderr = data => process.stderr.write(data), exitWith = code => { process.exitCode = code; } , watch = watchFolder, stopWatching = undefined, debounceMs = 600, readInput = pipedInput} = {}) {
|
|
404
415
|
let parsed;
|
|
416
|
+
if (["exec", "ssh"].includes(command)) argv = withCommandSeparator(argv);
|
|
405
417
|
try { parsed = parseArgs({ args: argv, options, allowPositionals: true }); }
|
|
406
418
|
catch (error) { throw new CliError(error.message); }
|
|
407
419
|
const { values, positionals } = parsed;
|
package/src/phone-sync.mjs
CHANGED
|
@@ -74,7 +74,7 @@ async function filesInGit(folder) {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
/** The files git tracks as executable (mode 100755) under folder, by their path from it; none outside a repository. */
|
|
77
|
-
async function executablesInGit(folder) {
|
|
77
|
+
export async function executablesInGit(folder) {
|
|
78
78
|
try {
|
|
79
79
|
const listed = (await run("git", ["-C", folder, "ls-files", "-s", "-z"], 30_000)).stdout;
|
|
80
80
|
// Each entry: <mode> <object> <stage>\t<path>, NUL-separated; the paths are from the folder.
|
package/src/session-ssh.mjs
CHANGED
|
@@ -1,12 +1,30 @@
|
|
|
1
1
|
import { spawn } from "node:child_process";
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
|
-
import { chmodSync, mkdirSync } from "node:fs";
|
|
3
|
+
import { chmodSync, existsSync, mkdirSync } from "node:fs";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
|
-
import { isAbsolute, join } from "node:path";
|
|
5
|
+
import { isAbsolute, join, win32 } from "node:path";
|
|
6
6
|
import { CliError } from "./api.mjs";
|
|
7
7
|
import { cliEntry } from "./ssh-config.mjs";
|
|
8
8
|
|
|
9
9
|
export const shellQuote = (value) => `'${String(value).replaceAll("'", "'\\''")}'`;
|
|
10
|
+
/**
|
|
11
|
+
* Windows' OpenSSH starts a ProxyCommand itself, with no shell, so single quotes reach Windows as part of
|
|
12
|
+
* the program's name ("CreateProcessW failed", "posix_spawnp"): there a path goes in double quotes, as in
|
|
13
|
+
* an SSH config. A Windows path cannot hold a double quote.
|
|
14
|
+
*/
|
|
15
|
+
export const proxyQuote = (value, platform = process.platform) => (platform === "win32" ? `"${value}"` : shellQuote(value));
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Windows keeps its OpenSSH client in System32\OpenSSH, which a reduced PATH (an agent's shell, an
|
|
19
|
+
* editor's task) can leave out: ssh, scp and ssh-keygen are found there when PATH has none.
|
|
20
|
+
*/
|
|
21
|
+
export function programPath(program, { platform = process.platform, env = process.env, exists = existsSync } = {}) {
|
|
22
|
+
if (platform !== "win32" || !["ssh", "scp", "ssh-keygen"].includes(program)) return program;
|
|
23
|
+
const path = env.PATH ?? env.Path ?? "";
|
|
24
|
+
if (path.split(";").some(dir => dir && exists(win32.join(dir, `${program}.exe`)))) return program;
|
|
25
|
+
const bundled = win32.join(env.SystemRoot ?? env.SYSTEMROOT ?? "C:\\Windows", "System32", "OpenSSH", `${program}.exe`);
|
|
26
|
+
return exists(bundled) ? bundled : program;
|
|
27
|
+
}
|
|
10
28
|
|
|
11
29
|
export function validateTarget(target) {
|
|
12
30
|
// A hostname or user-owned SSH config alias, never command-line options or a shell expression.
|
|
@@ -42,7 +60,7 @@ export function multiplexArgs(session, { platform = process.platform, home = hom
|
|
|
42
60
|
return ["-o", "ControlMaster=auto", "-o", `ControlPath=${path}`, "-o", "ControlPersist=120"];
|
|
43
61
|
}
|
|
44
62
|
|
|
45
|
-
export function sshArgs(session, { batch = true } = {}) {
|
|
63
|
+
export function sshArgs(session, { batch = true, platform = process.platform } = {}) {
|
|
46
64
|
validateTarget(session.target);
|
|
47
65
|
if (session.port !== undefined && (!Number.isInteger(session.port) || session.port < 1 || session.port > 65535)) throw new CliError("invalid SSH port");
|
|
48
66
|
if (session.identity && (!isAbsolute(session.identity) || /[\x00-\x1f\x7f]/.test(session.identity))) throw new CliError("invalid identity path");
|
|
@@ -51,20 +69,24 @@ export function sshArgs(session, { batch = true } = {}) {
|
|
|
51
69
|
}
|
|
52
70
|
return [
|
|
53
71
|
...(session.mode === "hosted" ? ["-F", "/dev/null", "-o", `UserKnownHostsFile=${session.knownHosts}`, "-o", "GlobalKnownHostsFile=/dev/null",
|
|
54
|
-
"-o", `HostKeyAlias=nomac-${session.id}`, "-o", `ProxyCommand=${[process.execPath, cliEntry(), "tunnel", session.id].map(
|
|
72
|
+
"-o", `HostKeyAlias=nomac-${session.id}`, "-o", `ProxyCommand=${[process.execPath, cliEntry(), "tunnel", session.id].map(value => proxyQuote(value, platform)).join(" ")}`] : []),
|
|
55
73
|
"-o", "StrictHostKeyChecking=yes", "-o", "ForwardAgent=no",
|
|
56
74
|
// Two minutes of silence: a network drop at the Macs' provider pauses the stream until it resumes.
|
|
57
75
|
"-o", "ConnectTimeout=15", "-o", "ServerAliveInterval=15", "-o", "ServerAliveCountMax=8",
|
|
58
|
-
...multiplexArgs(session),
|
|
76
|
+
...multiplexArgs(session, { platform }),
|
|
59
77
|
...(batch ? ["-o", "BatchMode=yes", "-o", "ClearAllForwardings=yes"] : []),
|
|
60
78
|
...(session.port ? ["-p", String(session.port)] : []),
|
|
61
79
|
...(session.identity ? ["-i", session.identity, "-o", "IdentitiesOnly=yes"] : []),
|
|
62
80
|
];
|
|
63
81
|
}
|
|
64
82
|
|
|
65
|
-
|
|
83
|
+
/**
|
|
84
|
+
* Runs a program without a shell. input is a Buffer or string, or a stream piped in; onStdout takes the
|
|
85
|
+
* output as it comes (a download) instead of capturing it.
|
|
86
|
+
*/
|
|
87
|
+
export function runProcess(program, args, { input, onStdout, inherit = false, maxBytes = 8 * 1024 * 1024, timeoutMs, env } = {}) {
|
|
66
88
|
return new Promise((resolve, reject) => {
|
|
67
|
-
const child = spawn(program, args, { shell: false, stdio: inherit ? "inherit" : ["pipe", "pipe", "pipe"], ...(env ? { env } : {}) });
|
|
89
|
+
const child = spawn(programPath(program, env ? { env } : {}), args, { shell: false, stdio: inherit ? "inherit" : ["pipe", "pipe", "pipe"], ...(env ? { env } : {}) });
|
|
68
90
|
const output = [], errors = [];
|
|
69
91
|
let bytes = 0, failure, timer;
|
|
70
92
|
const stop = (message) => { failure = new CliError(message); child.kill("SIGTERM"); };
|
|
@@ -72,17 +94,27 @@ export function runProcess(program, args, { input, inherit = false, maxBytes = 8
|
|
|
72
94
|
if (!inherit) {
|
|
73
95
|
for (const [stream, chunks] of [[child.stdout, output], [child.stderr, errors]]) {
|
|
74
96
|
stream.on("data", chunk => {
|
|
97
|
+
if (onStdout && stream === child.stdout) {
|
|
98
|
+
try { onStdout(chunk); } catch (error) { failure ??= error; child.kill("SIGTERM"); }
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
75
101
|
bytes += chunk.length;
|
|
76
102
|
if (bytes > maxBytes) stop("Command output exceeded the capture limit. Run the command through nomac ssh to stream its output.");
|
|
77
103
|
else chunks.push(chunk);
|
|
78
104
|
});
|
|
79
105
|
}
|
|
80
106
|
child.stdin.on("error", error => { if (error.code !== "EPIPE") failure = error; });
|
|
81
|
-
|
|
107
|
+
if (typeof input?.pipe === "function") {
|
|
108
|
+
input.on("error", error => { failure ??= error; child.kill("SIGTERM"); });
|
|
109
|
+
input.pipe(child.stdin);
|
|
110
|
+
} else child.stdin.end(input);
|
|
82
111
|
}
|
|
83
112
|
child.on("error", error => {
|
|
84
113
|
clearTimeout(timer);
|
|
85
|
-
|
|
114
|
+
const missing = error.code === "ENOENT" && (process.platform === "win32" && ["ssh", "scp", "ssh-keygen"].includes(program)
|
|
115
|
+
? `${program} is required: install Windows' OpenSSH Client (Settings, System, Optional features), then run this again.`
|
|
116
|
+
: `${program} is required but was not found in PATH.`);
|
|
117
|
+
reject(new CliError(error.message, missing || error.message, undefined, error.code === "ENOENT" ? "tool_missing" : "process_failed"));
|
|
86
118
|
});
|
|
87
119
|
child.on("close", (code, signal) => {
|
|
88
120
|
clearTimeout(timer);
|
|
@@ -148,9 +180,8 @@ export function forwardingArg(value) {
|
|
|
148
180
|
/** rsync has its own argument parser, not a POSIX shell: quoted apostrophes double. */
|
|
149
181
|
export const rsyncQuote = (value) => `'${String(value).replaceAll("'", "''")}'`;
|
|
150
182
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (index === -1) throw new CliError("invalid rsync server command");
|
|
183
|
+
/** Shell lines that enter the workspace and then a folder in it, refusing a link on the way. */
|
|
184
|
+
export function sourceChecks(session, from = ".") {
|
|
154
185
|
const source = String(from);
|
|
155
186
|
if (source.startsWith("/") || source.split("/").includes("..") || /[\x00-\x1f\x7f\\]/.test(source)) throw new CliError("invalid transfer source");
|
|
156
187
|
return [
|
|
@@ -158,6 +189,11 @@ export function rsyncServerCommand(session, argv, from = ".") {
|
|
|
158
189
|
`p=${shellQuote(source === "." ? "." : `./${source}`)}`,
|
|
159
190
|
'while [ "$p" != . ]; do test ! -L "$p" || exit 1; p=$(dirname "$p"); done',
|
|
160
191
|
`cd ${shellQuote(source)} || exit 1`,
|
|
161
|
-
|
|
162
|
-
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export function rsyncServerCommand(session, argv, from = ".") {
|
|
196
|
+
const index = argv.findIndex((arg, i) => arg === "rsync" && argv[i + 1] === "--server");
|
|
197
|
+
if (index === -1) throw new CliError("invalid rsync server command");
|
|
198
|
+
return [...sourceChecks(session, from), `exec ${argv.slice(index).map(shellQuote).join(" ")}`].join("\n");
|
|
163
199
|
}
|
package/src/session-transfer.mjs
CHANGED
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { constants, copyFileSync, createReadStream, existsSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, readlinkSync, renameSync, rmSync, chmodSync, utimesSync } from "node:fs";
|
|
3
|
-
import { basename, dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
2
|
+
import { constants, closeSync, copyFileSync, createReadStream, existsSync, lstatSync, mkdirSync, mkdtempSync, openSync, readFileSync, readdirSync, readlinkSync, renameSync, rmSync, chmodSync, utimesSync, writeSync } from "node:fs";
|
|
3
|
+
import { basename, delimiter, dirname, isAbsolute, join, posix, relative, resolve, sep } from "node:path";
|
|
4
4
|
import { homedir } from "node:os";
|
|
5
|
+
import { Readable } from "node:stream";
|
|
5
6
|
import { fileURLToPath } from "node:url";
|
|
6
7
|
import ignoreFactory from "ignore";
|
|
7
8
|
import { CliError } from "./api.mjs";
|
|
8
|
-
import {
|
|
9
|
+
import { executablesInGit } from "./phone-sync.mjs";
|
|
10
|
+
import { remote, rsyncQuote, runProcess, shellQuote, sourceChecks, sshArgs, validateWorkspace } from "./session-ssh.mjs";
|
|
11
|
+
|
|
12
|
+
/** Windows keeps no Unix mode, so there a file's fingerprint is its content alone, here and on the Mac. */
|
|
13
|
+
const MODELESS = process.platform === "win32";
|
|
14
|
+
export const withoutMode = value => value.replace(/^(file:[a-f0-9]{64}):[0-7]{1,4}$/, "$1");
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* How files travel: rsync where this computer has it, otherwise (always on Windows, where rsync is not
|
|
18
|
+
* part of the system) a tar stream over the same SSH connection, unpacked by the Mac's own tar.
|
|
19
|
+
*/
|
|
20
|
+
export function defaultTransport({ platform = process.platform, env = process.env, exists = existsSync } = {}) {
|
|
21
|
+
if (platform === "win32") return "tar";
|
|
22
|
+
return (env.PATH ?? "").split(delimiter).some(dir => dir && exists(join(dir, "rsync"))) ? "rsync" : "tar";
|
|
23
|
+
}
|
|
9
24
|
|
|
10
25
|
/**
|
|
11
26
|
* What sync leaves out unless .nomacignore brings it back: folders that are
|
|
@@ -95,7 +110,7 @@ export async function fingerprint(path) {
|
|
|
95
110
|
if (!stat.isFile()) return "other";
|
|
96
111
|
const hash = createHash("sha256");
|
|
97
112
|
for await (const chunk of createReadStream(path, { flags: constants.O_RDONLY | constants.O_NOFOLLOW })) hash.update(chunk);
|
|
98
|
-
return `file:${hash.digest("hex")}:${(stat.mode & 0o777).toString(8)}`;
|
|
113
|
+
return MODELESS ? `file:${hash.digest("hex")}` : `file:${hash.digest("hex")}:${(stat.mode & 0o777).toString(8)}`;
|
|
99
114
|
}
|
|
100
115
|
|
|
101
116
|
export function safeRelative(path) {
|
|
@@ -157,7 +172,7 @@ export async function remoteFingerprints(session, files, options) {
|
|
|
157
172
|
for (const path of batch) {
|
|
158
173
|
const value = Object.hasOwn(values, path) && values[path];
|
|
159
174
|
if (typeof value !== "string" || !/^(?:missing|symlink|other|link:[a-f0-9]{64}|file:[a-f0-9]{64}:[0-7]{1,4})$/.test(value)) throw new CliError("invalid remote fingerprint");
|
|
160
|
-
Object.defineProperty(result, path, { value, enumerable: true, configurable: true, writable: true });
|
|
175
|
+
Object.defineProperty(result, path, { value: MODELESS ? withoutMode(value) : value, enumerable: true, configurable: true, writable: true });
|
|
161
176
|
}
|
|
162
177
|
}
|
|
163
178
|
return result;
|
|
@@ -208,6 +223,144 @@ function transferArgs() {
|
|
|
208
223
|
return ["-rpt", "--checksum", "--itemize-changes", "--stats", "--timeout=60", "-e", [process.execPath, bridge].map(rsyncQuote).join(" ")];
|
|
209
224
|
}
|
|
210
225
|
|
|
226
|
+
const BLOCK = 512;
|
|
227
|
+
const padding = size => Buffer.alloc((BLOCK - (size % BLOCK)) % BLOCK);
|
|
228
|
+
/** A pax record, "LENGTH key=value\n", whose length counts itself. */
|
|
229
|
+
function paxRecord(key, value) {
|
|
230
|
+
const rest = Buffer.byteLength(` ${key}=${value}\n`);
|
|
231
|
+
let length = rest + 1;
|
|
232
|
+
while (String(length).length + rest !== length) length = String(length).length + rest;
|
|
233
|
+
return `${length} ${key}=${value}\n`;
|
|
234
|
+
}
|
|
235
|
+
function ustar({ path, type, size, mode, mtime, linkpath = "" }) {
|
|
236
|
+
const block = Buffer.alloc(BLOCK);
|
|
237
|
+
const field = (offset, length, value) => block.write(value, offset, length, "utf8");
|
|
238
|
+
const octal = (offset, length, value) => field(offset, length, value.toString(8).padStart(length - 1, "0") + "\0");
|
|
239
|
+
block.set(Buffer.from(path).subarray(0, 100), 0);
|
|
240
|
+
octal(100, 8, mode); octal(108, 8, 0); octal(116, 8, 0); octal(124, 12, size); octal(136, 12, mtime);
|
|
241
|
+
field(148, 8, " "); field(156, 1, type);
|
|
242
|
+
block.set(Buffer.from(linkpath).subarray(0, 100), 157);
|
|
243
|
+
field(257, 6, "ustar\0"); field(263, 2, "00");
|
|
244
|
+
const sum = block.reduce((total, byte) => total + byte, 0);
|
|
245
|
+
field(148, 8, sum.toString(8).padStart(6, "0") + "\0 ");
|
|
246
|
+
return block;
|
|
247
|
+
}
|
|
248
|
+
/** An entry's header: ustar, after a pax record for what ustar cannot hold (long or non-ASCII paths, big files). */
|
|
249
|
+
function tarHeader(entry) {
|
|
250
|
+
const ascii = value => Buffer.byteLength(value) <= 100 && /^[\x20-\x7e]*$/.test(value);
|
|
251
|
+
const records = [
|
|
252
|
+
...(ascii(entry.path) ? [] : [paxRecord("path", entry.path)]),
|
|
253
|
+
...(entry.linkpath === undefined || ascii(entry.linkpath) ? [] : [paxRecord("linkpath", entry.linkpath)]),
|
|
254
|
+
...(entry.size > 0o77777777777 ? [paxRecord("size", String(entry.size))] : []),
|
|
255
|
+
];
|
|
256
|
+
if (!records.length) return ustar(entry);
|
|
257
|
+
const body = Buffer.from(records.join(""));
|
|
258
|
+
return Buffer.concat([ustar({ path: "PaxHeader", type: "x", size: body.length, mode: 0o644, mtime: entry.mtime }), body, padding(body.length), ustar({ ...entry, size: Math.min(entry.size, 0o77777777777) })]);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* The project's changed files as a tar stream: files with their mode and mtime (as rsync -pt keeps them),
|
|
263
|
+
* symlinks as links, never followed. A file read while it changes is cut or padded to the size it had,
|
|
264
|
+
* and the check after the transfer names it.
|
|
265
|
+
*/
|
|
266
|
+
async function* tarOf(root, paths, modeOf) {
|
|
267
|
+
for (const path of paths) {
|
|
268
|
+
const full = join(root, path), stat = lstatSync(full), mtime = Math.floor(stat.mtimeMs / 1000);
|
|
269
|
+
if (stat.isSymbolicLink()) { yield tarHeader({ path, type: "2", size: 0, mode: 0o755, mtime, linkpath: readlinkSync(full) }); continue; }
|
|
270
|
+
yield tarHeader({ path, type: "0", size: stat.size, mode: modeOf(path, stat), mtime });
|
|
271
|
+
let sent = 0;
|
|
272
|
+
if (stat.size) for await (const chunk of createReadStream(full, { flags: constants.O_RDONLY | constants.O_NOFOLLOW, start: 0, end: stat.size - 1 })) { sent += chunk.length; yield chunk; }
|
|
273
|
+
if (sent < stat.size) yield Buffer.alloc(stat.size - sent);
|
|
274
|
+
yield padding(stat.size);
|
|
275
|
+
}
|
|
276
|
+
yield Buffer.alloc(BLOCK * 2);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** A path from a downloaded tar: relative, inside the folder, no backslash or control character. */
|
|
280
|
+
function entryPath(name) {
|
|
281
|
+
const path = name.replace(/^(?:\.\/)+/, "").replace(/\/+$/, "");
|
|
282
|
+
if (path === "" || path === ".") return ".";
|
|
283
|
+
if (path.startsWith("/") || path.split("/").includes("..") || /[\x00-\x1f\x7f\\]/.test(path)) throw new CliError("invalid download entry", `The Mac sent a path this computer cannot write safely: ${JSON.stringify(name)}.`);
|
|
284
|
+
return path;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Unpacks a tar stream from the Mac into an empty folder as it arrives: files with their mode and mtime and
|
|
289
|
+
* folders. Links and special files are left out, as rsync without --links leaves them.
|
|
290
|
+
*/
|
|
291
|
+
function untar(directory) {
|
|
292
|
+
let buffer = Buffer.alloc(0), remaining = 0, skip = 0, entry = null, pax = {}, done = false;
|
|
293
|
+
const text = (block, start, end) => { const raw = block.subarray(start, end); const nul = raw.indexOf(0); return (nul === -1 ? raw : raw.subarray(0, nul)).toString("utf8"); };
|
|
294
|
+
const number = (block, start, end) => {
|
|
295
|
+
if (block[start] & 0x80) { let value = 0; for (let i = start + 1; i < end; i++) value = value * 256 + block[i]; return value; }
|
|
296
|
+
return parseInt(text(block, start, end).trim() || "0", 8);
|
|
297
|
+
};
|
|
298
|
+
const finish = () => {
|
|
299
|
+
if (entry?.kind === "file") { closeSync(entry.fd); chmodSync(entry.full, entry.mode & 0o777); utimesSync(entry.full, entry.mtime, entry.mtime); }
|
|
300
|
+
if (entry?.kind === "pax") for (const record of Buffer.concat(entry.data).toString("utf8").split("\n")) {
|
|
301
|
+
const match = /^\d+ ([^=]+)=(.*)$/s.exec(record);
|
|
302
|
+
if (match) pax[match[1]] = match[2];
|
|
303
|
+
}
|
|
304
|
+
entry = null;
|
|
305
|
+
};
|
|
306
|
+
const header = block => {
|
|
307
|
+
if (block.every(byte => byte === 0)) { done = true; return; }
|
|
308
|
+
const stored = number(block, 148, 156), sum = block.reduce((total, byte, i) => total + (i >= 148 && i < 156 ? 32 : byte), 0);
|
|
309
|
+
if (stored !== sum) throw new CliError("invalid download", "The download from the Mac was cut or corrupted. Run nomac pull again.");
|
|
310
|
+
const type = String.fromCharCode(block[156] || 48), prefix = text(block, 345, 500);
|
|
311
|
+
const name = pax.path ?? (prefix ? `${prefix}/${text(block, 0, 100)}` : text(block, 0, 100));
|
|
312
|
+
const size = pax.size !== undefined ? Number(pax.size) : number(block, 124, 136);
|
|
313
|
+
const linkpath = pax.linkpath ?? text(block, 157, 257);
|
|
314
|
+
const mtime = pax.mtime !== undefined ? Math.floor(Number(pax.mtime)) : number(block, 136, 148);
|
|
315
|
+
remaining = size; skip = padding(size).length;
|
|
316
|
+
if (type === "x") { entry = { kind: "pax", data: [] }; return; }
|
|
317
|
+
pax = {};
|
|
318
|
+
if (type === "g") { entry = { kind: "skip" }; return; }
|
|
319
|
+
const path = entryPath(name);
|
|
320
|
+
if (type === "5") { if (path !== ".") mkdirSync(join(directory, path), { recursive: true, mode: 0o700 }); entry = { kind: "skip" }; return; }
|
|
321
|
+
if (type === "0" || type === "7") {
|
|
322
|
+
const full = join(directory, path);
|
|
323
|
+
mkdirSync(dirname(full), { recursive: true, mode: 0o700 });
|
|
324
|
+
entry = { kind: "file", full, fd: openSync(full, "wx", 0o600), mode: number(block, 100, 108), mtime };
|
|
325
|
+
return;
|
|
326
|
+
}
|
|
327
|
+
// A hard link is a second name for a file already unpacked: a copy of it.
|
|
328
|
+
if (type === "1") { mkdirSync(dirname(join(directory, path)), { recursive: true, mode: 0o700 }); copyFileSync(join(directory, entryPath(linkpath)), join(directory, path)); }
|
|
329
|
+
entry = { kind: "skip" };
|
|
330
|
+
};
|
|
331
|
+
return {
|
|
332
|
+
write(chunk) {
|
|
333
|
+
buffer = buffer.length ? Buffer.concat([buffer, chunk]) : chunk;
|
|
334
|
+
while (!done) {
|
|
335
|
+
if (remaining > 0) {
|
|
336
|
+
if (!buffer.length) return;
|
|
337
|
+
const part = buffer.subarray(0, Math.min(remaining, buffer.length));
|
|
338
|
+
buffer = buffer.subarray(part.length); remaining -= part.length;
|
|
339
|
+
if (entry?.kind === "file") writeSync(entry.fd, part);
|
|
340
|
+
else if (entry?.kind === "pax") entry.data.push(part);
|
|
341
|
+
if (remaining > 0) return;
|
|
342
|
+
finish();
|
|
343
|
+
} else if (skip > 0) {
|
|
344
|
+
if (!buffer.length) return;
|
|
345
|
+
const n = Math.min(skip, buffer.length);
|
|
346
|
+
buffer = buffer.subarray(n); skip -= n;
|
|
347
|
+
} else {
|
|
348
|
+
if (buffer.length < BLOCK) return;
|
|
349
|
+
const block = buffer.subarray(0, BLOCK);
|
|
350
|
+
buffer = buffer.subarray(BLOCK);
|
|
351
|
+
header(block);
|
|
352
|
+
if (!done && remaining === 0) finish();
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
},
|
|
356
|
+
/** Closes a file left open by a download that stopped. */
|
|
357
|
+
close() { if (entry?.kind === "file") { closeSync(entry.fd); entry = null; } },
|
|
358
|
+
end() {
|
|
359
|
+
if (!done || entry) throw new CliError("invalid download", "The download from the Mac ended early. Run nomac pull again.");
|
|
360
|
+
},
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
|
|
211
364
|
/** Size, the biggest files, and what was skipped with the .nomacignore line that would send it. */
|
|
212
365
|
function summarize(root, { files, links, skipped }) {
|
|
213
366
|
const sizes = files.map(path => ({ path, bytes: lstatSync(join(root, path)).size }));
|
|
@@ -265,7 +418,7 @@ function syncConflictError(paths, sent, removal = {}) {
|
|
|
265
418
|
* earlier sync sent and the person has since deleted here are removed on the Mac too, unless
|
|
266
419
|
* they changed there.
|
|
267
420
|
*/
|
|
268
|
-
export async function syncProject(session, store, { overwrite = false, dryRun = false, deleteMissing = false, run = runProcess } = {}) {
|
|
421
|
+
export async function syncProject(session, store, { overwrite = false, dryRun = false, deleteMissing = false, run = runProcess, transport = defaultTransport() } = {}) {
|
|
269
422
|
const selection = projectFiles(store.project);
|
|
270
423
|
const paths = [...selection.files, ...selection.links].sort();
|
|
271
424
|
const baseline = Object.assign(Object.create(null), store.baseline());
|
|
@@ -286,7 +439,13 @@ export async function syncProject(session, store, { overwrite = false, dryRun =
|
|
|
286
439
|
let output = "";
|
|
287
440
|
// Only what differs travels; a file already the same on the Mac is recorded as such.
|
|
288
441
|
for (const path of send) if (previous[path] === local[path]) baseline[path] = local[path];
|
|
289
|
-
if (changed.length) {
|
|
442
|
+
if (changed.length && transport === "tar") {
|
|
443
|
+
// The Mac's tar (bsdtar) refuses absolute paths, ".." and writing through a link, and replaces a link
|
|
444
|
+
// where a file goes. Windows keeps no executable bit: git's index has it for the files it tracks.
|
|
445
|
+
const executables = MODELESS ? await executablesInGit(store.project) : null;
|
|
446
|
+
const modeOf = (path, stat) => (executables ? (executables.has(path) ? 0o755 : 0o644) : stat.mode & 0o777);
|
|
447
|
+
await run("ssh", [...sshArgs(session), session.target, `cd ${shellQuote(validateWorkspace(session.workspace))} || exit 1\nexec tar -xpf -`], { input: Readable.from(tarOf(store.project, changed, modeOf)) });
|
|
448
|
+
} else if (changed.length) {
|
|
290
449
|
// --links sends a symlink as a link: its target is never read or followed.
|
|
291
450
|
output = (await run("rsync", [...transferArgs(), "--links", "--from0", "--files-from=-", `${store.project}/`, `${session.target}:./`], { input: Buffer.from(changed.join("\0") + "\0"), env: transferEnv(session) })).stdout;
|
|
292
451
|
}
|
|
@@ -331,7 +490,7 @@ export function localDestination(store, destination) {
|
|
|
331
490
|
return { requested, inProject, projectPath: inProject ? (rel.split(sep).join("/") || ".") : null };
|
|
332
491
|
}
|
|
333
492
|
|
|
334
|
-
export async function pullProject(session, store, destination, { from, overwrite = false, dryRun = false, run = runProcess } = {}) {
|
|
493
|
+
export async function pullProject(session, store, destination, { from, overwrite = false, dryRun = false, run = runProcess, transport = defaultTransport() } = {}) {
|
|
335
494
|
const { requested, inProject, projectPath } = localDestination(store, destination);
|
|
336
495
|
// A folder in the project comes from the same folder on the Mac unless --from says otherwise.
|
|
337
496
|
if (from === undefined && !inProject) throw new CliError("pull usage", `Name what to copy from the Mac: nomac pull ${destination} --from PATH, with PATH relative to the project folder there.`);
|
|
@@ -357,8 +516,16 @@ export async function pullProject(session, store, destination, { from, overwrite
|
|
|
357
516
|
const staging = mkdtempSync(join(store.directory, "pull-"));
|
|
358
517
|
const backup = join(store.directory, "backups", randomUUID());
|
|
359
518
|
try {
|
|
360
|
-
// Download into an empty private directory; rsync never receives the user's working tree as destination.
|
|
361
|
-
|
|
519
|
+
// Download into an empty private directory; rsync or tar never receives the user's working tree as destination.
|
|
520
|
+
if (transport === "tar") {
|
|
521
|
+
const unpack = untar(staging);
|
|
522
|
+
// The same checks as the rsync bridge: no link on the way to the source, which is read without macOS's metadata.
|
|
523
|
+
try {
|
|
524
|
+
await run("ssh", [...sshArgs(session), session.target, [...sourceChecks(session, source),
|
|
525
|
+
`COPYFILE_DISABLE=1 exec tar -cf - --no-mac-metadata --no-xattrs --no-acls --no-fflags -- ${shellQuote(single ?? ".")}`].join("\n")], { onStdout: chunk => unpack.write(chunk) });
|
|
526
|
+
} finally { unpack.close(); }
|
|
527
|
+
unpack.end();
|
|
528
|
+
} else await run("rsync", [...transferArgs(), `${session.target}:./${single ?? ""}`, `${staging}/`], { env: transferEnv(session, source) });
|
|
362
529
|
const files = [];
|
|
363
530
|
const walk = (directory) => {
|
|
364
531
|
for (const entry of readdirSync(directory, { withFileTypes: true })) {
|
package/src/sessions.mjs
CHANGED
|
@@ -29,7 +29,8 @@ Options:
|
|
|
29
29
|
pull --from PATH choose a different remote directory
|
|
30
30
|
start/sync/pull/stop --json return machine-readable results
|
|
31
31
|
|
|
32
|
-
Requires OpenSSH and rsync
|
|
32
|
+
Requires OpenSSH locally, and rsync where it is installed (else files go as tar over SSH);
|
|
33
|
+
macOS, rsync and Perl checksum/JSON modules remotely.
|
|
33
34
|
SSH host keys must already be verified in your known_hosts. Your SSH config/agent
|
|
34
35
|
can authenticate; private keys never leave this device. Passwordless sudo is not
|
|
35
36
|
assumed. Use .nomacignore and .gitignore for project exclusions. Symlinks are
|