@nomac/cli 0.21.3 → 0.22.0
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/skill/SKILL.md +11 -7
- package/src/hosted-sessions.mjs +49 -24
- package/src/phone-sync.mjs +1 -1
- package/src/session-ssh.mjs +50 -14
- package/src/session-transfer.mjs +225 -20
- package/src/sessions.mjs +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nomac/cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
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/skill/SKILL.md
CHANGED
|
@@ -18,7 +18,7 @@ shell), a NoMac cloud iPhone is the lighter choice, from the same credit: the
|
|
|
18
18
|
phone tools (`phone`, `phone_app_run`, `phone_ui_look`, `phone_ui_act` and the
|
|
19
19
|
other `phone_*`), which NoMac's MCP lists beside the Mac (`nomac mcp` and
|
|
20
20
|
https://mcp.nomac.app/mcp); https://mcp.nomac.app/simulator for the phone
|
|
21
|
-
alone; or `npx simulator-use cloud start` from a terminal
|
|
21
|
+
alone; or `npx simulator-use@0.15.1 cloud start` from a terminal
|
|
22
22
|
(https://nomac.app/simulator). `phone_app_run` builds from a git link, or from
|
|
23
23
|
the project here: in `nomac mcp`, `local_path` sends the folder (only what
|
|
24
24
|
changed); on the hosted server, `phone_upload_link` answers one command that
|
|
@@ -38,10 +38,10 @@ Store screenshots start the phone with `device: "iphone-pro-max"` (6.9-inch,
|
|
|
38
38
|
not a payment. No Apple Developer account is needed for an unsigned build.
|
|
39
39
|
Under an hour left, answers say `low_credit` with a `buy_url`: tell the
|
|
40
40
|
human before a long build (a refill of 3 packs, $30, earns bonus credit).
|
|
41
|
-
2. Install `npm install -g @nomac/cli
|
|
41
|
+
2. Install `npm install -g @nomac/cli@0.22.0` (it updates itself after), run `nomac login`, and give the human
|
|
42
42
|
the printed device URL and code. With a key the human made at
|
|
43
43
|
https://nomac.app/keys: `nomac login --with-token < key.txt`, or set
|
|
44
|
-
`NOMAC_API_KEY`. MCP: `claude mcp add nomac -- npx @nomac/cli mcp`.
|
|
44
|
+
`NOMAC_API_KEY`. MCP: `claude mcp add nomac -- npx -y @nomac/cli@0.22.0 mcp`.
|
|
45
45
|
Hosted MCP: https://mcp.nomac.app/mcp with `Authorization: Bearer <nmk_key>`.
|
|
46
46
|
3. `nomac start --json`, wait until ready, `nomac sync .`, then `nomac ssh`.
|
|
47
47
|
Run commands with `nomac exec -- COMMAND`: it prints the output as it
|
|
@@ -87,9 +87,13 @@ Keep the same request key and arguments after uncertain responses; inspect
|
|
|
87
87
|
the original job before retrying. Output retrieval failure is not job failure.
|
|
88
88
|
A command runs up to its `timeout_seconds` (300 by default, 3600 at most) and
|
|
89
89
|
says so when that limit stops it; SSH can run longer within session limits.
|
|
90
|
-
CLI sync sends every file
|
|
91
|
-
|
|
92
|
-
|
|
90
|
+
CLI sync sends every file but secrets, and symlinks as links, and skips
|
|
91
|
+
dependency and build folders, .git and folders .gitignore lists. Secrets stay
|
|
92
|
+
on this computer: .env files (not .env.example), .p8, .p12, .pfx, keystores,
|
|
93
|
+
private SSH keys, .netrc, and .pem or .key files with a private key. Give them
|
|
94
|
+
to every Mac encrypted with `nomac secrets set --from-env-file .env` or
|
|
95
|
+
`nomac secrets set NAME --file PATH`. A `!` line in .nomacignore sends any of
|
|
96
|
+
them anyway. A file changed on the Mac since the
|
|
93
97
|
last sync is left alone and named (the rest is sent, and sync exits 1): `nomac
|
|
94
98
|
pull` it, or `nomac sync . --overwrite`. Sync deletes nothing on the Mac
|
|
95
99
|
unless `--delete`, which removes what it sent earlier and you deleted here.
|
|
@@ -131,7 +135,7 @@ name at the new Mac. Then VS Code or Cursor Remote-SSH and Claude Code's SSH
|
|
|
131
135
|
sessions connect to `nomac`; `ssh -t nomac 'cd ~/drive/src/<folder> && npx -y
|
|
132
136
|
@anthropic-ai/claude-code'` runs an agent on the Mac itself (keep its API key
|
|
133
137
|
as a NoMac secret); and an MCP server that needs a Mac runs from Windows or
|
|
134
|
-
Linux over SSH: `claude mcp add xcode -- ssh nomac npx -y mobilebuildmcp@
|
|
138
|
+
Linux over SSH: `claude mcp add xcode -- ssh nomac npx -y mobilebuildmcp@2.7.1
|
|
135
139
|
mcp` (MobileBuildMCP, formerly XcodeBuildMCP). An open SSH connection keeps
|
|
136
140
|
the Mac running until its time or spend limit: `nomac stop` when done.
|
|
137
141
|
|
package/src/hosted-sessions.mjs
CHANGED
|
@@ -30,7 +30,9 @@ const HELP = `Nomac cloud Mac sessions
|
|
|
30
30
|
(~/workspace with --drive none). A file changed on the Mac
|
|
31
31
|
since is left alone and named (--overwrite replaces it);
|
|
32
32
|
--delete also removes there what you deleted here;
|
|
33
|
-
--watch syncs again whenever files change, until Ctrl-C
|
|
33
|
+
--watch syncs again whenever files change, until Ctrl-C.
|
|
34
|
+
Secrets stay here (.env, .p8, .p12, keystores, private
|
|
35
|
+
keys): nomac secrets gives them to every Mac encrypted
|
|
34
36
|
nomac ssh [--forward 8080:3000] full Mac shell and port forwarding
|
|
35
37
|
nomac ssh -- command args run any command over SSH
|
|
36
38
|
nomac desktop [--no-open] [--json] the full macOS desktop in your browser
|
|
@@ -171,9 +173,10 @@ function budget(value = "5") {
|
|
|
171
173
|
if (result <= 0n) throw new CliError("--max-cost must be positive.");
|
|
172
174
|
return result.toString();
|
|
173
175
|
}
|
|
174
|
-
|
|
176
|
+
/** A whole-number option, named with its unit in the error (November 6, 2026: "--idle-timeout 7200" said only "Invalid numeric option"). */
|
|
177
|
+
function integer(value, fallback, min, max, name = "number") {
|
|
175
178
|
if (value === undefined) return fallback;
|
|
176
|
-
if (!/^\d+$/.test(value) || !Number.isSafeInteger(Number(value)) || Number(value) < min || Number(value) > max) throw new CliError(
|
|
179
|
+
if (!/^\d+$/.test(value) || !Number.isSafeInteger(Number(value)) || Number(value) < min || Number(value) > max) throw new CliError(`Invalid ${name}: give a whole number from ${min} to ${max}${/^\d+$/.test(value) ? "" : ", with no unit"}.`);
|
|
177
180
|
return Number(value);
|
|
178
181
|
}
|
|
179
182
|
const validRequestKey = value => typeof value === "string" && /^[A-Za-z0-9_.:-]{8,128}$/.test(value);
|
|
@@ -294,12 +297,12 @@ async function waitForCommand(id, base, started, waitSeconds, { request, transpo
|
|
|
294
297
|
// What it printed so far is shown, and the next wait for it goes on from there.
|
|
295
298
|
if (live) { await stream(); keepCursors(job.id, cursor); }
|
|
296
299
|
const unknown = job.state === "unknown";
|
|
297
|
-
const busyNote = busy ? "The Mac is busy (for example, the iOS Simulator
|
|
300
|
+
const busyNote = busy ? "The Mac is busy (for example, builds using all its CPUs and memory, or the iOS Simulator booting) and answers slowly; running fewer builds at once keeps it responsive. " : "";
|
|
298
301
|
const next = unknown ? `The Mac could not confirm whether ${job.id} finished. Inspect it with nomac job ${job.id}; do not rerun it.`
|
|
299
302
|
: `${busyNote}Still running after ${waitSeconds} s; it keeps running on the Mac.${stopsAt(job)} Wait for it: nomac job ${job.id} --wait 600. Next time wait up front: nomac exec --wait 600 -- command. Over an hour: nomac ssh -- command. Do not rerun it; to stop it: nomac cancel ${job.id}.`;
|
|
300
303
|
if (json) print(JSON.stringify({ ...job, ...(busy ? { busy: true } : {}), next_step: next }, null, 2));
|
|
301
304
|
else if (unknown) stderr(`nomac: ${next}\n`);
|
|
302
|
-
else stderr(`nomac: ${busy ? "the Mac is busy (for example, the iOS Simulator
|
|
305
|
+
else stderr(`nomac: ${busy ? "the Mac is busy (for example, builds using all its CPUs and memory, or the iOS Simulator booting); " : ""}still running after ${waitSeconds} s (${job.id}); it keeps running on the Mac.${stopsAt(job)}\n wait for it: nomac job ${job.id} --wait 600\n stop it: nomac cancel ${job.id}\n next time, wait longer: nomac exec --wait 600 -- command\n over an hour: nomac ssh -- command\n`);
|
|
303
306
|
exitWith(unknown ? 1 : STILL_RUNNING);
|
|
304
307
|
return job;
|
|
305
308
|
}
|
|
@@ -392,6 +395,17 @@ export function statusSummary(session, image) {
|
|
|
392
395
|
return lines.join("\n");
|
|
393
396
|
}
|
|
394
397
|
|
|
398
|
+
/**
|
|
399
|
+
* PowerShell drops a "--" on its way through npm's nomac.ps1 shim, so exec and ssh find their command
|
|
400
|
+
* without one too: it starts at the first word that is not one of nomac's own options or their values.
|
|
401
|
+
*/
|
|
402
|
+
export function withCommandSeparator(argv) {
|
|
403
|
+
if (argv.includes("--")) return argv;
|
|
404
|
+
const { tokens } = parseArgs({ args: argv, options, allowPositionals: true, strict: false, tokens: true });
|
|
405
|
+
const first = tokens.find(token => token.kind === "positional");
|
|
406
|
+
return first ? [...argv.slice(0, first.index), "--", ...argv.slice(first.index)] : argv;
|
|
407
|
+
}
|
|
408
|
+
|
|
395
409
|
/** With a cache, one access token serves a whole command until shortly before it expires. */
|
|
396
410
|
export async function hostedRequest(id, method, path, params = {}, { request = api, transport = macRequest, cache } = {}) {
|
|
397
411
|
const reusable = cache?.access && Date.parse(cache.access.expires_at) - Date.now() > 30_000;
|
|
@@ -402,11 +416,12 @@ export async function hostedRequest(id, method, path, params = {}, { request = a
|
|
|
402
416
|
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
417
|
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
418
|
let parsed;
|
|
419
|
+
if (["exec", "ssh"].includes(command)) argv = withCommandSeparator(argv);
|
|
405
420
|
try { parsed = parseArgs({ args: argv, options, allowPositionals: true }); }
|
|
406
421
|
catch (error) { throw new CliError(error.message); }
|
|
407
422
|
const { values, positionals } = parsed;
|
|
408
423
|
if (values.help) return print(command === "desktop" ? DESKTOP_HELP : HELP);
|
|
409
|
-
const wait = ["start", "attach", "stop"].includes(command) ? integer(values.wait, command === "stop" ? 60 : 300, 0, command === "stop" ? 300 : 600) : 0;
|
|
424
|
+
const wait = ["start", "attach", "stop"].includes(command) ? integer(values.wait, command === "stop" ? 60 : 300, 0, command === "stop" ? 300 : 600, "--wait (seconds)") : 0;
|
|
410
425
|
if (command === "start" && values["request-key"] !== undefined && !validRequestKey(values["request-key"])) throw new CliError("Use a request key of 8–128 letters, digits, underscores, periods, colons or hyphens.");
|
|
411
426
|
// The image the last read reported (macOS, Xcodes, Simulators), for start's summary.
|
|
412
427
|
let lastImage;
|
|
@@ -552,7 +567,7 @@ export async function hostedSessionCommand(command, argv, { store = sessionStore
|
|
|
552
567
|
}
|
|
553
568
|
if (command === "extend") {
|
|
554
569
|
if (positionals.length > 1) throw new CliError("Usage: nomac extend [MINUTES] [--json]");
|
|
555
|
-
const minutes = integer(positionals[0], 60, 1, 480);
|
|
570
|
+
const minutes = integer(positionals[0], 60, 1, 480, "number of minutes");
|
|
556
571
|
const local = localSession();
|
|
557
572
|
const result = await request("POST", `/api/v1/mac/sessions/${sessionId(local.id)}/extend`, { body: { minutes } });
|
|
558
573
|
saveView(local, result.session);
|
|
@@ -588,7 +603,7 @@ export async function hostedSessionCommand(command, argv, { store = sessionStore
|
|
|
588
603
|
}
|
|
589
604
|
if (["exec", "job", "output"].includes(command)) {
|
|
590
605
|
const local = localSession(), base = `/api/v1/mac/sessions/${sessionId(local.id)}`;
|
|
591
|
-
let path, method = "GET", params = {}, waitSeconds = integer(values.wait, 30, 0, 3600);
|
|
606
|
+
let path, method = "GET", params = {}, waitSeconds = integer(values.wait, 30, 0, 3600, "--wait (seconds)");
|
|
592
607
|
if (command === "exec") {
|
|
593
608
|
if (!positionals.length || !argv.includes("--")) throw new CliError("Use nomac exec -- command args");
|
|
594
609
|
if (!local.workspaceDecided && !local.workspace) { await refresh(local); if (local.state === "ready") await placeWorkspace(local); }
|
|
@@ -599,8 +614,8 @@ export async function hostedSessionCommand(command, argv, { store = sessionStore
|
|
|
599
614
|
// A command may run an hour (the gateway's limit); waiting can cover all of it.
|
|
600
615
|
// A command given a long --timeout and no --wait is waited for too, up to 10 minutes (what an
|
|
601
616
|
// agent's shell usually allows); the line printed after 5 s says how to go on if interrupted.
|
|
602
|
-
const given = values.timeout === undefined ? null : integer(values.timeout, 0, 1, 3600);
|
|
603
|
-
const waitFor = waitSeconds = integer(values.wait, given === null ? 30 : Math.min(given, 600), 0, 3600);
|
|
617
|
+
const given = values.timeout === undefined ? null : integer(values.timeout, 0, 1, 3600, "--timeout (seconds)");
|
|
618
|
+
const waitFor = waitSeconds = integer(values.wait, given === null ? 30 : Math.min(given, 600), 0, 3600, "--wait (seconds)");
|
|
604
619
|
const timeout = given ?? (values["request-key"] === undefined ? Math.max(300, waitFor) : 600);
|
|
605
620
|
const line = isShellLine(positionals), dir = local.workspaceOnDrive ? local.workspace : null;
|
|
606
621
|
const onMac = dir ? (line ? ["/bin/bash", "-lc", `cd ${shellQuote(dir)} && ${positionals[0]}`] : ["/bin/bash", "-lc", `cd ${shellQuote(dir)} && exec "$@"`, "nomac", ...positionals])
|
|
@@ -658,7 +673,7 @@ export async function hostedSessionCommand(command, argv, { store = sessionStore
|
|
|
658
673
|
return saved;
|
|
659
674
|
}
|
|
660
675
|
if (positionals.length) throw new CliError("Usage: nomac record [--simulator] [--max SECONDS], then nomac record stop [-o FILE]");
|
|
661
|
-
const started = await startRecording(local.id, { target: values.simulator ? "simulator" : "screen", maxSeconds: values.max === undefined ? undefined : integer(values.max, 300, 1, 1800) });
|
|
676
|
+
const started = await startRecording(local.id, { target: values.simulator ? "simulator" : "screen", maxSeconds: values.max === undefined ? undefined : integer(values.max, 300, 1, 1800, "--max (seconds)") });
|
|
662
677
|
print(values.json ? JSON.stringify(started, null, 2) : `Recording the ${values.simulator ? "Simulator" : "screen"}; it stops by itself after ${started.max_seconds} s.\nSave it: nomac record stop`);
|
|
663
678
|
return started;
|
|
664
679
|
}
|
|
@@ -811,23 +826,33 @@ export async function hostedSessionCommand(command, argv, { store = sessionStore
|
|
|
811
826
|
const target = wanted ? live.find(s => s.id === wanted) : live.length === 1 ? live[0] : undefined;
|
|
812
827
|
if (!target) throw new CliError(wanted ? `No running Mac ${wanted} in this account.` : live.length ? `This account has ${live.length} running Macs: name one, nomac attach ${live.map(s => s.id).join(" | ")}` : "This account has no running Mac. Start one: nomac start");
|
|
813
828
|
const local = await attachTo(target);
|
|
814
|
-
return output({ session: local.snapshot, attached: `Attached to ${local.id}, started elsewhere: ssh, sync, pull and exec work from this folder.`, ...project(local) });
|
|
829
|
+
return output({ session: local.snapshot, attached: `Attached to ${local.id}, started elsewhere: ssh, sync, pull and exec work from this folder. If another agent or folder is using it, leave it running when you are done here (nomac stop stops it for everyone).`, ...project(local) });
|
|
815
830
|
}
|
|
816
831
|
if (command === "start") {
|
|
817
832
|
if (positionals.length) throw new CliError("Usage: nomac start [--wait SECONDS] [--json]");
|
|
818
833
|
let local = store.load();
|
|
819
834
|
if (local && local.mode !== "hosted") throw new CliError("An existing Mac workspace is attached.");
|
|
820
|
-
const limits = { max_duration_seconds: integer(values["max-duration"], 7200, 60, 28800), idle_timeout_seconds: integer(values["idle-timeout"], 600, 60, 3600), spend_cap_microunits: budget(values["max-cost"]) };
|
|
835
|
+
const limits = { max_duration_seconds: integer(values["max-duration"], 7200, 60, 28800, "--max-duration (seconds, 8 hours at most)"), idle_timeout_seconds: integer(values["idle-timeout"], 600, 60, 3600, "--idle-timeout (seconds, 1 hour at most)"), spend_cap_microunits: budget(values["max-cost"]) };
|
|
821
836
|
const access = accessOption(values);
|
|
822
837
|
// Older CLIs persisted invalid keys before contacting the API. Such a key
|
|
823
838
|
// cannot pass server validation and therefore cannot have allocated a VM.
|
|
824
839
|
if (local && !local.id && !validRequestKey(local.requestKey)) { local.state = "failed"; store.save(local); }
|
|
840
|
+
// A Mac this folder attached to (nomac attach) was started elsewhere, and someone may be using it there: --parallel
|
|
841
|
+
// leaves it running and starts a Mac of this folder's own, and a start with other settings never says to stop it.
|
|
842
|
+
const limitFlags = [["max-duration", "max_duration_seconds"], ["idle-timeout", "idle_timeout_seconds"], ["max-cost", "spend_cap_microunits"]];
|
|
843
|
+
const otherAccess = () => access && JSON.stringify({ prompts: "auto", gatekeeper: "off", ...access }) !== JSON.stringify({ prompts: "auto", gatekeeper: "off", ...local.request.access });
|
|
844
|
+
const attachedElsewhere = local?.attached && local.id && !["stopped", "failed"].includes(local.state);
|
|
845
|
+
if (attachedElsewhere && values.parallel) local = undefined;
|
|
846
|
+
else if (attachedElsewhere && (limitFlags.some(([flag, field]) => values[flag] !== undefined && limits[field] !== local.request.limits[field])
|
|
847
|
+
|| (values["request-key"] !== undefined && values["request-key"] !== local.requestKey) || otherAccess())) {
|
|
848
|
+
throw new CliError(`This folder is attached to your account's Mac ${local.id}, started elsewhere with other settings; another agent or folder may be using it, so leave it running: stopping it ends their work. Use it as it is (start without --max-duration, --idle-timeout, --max-cost, --request-key or access options), or start a Mac of this folder's own at the same time, with your options: nomac start --parallel`);
|
|
849
|
+
}
|
|
825
850
|
if (local && !["stopped", "failed"].includes(local.state)) {
|
|
826
|
-
for (const [flag, field] of
|
|
827
|
-
if (values[flag] !== undefined && limits[field] !== local.request.limits[field]) throw new CliError(
|
|
851
|
+
for (const [flag, field] of limitFlags) {
|
|
852
|
+
if (values[flag] !== undefined && limits[field] !== local.request.limits[field]) throw new CliError(`This folder's Mac ${local.id} is running with other limits. Use it as it is (nomac extend MINUTES gives it more time while it runs), or, once it is finished, stop it (nomac stop) and start again with these. If another agent uses this folder, leave it running: stopping it ends their work.`);
|
|
828
853
|
}
|
|
829
854
|
if (values["request-key"] !== undefined && values["request-key"] !== local.requestKey) throw new CliError("A session is already attached under a different request key. Inspect it or stop it before starting another.");
|
|
830
|
-
if (
|
|
855
|
+
if (otherAccess()) {
|
|
831
856
|
throw new CliError("This session was started with different --prompts, --gatekeeper or --permit. Stop it and start a fresh session to change them; nomac permit grants more on the running Mac.");
|
|
832
857
|
}
|
|
833
858
|
}
|
|
@@ -845,17 +870,17 @@ export async function hostedSessionCommand(command, argv, { store = sessionStore
|
|
|
845
870
|
let result;
|
|
846
871
|
try { result = await request("POST", "/api/v1/mac/sessions", { idempotencyKey: local.requestKey, body: local.request }); }
|
|
847
872
|
catch (error) {
|
|
848
|
-
// One Mac runs per account: when it was started elsewhere (an agent over MCP, the dashboard), use it from here.
|
|
849
|
-
if (error.code === "session_limit_reached" && values["request-key"] === undefined && !values.parallel) {
|
|
850
|
-
const [running] = await liveSessions();
|
|
851
|
-
if (running) {
|
|
852
|
-
local = await attachTo(running);
|
|
853
|
-
return output({ session: local.snapshot, attached: `Your account's Mac ${local.id} was already running (started elsewhere): this folder now uses it.`, ...project(local) });
|
|
854
|
-
}
|
|
855
|
-
}
|
|
856
873
|
if (!previouslyUncertain && startRejections.has(error.code) && Number.isInteger(error.status) && error.status >= 400 && error.status <= 503) {
|
|
857
874
|
local.state = "failed"; local.rejection = error.code; store.save(local);
|
|
858
875
|
}
|
|
876
|
+
// The account's Mac runs elsewhere (another agent or folder, MCP, the dashboard). This folder is never moved
|
|
877
|
+
// onto it unasked: November 6, 2026, a customer's two agents were each moved onto the other's Mac when their
|
|
878
|
+
// own start was refused, and stopped it at their end, killing the other's builds for hours. Using it is a
|
|
879
|
+
// choice made by its id.
|
|
880
|
+
if (error.code === "session_limit_reached") {
|
|
881
|
+
const running = (await liveSessions().catch(() => [])).map(s => s.id);
|
|
882
|
+
if (running.length) throw new CliError(error.message, `${error.friendly ?? error.message}\n use ${running.length === 1 ? "it" : "one"} from this folder: nomac attach ${running.join(" | ")}${values.parallel ? "" : "\n a Mac of this folder's own at the same time: nomac start --parallel"}`, error.status, error.code, error.details);
|
|
883
|
+
}
|
|
859
884
|
throw error;
|
|
860
885
|
}
|
|
861
886
|
// The start's warning (what the Mac allows, how to opt out) is shown with every start of this session.
|
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,17 +1,31 @@
|
|
|
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, readSync, 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
|
|
12
|
-
* rebuilt on the Mac anyway, git history, Finder metadata
|
|
13
|
-
*
|
|
14
|
-
* .env files, keys and certificates travel like any other file.
|
|
27
|
+
* rebuilt on the Mac anyway, git history, Finder metadata, NoMac's own files,
|
|
28
|
+
* and secrets (secretFile). nomac secrets puts a secret on every Mac encrypted.
|
|
15
29
|
*/
|
|
16
30
|
const BUILT_IN = [
|
|
17
31
|
{ pattern: "node_modules/", reason: "rebuilt on the Mac" },
|
|
@@ -28,6 +42,30 @@ const BUILT_IN = [
|
|
|
28
42
|
{ pattern: "nomac-screenshot.jpg", reason: "saved by nomac screenshot" },
|
|
29
43
|
{ pattern: "nomac-recording.mp4", reason: "saved by nomac record" },
|
|
30
44
|
];
|
|
45
|
+
/** The reason sync gives for a secret it kept on this computer. */
|
|
46
|
+
export const SECRET_REASON = "a secret, kept on this computer";
|
|
47
|
+
const PRIVATE_KEY = /-----BEGIN (?:[A-Z0-9]+ )*PRIVATE KEY-----/;
|
|
48
|
+
/** Whether a .pem or .key file holds a private key (a certificate an app bundles holds none), from its first 1 MiB. */
|
|
49
|
+
function holdsPrivateKey(path) {
|
|
50
|
+
let fd;
|
|
51
|
+
try {
|
|
52
|
+
fd = openSync(path, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
53
|
+
const buffer = Buffer.alloc(1024 * 1024), read = readSync(fd, buffer, 0, buffer.length, 0);
|
|
54
|
+
return PRIVATE_KEY.test(buffer.subarray(0, read).toString("latin1"));
|
|
55
|
+
} catch { return false; } finally { if (fd !== undefined) closeSync(fd); }
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Secrets sync keeps on this computer unless a .nomacignore `!` line sends one: environment files (not their
|
|
59
|
+
* examples), App Store Connect keys and signing identities (.p8, .p12, .pfx), Android keystores, private SSH keys,
|
|
60
|
+
* .netrc, and .pem or .key files that hold a private key. Provisioning profiles and certificates are not secret and
|
|
61
|
+
* are sent: a device build on the Mac needs them.
|
|
62
|
+
*/
|
|
63
|
+
export function secretFile(name, path) {
|
|
64
|
+
if (/^\.env(\..+)?$/.test(name)) return !/\.(example|sample|template)$/.test(name);
|
|
65
|
+
if (/\.(p8|p12|pfx|keystore|jks)$/i.test(name) || /^id_(rsa|dsa|ecdsa|ed25519)$/.test(name) || name === ".netrc") return true;
|
|
66
|
+
return /\.(pem|key)$/i.test(name) && holdsPrivateKey(path);
|
|
67
|
+
}
|
|
68
|
+
|
|
31
69
|
// Built-in patterns are names, matched against the entry's own name, so a folder
|
|
32
70
|
// brought back with `!` is never skipped again through its parent.
|
|
33
71
|
const builtInRules = BUILT_IN.map(({ pattern, reason }) => ({ rules: ignoreFactory().add(pattern), reason }));
|
|
@@ -49,8 +87,8 @@ function verdict(scopes, path, isDirectory) {
|
|
|
49
87
|
}
|
|
50
88
|
|
|
51
89
|
/**
|
|
52
|
-
* The project as sync sends it. Every file is sent, including files a
|
|
53
|
-
* .gitignore lists (
|
|
90
|
+
* The project as sync sends it. Every file but a secret is sent, including files a
|
|
91
|
+
* .gitignore lists (local configuration, temporary files): they are
|
|
54
92
|
* small and often needed to build. Folders a .gitignore lists are skipped,
|
|
55
93
|
* because they are dependencies and build output. .nomacignore decides last:
|
|
56
94
|
* it can skip anything more, and its `!` lines bring back anything, the
|
|
@@ -75,6 +113,8 @@ export function projectFiles(root) {
|
|
|
75
113
|
reason = builtInRules.find(rule => rule.rules.ignores(entry.name + (isDirectory ? "/" : "")))?.reason;
|
|
76
114
|
if (!reason && isDirectory && verdict(git, path, true)) reason = "listed in .gitignore";
|
|
77
115
|
}
|
|
116
|
+
// A secret stays here even inside a folder .nomacignore brought back; only its own `!` line sends it.
|
|
117
|
+
if (!reason && entry.isFile() && secretFile(entry.name, path)) reason = SECRET_REASON;
|
|
78
118
|
const decision = verdict(nomac, path, isDirectory);
|
|
79
119
|
const bringsBack = decision === false && reason !== undefined;
|
|
80
120
|
if (decision === true) reason = "listed in .nomacignore";
|
|
@@ -95,7 +135,7 @@ export async function fingerprint(path) {
|
|
|
95
135
|
if (!stat.isFile()) return "other";
|
|
96
136
|
const hash = createHash("sha256");
|
|
97
137
|
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)}`;
|
|
138
|
+
return MODELESS ? `file:${hash.digest("hex")}` : `file:${hash.digest("hex")}:${(stat.mode & 0o777).toString(8)}`;
|
|
99
139
|
}
|
|
100
140
|
|
|
101
141
|
export function safeRelative(path) {
|
|
@@ -157,7 +197,7 @@ export async function remoteFingerprints(session, files, options) {
|
|
|
157
197
|
for (const path of batch) {
|
|
158
198
|
const value = Object.hasOwn(values, path) && values[path];
|
|
159
199
|
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 });
|
|
200
|
+
Object.defineProperty(result, path, { value: MODELESS ? withoutMode(value) : value, enumerable: true, configurable: true, writable: true });
|
|
161
201
|
}
|
|
162
202
|
}
|
|
163
203
|
return result;
|
|
@@ -208,6 +248,144 @@ function transferArgs() {
|
|
|
208
248
|
return ["-rpt", "--checksum", "--itemize-changes", "--stats", "--timeout=60", "-e", [process.execPath, bridge].map(rsyncQuote).join(" ")];
|
|
209
249
|
}
|
|
210
250
|
|
|
251
|
+
const BLOCK = 512;
|
|
252
|
+
const padding = size => Buffer.alloc((BLOCK - (size % BLOCK)) % BLOCK);
|
|
253
|
+
/** A pax record, "LENGTH key=value\n", whose length counts itself. */
|
|
254
|
+
function paxRecord(key, value) {
|
|
255
|
+
const rest = Buffer.byteLength(` ${key}=${value}\n`);
|
|
256
|
+
let length = rest + 1;
|
|
257
|
+
while (String(length).length + rest !== length) length = String(length).length + rest;
|
|
258
|
+
return `${length} ${key}=${value}\n`;
|
|
259
|
+
}
|
|
260
|
+
function ustar({ path, type, size, mode, mtime, linkpath = "" }) {
|
|
261
|
+
const block = Buffer.alloc(BLOCK);
|
|
262
|
+
const field = (offset, length, value) => block.write(value, offset, length, "utf8");
|
|
263
|
+
const octal = (offset, length, value) => field(offset, length, value.toString(8).padStart(length - 1, "0") + "\0");
|
|
264
|
+
block.set(Buffer.from(path).subarray(0, 100), 0);
|
|
265
|
+
octal(100, 8, mode); octal(108, 8, 0); octal(116, 8, 0); octal(124, 12, size); octal(136, 12, mtime);
|
|
266
|
+
field(148, 8, " "); field(156, 1, type);
|
|
267
|
+
block.set(Buffer.from(linkpath).subarray(0, 100), 157);
|
|
268
|
+
field(257, 6, "ustar\0"); field(263, 2, "00");
|
|
269
|
+
const sum = block.reduce((total, byte) => total + byte, 0);
|
|
270
|
+
field(148, 8, sum.toString(8).padStart(6, "0") + "\0 ");
|
|
271
|
+
return block;
|
|
272
|
+
}
|
|
273
|
+
/** An entry's header: ustar, after a pax record for what ustar cannot hold (long or non-ASCII paths, big files). */
|
|
274
|
+
function tarHeader(entry) {
|
|
275
|
+
const ascii = value => Buffer.byteLength(value) <= 100 && /^[\x20-\x7e]*$/.test(value);
|
|
276
|
+
const records = [
|
|
277
|
+
...(ascii(entry.path) ? [] : [paxRecord("path", entry.path)]),
|
|
278
|
+
...(entry.linkpath === undefined || ascii(entry.linkpath) ? [] : [paxRecord("linkpath", entry.linkpath)]),
|
|
279
|
+
...(entry.size > 0o77777777777 ? [paxRecord("size", String(entry.size))] : []),
|
|
280
|
+
];
|
|
281
|
+
if (!records.length) return ustar(entry);
|
|
282
|
+
const body = Buffer.from(records.join(""));
|
|
283
|
+
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) })]);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* The project's changed files as a tar stream: files with their mode and mtime (as rsync -pt keeps them),
|
|
288
|
+
* symlinks as links, never followed. A file read while it changes is cut or padded to the size it had,
|
|
289
|
+
* and the check after the transfer names it.
|
|
290
|
+
*/
|
|
291
|
+
async function* tarOf(root, paths, modeOf) {
|
|
292
|
+
for (const path of paths) {
|
|
293
|
+
const full = join(root, path), stat = lstatSync(full), mtime = Math.floor(stat.mtimeMs / 1000);
|
|
294
|
+
if (stat.isSymbolicLink()) { yield tarHeader({ path, type: "2", size: 0, mode: 0o755, mtime, linkpath: readlinkSync(full) }); continue; }
|
|
295
|
+
yield tarHeader({ path, type: "0", size: stat.size, mode: modeOf(path, stat), mtime });
|
|
296
|
+
let sent = 0;
|
|
297
|
+
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; }
|
|
298
|
+
if (sent < stat.size) yield Buffer.alloc(stat.size - sent);
|
|
299
|
+
yield padding(stat.size);
|
|
300
|
+
}
|
|
301
|
+
yield Buffer.alloc(BLOCK * 2);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** A path from a downloaded tar: relative, inside the folder, no backslash or control character. */
|
|
305
|
+
function entryPath(name) {
|
|
306
|
+
const path = name.replace(/^(?:\.\/)+/, "").replace(/\/+$/, "");
|
|
307
|
+
if (path === "" || path === ".") return ".";
|
|
308
|
+
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)}.`);
|
|
309
|
+
return path;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Unpacks a tar stream from the Mac into an empty folder as it arrives: files with their mode and mtime and
|
|
314
|
+
* folders. Links and special files are left out, as rsync without --links leaves them.
|
|
315
|
+
*/
|
|
316
|
+
function untar(directory) {
|
|
317
|
+
let buffer = Buffer.alloc(0), remaining = 0, skip = 0, entry = null, pax = {}, done = false;
|
|
318
|
+
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"); };
|
|
319
|
+
const number = (block, start, end) => {
|
|
320
|
+
if (block[start] & 0x80) { let value = 0; for (let i = start + 1; i < end; i++) value = value * 256 + block[i]; return value; }
|
|
321
|
+
return parseInt(text(block, start, end).trim() || "0", 8);
|
|
322
|
+
};
|
|
323
|
+
const finish = () => {
|
|
324
|
+
if (entry?.kind === "file") { closeSync(entry.fd); chmodSync(entry.full, entry.mode & 0o777); utimesSync(entry.full, entry.mtime, entry.mtime); }
|
|
325
|
+
if (entry?.kind === "pax") for (const record of Buffer.concat(entry.data).toString("utf8").split("\n")) {
|
|
326
|
+
const match = /^\d+ ([^=]+)=(.*)$/s.exec(record);
|
|
327
|
+
if (match) pax[match[1]] = match[2];
|
|
328
|
+
}
|
|
329
|
+
entry = null;
|
|
330
|
+
};
|
|
331
|
+
const header = block => {
|
|
332
|
+
if (block.every(byte => byte === 0)) { done = true; return; }
|
|
333
|
+
const stored = number(block, 148, 156), sum = block.reduce((total, byte, i) => total + (i >= 148 && i < 156 ? 32 : byte), 0);
|
|
334
|
+
if (stored !== sum) throw new CliError("invalid download", "The download from the Mac was cut or corrupted. Run nomac pull again.");
|
|
335
|
+
const type = String.fromCharCode(block[156] || 48), prefix = text(block, 345, 500);
|
|
336
|
+
const name = pax.path ?? (prefix ? `${prefix}/${text(block, 0, 100)}` : text(block, 0, 100));
|
|
337
|
+
const size = pax.size !== undefined ? Number(pax.size) : number(block, 124, 136);
|
|
338
|
+
const linkpath = pax.linkpath ?? text(block, 157, 257);
|
|
339
|
+
const mtime = pax.mtime !== undefined ? Math.floor(Number(pax.mtime)) : number(block, 136, 148);
|
|
340
|
+
remaining = size; skip = padding(size).length;
|
|
341
|
+
if (type === "x") { entry = { kind: "pax", data: [] }; return; }
|
|
342
|
+
pax = {};
|
|
343
|
+
if (type === "g") { entry = { kind: "skip" }; return; }
|
|
344
|
+
const path = entryPath(name);
|
|
345
|
+
if (type === "5") { if (path !== ".") mkdirSync(join(directory, path), { recursive: true, mode: 0o700 }); entry = { kind: "skip" }; return; }
|
|
346
|
+
if (type === "0" || type === "7") {
|
|
347
|
+
const full = join(directory, path);
|
|
348
|
+
mkdirSync(dirname(full), { recursive: true, mode: 0o700 });
|
|
349
|
+
entry = { kind: "file", full, fd: openSync(full, "wx", 0o600), mode: number(block, 100, 108), mtime };
|
|
350
|
+
return;
|
|
351
|
+
}
|
|
352
|
+
// A hard link is a second name for a file already unpacked: a copy of it.
|
|
353
|
+
if (type === "1") { mkdirSync(dirname(join(directory, path)), { recursive: true, mode: 0o700 }); copyFileSync(join(directory, entryPath(linkpath)), join(directory, path)); }
|
|
354
|
+
entry = { kind: "skip" };
|
|
355
|
+
};
|
|
356
|
+
return {
|
|
357
|
+
write(chunk) {
|
|
358
|
+
buffer = buffer.length ? Buffer.concat([buffer, chunk]) : chunk;
|
|
359
|
+
while (!done) {
|
|
360
|
+
if (remaining > 0) {
|
|
361
|
+
if (!buffer.length) return;
|
|
362
|
+
const part = buffer.subarray(0, Math.min(remaining, buffer.length));
|
|
363
|
+
buffer = buffer.subarray(part.length); remaining -= part.length;
|
|
364
|
+
if (entry?.kind === "file") writeSync(entry.fd, part);
|
|
365
|
+
else if (entry?.kind === "pax") entry.data.push(part);
|
|
366
|
+
if (remaining > 0) return;
|
|
367
|
+
finish();
|
|
368
|
+
} else if (skip > 0) {
|
|
369
|
+
if (!buffer.length) return;
|
|
370
|
+
const n = Math.min(skip, buffer.length);
|
|
371
|
+
buffer = buffer.subarray(n); skip -= n;
|
|
372
|
+
} else {
|
|
373
|
+
if (buffer.length < BLOCK) return;
|
|
374
|
+
const block = buffer.subarray(0, BLOCK);
|
|
375
|
+
buffer = buffer.subarray(BLOCK);
|
|
376
|
+
header(block);
|
|
377
|
+
if (!done && remaining === 0) finish();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
/** Closes a file left open by a download that stopped. */
|
|
382
|
+
close() { if (entry?.kind === "file") { closeSync(entry.fd); entry = null; } },
|
|
383
|
+
end() {
|
|
384
|
+
if (!done || entry) throw new CliError("invalid download", "The download from the Mac ended early. Run nomac pull again.");
|
|
385
|
+
},
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
211
389
|
/** Size, the biggest files, and what was skipped with the .nomacignore line that would send it. */
|
|
212
390
|
function summarize(root, { files, links, skipped }) {
|
|
213
391
|
const sizes = files.map(path => ({ path, bytes: lstatSync(join(root, path)).size }));
|
|
@@ -227,10 +405,17 @@ export function describeSync(result) {
|
|
|
227
405
|
if (result.conflicts?.length) lines.push(`Would not send, changed on the Mac since the last sync: ${result.conflicts.slice(0, 10).join(", ")}${result.conflicts.length > 10 ? ` and ${result.conflicts.length - 10} more` : ""}.`);
|
|
228
406
|
if (result.deleted?.length) lines.push(`${result.dry_run ? "Would delete" : "Deleted"} on the Mac, as here: ${result.deleted.slice(0, 10).join(", ")}${result.deleted.length > 10 ? ` and ${result.deleted.length - 10} more` : ""}.`);
|
|
229
407
|
if (result.kept_on_mac?.length) lines.push(`Kept on the Mac, changed there since: ${result.kept_on_mac.slice(0, 10).join(", ")}${result.kept_on_mac.length > 10 ? ` and ${result.kept_on_mac.length - 10} more` : ""}.`);
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
408
|
+
const secrets = result.skipped.filter(item => item.reason === SECRET_REASON), others = result.skipped.filter(item => item.reason !== SECRET_REASON);
|
|
409
|
+
if (others.length) {
|
|
410
|
+
const shown = others.slice(0, 6).map(item => `${item.path} (${item.reason})`).join(", ");
|
|
411
|
+
lines.push(`Not sent: ${shown}${others.length > 6 ? `, and ${others.length - 6} more (--json lists them)` : ""}. To send one, add a line such as ${others[0].send_with} to .nomacignore.`);
|
|
412
|
+
}
|
|
413
|
+
if (secrets.length) {
|
|
414
|
+
const env = secrets.find(item => /(^|\/)\.env(\.[^/]+)?$/.test(item.path)), file = secrets.find(item => item !== env);
|
|
415
|
+
const ways = [env && `nomac secrets set --from-env-file ${env.path} (its lines as variables)`, file && `nomac secrets set NAME --file ${file.path} --destination ~/... (a file)`].filter(Boolean).join(", or ");
|
|
416
|
+
lines.push(`Kept on this computer, as secrets: ${listed(secrets.map(item => item.path))}. To give every Mac a secret encrypted: ${ways}. To sync one anyway, add a line such as ${secrets[0].send_with} to .nomacignore.`);
|
|
233
417
|
}
|
|
418
|
+
if (result.secrets_left_on_mac?.length) lines.push(`An earlier sync sent ${listed(result.secrets_left_on_mac)} to the Mac's drive, where ${result.secrets_left_on_mac.length === 1 ? "it stays" : "they stay"} until removed: nomac exec -- rm ${result.secrets_left_on_mac.map(shellQuote).join(" ")}`);
|
|
234
419
|
return lines.join("\n");
|
|
235
420
|
}
|
|
236
421
|
|
|
@@ -265,7 +450,7 @@ function syncConflictError(paths, sent, removal = {}) {
|
|
|
265
450
|
* earlier sync sent and the person has since deleted here are removed on the Mac too, unless
|
|
266
451
|
* they changed there.
|
|
267
452
|
*/
|
|
268
|
-
export async function syncProject(session, store, { overwrite = false, dryRun = false, deleteMissing = false, run = runProcess } = {}) {
|
|
453
|
+
export async function syncProject(session, store, { overwrite = false, dryRun = false, deleteMissing = false, run = runProcess, transport = defaultTransport() } = {}) {
|
|
269
454
|
const selection = projectFiles(store.project);
|
|
270
455
|
const paths = [...selection.files, ...selection.links].sort();
|
|
271
456
|
const baseline = Object.assign(Object.create(null), store.baseline());
|
|
@@ -282,11 +467,17 @@ export async function syncProject(session, store, { overwrite = false, dryRun =
|
|
|
282
467
|
const changed = send.filter(path => previous[path] !== local[path]), summary = summarize(store.project, selection);
|
|
283
468
|
const onMac = gone.length ? await remoteFingerprints(session, gone, { run }) : {};
|
|
284
469
|
const removable = gone.filter(path => onMac[path] === baseline[path]), keptOnMac = gone.filter(path => !["missing", baseline[path]].includes(onMac[path]));
|
|
285
|
-
if (dryRun) return { dry_run: true, files: paths.length, changed, ...(conflicts.length ? { conflicts } : {}), ...(deleteMissing ? { deleted: removable, kept_on_mac: keptOnMac } : {}), ...summary };
|
|
470
|
+
if (dryRun) return { dry_run: true, files: paths.length, changed, ...(conflicts.length ? { conflicts } : {}), ...(deleteMissing ? { deleted: removable, kept_on_mac: keptOnMac } : {}), ...summary, ...leftOnMac(selection, baseline) };
|
|
286
471
|
let output = "";
|
|
287
472
|
// Only what differs travels; a file already the same on the Mac is recorded as such.
|
|
288
473
|
for (const path of send) if (previous[path] === local[path]) baseline[path] = local[path];
|
|
289
|
-
if (changed.length) {
|
|
474
|
+
if (changed.length && transport === "tar") {
|
|
475
|
+
// The Mac's tar (bsdtar) refuses absolute paths, ".." and writing through a link, and replaces a link
|
|
476
|
+
// where a file goes. Windows keeps no executable bit: git's index has it for the files it tracks.
|
|
477
|
+
const executables = MODELESS ? await executablesInGit(store.project) : null;
|
|
478
|
+
const modeOf = (path, stat) => (executables ? (executables.has(path) ? 0o755 : 0o644) : stat.mode & 0o777);
|
|
479
|
+
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)) });
|
|
480
|
+
} else if (changed.length) {
|
|
290
481
|
// --links sends a symlink as a link: its target is never read or followed.
|
|
291
482
|
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
483
|
}
|
|
@@ -304,7 +495,13 @@ export async function syncProject(session, store, { overwrite = false, dryRun =
|
|
|
304
495
|
throw error;
|
|
305
496
|
}
|
|
306
497
|
if (conflicts.length) throw syncConflictError(conflicts, changed.length, deleteMissing ? { deleted, kept_on_mac: keptOnMac } : {});
|
|
307
|
-
return { files: paths.length, changed, ...(deleteMissing ? { deleted, kept_on_mac: keptOnMac } : {}), ...summary, output };
|
|
498
|
+
return { files: paths.length, changed, ...(deleteMissing ? { deleted, kept_on_mac: keptOnMac } : {}), ...summary, ...leftOnMac(selection, baseline), output };
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
/** Secrets an earlier CLI sent (it sent every file) that are still in the Mac's copy: sync never deletes them itself. */
|
|
502
|
+
function leftOnMac(selection, baseline) {
|
|
503
|
+
const left = selection.skipped.filter(item => item.reason === SECRET_REASON && baseline[item.path] !== undefined).map(item => item.path);
|
|
504
|
+
return left.length ? { secrets_left_on_mac: left } : {};
|
|
308
505
|
}
|
|
309
506
|
|
|
310
507
|
/** Refuse symlink ancestors, including the destination itself, before local writes. */
|
|
@@ -331,7 +528,7 @@ export function localDestination(store, destination) {
|
|
|
331
528
|
return { requested, inProject, projectPath: inProject ? (rel.split(sep).join("/") || ".") : null };
|
|
332
529
|
}
|
|
333
530
|
|
|
334
|
-
export async function pullProject(session, store, destination, { from, overwrite = false, dryRun = false, run = runProcess } = {}) {
|
|
531
|
+
export async function pullProject(session, store, destination, { from, overwrite = false, dryRun = false, run = runProcess, transport = defaultTransport() } = {}) {
|
|
335
532
|
const { requested, inProject, projectPath } = localDestination(store, destination);
|
|
336
533
|
// A folder in the project comes from the same folder on the Mac unless --from says otherwise.
|
|
337
534
|
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 +554,16 @@ export async function pullProject(session, store, destination, { from, overwrite
|
|
|
357
554
|
const staging = mkdtempSync(join(store.directory, "pull-"));
|
|
358
555
|
const backup = join(store.directory, "backups", randomUUID());
|
|
359
556
|
try {
|
|
360
|
-
// Download into an empty private directory; rsync never receives the user's working tree as destination.
|
|
361
|
-
|
|
557
|
+
// Download into an empty private directory; rsync or tar never receives the user's working tree as destination.
|
|
558
|
+
if (transport === "tar") {
|
|
559
|
+
const unpack = untar(staging);
|
|
560
|
+
// The same checks as the rsync bridge: no link on the way to the source, which is read without macOS's metadata.
|
|
561
|
+
try {
|
|
562
|
+
await run("ssh", [...sshArgs(session), session.target, [...sourceChecks(session, source),
|
|
563
|
+
`COPYFILE_DISABLE=1 exec tar -cf - --no-mac-metadata --no-xattrs --no-acls --no-fflags -- ${shellQuote(single ?? ".")}`].join("\n")], { onStdout: chunk => unpack.write(chunk) });
|
|
564
|
+
} finally { unpack.close(); }
|
|
565
|
+
unpack.end();
|
|
566
|
+
} else await run("rsync", [...transferArgs(), `${session.target}:./${single ?? ""}`, `${staging}/`], { env: transferEnv(session, source) });
|
|
362
567
|
const files = [];
|
|
363
568
|
const walk = (directory) => {
|
|
364
569
|
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
|