@hanzo/cli 8.5.1341 → 8.5.1422
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/bin/hanzo.js +16 -4
- package/install.js +81 -26
- package/package.json +1 -1
package/bin/hanzo.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
// `hanzo-vm`, `zt`, `hanzod`) are always discoverable, and lazily fetches any
|
|
7
7
|
// missing companion binary on first invocation via `../install.js`.
|
|
8
8
|
const { spawnSync } = require("node:child_process");
|
|
9
|
-
const { existsSync, realpathSync } = require("node:fs");
|
|
9
|
+
const { accessSync, constants, existsSync, realpathSync, statSync } = require("node:fs");
|
|
10
10
|
const { basename, delimiter, join } = require("node:path");
|
|
11
11
|
|
|
12
12
|
const ext = process.platform === "win32" ? ".exe" : "";
|
|
@@ -35,14 +35,16 @@ const TOOL_BINS = {
|
|
|
35
35
|
// count: treating one as the tool skips the download and then execs nothing.
|
|
36
36
|
function realBinary(p) {
|
|
37
37
|
try {
|
|
38
|
-
|
|
38
|
+
if (!statSync(p).isFile()) return false;
|
|
39
|
+
if (process.platform !== "win32") accessSync(p, constants.X_OK);
|
|
40
|
+
return !realpathSync(p).toLowerCase().endsWith(".js");
|
|
39
41
|
} catch {
|
|
40
42
|
return false;
|
|
41
43
|
}
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
function hasLocal(tool) {
|
|
45
|
-
return
|
|
47
|
+
return realBinary(join(__dirname, (TOOL_BINS[tool] || tool) + ext));
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
function hasTool(tool) {
|
|
@@ -135,4 +137,14 @@ const r = spawnSync(bin, argv, {
|
|
|
135
137
|
env,
|
|
136
138
|
});
|
|
137
139
|
if (r.error) { console.error("hanzo:", r.error.message); process.exit(1); }
|
|
138
|
-
|
|
140
|
+
if (r.signal) {
|
|
141
|
+
// Node reserves SIGUSR1 for the debugger and ignores SIGPIPE. Sending
|
|
142
|
+
// these to ourselves would start an inspector (or silently do nothing)
|
|
143
|
+
// rather than preserving the native program's termination semantics.
|
|
144
|
+
if (process.platform !== "win32" && r.signal !== "SIGUSR1" && r.signal !== "SIGPIPE") {
|
|
145
|
+
try { process.kill(process.pid, r.signal); } catch { /* use shell exit status below */ }
|
|
146
|
+
}
|
|
147
|
+
const number = require("node:os").constants.signals[r.signal];
|
|
148
|
+
process.exit(typeof number === "number" ? 128 + number : 1);
|
|
149
|
+
}
|
|
150
|
+
process.exit(r.status ?? 1);
|
package/install.js
CHANGED
|
@@ -15,8 +15,23 @@
|
|
|
15
15
|
// binaries (`dev`, `mcp`, `engine`, `node`, `zt`, `vm`, `gen`) are fetched
|
|
16
16
|
// lazily on first use by `bin/hanzo.js` (or eagerly with `HANZO_NPM_EAGER=1` /
|
|
17
17
|
// `node install.js --all`).
|
|
18
|
-
const {
|
|
18
|
+
const {
|
|
19
|
+
createWriteStream,
|
|
20
|
+
mkdirSync,
|
|
21
|
+
mkdtempSync,
|
|
22
|
+
chmodSync,
|
|
23
|
+
copyFileSync,
|
|
24
|
+
cpSync,
|
|
25
|
+
existsSync,
|
|
26
|
+
lstatSync,
|
|
27
|
+
readdirSync,
|
|
28
|
+
rmSync,
|
|
29
|
+
symlinkSync,
|
|
30
|
+
unlinkSync,
|
|
31
|
+
writeFileSync,
|
|
32
|
+
} = require("node:fs");
|
|
19
33
|
const { createHash } = require("node:crypto");
|
|
34
|
+
const { tmpdir } = require("node:os");
|
|
20
35
|
const { rm } = require("node:fs/promises");
|
|
21
36
|
const { join } = require("node:path");
|
|
22
37
|
const { execFileSync } = require("node:child_process");
|
|
@@ -87,8 +102,10 @@ function linkAlias(dest, binName, aliasName, ext) {
|
|
|
87
102
|
const src = join(dest, `${binName}${ext}`);
|
|
88
103
|
const dst = join(dest, `${aliasName}${ext}`);
|
|
89
104
|
if (!existsSync(src)) return;
|
|
105
|
+
// existsSync misses dangling aliases; unlink before try so fallback copyFileSync
|
|
106
|
+
// can never write through a stale symlink.
|
|
107
|
+
if (lstatSync(dst, { throwIfNoEntry: false })) unlinkSync(dst);
|
|
90
108
|
try {
|
|
91
|
-
if (existsSync(dst)) unlinkSync(dst);
|
|
92
109
|
symlinkSync(`${binName}${ext}`, dst);
|
|
93
110
|
} catch {
|
|
94
111
|
copyFileSync(src, dst);
|
|
@@ -101,36 +118,67 @@ async function downloadAndExtract({ assetUrl, sha256Url, assetName, dest }) {
|
|
|
101
118
|
let want = null;
|
|
102
119
|
if (sha256Url) {
|
|
103
120
|
want = (await (await get(sha256Url)).text()).trim().split(/\s+/)[0];
|
|
121
|
+
if (!/^[0-9a-f]{64}$/i.test(want)) {
|
|
122
|
+
throw new Error(`invalid checksum for ${assetName}: expected a 64-character SHA-256 digest`);
|
|
123
|
+
}
|
|
124
|
+
want = want.toLowerCase();
|
|
104
125
|
}
|
|
105
126
|
|
|
106
127
|
const res = await get(assetUrl);
|
|
107
128
|
const hash = createHash("sha256");
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
129
|
+
try {
|
|
130
|
+
const file = createWriteStream(tmp);
|
|
131
|
+
await pipeline(res.body, async function* (src) {
|
|
132
|
+
for await (const chunk of src) { hash.update(chunk); yield chunk; }
|
|
133
|
+
}, file);
|
|
112
134
|
|
|
113
|
-
|
|
114
|
-
|
|
135
|
+
const got = hash.digest("hex");
|
|
136
|
+
if (want && got !== want) {
|
|
137
|
+
throw new Error(`checksum mismatch for ${assetName}\n expected ${want}\n got ${got}`);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
extractArchive(tmp, dest);
|
|
141
|
+
} finally {
|
|
115
142
|
await rm(tmp, { force: true });
|
|
116
|
-
throw new Error(`checksum mismatch for ${assetName}\n expected ${want}\n got ${got}`);
|
|
117
143
|
}
|
|
118
|
-
|
|
119
|
-
extractArchive(tmp, dest);
|
|
120
|
-
await rm(tmp, { force: true });
|
|
121
144
|
}
|
|
122
145
|
|
|
123
146
|
async function fetchStandardTool({ base, bin, alias, target, dest, ext }) {
|
|
124
147
|
const asset = `${bin}-${target}.tar.gz`;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
148
|
+
// Validate the new release away from the live installation. An old binary
|
|
149
|
+
// must not make a malformed archive look correct, and a failed download
|
|
150
|
+
// must not remove or rewrite the previously working installation.
|
|
151
|
+
const staging = mkdtempSync(join(tmpdir(), "hanzo-npm-staging-"));
|
|
152
|
+
try {
|
|
153
|
+
await downloadAndExtract({
|
|
154
|
+
assetUrl: `${base}/${asset}`,
|
|
155
|
+
sha256Url: `${base}/${asset}.sha256`,
|
|
156
|
+
assetName: asset,
|
|
157
|
+
dest: staging,
|
|
158
|
+
});
|
|
159
|
+
const out = join(staging, `${bin}${ext}`);
|
|
160
|
+
// Directories, zero-byte placeholders and symlinks are not native binaries.
|
|
161
|
+
const extracted = lstatSync(out, { throwIfNoEntry: false });
|
|
162
|
+
if (!extracted?.isFile() || extracted.size === 0) {
|
|
163
|
+
throw new Error(`release ${asset} is missing native binary ${bin}${ext} (expected a non-empty regular file)`);
|
|
164
|
+
}
|
|
165
|
+
if (process.platform !== "win32") chmodSync(out, 0o755);
|
|
166
|
+
// Preserve any sidecar files shipped with a release, not only the
|
|
167
|
+
// executable, while retaining the previous contents until validation.
|
|
168
|
+
mkdirSync(dest, { recursive: true });
|
|
169
|
+
for (const name of readdirSync(staging)) {
|
|
170
|
+
const dst = join(dest, name);
|
|
171
|
+
// Unlink existing entries (including dangling symlinks) before copying:
|
|
172
|
+
// avoids macOS cpSync aborting on dangling symlinks and Linux ETXTBSY.
|
|
173
|
+
if (lstatSync(dst, { throwIfNoEntry: false })) rmSync(dst, { recursive: true, force: true });
|
|
174
|
+
cpSync(join(staging, name), dst, { recursive: true, force: true });
|
|
175
|
+
}
|
|
176
|
+
const installed = join(dest, `${bin}${ext}`);
|
|
177
|
+
if (process.platform !== "win32") chmodSync(installed, 0o755);
|
|
178
|
+
linkAlias(dest, bin, alias, ext);
|
|
179
|
+
} finally {
|
|
180
|
+
rmSync(staging, { recursive: true, force: true });
|
|
181
|
+
}
|
|
134
182
|
}
|
|
135
183
|
|
|
136
184
|
async function fetchEngine({ key, dest, ext }) {
|
|
@@ -187,9 +235,15 @@ shift
|
|
|
187
235
|
model=""; port=""; set -- "$@" --hanzo-end
|
|
188
236
|
while [ "$1" != --hanzo-end ]; do
|
|
189
237
|
case "$1" in
|
|
190
|
-
-m|--model)
|
|
191
|
-
|
|
192
|
-
|
|
238
|
+
-m|--model|-p|--port|--overlay)
|
|
239
|
+
case "\${2:-}" in
|
|
240
|
+
""|--hanzo-end|-*) echo "hanzo-engine: $1 requires a value" >&2; exit 2 ;;
|
|
241
|
+
esac
|
|
242
|
+
case "$1" in
|
|
243
|
+
-m|--model) model="$2" ;;
|
|
244
|
+
-p|--port) port="$2" ;;
|
|
245
|
+
esac
|
|
246
|
+
shift 2 ;;
|
|
193
247
|
*) set -- "$@" "$1"; shift ;;
|
|
194
248
|
esac
|
|
195
249
|
done
|
|
@@ -278,7 +332,8 @@ async function installOne(name, { key, target, dest, ext, optional }) {
|
|
|
278
332
|
const label = tool.versioned && !fellBack ? `${tool.bin} ${VERSION}` : `${tool.bin} (latest release)`;
|
|
279
333
|
console.error(`@hanzo/cli: installed ${label} (${target})`);
|
|
280
334
|
} catch (err) {
|
|
281
|
-
|
|
335
|
+
const msg = String(err.message || err);
|
|
336
|
+
if (!optional || msg.includes("checksum mismatch") || msg.includes("invalid checksum")) throw err;
|
|
282
337
|
console.warn(`@hanzo/cli: optional companion ${tool.bin} skipped (${err.message})`);
|
|
283
338
|
}
|
|
284
339
|
}
|
|
@@ -325,4 +380,4 @@ if (require.main === module) {
|
|
|
325
380
|
});
|
|
326
381
|
}
|
|
327
382
|
|
|
328
|
-
module.exports = { extractArchive, resolveTarExecutable };
|
|
383
|
+
module.exports = { extractArchive, resolveTarExecutable, ENGINE_ADAPTER };
|