@ziniao-open/cli 0.1.1-beta.11 → 0.1.1-beta.12
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/scripts/run.js +58 -6
package/package.json
CHANGED
package/scripts/run.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const { existsSync, lstatSync, readFileSync, realpathSync, writeFileSync } = require("node:fs");
|
|
3
|
+
const { existsSync, lstatSync, readFileSync, realpathSync, unlinkSync, writeFileSync } = require("node:fs");
|
|
4
4
|
const { homedir } = require("node:os");
|
|
5
5
|
const { join, toNamespacedPath } = require("node:path");
|
|
6
6
|
const { createHash } = require("node:crypto");
|
|
@@ -29,12 +29,23 @@ function readState(dir) {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
function unsupportedBuild(dir) {
|
|
33
|
+
try {
|
|
34
|
+
const cached = JSON.parse(readFileSync(join(dir, "unsupported"), "utf8"));
|
|
35
|
+
const binary = lstatSync(bundledBinaryPath);
|
|
36
|
+
// 同版本重装也可能更换本地构建;文件变化后必须重新检测更新能力。
|
|
37
|
+
return binary.isFile() && cached.size === binary.size && cached.modified_ms === Math.trunc(binary.mtimeMs);
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
32
43
|
function releaseBinaryPath(dir, release) {
|
|
33
44
|
if (typeof release?.version !== "string" || typeof release.directory !== "string"
|
|
34
|
-
|| !/^[0-9A-Za-z.+-]+-[a-f0-9]{64}
|
|
45
|
+
|| !/^[0-9A-Za-z.+-]+-[a-f0-9]{64}(-[a-f0-9]{16})?$/.test(release.directory)
|
|
35
46
|
|| !release.directory.startsWith(`${release.version}-`)) return null;
|
|
36
47
|
const binaryPath = join(dir, "versions", release.directory, binaryName);
|
|
37
|
-
if (existsSync(join(dir, `failed-${release.directory}`))) return null;
|
|
48
|
+
if (existsSync(join(dir, `failed-${release.directory}`)) || existsSync(join(dir, `gc-${release.directory}`))) return null;
|
|
38
49
|
try {
|
|
39
50
|
return lstatSync(binaryPath, { throwIfNoEntry: false })?.isFile() ? binaryPath : null;
|
|
40
51
|
} catch {
|
|
@@ -42,12 +53,32 @@ function releaseBinaryPath(dir, release) {
|
|
|
42
53
|
}
|
|
43
54
|
}
|
|
44
55
|
|
|
56
|
+
function permanentLaunchFailure(error, binaryPath, release) {
|
|
57
|
+
if (["ENOENT", "ENOEXEC", "EFTYPE", "EACCES", "EPERM"].includes(error.code)) return true;
|
|
58
|
+
if (process.platform !== "win32" || error.code !== "UNKNOWN") return false;
|
|
59
|
+
// Windows 部分格式错误只返回 UNKNOWN。仅有文件损坏证据时才隔离,保留临时错误的重试能力。
|
|
60
|
+
try {
|
|
61
|
+
const data = readFileSync(binaryPath);
|
|
62
|
+
if (/^[a-f0-9]{64}$/.test(release.sha256 || "")) {
|
|
63
|
+
return createHash("sha256").update(data).digest("hex") !== release.sha256;
|
|
64
|
+
}
|
|
65
|
+
// 兼容旧状态尚未保存文件摘要的安装。
|
|
66
|
+
if (data.length < 64 || data.readUInt16LE(0) !== 0x5a4d) return true;
|
|
67
|
+
const offset = data.readUInt32LE(0x3c);
|
|
68
|
+
return offset > data.length - 24 || data.readUInt32LE(offset) !== 0x00004550;
|
|
69
|
+
} catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
45
74
|
// npm 正式包使用三段版本号,测试包沿用流水线的 beta 编号。
|
|
46
75
|
const channel = /^\d+\.\d+\.\d+$/.test(pkg.version) ? "latest"
|
|
47
76
|
: /^\d+\.\d+\.\d+-beta\.\d+$/.test(pkg.version) ? "beta" : null;
|
|
48
77
|
const ci = !["", "false", "0"].includes((process.env.CI || "").toLowerCase());
|
|
49
78
|
const forceBundled = ci || process.env.ZINIAO_CLI_USE_BUNDLED === "1";
|
|
50
79
|
const env = { ...process.env };
|
|
80
|
+
// 始终由实际 npm 包位置覆盖,缓存 exe 不能用自身目录推断配套脚本的位置。
|
|
81
|
+
env.ZINIAO_CLI_PACKAGE_ROOT = packageRoot;
|
|
51
82
|
delete env.ZINIAO_CLI_LAUNCHER_DIR;
|
|
52
83
|
delete env.ZINIAO_CLI_LAUNCHER_VERSION;
|
|
53
84
|
let candidates = [];
|
|
@@ -68,7 +99,8 @@ if (channel === "latest" || channel === "beta") {
|
|
|
68
99
|
}
|
|
69
100
|
}
|
|
70
101
|
if (!forceBundled && state.auto !== false && process.env.ZINIAO_CLI_AUTO_UPDATE !== "0"
|
|
71
|
-
&&
|
|
102
|
+
&& !unsupportedBuild(updateDir)
|
|
103
|
+
&& process.env.ZINIAO_CLI_NO_UPDATE_CHECK !== "1" && args[0] !== "update"
|
|
72
104
|
&& !args.some(arg => ["--help", "-h", "--version", "-v", "version"].includes(arg))
|
|
73
105
|
&& !(Date.parse(state.next_check_at) > Date.now())) {
|
|
74
106
|
// 业务进程不继承下载进程的句柄。下载进程只写用户目录,进程锁在 Go 中处理。
|
|
@@ -85,14 +117,34 @@ if (channel === "latest" || channel === "beta") {
|
|
|
85
117
|
candidates.push({ binaryPath: bundledBinaryPath });
|
|
86
118
|
|
|
87
119
|
for (const { binaryPath, release } of candidates) {
|
|
88
|
-
|
|
120
|
+
// 父进程在同步业务调用期间存活,清理器据此保护候选文件;异常退出的记录可自动回收。
|
|
121
|
+
const lease = release && join(updateDir, `in-use-${process.pid}-${release.directory}`);
|
|
122
|
+
let result;
|
|
123
|
+
let attempted = false;
|
|
124
|
+
try {
|
|
125
|
+
if (lease) {
|
|
126
|
+
try { writeFileSync(lease, "", { mode: 0o600 }); } catch {
|
|
127
|
+
// 记录失败不能改变业务选版;新版进程持有只读共享锁,旧版缺少锁文件时清理器保守保留。
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (release && existsSync(join(updateDir, `gc-${release.directory}`))) continue;
|
|
131
|
+
attempted = true;
|
|
132
|
+
result = spawnSync(executablePath(binaryPath), args, { env, stdio: "inherit", windowsHide: false });
|
|
133
|
+
} catch (error) {
|
|
134
|
+
// 仅处理真正的启动异常,已启动的业务请求仍不重放。
|
|
135
|
+
result = { error, pid: 0 };
|
|
136
|
+
} finally {
|
|
137
|
+
if (lease) {
|
|
138
|
+
try { unlinkSync(lease); } catch { /* 清理失败的记录由更新器检查进程是否存活。 */ }
|
|
139
|
+
}
|
|
140
|
+
}
|
|
89
141
|
if (!result.error) {
|
|
90
142
|
// 被信号终止时返回失败,尤其不能在此时重试已启动的业务请求。
|
|
91
143
|
process.exit(result.status ?? (result.signal ? 1 : 0));
|
|
92
144
|
}
|
|
93
145
|
if (release && result.pid === 0) {
|
|
94
146
|
// 仅隔离文件缺失、格式或执行权限问题;资源不足等临时错误留待下次调用重试。
|
|
95
|
-
if (
|
|
147
|
+
if (attempted && permanentLaunchFailure(result.error, binaryPath, release)) {
|
|
96
148
|
try {
|
|
97
149
|
writeFileSync(join(updateDir, `failed-${release.directory}`), "", { flag: "wx", mode: 0o600 });
|
|
98
150
|
} catch {
|