npm-shim.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. // Wired up at release time as the main package's `bin`:
  15. // "bin": { "codegraph": "scripts/npm-shim.js" }
  16. // with the platform packages listed in `optionalDependencies`.
  17. var childProcess = require('child_process');
  18. var target = process.platform + '-' + process.arch; // e.g. darwin-arm64, linux-x64
  19. var pkg = '@colbymchenry/codegraph-' + target;
  20. var isWindows = process.platform === 'win32';
  21. // On Windows the bundle's launcher is a .cmd batch file. Modern Node refuses to
  22. // spawn .cmd/.bat directly — spawnSync throws EINVAL (the CVE-2024-27980
  23. // hardening, observed on Node 24). So on Windows we skip the .cmd and invoke the
  24. // bundled node.exe against the app entry point directly. On unix the bin launcher
  25. // is a shell script that spawns cleanly.
  26. var command, args;
  27. try {
  28. if (isWindows) {
  29. command = require.resolve(pkg + '/node.exe');
  30. var entry = require.resolve(pkg + '/lib/dist/bin/codegraph.js');
  31. // --liftoff-only: keep tree-sitter's WASM grammars off V8's turboshaft tier
  32. // to avoid the Zone OOM on Node >= 22 (issues #293/#298). The unix launcher
  33. // passes this too; on Windows we invoke node.exe directly so add it here.
  34. args = ['--liftoff-only', entry].concat(process.argv.slice(2));
  35. } else {
  36. command = require.resolve(pkg + '/bin/codegraph');
  37. args = process.argv.slice(2);
  38. }
  39. } catch (e) {
  40. process.stderr.write(
  41. 'codegraph: no prebuilt bundle for ' + target + '.\n' +
  42. 'Expected the optional package ' + pkg + ' to be installed.\n' +
  43. 'Try reinstalling: npm i -g @colbymchenry/codegraph\n' +
  44. 'Or use the standalone installer (no Node required):\n' +
  45. ' curl -fsSL https://raw.githubusercontent.com/colbymchenry/codegraph/main/install.sh | sh\n'
  46. );
  47. process.exit(1);
  48. }
  49. var res = childProcess.spawnSync(command, args, { stdio: 'inherit' });
  50. if (res.error) {
  51. process.stderr.write('codegraph: ' + res.error.message + '\n');
  52. process.exit(1);
  53. }
  54. process.exit(res.status === null ? 1 : res.status);