npm-shim.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * npm thin-installer launcher (`scripts/npm-shim.js`) tests.
  3. *
  4. * The shim runs on the user's own Node, locates the per-platform optionalDependency
  5. * bundle, and — when a registry mirror failed to deliver it (issue #303) — falls
  6. * back to downloading the bundle from GitHub Releases. These tests exercise that
  7. * shim as a real subprocess from a temp "main package" dir (its own package.json
  8. * + node_modules), so resolution and version lookup behave hermetically.
  9. *
  10. * The download/checksum paths run against a local self-signed HTTPS server via
  11. * CODEGRAPH_DOWNLOAD_BASE — no real network, no published release needed. The
  12. * shim is launched with async `spawn` (not spawnSync), so the test's event loop
  13. * stays free to serve those requests.
  14. *
  15. * POSIX only: the fake bundle launcher is a shell script and extraction uses the
  16. * system `tar`. Skipped on Windows (where the shim's exec path differs anyway).
  17. */
  18. import { describe, it, expect, beforeAll, afterAll } from 'vitest';
  19. import { spawn, execSync } from 'child_process';
  20. import * as https from 'https';
  21. import * as fs from 'fs';
  22. import * as os from 'os';
  23. import * as path from 'path';
  24. import * as crypto from 'crypto';
  25. import type { AddressInfo } from 'net';
  26. const SHIM_SRC = path.join(__dirname, '..', 'scripts', 'npm-shim.js');
  27. const target = `${process.platform}-${process.arch}`;
  28. const asset = `codegraph-${target}.tar.gz`;
  29. const isWindows = process.platform === 'win32';
  30. function hasOpenssl(): boolean {
  31. try { execSync('openssl version', { stdio: 'ignore' }); return true; } catch { return false; }
  32. }
  33. const CAN_NET = !isWindows && hasOpenssl();
  34. function mkTmp(label: string): string {
  35. return fs.mkdtempSync(path.join(os.tmpdir(), `cg-shim-${label}-`));
  36. }
  37. // A temp dir standing in for the installed @colbymchenry/codegraph main package.
  38. function makePkg(version = '9.9.9-test'): string {
  39. const dir = mkTmp('pkg');
  40. fs.copyFileSync(SHIM_SRC, path.join(dir, 'npm-shim.js'));
  41. fs.writeFileSync(path.join(dir, 'package.json'),
  42. JSON.stringify({ name: '@colbymchenry/codegraph', version }) + '\n');
  43. return dir;
  44. }
  45. // A fake bundle launcher that prints a marker + its args, so we can prove the
  46. // shim found and exec'd it (and passed args through).
  47. function writeLauncher(binDir: string): void {
  48. fs.mkdirSync(binDir, { recursive: true });
  49. const p = path.join(binDir, 'codegraph');
  50. fs.writeFileSync(p, '#!/bin/sh\necho "FAKE_BUNDLE_RAN args:$*"\n');
  51. fs.chmodSync(p, 0o755);
  52. }
  53. // Launch the shim with async spawn so the in-process HTTPS server can respond
  54. // while it runs (spawnSync would block this event loop and deadlock).
  55. function runShim(pkgDir: string, args: string[], env: Record<string, string>) {
  56. return new Promise<{ status: number | null; stdout: string; stderr: string }>((resolve) => {
  57. const child = spawn(process.execPath, [path.join(pkgDir, 'npm-shim.js'), ...args], {
  58. env: { ...process.env, ...env },
  59. });
  60. let stdout = '', stderr = '';
  61. child.stdout.on('data', (d) => { stdout += d.toString(); });
  62. child.stderr.on('data', (d) => { stderr += d.toString(); });
  63. child.on('close', (status) => resolve({ status, stdout, stderr }));
  64. });
  65. }
  66. describe.skipIf(isWindows)('npm-shim launcher', () => {
  67. it('runs the installed optional-dependency bundle without any download', async () => {
  68. const pkg = makePkg();
  69. const platformPkg = path.join(pkg, 'node_modules', '@colbymchenry', `codegraph-${target}`);
  70. writeLauncher(path.join(platformPkg, 'bin'));
  71. fs.writeFileSync(path.join(platformPkg, 'package.json'),
  72. JSON.stringify({ name: `@colbymchenry/codegraph-${target}`, version: '9.9.9-test' }) + '\n');
  73. const cache = mkTmp('cache');
  74. const r = await runShim(pkg, ['--probe-abc'], { CODEGRAPH_INSTALL_DIR: cache });
  75. expect(r.status).toBe(0);
  76. expect(r.stdout).toContain('FAKE_BUNDLE_RAN');
  77. expect(r.stdout).toContain('--probe-abc'); // args passed through
  78. expect(r.stderr).not.toContain('downloading'); // never reached the fallback
  79. expect(fs.existsSync(path.join(cache, 'bundles'))).toBe(false);
  80. });
  81. it('uses an already-cached bundle even when downloads are disabled', async () => {
  82. const pkg = makePkg('1.2.3-cached');
  83. const cache = mkTmp('cache');
  84. writeLauncher(path.join(cache, 'bundles', `${target}-1.2.3-cached`, 'bin'));
  85. const r = await runShim(pkg, ['--probe-xyz'], {
  86. CODEGRAPH_INSTALL_DIR: cache,
  87. CODEGRAPH_NO_DOWNLOAD: '1',
  88. });
  89. expect(r.status).toBe(0);
  90. expect(r.stdout).toContain('FAKE_BUNDLE_RAN');
  91. expect(r.stdout).toContain('--probe-xyz');
  92. expect(r.stderr).toBe('');
  93. });
  94. it('prints actionable guidance and exits 1 when disabled with no bundle', async () => {
  95. const pkg = makePkg();
  96. const r = await runShim(pkg, ['--version'], {
  97. CODEGRAPH_INSTALL_DIR: mkTmp('cache'),
  98. CODEGRAPH_NO_DOWNLOAD: '1',
  99. });
  100. expect(r.status).toBe(1);
  101. expect(r.stderr).toContain(`no prebuilt bundle for ${target}`);
  102. expect(r.stderr).toContain(`@colbymchenry/codegraph-${target}`);
  103. expect(r.stderr).toContain('--registry=https://registry.npmjs.org');
  104. expect(r.stderr).toContain('install.sh');
  105. });
  106. });
  107. describe.skipIf(!CAN_NET)('npm-shim download fallback (local HTTPS)', () => {
  108. let server: https.Server;
  109. let port = 0;
  110. let fixtureBytes: Buffer;
  111. let fixtureSha: string;
  112. let sumsBody: string | null = null; // per-test: SHA256SUMS contents, or null for 404
  113. beforeAll(async () => {
  114. // Self-signed cert for the mock release host.
  115. const cdir = mkTmp('tls');
  116. const keyP = path.join(cdir, 'key.pem');
  117. const certP = path.join(cdir, 'cert.pem');
  118. execSync(
  119. `openssl req -x509 -newkey rsa:2048 -nodes -keyout ${keyP} -out ${certP} -days 1 -subj "/CN=localhost"`,
  120. { stdio: 'ignore' },
  121. );
  122. // Build a fake bundle archive (codegraph-<target>/bin/codegraph), like a real release asset.
  123. const work = mkTmp('fixture');
  124. writeLauncher(path.join(work, `codegraph-${target}`, 'bin'));
  125. const archive = path.join(work, asset);
  126. execSync(`tar -czf ${JSON.stringify(archive)} -C ${JSON.stringify(work)} codegraph-${target}`);
  127. fixtureBytes = fs.readFileSync(archive);
  128. fixtureSha = crypto.createHash('sha256').update(fixtureBytes).digest('hex');
  129. server = https.createServer({ key: fs.readFileSync(keyP), cert: fs.readFileSync(certP) }, (req, res) => {
  130. const url = req.url || '';
  131. if (url.endsWith(`/${asset}`)) {
  132. res.writeHead(200); res.end(fixtureBytes);
  133. } else if (url.endsWith('/SHA256SUMS')) {
  134. if (sumsBody === null) { res.writeHead(404); res.end('not found'); }
  135. else { res.writeHead(200); res.end(sumsBody); }
  136. } else {
  137. res.writeHead(404); res.end('not found');
  138. }
  139. });
  140. await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
  141. port = (server.address() as AddressInfo).port;
  142. }, 30000);
  143. afterAll(() => { server?.close(); });
  144. function netEnv(cache: string): Record<string, string> {
  145. return {
  146. CODEGRAPH_INSTALL_DIR: cache,
  147. CODEGRAPH_DOWNLOAD_BASE: `https://127.0.0.1:${port}`,
  148. NODE_TLS_REJECT_UNAUTHORIZED: '0',
  149. };
  150. }
  151. it('downloads, verifies the checksum, extracts, and execs the bundle', async () => {
  152. sumsBody = `${fixtureSha} ${asset}\n`;
  153. const pkg = makePkg('5.0.0-net');
  154. const cache = mkTmp('cache');
  155. const r = await runShim(pkg, ['--probe-net'], netEnv(cache));
  156. expect(r.stderr).toContain('downloading');
  157. expect(r.stderr).toContain('checksum verified');
  158. expect(r.status).toBe(0);
  159. expect(r.stdout).toContain('FAKE_BUNDLE_RAN');
  160. expect(r.stdout).toContain('--probe-net');
  161. expect(fs.existsSync(path.join(cache, 'bundles', `${target}-5.0.0-net`, 'bin', 'codegraph'))).toBe(true);
  162. }, 20000);
  163. it('aborts (exit 1) on a checksum mismatch and caches nothing', async () => {
  164. sumsBody = `${'0'.repeat(64)} ${asset}\n`;
  165. const pkg = makePkg('5.0.0-bad');
  166. const cache = mkTmp('cache');
  167. const r = await runShim(pkg, ['--version'], netEnv(cache));
  168. expect(r.status).toBe(1);
  169. expect(r.stderr).toContain('checksum mismatch');
  170. expect(r.stdout).not.toContain('FAKE_BUNDLE_RAN'); // never exec'd a tampered bundle
  171. expect(fs.existsSync(path.join(cache, 'bundles', `${target}-5.0.0-bad`))).toBe(false);
  172. }, 20000);
  173. it('proceeds when no SHA256SUMS is published (older releases)', async () => {
  174. sumsBody = null; // 404
  175. const pkg = makePkg('5.0.0-nosums');
  176. const cache = mkTmp('cache');
  177. const r = await runShim(pkg, ['--version'], netEnv(cache));
  178. expect(r.status).toBe(0);
  179. expect(r.stderr).toContain('downloading');
  180. expect(r.stderr).not.toContain('checksum verified'); // skipped, not failed
  181. expect(r.stdout).toContain('FAKE_BUNDLE_RAN');
  182. }, 20000);
  183. });