返回 CodeWhale
artifacts.js
根目录 / npm / codewhale / scripts / artifacts.js
1 const path = require("path");
2 const os = require("os");
3 const compiledHosts = require("./compiled-hosts");
4
5 const CHECKSUM_MANIFEST = "codewhale-artifacts-sha256.txt";
6 const CHECKSUM_SIGNATURE = `${CHECKSUM_MANIFEST}.sig`;
7 const BUNDLE_CHECKSUM_MANIFEST = "codewhale-bundles-sha256.txt";
8 const WINDOWS_INSTALLER_ASSET = "CodeWhaleSetup.exe";
9 const RELEASE_GITHUB_HOSTS = new Set([
10 "github.com",
11 "api.github.com",
12 "objects.githubusercontent.com",
13 "release-assets.githubusercontent.com",
14 "github-releases.githubusercontent.com",
15 ]);
16 const RELEASE_CNB_HOST = "cnb.cool";
17 const UPDATE_ALLOWED_HOSTS_ENV = "CODEWHALE_UPDATE_ALLOWED_HOSTS";
18 const transportOptions = {
19 allowLoopbackHttp: false,
20 };
21
22 // Compatibility bridge introduced in v0.9.5: already-shipped v0.9.4 clients
23 // require these names before
24 // they will advertise or install a newer release. They contain byte-identical
25 // copies of the consolidated `codewhale` runtime; current installers do not
26 // expose them as a third command.
27 const LEGACY_TUI_BRIDGE_ASSET_NAMES = [
28 "codewhale-tui-linux-x64",
29 "codewhale-tui-linux-arm64",
30 "codewhale-tui-android-arm64",
31 // Published for symmetry with every other platform: the name exists so
32 // release completeness stays one rule, not a case analysis. No shipped client
33 // requests it; nothing else references it.
34 "codewhale-tui-ohos-arm64",
35 "codewhale-tui-macos-x64",
36 "codewhale-tui-macos-arm64",
37 "codewhale-tui-windows-x64.exe",
38 "codewhale-tui-windows-arm64.exe",
39 ];
40
41 const CNB_BINARY_ASSET_NAMES = [
42 "codewhale-linux-x64",
43 "codew-linux-x64",
44 "codewhale-tui-linux-x64",
45 ];
46 const CNB_RELEASE_ASSET_NAMES = [
47 ...CNB_BINARY_ASSET_NAMES,
48 CHECKSUM_MANIFEST,
49 ];
50
51 const BUNDLE_ASSET_NAMES = [
52 "codewhale-linux-x64.tar.gz",
53 "codewhale-linux-arm64.tar.gz",
54 "codewhale-android-arm64.tar.gz",
55 "codewhale-ohos-arm64.tar.gz",
56 "codewhale-macos-x64.tar.gz",
57 "codewhale-macos-arm64.tar.gz",
58 "codewhale-windows-x64.zip",
59 "codewhale-windows-x64-portable.zip",
60 "codewhale-windows-arm64.zip",
61 "codewhale-windows-arm64-portable.zip",
62 ];
63
64 const ASSET_MATRIX = {
65 linux: {
66 x64: ["codewhale-linux-x64", "codew-linux-x64"],
67 arm64: ["codewhale-linux-arm64", "codew-linux-arm64"],
68 },
69 android: {
70 arm64: ["codewhale-android-arm64", "codew-android-arm64"],
71 },
72 // Native HarmonyOS/OpenHarmony (aarch64-unknown-linux-ohos). These binaries
73 // only run on a device when they are code-signed, so the release build
74 // self-signs them; see docs/HarmonyOS.md.
75 openharmony: {
76 arm64: ["codewhale-ohos-arm64", "codew-ohos-arm64"],
77 },
78 darwin: {
79 x64: ["codewhale-macos-x64", "codew-macos-x64"],
80 arm64: ["codewhale-macos-arm64", "codew-macos-arm64"],
81 },
82 win32: {
83 x64: ["codewhale-windows-x64.exe", "codew-windows-x64.exe", "codewhale.bat"],
84 arm64: ["codewhale-windows-arm64.exe", "codew-windows-arm64.exe"],
85 },
86 };
87
88 function detectBinaryNames() {
89 const platform = os.platform();
90 const arch = os.arch();
91 const defaults = ASSET_MATRIX[platform];
92 if (!defaults) {
93 const supported = Object.keys(ASSET_MATRIX).map(p => `'${p}'`).join(', ');
94 throw new Error(
95 `Unsupported platform: ${platform}. Supported platforms: ${supported}.\n\n` +
96 unsupportedBuildHint(),
97 );
98 }
99 const pair = defaults[arch];
100 if (!pair) {
101 const supported = Object.keys(defaults).map(a => `'${a}'`).join(', ');
102 const hint = platform === "linux" && arch === "riscv64" ? unsupportedRiscvHint() : unsupportedBuildHint();
103 throw new Error(
104 `Unsupported architecture: ${arch} on platform ${platform}. ` +
105 `Supported architectures: ${supported}.\n\n` +
106 hint,
107 );
108 }
109 return {
110 platform,
111 arch,
112 codewhale: pair[0],
113 codew: pair[1],
114 };
115 }
116
117 function unsupportedBuildHint() {
118 return [
119 "No prebuilt binary is available for this platform/architecture combo.",
120 "You can still run codewhale by building from source with Cargo (single binary):",
121 "",
122 " # Requires Rust 1.89+ (https://rustup.rs)",
123 " cargo install codewhale-cli --locked # provides `codewhale`",
124 "",
125 "Or build from a checkout:",
126 "",
127 " git clone https://github.com/codewhale-hq/CodeWhale.git",
128 " cd CodeWhale",
129 " cargo install --path crates/cli --locked # single binary",
130 "",
131 "See https://github.com/codewhale-hq/CodeWhale/blob/main/docs/INSTALL.md",
132 "for cross-compilation, mirror, Linux ARM64, FreeBSD, and winget specifics.",
133 ].join("\n");
134 }
135
136 function openHarmonyDirections() {
137 return [
138 "On a HarmonyOS PC, install Codewhale inside the Linux subsystem (融合开发引擎, openEuler):",
139 "",
140 " curl -fsSL https://codewhale.net/install.sh | sh",
141 "",
142 "Or build the native aarch64-unknown-linux-ohos binary from source by following",
143 "\"Cross-Compiling To OpenHarmony\" in docs/HarmonyOS.md of codewhale-hq/CodeWhale.",
144 ].join("\n");
145 }
146
147 function unsupportedRiscvHint() {
148 return [
149 "Linux riscv64 prebuilt binaries are temporarily unavailable.",
150 "CodeWhale currently depends on rquickjs-sys, which does not ship",
151 "riscv64gc-unknown-linux-gnu bindings in the locked dependency set.",
152 "",
153 "Track the release notes and docs/INSTALL.md for the next RISC-V support update.",
154 ].join("\n");
155 }
156
157 function executableName(base, platform) {
158 return platform === "win32" ? `${base}.exe` : base;
159 }
160
161 function ensureTrailingSlash(baseUrl) {
162 const trimmed = String(baseUrl).trim();
163 return trimmed.endsWith("/") ? trimmed : `${trimmed}/`;
164 }
165
166 function githubReleaseBaseUrl(version, repo = "codewhale-hq/CodeWhale") {
167 return `https://github.com/${repo}/releases/download/v${version}/`;
168 }
169
170 function cnbReleaseBaseUrl(version) {
171 return `https://cnb.cool/codewhale.net/codewhale/-/releases/download/v${version}/`;
172 }
173
174 function releaseAssetUrlFromBase(baseName, baseUrl) {
175 return new URL(baseName, ensureTrailingSlash(baseUrl)).toString();
176 }
177
178 function explicitReleaseBase(env = process.env) {
179 const candidates = [
180 env.CODEWHALE_RELEASE_BASE_URL,
181 env.DEEPSEEK_TUI_RELEASE_BASE_URL,
182 env.DEEPSEEK_RELEASE_BASE_URL,
183 ];
184 for (const candidate of candidates) {
185 const override = String(candidate || "").trim();
186 if (override) {
187 return ensureTrailingSlash(override);
188 }
189 }
190 return "";
191 }
192
193 function hasExplicitReleaseBase(env = process.env) {
194 return Boolean(explicitReleaseBase(env));
195 }
196
197 function isCnbSupportedTarget(
198 platform = os.platform(),
199 arch = os.arch(),
200 ) {
201 return platform === "linux" && arch === "x64";
202 }
203
204 function releaseBaseUrl(version, repo = "codewhale-hq/CodeWhale") {
205 // CODEWHALE_RELEASE_BASE_URL is the canonical override.
206 // DEEPSEEK_TUI_RELEASE_BASE_URL / DEEPSEEK_RELEASE_BASE_URL are legacy aliases.
207 const override = explicitReleaseBase();
208 if (override) {
209 return override;
210 }
211 // When CODEWHALE_USE_CNB_MIRROR is set, use the CNB (China-friendly)
212 // mirror that already builds and publishes binary release assets.
213 if (usesCnbMirror()) {
214 assertCnbMirrorSupportedPlatform();
215 return cnbReleaseBaseUrl(version);
216 }
217 return githubReleaseBaseUrl(version, repo);
218 }
219
220 function usesCnbMirror(env = process.env) {
221 return !hasExplicitReleaseBase(env) && env.CODEWHALE_USE_CNB_MIRROR === "1";
222 }
223
224 function shouldRaceFirstPartyMirrors(
225 env = process.env,
226 platform = os.platform(),
227 arch = os.arch(),
228 ) {
229 return (
230 isCnbSupportedTarget(platform, arch) &&
231 !hasExplicitReleaseBase(env) &&
232 env.CODEWHALE_USE_CNB_MIRROR !== "1"
233 );
234 }
235
236 function firstPartyReleaseSources(version, repo = "codewhale-hq/CodeWhale") {
237 return [
238 {
239 id: "github",
240 label: "GitHub Releases",
241 baseUrl: githubReleaseBaseUrl(version, repo),
242 },
243 {
244 id: "cnb",
245 label: "CNB first-party mirror",
246 baseUrl: cnbReleaseBaseUrl(version),
247 },
248 ];
249 }
250
251 function assertCnbMirrorSupportedPlatform(
252 platform = os.platform(),
253 arch = os.arch(),
254 ) {
255 if (isCnbSupportedTarget(platform, arch)) {
256 return;
257 }
258 throw new Error(
259 "CODEWHALE_USE_CNB_MIRROR=1 currently supports only Linux x64; " +
260 `detected ${platform} ${arch}. Native HarmonyOS has its own asset and ` +
261 "no CNB mirror. " +
262 "Use the GitHub Release or set CODEWHALE_RELEASE_BASE_URL to a " +
263 "complete mirror for this platform.",
264 );
265 }
266
267 function releaseAssetUrl(baseName, version, repo = "codewhale-hq/CodeWhale") {
268 return releaseAssetUrlFromBase(baseName, releaseBaseUrl(version, repo));
269 }
270
271 function checksumManifestUrl(version, repo = "codewhale-hq/CodeWhale") {
272 return releaseAssetUrl(CHECKSUM_MANIFEST, version, repo);
273 }
274
275 function redactUrl(rawUrl) {
276 const input = String(rawUrl || "");
277 try {
278 const parsed = new URL(input);
279 if (parsed.username || parsed.password) {
280 parsed.username = "";
281 parsed.password = "";
282 return parsed.toString();
283 }
284 return input;
285 } catch {
286 return input.replace(/^([a-z][a-z0-9+\-.]*:\/\/)[^/@\s]+@/i, "$1");
287 }
288 }
289
290 function extraAllowedReleaseHosts(env = process.env) {
291 const extra = new Set();
292 const override = explicitReleaseBase(env);
293 if (override) {
294 try {
295 const parsed = new URL(override);
296 if (parsed.hostname) {
297 extra.add(parsed.hostname.toLowerCase());
298 }
299 } catch {
300 }
301 }
302 const configured = env && env[UPDATE_ALLOWED_HOSTS_ENV];
303 if (configured) {
304 for (const part of String(configured).split(",")) {
305 const host = part.trim().toLowerCase();
306 if (host) {
307 extra.add(host);
308 }
309 }
310 }
311 return extra;
312 }
313
314 function isAllowedReleaseHost(hostname, env = process.env) {
315 const host = String(hostname || "").toLowerCase();
316 if (!host) {
317 return false;
318 }
319 if (
320 RELEASE_GITHUB_HOSTS.has(host) ||
321 host === RELEASE_CNB_HOST ||
322 host.endsWith(`.${RELEASE_CNB_HOST}`)
323 ) {
324 return true;
325 }
326 return extraAllowedReleaseHosts(env).has(host);
327 }
328
329 function isLoopbackHost(hostname) {
330 const host = String(hostname || "").toLowerCase();
331 return host === "127.0.0.1" || host === "localhost" || host === "::1" || host === "[::1]";
332 }
333
334 function validateReleaseUrl(rawUrl, options = {}) {
335 const env = options.env || process.env;
336 const allowLoopbackHttp =
337 options.allowLoopbackHttp === undefined
338 ? transportOptions.allowLoopbackHttp
339 : Boolean(options.allowLoopbackHttp);
340 const safeUrl = redactUrl(rawUrl);
341 let parsed;
342 try {
343 parsed = new URL(rawUrl);
344 } catch (err) {
345 throw new Error(`Invalid release URL: ${safeUrl} (${err.message})`);
346 }
347 if (allowLoopbackHttp && parsed.protocol === "http:" && isLoopbackHost(parsed.hostname)) {
348 return parsed;
349 }
350 if (parsed.protocol !== "https:") {
351 throw new Error(`Release download requires HTTPS, not ${parsed.protocol}: ${safeUrl}`);
352 }
353 if (!isAllowedReleaseHost(parsed.hostname, env)) {
354 throw new Error(
355 `Release host "${parsed.hostname}" is not an allowed release host. ` +
356 "A private mirror's host is allowed when it is the CODEWHALE_RELEASE_BASE_URL HTTPS base URL; " +
357 `any other host can be added with ${UPDATE_ALLOWED_HOSTS_ENV}=host1,host2`,
358 );
359 }
360 return parsed;
361 }
362
363 function releaseBinaryDirectory() {
364 return path.join(__dirname, "..", "bin", "downloads");
365 }
366
367 function allAssetNames() {
368 const names = [];
369 for (const platformAssets of Object.values(ASSET_MATRIX)) {
370 for (const assets of Object.values(platformAssets)) {
371 names.push(...assets);
372 }
373 }
374 return Array.from(new Set(names));
375 }
376
377 function allReleaseAssetNames(catalog) {
378 return [
379 ...allAssetNames(),
380 ...LEGACY_TUI_BRIDGE_ASSET_NAMES,
381 ...BUNDLE_ASSET_NAMES,
382 WINDOWS_INSTALLER_ASSET,
383 BUNDLE_CHECKSUM_MANIFEST,
384 CHECKSUM_MANIFEST,
385 ...compiledHosts.assets(catalog),
386 ];
387 }
388
389 function checksummedReleaseAssetNames(catalog) {
390 return allReleaseAssetNames(catalog).filter((name) => name !== CHECKSUM_MANIFEST);
391 }
392
393 module.exports = {
394 allAssetNames,
395 allReleaseAssetNames,
396 assertCnbMirrorSupportedPlatform,
397 BUNDLE_ASSET_NAMES,
398 BUNDLE_CHECKSUM_MANIFEST,
399 CHECKSUM_MANIFEST,
400 CHECKSUM_SIGNATURE,
401 checksummedReleaseAssetNames,
402 CNB_BINARY_ASSET_NAMES,
403 CNB_RELEASE_ASSET_NAMES,
404 checksumManifestUrl,
405 cnbReleaseBaseUrl,
406 detectBinaryNames,
407 executableName,
408 explicitReleaseBase,
409 firstPartyReleaseSources,
410 githubReleaseBaseUrl,
411 hasExplicitReleaseBase,
412 isAllowedReleaseHost,
413 isCnbSupportedTarget,
414 LEGACY_TUI_BRIDGE_ASSET_NAMES,
415 openHarmonyDirections,
416 redactUrl,
417 releaseAssetUrl,
418 releaseAssetUrlFromBase,
419 releaseBaseUrl,
420 releaseBinaryDirectory,
421 shouldRaceFirstPartyMirrors,
422 transportOptions,
423 UPDATE_ALLOWED_HOSTS_ENV,
424 usesCnbMirror,
425 validateReleaseUrl,
426 WINDOWS_INSTALLER_ASSET,
427 };
428
428 lines JAVASCRIPT