1
0

npm-shim.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env node
  2. 'use strict';
  3. //
  4. // npm thin-installer launcher for CodeGraph.
  5. //
  6. // The heavy artifact (a vendored Node runtime + the app) ships as a per-platform
  7. // optionalDependency: @colbymchenry/codegraph-<platform>-<arch>. npm installs
  8. // only the one matching the host, via each package's `os`/`cpu` fields (the
  9. // esbuild pattern). This shim — run by the user's OWN Node — locates that bundle
  10. // and execs its launcher, so the real work always runs on the bundled Node 24
  11. // (with node:sqlite), regardless of the user's Node version. The user's Node is
  12. // only ever a launcher; even an ancient version can run this file.
  13. //
  14. // Self-heal (issue #303): some registries — notably the npmmirror/cnpm mirrors,
  15. // and some corporate proxies — don't reliably mirror the per-platform
  16. // optionalDependencies. npm treats an unfetchable optional dep as success and
  17. // silently skips it, so the bundle goes missing and every command fails. When
  18. // the installed bundle can't be resolved, this shim falls back to downloading
  19. // the matching bundle straight from GitHub Releases — the very archive
  20. // install.sh uses — into a cache dir, then runs that. Knobs:
  21. // CODEGRAPH_NO_DOWNLOAD=1 disable the network fallback (print guidance)
  22. // CODEGRAPH_INSTALL_DIR=DIR cache location (default: ~/.codegraph)
  23. // CODEGRAPH_DOWNLOAD_BASE=URL release-download base (for mirrors/air-gapped)
  24. //
  25. // Wired up at release time as the main package's `bin`:
  26. // "bin": { "codegraph": "npm-shim.js" }
  27. // with the platform packages listed in `optionalDependencies`.
  28. var childProcess = require('child_process');
  29. var fs = require('fs');
  30. var os = require('os');
  31. var path = require('path');
  32. var target = process.platform + '-' + process.arch; // e.g. darwin-arm64, linux-x64
  33. var pkg = '@colbymchenry/codegraph-' + target;
  34. var isWindows = process.platform === 'win32';
  35. var REPO = 'colbymchenry/codegraph';
  36. main().catch(function (e) {
  37. process.stderr.write('codegraph: ' + (e && e.message ? e.message : String(e)) + '\n');
  38. process.exit(1);
  39. });
  40. async function main() {
  41. // Happy path: the npm-installed optional dependency. Fall back to a download
  42. // when the registry didn't deliver it.
  43. var resolved = resolveInstalledBundle() || (await selfHealBundle());
  44. var res = childProcess.spawnSync(resolved.command, resolved.args, { stdio: 'inherit' });
  45. if (res.error) {
  46. process.stderr.write('codegraph: ' + res.error.message + '\n');
  47. process.exit(1);
  48. }
  49. process.exit(res.status === null ? 1 : res.status);
  50. }
  51. // Resolve the launcher from the installed per-platform optionalDependency.
  52. // Returns {command, args} or null if the package isn't installed.
  53. function resolveInstalledBundle() {
  54. try {
  55. if (isWindows) {
  56. // Modern Node refuses to spawn the bundle's .cmd directly (EINVAL, the
  57. // CVE-2024-27980 hardening on Node 24), so invoke the bundled node.exe
  58. // against the app entry point and pass --liftoff-only here.
  59. var nodeExe = require.resolve(pkg + '/node.exe');
  60. var entry = require.resolve(pkg + '/lib/dist/bin/codegraph.js');
  61. return { command: nodeExe, args: liftoff(entry) };
  62. }
  63. return { command: require.resolve(pkg + '/bin/codegraph'), args: process.argv.slice(2) };
  64. } catch (e) {
  65. return null;
  66. }
  67. }
  68. // Locate the launcher inside an extracted GitHub bundle directory (same
  69. // node/lib/bin layout as the npm platform package). Returns {command, args} or
  70. // null when the directory doesn't hold a usable bundle yet.
  71. function launcherIn(dir) {
  72. if (isWindows) {
  73. var nodeExe = path.join(dir, 'node.exe');
  74. var entry = path.join(dir, 'lib', 'dist', 'bin', 'codegraph.js');
  75. if (fs.existsSync(nodeExe) && fs.existsSync(entry)) {
  76. return { command: nodeExe, args: liftoff(entry) };
  77. }
  78. } else {
  79. var launcher = path.join(dir, 'bin', 'codegraph');
  80. if (fs.existsSync(launcher)) return { command: launcher, args: process.argv.slice(2) };
  81. }
  82. return null;
  83. }
  84. // --liftoff-only keeps tree-sitter's WASM grammars off V8's turboshaft tier to
  85. // avoid the Zone OOM on Node >= 22 (issues #293/#298). The unix bin/codegraph
  86. // launcher already passes it; on Windows we invoke node.exe directly so add it.
  87. function liftoff(entry) {
  88. return ['--liftoff-only', entry].concat(process.argv.slice(2));
  89. }
  90. // Download + cache the platform bundle from GitHub Releases. Returns
  91. // {command, args}; exits the process with guidance if it can't.
  92. async function selfHealBundle() {
  93. var version = readVersion();
  94. var bundlesDir = path.join(process.env.CODEGRAPH_INSTALL_DIR || path.join(os.homedir(), '.codegraph'), 'bundles');
  95. var dest = path.join(bundlesDir, target + '-' + version);
  96. // Already downloaded by a previous run? Use it even when downloads are
  97. // disabled — CODEGRAPH_NO_DOWNLOAD blocks fetching, not a cached bundle.
  98. var cached = launcherIn(dest);
  99. if (cached) return cached;
  100. if (process.env.CODEGRAPH_NO_DOWNLOAD) {
  101. fail('the network fallback is disabled (CODEGRAPH_NO_DOWNLOAD is set).');
  102. }
  103. var asset = 'codegraph-' + target + (isWindows ? '.zip' : '.tar.gz');
  104. var base = process.env.CODEGRAPH_DOWNLOAD_BASE || ('https://github.com/' + REPO + '/releases/download');
  105. var url = base + '/v' + version + '/' + asset;
  106. process.stderr.write(
  107. 'codegraph: platform bundle missing (registry did not provide ' + pkg + ').\n' +
  108. 'codegraph: downloading ' + asset + ' from GitHub Releases (' + version + ')...\n'
  109. );
  110. // Stage inside bundlesDir so the final rename is on the same filesystem (atomic,
  111. // no EXDEV across tmpfs). Strip the archive's top-level codegraph-<target>/ dir.
  112. fs.mkdirSync(bundlesDir, { recursive: true });
  113. var stage = fs.mkdtempSync(path.join(bundlesDir, '.dl-'));
  114. try {
  115. var archivePath = path.join(stage, asset);
  116. await download(url, archivePath, 6);
  117. await verifyChecksum(archivePath, asset, base, version);
  118. var extracted = path.join(stage, 'bundle');
  119. fs.mkdirSync(extracted);
  120. extract(archivePath, extracted);
  121. var raced = launcherIn(dest); // another process may have finished meanwhile
  122. if (raced) { rmrf(stage); return raced; }
  123. try {
  124. fs.renameSync(extracted, dest);
  125. } catch (e) {
  126. var other = launcherIn(dest); // lost the race but theirs is valid
  127. if (other) { rmrf(stage); return other; }
  128. throw e;
  129. }
  130. } catch (e) {
  131. rmrf(stage);
  132. fail('download failed (' + e.message + ').\n URL: ' + url);
  133. }
  134. rmrf(stage);
  135. var ready = launcherIn(dest);
  136. if (!ready) fail('downloaded bundle is missing its launcher under ' + dest + '.');
  137. process.stderr.write('codegraph: bundle ready.\n');
  138. return ready;
  139. }
  140. function readVersion() {
  141. try {
  142. return require(path.join(__dirname, 'package.json')).version;
  143. } catch (e) {
  144. fail('could not read this package\'s version to locate a matching release.');
  145. }
  146. }
  147. // GET with manual redirect following (GitHub release URLs redirect to a CDN).
  148. function download(url, dest, redirectsLeft) {
  149. return new Promise(function (resolve, reject) {
  150. var https = require('https');
  151. // timeout is an idle/inactivity timeout — it won't kill a slow-but-progressing
  152. // download, only a stalled connection (so a blocked mirror fails fast with
  153. // guidance instead of hanging the user's command forever).
  154. var req = https.get(url, { headers: { 'User-Agent': 'codegraph-npm-shim' }, timeout: 30000 }, function (res) {
  155. var status = res.statusCode;
  156. if (status >= 300 && status < 400 && res.headers.location) {
  157. res.resume();
  158. if (redirectsLeft <= 0) { reject(new Error('too many redirects')); return; }
  159. download(new URL(res.headers.location, url).toString(), dest, redirectsLeft - 1).then(resolve, reject);
  160. return;
  161. }
  162. if (status !== 200) { res.resume(); reject(new Error('HTTP ' + status)); return; }
  163. var file = fs.createWriteStream(dest);
  164. res.on('error', reject);
  165. res.pipe(file);
  166. file.on('error', reject);
  167. file.on('finish', function () { file.close(function () { resolve(); }); });
  168. });
  169. req.on('timeout', function () { req.destroy(new Error('connection timed out')); });
  170. req.on('error', reject);
  171. });
  172. }
  173. // Best-effort integrity check. When the release publishes a SHA256SUMS file, the
  174. // downloaded archive MUST match its listed hash or we abort. When that file is
  175. // absent (older releases) or simply unreachable, we proceed — the archive still
  176. // arrived from GitHub over TLS. So tampering/corruption is caught, while a
  177. // missing checksum never breaks an install.
  178. async function verifyChecksum(archivePath, asset, base, version) {
  179. var sumsPath = archivePath + '.SHA256SUMS';
  180. try {
  181. await download(base + '/v' + version + '/SHA256SUMS', sumsPath, 6);
  182. } catch (e) {
  183. return; // not published / unreachable → skip
  184. }
  185. var expected = null;
  186. var lines = fs.readFileSync(sumsPath, 'utf8').split('\n');
  187. for (var i = 0; i < lines.length; i++) {
  188. var m = lines[i].trim().match(/^([0-9a-fA-F]{64})\s+\*?(.+)$/);
  189. if (m && path.basename(m[2].trim()) === asset) { expected = m[1].toLowerCase(); break; }
  190. }
  191. if (!expected) return; // asset not listed → nothing to check
  192. var actual = require('crypto').createHash('sha256').update(fs.readFileSync(archivePath)).digest('hex');
  193. if (actual !== expected) {
  194. throw new Error('checksum mismatch for ' + asset +
  195. ' (expected ' + expected.slice(0, 12) + '…, got ' + actual.slice(0, 12) + '…)');
  196. }
  197. process.stderr.write('codegraph: checksum verified.\n');
  198. }
  199. // Extract via the system tar — present on macOS, Linux, and Windows 10+
  200. // (bsdtar reads .zip too). No third-party dependency in the shim.
  201. function extract(archive, destDir) {
  202. var args = isWindows
  203. ? ['-xf', archive, '-C', destDir, '--strip-components=1']
  204. : ['-xzf', archive, '-C', destDir, '--strip-components=1'];
  205. var res = childProcess.spawnSync('tar', args, { stdio: 'ignore' });
  206. if (res.error) throw new Error('tar unavailable: ' + res.error.message);
  207. if (res.status !== 0) throw new Error('tar exited ' + res.status);
  208. }
  209. function rmrf(p) {
  210. try { fs.rmSync(p, { recursive: true, force: true }); } catch (e) { /* best effort */ }
  211. }
  212. function fail(reason) {
  213. process.stderr.write(
  214. 'codegraph: no prebuilt bundle for ' + target + '.\n' +
  215. (reason ? 'codegraph: ' + reason + '\n' : '') +
  216. 'Expected the optional package ' + pkg + ' to be installed.\n' +
  217. 'A registry mirror (e.g. npmmirror/cnpm) that did not mirror the per-platform\n' +
  218. 'package is the usual cause. Fixes:\n' +
  219. ' - install from the official registry:\n' +
  220. ' npm i -g @colbymchenry/codegraph --registry=https://registry.npmjs.org\n' +
  221. ' - or use the standalone installer (no Node required):\n' +
  222. ' curl -fsSL https://raw.githubusercontent.com/' + REPO + '/main/install.sh | sh\n'
  223. );
  224. process.exit(1);
  225. }