codewhale 0.9.8 → 0.9.10

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.
@@ -23,12 +23,22 @@ const { URL } = require("url");
23
23
  const { mkdir, chmod, stat, rename, readFile, unlink, writeFile } = fs.promises;
24
24
  const { createWriteStream } = fs;
25
25
  const path = require("path");
26
+ const os = require("os");
26
27
 
27
28
  const {
29
+ CHECKSUM_MANIFEST,
30
+ assertCnbMirrorSupportedPlatform,
28
31
  checksumManifestUrl,
32
+ cnbReleaseBaseUrl,
29
33
  detectBinaryNames,
34
+ explicitReleaseBase,
35
+ firstPartyReleaseSources,
36
+ githubReleaseBaseUrl,
30
37
  releaseAssetUrl,
38
+ releaseAssetUrlFromBase,
31
39
  releaseBinaryDirectory,
40
+ shouldRaceFirstPartyMirrors,
41
+ usesCnbMirror,
32
42
  } = require("./artifacts");
33
43
  const { preflightGlibc } = require("./preflight-glibc");
34
44
  const pkg = require("../package.json");
@@ -37,6 +47,8 @@ const DEFAULT_TIMEOUT_MS = 300_000; // 5 minutes per attempt
37
47
  const DEFAULT_STALL_MS = 30_000; // abort if no bytes for 30s
38
48
  const OPTIONAL_TIMEOUT_MS = 15_000; // fail fast during optional npm postinstall
39
49
  const OPTIONAL_STALL_MS = 5_000; // avoid long hangs when install can recover on first run
50
+ const MANIFEST_TIMEOUT_MS = 15_000; // small checksum probes must not wait on a binary budget
51
+ const MANIFEST_STALL_MS = 5_000;
40
52
  const MAX_ATTEMPTS = 5;
41
53
  const OPTIONAL_MAX_ATTEMPTS = 1; // runtime keeps the full retry budget on first launch
42
54
  const BASE_BACKOFF_MS = 1_000;
@@ -78,6 +90,18 @@ class DownloadTimeoutError extends Error {
78
90
  }
79
91
  }
80
92
 
93
+ function abortError(message) {
94
+ const err = new Error(message || "The operation was aborted");
95
+ err.name = "AbortError";
96
+ err.code = "ABORT_ERR";
97
+ err.nonRetryable = true;
98
+ return err;
99
+ }
100
+
101
+ function isAbortError(err) {
102
+ return Boolean(err) && (err.name === "AbortError" || err.code === "ABORT_ERR");
103
+ }
104
+
81
105
  // Binary-version precedence must match run.js and verify-release-assets.js so
82
106
  // install-time asset resolution agrees with runtime and release verification.
83
107
  // `codewhaleBinaryVersion` lets a packaging-only npm release target a specific
@@ -85,6 +109,7 @@ class DownloadTimeoutError extends Error {
85
109
  // for backward compatibility (#3769). `pkgObj`/`env` are injectable for tests.
86
110
  function resolvePackageVersion(pkgObj = pkg, env = process.env) {
87
111
  const configuredVersion =
112
+ env.CODEWHALE_VERSION ||
88
113
  env.DEEPSEEK_TUI_VERSION ||
89
114
  env.DEEPSEEK_VERSION ||
90
115
  pkgObj.codewhaleBinaryVersion ||
@@ -93,18 +118,40 @@ function resolvePackageVersion(pkgObj = pkg, env = process.env) {
93
118
  return String(configuredVersion).trim();
94
119
  }
95
120
 
96
- function resolveRepo() {
97
- return process.env.DEEPSEEK_TUI_GITHUB_REPO || process.env.DEEPSEEK_GITHUB_REPO || "Hmbown/CodeWhale";
121
+ function resolveRepo(env = process.env) {
122
+ return (
123
+ env.CODEWHALE_GITHUB_REPO ||
124
+ env.DEEPSEEK_TUI_GITHUB_REPO ||
125
+ env.DEEPSEEK_GITHUB_REPO ||
126
+ "Hmbown/CodeWhale"
127
+ );
98
128
  }
99
129
 
100
130
  function isOptionalInstall(argv = process.argv.slice(2), env = process.env) {
101
131
  return (
102
132
  argv.includes("--optional") ||
133
+ env.CODEWHALE_OPTIONAL_INSTALL === "1" ||
103
134
  env.DEEPSEEK_TUI_OPTIONAL_INSTALL === "1" ||
104
135
  env.DEEPSEEK_OPTIONAL_INSTALL === "1"
105
136
  );
106
137
  }
107
138
 
139
+ function shouldForceDownload(env = process.env) {
140
+ return (
141
+ env.CODEWHALE_FORCE_DOWNLOAD === "1" ||
142
+ env.DEEPSEEK_TUI_FORCE_DOWNLOAD === "1" ||
143
+ env.DEEPSEEK_FORCE_DOWNLOAD === "1"
144
+ );
145
+ }
146
+
147
+ function shouldDisableInstall(env = process.env) {
148
+ return (
149
+ env.CODEWHALE_DISABLE_INSTALL === "1" ||
150
+ env.DEEPSEEK_TUI_DISABLE_INSTALL === "1" ||
151
+ env.DEEPSEEK_DISABLE_INSTALL === "1"
152
+ );
153
+ }
154
+
108
155
  function isInstallContext(context) {
109
156
  return context === "install";
110
157
  }
@@ -160,11 +207,14 @@ function binaryPaths() {
160
207
  // Logging / progress
161
208
  // ────────────────────────────────────────────────────────────────────────────
162
209
 
163
- function isQuietInstall() {
164
- if (process.env.DEEPSEEK_TUI_QUIET_INSTALL === "1") {
210
+ function isQuietInstall(env = process.env) {
211
+ if (
212
+ env.CODEWHALE_QUIET_INSTALL === "1" ||
213
+ env.DEEPSEEK_TUI_QUIET_INSTALL === "1"
214
+ ) {
165
215
  return true;
166
216
  }
167
- const level = (process.env.npm_config_loglevel || "").toLowerCase();
217
+ const level = (env.npm_config_loglevel || "").toLowerCase();
168
218
  return level === "silent" || level === "error";
169
219
  }
170
220
 
@@ -179,6 +229,7 @@ function installFailureHint(error) {
179
229
  const message = error && error.message ? String(error.message) : "";
180
230
  const code = error && error.code ? String(error.code) : "";
181
231
  const releaseBase =
232
+ process.env.CODEWHALE_RELEASE_BASE_URL ||
182
233
  process.env.DEEPSEEK_TUI_RELEASE_BASE_URL ||
183
234
  process.env.DEEPSEEK_RELEASE_BASE_URL;
184
235
  const networkMarkers = [
@@ -201,7 +252,7 @@ function installFailureHint(error) {
201
252
  if (releaseBase) {
202
253
  return [
203
254
  "codewhale install hint:",
204
- ` DEEPSEEK_TUI_RELEASE_BASE_URL is set to ${releaseBase}`,
255
+ ` CODEWHALE_RELEASE_BASE_URL resolves to ${releaseBase}`,
205
256
  " Verify that this directory contains codewhale-artifacts-sha256.txt",
206
257
  " plus the codewhale/codew binary assets for your platform (single binary).",
207
258
  ].join("\n");
@@ -210,15 +261,18 @@ function installFailureHint(error) {
210
261
  return [
211
262
  "codewhale install hint:",
212
263
  " The npm package downloads prebuilt binaries from GitHub Releases.",
213
- " If GitHub is unavailable on this network, mirror the release assets and set:",
214
- " DEEPSEEK_TUI_RELEASE_BASE_URL=https://<mirror>/<release-asset-directory>/",
264
+ " On Linux x64 it also probes the CNB first-party checksum manifest and uses",
265
+ " the first source whose HTTP response and manifest validate.",
266
+ " If both are unavailable, mirror the release assets and set:",
267
+ " CODEWHALE_RELEASE_BASE_URL=https://<mirror>/<release-asset-directory>/",
268
+ " or CODEWHALE_USE_CNB_MIRROR=1 on Linux x64.",
215
269
  " The directory must contain codewhale-artifacts-sha256.txt and the platform binaries.",
216
270
  " See docs/INSTALL.md#npm-binary-download-times-out.",
217
271
  ].join("\n");
218
272
  }
219
273
 
220
- function envInt(name, fallback) {
221
- const raw = process.env[name];
274
+ function envInt(name, fallback, env = process.env) {
275
+ const raw = env[name];
222
276
  if (!raw) {
223
277
  return fallback;
224
278
  }
@@ -229,17 +283,27 @@ function envInt(name, fallback) {
229
283
  return parsed;
230
284
  }
231
285
 
232
- function downloadTimeoutMs(context = "runtime") {
286
+ function downloadTimeoutMs(context = "runtime", env = process.env) {
233
287
  return envInt(
234
- "DEEPSEEK_TUI_DOWNLOAD_TIMEOUT_MS",
235
- envInt("DEEPSEEK_DOWNLOAD_TIMEOUT_MS", defaultTimeoutMs(context)),
288
+ "CODEWHALE_DOWNLOAD_TIMEOUT_MS",
289
+ envInt(
290
+ "DEEPSEEK_TUI_DOWNLOAD_TIMEOUT_MS",
291
+ envInt("DEEPSEEK_DOWNLOAD_TIMEOUT_MS", defaultTimeoutMs(context, env), env),
292
+ env,
293
+ ),
294
+ env,
236
295
  );
237
296
  }
238
297
 
239
- function downloadStallMs(context = "runtime") {
298
+ function downloadStallMs(context = "runtime", env = process.env) {
240
299
  return envInt(
241
- "DEEPSEEK_TUI_DOWNLOAD_STALL_MS",
242
- envInt("DEEPSEEK_DOWNLOAD_STALL_MS", defaultStallMs(context)),
300
+ "CODEWHALE_DOWNLOAD_STALL_MS",
301
+ envInt(
302
+ "DEEPSEEK_TUI_DOWNLOAD_STALL_MS",
303
+ envInt("DEEPSEEK_DOWNLOAD_STALL_MS", defaultStallMs(context, env), env),
304
+ env,
305
+ ),
306
+ env,
243
307
  );
244
308
  }
245
309
 
@@ -504,6 +568,8 @@ function httpRequest(rawUrl, opts = {}) {
504
568
  let settled = false;
505
569
  let req = null;
506
570
  let res = null;
571
+ const signal = opts.signal;
572
+ let onAbort = null;
507
573
 
508
574
  const cleanup = () => {
509
575
  if (totalTimer) {
@@ -514,6 +580,10 @@ function httpRequest(rawUrl, opts = {}) {
514
580
  clearTimeout(stallTimer);
515
581
  stallTimer = null;
516
582
  }
583
+ if (signal && onAbort) {
584
+ signal.removeEventListener("abort", onAbort);
585
+ onAbort = null;
586
+ }
517
587
  };
518
588
 
519
589
  const fail = (err) => {
@@ -533,11 +603,22 @@ function httpRequest(rawUrl, opts = {}) {
533
603
  reject(err);
534
604
  };
535
605
 
606
+ if (signal) {
607
+ if (signal.aborted) {
608
+ reject(abortError());
609
+ return;
610
+ }
611
+ onAbort = function () {
612
+ fail(abortError());
613
+ };
614
+ signal.addEventListener("abort", onAbort);
615
+ }
616
+
536
617
  if (totalTimeoutMs > 0) {
537
618
  totalTimer = setTimeout(() => {
538
619
  fail(new DownloadTimeoutError(
539
620
  `download exceeded total timeout of ${totalTimeoutMs} ms ` +
540
- `(set DEEPSEEK_TUI_DOWNLOAD_TIMEOUT_MS to raise it; current stall budget is ${stallMs} ms)`,
621
+ `(set CODEWHALE_DOWNLOAD_TIMEOUT_MS to raise it; current stall budget is ${stallMs} ms)`,
541
622
  ));
542
623
  }, totalTimeoutMs);
543
624
  }
@@ -548,7 +629,7 @@ function httpRequest(rawUrl, opts = {}) {
548
629
  stallTimer = setTimeout(() => {
549
630
  fail(new DownloadTimeoutError(
550
631
  `download stalled — no bytes received for ${stallMs} ms ` +
551
- `(set DEEPSEEK_TUI_DOWNLOAD_STALL_MS to raise it; total budget is ${totalTimeoutMs} ms)`,
632
+ `(set CODEWHALE_DOWNLOAD_STALL_MS to raise it; total budget is ${totalTimeoutMs} ms)`,
552
633
  ));
553
634
  }, stallMs);
554
635
  };
@@ -765,7 +846,9 @@ function httpRequest(rawUrl, opts = {}) {
765
846
 
766
847
  function isRetryable(err) {
767
848
  if (!err) return false;
849
+ if (isAbortError(err)) return false;
768
850
  if (err.nonRetryable) return false;
851
+ if (err.retryable === true) return true;
769
852
  if (err instanceof NonRetryableError) return false;
770
853
  if (err instanceof DownloadTimeoutError) return true;
771
854
  // withRetry() rethrows a plain Error while preserving name/status, so wrapped
@@ -796,15 +879,48 @@ function sleep(ms) {
796
879
  return new Promise((resolve) => setTimeout(resolve, ms));
797
880
  }
798
881
 
799
- async function withRetry(label, fn, context = "runtime") {
882
+ function sleepWithSignal(ms, signal) {
883
+ if (!signal) {
884
+ return sleep(ms);
885
+ }
886
+ if (signal.aborted) {
887
+ return Promise.reject(abortError());
888
+ }
889
+ return new Promise((resolve, reject) => {
890
+ let timer = null;
891
+ const cleanup = () => {
892
+ if (timer) {
893
+ clearTimeout(timer);
894
+ timer = null;
895
+ }
896
+ signal.removeEventListener("abort", onAbort);
897
+ };
898
+ const onAbort = () => {
899
+ cleanup();
900
+ reject(abortError());
901
+ };
902
+ timer = setTimeout(() => {
903
+ cleanup();
904
+ resolve();
905
+ }, ms);
906
+ signal.addEventListener("abort", onAbort, { once: true });
907
+ });
908
+ }
909
+
910
+ async function withRetry(label, fn, context, signal) {
911
+ const resolvedContext =
912
+ context === undefined || context === null ? "runtime" : context;
800
913
  let lastErr;
801
- const attemptLimit = maxAttempts(context);
914
+ const attemptLimit = maxAttempts(resolvedContext);
802
915
  for (let attempt = 1; attempt <= attemptLimit; attempt++) {
916
+ if (signal && signal.aborted) {
917
+ throw abortError();
918
+ }
803
919
  try {
804
920
  return await fn(attempt);
805
921
  } catch (err) {
806
922
  lastErr = err;
807
- if (!isRetryable(err) || attempt === attemptLimit) {
923
+ if (isAbortError(err) || !isRetryable(err) || attempt === attemptLimit) {
808
924
  break;
809
925
  }
810
926
  const wait = backoffDelay(attempt);
@@ -817,7 +933,10 @@ async function withRetry(label, fn, context = "runtime") {
817
933
  process.stderr.write(`${hint}\n`);
818
934
  }
819
935
  }
820
- await sleep(wait);
936
+ await sleepWithSignal(wait, signal);
937
+ if (signal && signal.aborted) {
938
+ throw abortError();
939
+ }
821
940
  }
822
941
  }
823
942
  const msg = lastErr && lastErr.message ? lastErr.message : String(lastErr);
@@ -866,13 +985,24 @@ async function followRedirects(url, opts = {}) {
866
985
  throw new NonRetryableError(`too many redirects starting at ${url}`);
867
986
  }
868
987
 
869
- function streamToFile(response, destination, progress) {
988
+ function streamToFile(response, destination, progress, signal) {
870
989
  return new Promise((resolve, reject) => {
871
990
  const sink = createWriteStream(destination);
872
991
  let done = false;
992
+ const onAbort = () => {
993
+ try {
994
+ response.destroy();
995
+ } catch {
996
+ // ignore
997
+ }
998
+ finish(abortError());
999
+ };
873
1000
  const finish = (err) => {
874
1001
  if (done) return;
875
1002
  done = true;
1003
+ if (signal) {
1004
+ signal.removeEventListener("abort", onAbort);
1005
+ }
876
1006
  if (err) {
877
1007
  sink.destroy();
878
1008
  reject(err);
@@ -886,6 +1016,13 @@ function streamToFile(response, destination, progress) {
886
1016
  response.on("error", (err) => finish(err));
887
1017
  sink.on("error", (err) => finish(err));
888
1018
  sink.on("finish", () => finish(null));
1019
+ if (signal) {
1020
+ if (signal.aborted) {
1021
+ onAbort();
1022
+ return;
1023
+ }
1024
+ signal.addEventListener("abort", onAbort, { once: true });
1025
+ }
889
1026
  response.pipe(sink);
890
1027
  });
891
1028
  }
@@ -901,6 +1038,7 @@ async function download(url, destination, options = {}) {
901
1038
  context,
902
1039
  totalTimeoutMs: downloadTimeoutMs(context),
903
1040
  stallMs: downloadStallMs(context),
1041
+ signal: options.signal,
904
1042
  });
905
1043
  const response = result.response;
906
1044
  const lenHeader = response.headers["content-length"];
@@ -910,7 +1048,7 @@ async function download(url, destination, options = {}) {
910
1048
  logInfo(`retry attempt ${attempt}/${attemptLimit} for ${assetName}`);
911
1049
  }
912
1050
  try {
913
- await streamToFile(response, destination, progress);
1051
+ await streamToFile(response, destination, progress, options.signal);
914
1052
  } catch (err) {
915
1053
  // Ensure we don't leave a partial file confusing future attempts.
916
1054
  try {
@@ -921,17 +1059,26 @@ async function download(url, destination, options = {}) {
921
1059
  throw err;
922
1060
  }
923
1061
  progress.finish();
924
- }, context);
1062
+ }, context, options.signal);
925
1063
  }
926
1064
 
927
1065
  async function downloadText(url, options = {}) {
928
1066
  const context =
929
1067
  options.context === undefined || options.context === null ? "runtime" : options.context;
1068
+ const totalTimeoutMs =
1069
+ options.totalTimeoutMs === undefined || options.totalTimeoutMs === null
1070
+ ? downloadTimeoutMs(context)
1071
+ : options.totalTimeoutMs;
1072
+ const stallMs =
1073
+ options.stallMs === undefined || options.stallMs === null
1074
+ ? downloadStallMs(context)
1075
+ : options.stallMs;
930
1076
  return withRetry(`fetch ${url}`, async () => {
931
1077
  const result = await followRedirects(url, {
932
1078
  context,
933
- totalTimeoutMs: downloadTimeoutMs(context),
934
- stallMs: downloadStallMs(context),
1079
+ totalTimeoutMs,
1080
+ stallMs,
1081
+ signal: options.signal,
935
1082
  });
936
1083
  const response = result.response;
937
1084
  response.setEncoding("utf8");
@@ -946,16 +1093,48 @@ async function downloadText(url, options = {}) {
946
1093
  // disturbing the stall detection.
947
1094
  return new Promise((resolve, reject) => {
948
1095
  const chunks = [];
1096
+ let settled = false;
1097
+ const signal = options.signal;
1098
+ const cleanup = () => {
1099
+ if (signal) {
1100
+ signal.removeEventListener("abort", onAbort);
1101
+ }
1102
+ };
1103
+ const finish = (error, value) => {
1104
+ if (settled) return;
1105
+ settled = true;
1106
+ cleanup();
1107
+ if (error) {
1108
+ reject(error);
1109
+ } else {
1110
+ resolve(value);
1111
+ }
1112
+ };
1113
+ const onAbort = () => {
1114
+ try {
1115
+ response.destroy();
1116
+ } catch {
1117
+ // ignore
1118
+ }
1119
+ finish(abortError());
1120
+ };
949
1121
  response.on("data", (chunk) => {
950
1122
  chunks.push(chunk);
951
1123
  });
952
1124
  response.on("end", () => {
953
- resolve(chunks.join(""));
1125
+ finish(null, chunks.join(""));
954
1126
  });
955
- response.on("error", reject);
1127
+ response.on("error", (error) => finish(error));
1128
+ if (signal) {
1129
+ if (signal.aborted) {
1130
+ onAbort();
1131
+ return;
1132
+ }
1133
+ signal.addEventListener("abort", onAbort, { once: true });
1134
+ }
956
1135
  response.resume();
957
1136
  });
958
- }, context);
1137
+ }, context, options.signal);
959
1138
  }
960
1139
 
961
1140
  async function readLocalVersion(file) {
@@ -980,7 +1159,7 @@ function parseChecksumManifest(text) {
980
1159
  }
981
1160
  const match = trimmed.match(/^([a-fA-F0-9]{64})\s+\*?(.+)$/);
982
1161
  if (!match) {
983
- throw new Error(`Invalid checksum manifest line: ${trimmed}`);
1162
+ throw new NonRetryableError(`Invalid checksum manifest line: ${trimmed}`);
984
1163
  }
985
1164
  checksums.set(match[2], match[1].toLowerCase());
986
1165
  }
@@ -992,17 +1171,20 @@ async function sha256File(filePath) {
992
1171
  return crypto.createHash("sha256").update(content).digest("hex");
993
1172
  }
994
1173
 
995
- async function verifyChecksum(filePath, assetName, checksums) {
1174
+ async function verifyChecksum(filePath, assetName, checksums, sourceLabel) {
996
1175
  const expected = checksums.get(assetName);
997
1176
  if (!expected) {
998
- throw new NonRetryableError(`Checksum manifest is missing ${assetName}`);
1177
+ const from = sourceLabel ? ` from ${sourceLabel}` : "";
1178
+ throw new NonRetryableError(`Checksum manifest is missing ${assetName}${from}`);
999
1179
  }
1000
1180
  const actual = await sha256File(filePath);
1001
1181
  if (actual !== expected) {
1002
1182
  // Bytes are corrupted; another fetch is unlikely to help without a fix
1003
- // upstream. Mark non-retryable.
1183
+ // upstream. Mark non-retryable. Never mix a locked source's bytes with
1184
+ // another source's manifest.
1185
+ const from = sourceLabel ? ` from ${sourceLabel}` : "";
1004
1186
  throw new NonRetryableError(
1005
- `Checksum mismatch for ${assetName}: expected ${expected}, got ${actual}`,
1187
+ `Checksum mismatch for ${assetName}${from}: expected ${expected}, got ${actual}`,
1006
1188
  );
1007
1189
  }
1008
1190
  }
@@ -1016,6 +1198,234 @@ async function checksumMatches(filePath, assetName, checksums) {
1016
1198
  return actual === expected;
1017
1199
  }
1018
1200
 
1201
+ function formatSourceReceipt(source, version) {
1202
+ return [
1203
+ `source=${source.id}`,
1204
+ `label=${source.label}`,
1205
+ `base=${source.baseUrl}`,
1206
+ `version=${version}`,
1207
+ "",
1208
+ ].join("\n");
1209
+ }
1210
+
1211
+ async function writeSourceReceipt(targetPath, source, version) {
1212
+ await writeFile(`${targetPath}.source`, formatSourceReceipt(source, version), "utf8");
1213
+ }
1214
+
1215
+ function assertManifestHasAssets(checksums, requiredAssets, label) {
1216
+ const missing = [];
1217
+ for (let i = 0; i < requiredAssets.length; i += 1) {
1218
+ const asset = requiredAssets[i];
1219
+ if (!checksums.has(asset)) {
1220
+ missing.push(asset);
1221
+ }
1222
+ }
1223
+ if (missing.length > 0) {
1224
+ throw new NonRetryableError(
1225
+ `${label} checksum manifest is missing ${missing.join(", ")}`,
1226
+ );
1227
+ }
1228
+ }
1229
+
1230
+ async function fetchChecksumManifest(url, options) {
1231
+ const fetchText = options.fetchText || downloadText;
1232
+ const text = await fetchText(url, {
1233
+ context: options.context,
1234
+ signal: options.signal,
1235
+ totalTimeoutMs:
1236
+ options.totalTimeoutMs === undefined || options.totalTimeoutMs === null
1237
+ ? MANIFEST_TIMEOUT_MS
1238
+ : options.totalTimeoutMs,
1239
+ stallMs:
1240
+ options.stallMs === undefined || options.stallMs === null
1241
+ ? MANIFEST_STALL_MS
1242
+ : options.stallMs,
1243
+ });
1244
+ return parseChecksumManifest(text);
1245
+ }
1246
+
1247
+ async function loadSourceManifest(source, options) {
1248
+ const url = releaseAssetUrlFromBase(CHECKSUM_MANIFEST, source.baseUrl);
1249
+ const checksums = await fetchChecksumManifest(url, options);
1250
+ assertManifestHasAssets(checksums, options.requiredAssets || [], source.label);
1251
+ return {
1252
+ id: source.id,
1253
+ label: source.label,
1254
+ baseUrl: source.baseUrl,
1255
+ checksums,
1256
+ };
1257
+ }
1258
+
1259
+ function aggregateSourceErrors(options, failures) {
1260
+ const parts = [];
1261
+ let allNonRetryable = failures.length > 0;
1262
+ let anyRetryable = false;
1263
+ for (let i = 0; i < failures.length; i += 1) {
1264
+ const failure = failures[i];
1265
+ const message =
1266
+ failure.error && failure.error.message
1267
+ ? failure.error.message
1268
+ : String(failure.error);
1269
+ parts.push(`${failure.source.label}: ${message}`);
1270
+ if (
1271
+ !(
1272
+ failure.error &&
1273
+ (failure.error.nonRetryable || failure.error instanceof NonRetryableError)
1274
+ )
1275
+ ) {
1276
+ allNonRetryable = false;
1277
+ }
1278
+ if (isRetryable(failure.error)) {
1279
+ anyRetryable = true;
1280
+ }
1281
+ }
1282
+ const err = new Error(
1283
+ `No usable first-party release source for v${options.version}. ${parts.join("; ")}`,
1284
+ );
1285
+ if (allNonRetryable) {
1286
+ err.nonRetryable = true;
1287
+ } else if (anyRetryable) {
1288
+ err.retryable = true;
1289
+ }
1290
+ return err;
1291
+ }
1292
+
1293
+ async function raceFirstPartyManifests(sources, options) {
1294
+ logInfo(
1295
+ `probing ${sources.map((source) => source.label).join(" and ")} checksum manifests`,
1296
+ );
1297
+ const controllers = sources.map(() => new AbortController());
1298
+
1299
+ return new Promise((resolve, reject) => {
1300
+ let remaining = sources.length;
1301
+ const failures = [];
1302
+ let settled = false;
1303
+
1304
+ const finishSuccess = (index, selected) => {
1305
+ if (settled) {
1306
+ return;
1307
+ }
1308
+ settled = true;
1309
+ for (let i = 0; i < controllers.length; i += 1) {
1310
+ if (i !== index) {
1311
+ try {
1312
+ controllers[i].abort();
1313
+ } catch {
1314
+ // ignore
1315
+ }
1316
+ }
1317
+ }
1318
+ logInfo(`selected ${selected.label} for v${options.version}`);
1319
+ resolve(selected);
1320
+ };
1321
+
1322
+ const finishFailure = (source, error) => {
1323
+ if (settled) {
1324
+ return;
1325
+ }
1326
+ if (isAbortError(error)) {
1327
+ remaining -= 1;
1328
+ if (remaining === 0) {
1329
+ settled = true;
1330
+ reject(aggregateSourceErrors(options, failures));
1331
+ }
1332
+ return;
1333
+ }
1334
+ failures.push({ source, error });
1335
+ remaining -= 1;
1336
+ if (remaining === 0) {
1337
+ settled = true;
1338
+ reject(aggregateSourceErrors(options, failures));
1339
+ }
1340
+ };
1341
+
1342
+ for (let i = 0; i < sources.length; i += 1) {
1343
+ const source = sources[i];
1344
+ loadSourceManifest(source, {
1345
+ context: options.context,
1346
+ fetchText: options.fetchText,
1347
+ requiredAssets: options.requiredAssets,
1348
+ signal: controllers[i].signal,
1349
+ }).then(
1350
+ (selected) => finishSuccess(i, selected),
1351
+ (error) => finishFailure(source, error),
1352
+ );
1353
+ }
1354
+ });
1355
+ }
1356
+
1357
+ async function selectReleaseSource(options) {
1358
+ const version = options.version;
1359
+ const repo = options.repo || "Hmbown/CodeWhale";
1360
+ const env = options.env || process.env;
1361
+ const platform =
1362
+ options.platform === undefined || options.platform === null
1363
+ ? os.platform()
1364
+ : options.platform;
1365
+ const arch =
1366
+ options.arch === undefined || options.arch === null ? os.arch() : options.arch;
1367
+ const requiredAssets = options.requiredAssets || [];
1368
+ const context =
1369
+ options.context === undefined || options.context === null
1370
+ ? "runtime"
1371
+ : options.context;
1372
+ const fetchText = options.fetchText;
1373
+ const override = explicitReleaseBase(env);
1374
+ if (override) {
1375
+ logInfo(`using explicit release base for v${version}`);
1376
+ return loadSourceManifest(
1377
+ {
1378
+ id: "override",
1379
+ label: "explicit release base",
1380
+ baseUrl: override,
1381
+ },
1382
+ {
1383
+ context,
1384
+ fetchText,
1385
+ requiredAssets,
1386
+ },
1387
+ );
1388
+ }
1389
+ if (usesCnbMirror(env)) {
1390
+ assertCnbMirrorSupportedPlatform(platform, arch);
1391
+ logInfo(`using CNB first-party mirror for v${version}`);
1392
+ return loadSourceManifest(
1393
+ {
1394
+ id: "cnb",
1395
+ label: "CNB first-party mirror",
1396
+ baseUrl: cnbReleaseBaseUrl(version),
1397
+ },
1398
+ {
1399
+ context,
1400
+ fetchText,
1401
+ requiredAssets,
1402
+ },
1403
+ );
1404
+ }
1405
+ if (shouldRaceFirstPartyMirrors(env, platform, arch)) {
1406
+ const sources = options.sources || firstPartyReleaseSources(version, repo);
1407
+ return raceFirstPartyManifests(sources, {
1408
+ version,
1409
+ context,
1410
+ fetchText,
1411
+ requiredAssets,
1412
+ });
1413
+ }
1414
+ logInfo(`using GitHub Releases for v${version}`);
1415
+ return loadSourceManifest(
1416
+ {
1417
+ id: "github",
1418
+ label: "GitHub Releases",
1419
+ baseUrl: githubReleaseBaseUrl(version, repo),
1420
+ },
1421
+ {
1422
+ context,
1423
+ fetchText,
1424
+ requiredAssets,
1425
+ },
1426
+ );
1427
+ }
1428
+
1019
1429
  async function loadChecksums(version, repo, options = {}) {
1020
1430
  return parseChecksumManifest(await downloadText(checksumManifestUrl(version, repo), options));
1021
1431
  }
@@ -1058,10 +1468,23 @@ async function adoptExistingBinaryIfValid(targetPath, assetName, version, getChe
1058
1468
  return false;
1059
1469
  }
1060
1470
 
1471
+ async function resolveLockedSource(options) {
1472
+ let sourceId = options.sourceId;
1473
+ let sourceLabel = options.sourceLabel;
1474
+ let baseUrl = options.baseUrl;
1475
+ if (options.getSource) {
1476
+ const source = await options.getSource();
1477
+ sourceId = source.id;
1478
+ sourceLabel = source.label;
1479
+ baseUrl = source.baseUrl;
1480
+ }
1481
+ return { sourceId, sourceLabel, baseUrl };
1482
+ }
1483
+
1061
1484
  async function ensureBinary(targetPath, assetName, version, repo, getChecksums, options = {}) {
1062
1485
  const marker = `${targetPath}.version`;
1063
- const downloadIfNeeded =
1064
- process.env.DEEPSEEK_TUI_FORCE_DOWNLOAD === "1" || process.env.DEEPSEEK_FORCE_DOWNLOAD === "1";
1486
+ const env = options.env || process.env;
1487
+ const downloadIfNeeded = shouldForceDownload(env);
1065
1488
  if (!downloadIfNeeded) {
1066
1489
  const existing = await fileExists(targetPath);
1067
1490
  if (existing) {
@@ -1071,15 +1494,30 @@ async function ensureBinary(targetPath, assetName, version, repo, getChecksums,
1071
1494
  }
1072
1495
  }
1073
1496
  if (await adoptExistingBinaryIfValid(targetPath, assetName, version, getChecksums, marker)) {
1497
+ const locked = await resolveLockedSource(options);
1498
+ if (locked.sourceId) {
1499
+ await writeSourceReceipt(targetPath, {
1500
+ id: locked.sourceId,
1501
+ label: locked.sourceLabel || locked.sourceId,
1502
+ baseUrl: locked.baseUrl || "",
1503
+ }, version);
1504
+ }
1074
1505
  return targetPath;
1075
1506
  }
1076
1507
  }
1077
1508
  const checksums = await getChecksums();
1078
- const url = releaseAssetUrl(assetName, version, repo);
1509
+ const locked = await resolveLockedSource(options);
1510
+ const url = locked.baseUrl
1511
+ ? releaseAssetUrlFromBase(assetName, locked.baseUrl)
1512
+ : releaseAssetUrl(assetName, version, repo);
1079
1513
  const destination = `${targetPath}.${process.pid}.${Date.now()}.download`;
1080
- await download(url, destination, { assetName, context: options.context });
1514
+ const downloadFn = options.download || download;
1515
+ const progressName = locked.sourceLabel
1516
+ ? `${assetName} from ${locked.sourceLabel}`
1517
+ : assetName;
1518
+ await downloadFn(url, destination, { assetName: progressName, context: options.context });
1081
1519
  try {
1082
- await verifyChecksum(destination, assetName, checksums);
1520
+ await verifyChecksum(destination, assetName, checksums, locked.sourceLabel);
1083
1521
  preflightGlibc(destination);
1084
1522
  } catch (error) {
1085
1523
  await unlink(destination).catch(() => {});
@@ -1090,6 +1528,13 @@ async function ensureBinary(targetPath, assetName, version, repo, getChecksums,
1090
1528
  }
1091
1529
  await rename(destination, targetPath);
1092
1530
  await writeFile(marker, String(version), "utf8");
1531
+ if (locked.sourceId) {
1532
+ await writeSourceReceipt(targetPath, {
1533
+ id: locked.sourceId,
1534
+ label: locked.sourceLabel || locked.sourceId,
1535
+ baseUrl: locked.baseUrl || "",
1536
+ }, version);
1537
+ }
1093
1538
  return targetPath;
1094
1539
  }
1095
1540
 
@@ -1108,32 +1553,54 @@ function shouldIgnoreInstallFailure(
1108
1553
  async function run(options = {}) {
1109
1554
  const context =
1110
1555
  options.context === undefined || options.context === null ? "runtime" : options.context;
1111
- if (process.env.DEEPSEEK_TUI_DISABLE_INSTALL === "1" || process.env.DEEPSEEK_DISABLE_INSTALL === "1") {
1556
+ const env = options.env || process.env;
1557
+ if (shouldDisableInstall(env)) {
1112
1558
  return;
1113
1559
  }
1114
- if (shouldSkipOptionalPostinstall(context)) {
1560
+ if (shouldSkipOptionalPostinstall(context, process.argv.slice(2), env)) {
1115
1561
  logInfo(
1116
1562
  "pnpm optional postinstall detected; skipping install-time download. The binary will be checked on first run.",
1117
1563
  );
1118
1564
  return;
1119
1565
  }
1120
- const version = resolvePackageVersion();
1121
- const repo = resolveRepo();
1122
- const paths = binaryPaths();
1123
- const releaseDir = releaseBinaryDirectory();
1566
+ const version = resolvePackageVersion(pkg, env);
1567
+ const repo = resolveRepo(env);
1568
+ const paths = options.paths || binaryPaths();
1569
+ const releaseDir = options.releaseDir || releaseBinaryDirectory();
1124
1570
  await mkdir(releaseDir, { recursive: true });
1125
1571
 
1126
- let checksumsPromise;
1127
- const getChecksums = () => {
1128
- if (!checksumsPromise) {
1129
- checksumsPromise = loadChecksums(version, repo, { context });
1572
+ let sourcePromise;
1573
+ const getSource = () => {
1574
+ if (!sourcePromise) {
1575
+ sourcePromise = selectReleaseSource({
1576
+ version,
1577
+ repo,
1578
+ requiredAssets: [paths.codewhale.asset, paths.codew.asset],
1579
+ context,
1580
+ env,
1581
+ platform: options.platform,
1582
+ arch: options.arch,
1583
+ sources: options.sources,
1584
+ fetchText: options.fetchText,
1585
+ });
1130
1586
  }
1131
- return checksumsPromise;
1587
+ return sourcePromise;
1132
1588
  };
1589
+ const getChecksums = () => getSource().then((source) => source.checksums);
1133
1590
 
1134
1591
  await Promise.all([
1135
- ensureBinary(paths.codewhale.target, paths.codewhale.asset, version, repo, getChecksums, { context }),
1136
- ensureBinary(paths.codew.target, paths.codew.asset, version, repo, getChecksums, { context }),
1592
+ ensureBinary(paths.codewhale.target, paths.codewhale.asset, version, repo, getChecksums, {
1593
+ context,
1594
+ getSource,
1595
+ download: options.download,
1596
+ env,
1597
+ }),
1598
+ ensureBinary(paths.codew.target, paths.codew.asset, version, repo, getChecksums, {
1599
+ context,
1600
+ getSource,
1601
+ download: options.download,
1602
+ env,
1603
+ }),
1137
1604
  ]); // single binary
1138
1605
  }
1139
1606
 
@@ -1159,17 +1626,29 @@ module.exports = {
1159
1626
  run,
1160
1627
  _internal: {
1161
1628
  resolvePackageVersion,
1629
+ resolveRepo,
1162
1630
  isOptionalInstall,
1631
+ shouldForceDownload,
1632
+ shouldDisableInstall,
1633
+ isQuietInstall,
1163
1634
  adoptExistingBinaryIfValid,
1164
1635
  shouldIgnoreInstallFailure,
1165
1636
  shouldSkipOptionalPostinstall,
1166
1637
  httpRequest,
1167
1638
  defaultTimeoutMs,
1168
1639
  defaultStallMs,
1640
+ downloadTimeoutMs,
1641
+ downloadStallMs,
1169
1642
  binaryPaths,
1170
1643
  ensureBinary,
1171
1644
  maxAttempts,
1172
1645
  withRetry,
1646
+ selectReleaseSource,
1647
+ downloadText,
1648
+ download,
1649
+ parseChecksumManifest,
1650
+ MANIFEST_TIMEOUT_MS,
1651
+ MANIFEST_STALL_MS,
1173
1652
  },
1174
1653
  };
1175
1654