|
| 1 | +import { equal, match } from 'node:assert/strict'; |
| 2 | +import { cpSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { createServer, type IncomingMessage, type ServerResponse } from 'node:http'; |
| 4 | +import { tmpdir } from 'node:os'; |
| 5 | +import path from 'node:path'; |
| 6 | +import { afterEach, beforeEach, describe, it } from 'node:test'; |
| 7 | +import { gzipSync } from 'node:zlib'; |
| 8 | +import { runCommand } from '@oclif/test'; |
| 9 | + |
| 10 | +const fixturesDir = path.resolve(import.meta.dirname, '../../fixtures/install'); |
| 11 | +const projectFixtureDir = path.join(fixturesDir, 'simple-project'); |
| 12 | +const packageFixtureDir = path.join(fixturesDir, 'hd-demo-dep'); |
| 13 | + |
| 14 | +describe('install e2e', () => { |
| 15 | + const originalCwd = process.cwd(); |
| 16 | + const originalCatalogUrl = process.env.HD_INSTALL_CATALOG_URL; |
| 17 | + const originalRegistryOverride = process.env.HD_INSTALL_NPM_REGISTRY_URL; |
| 18 | + const originalNpmCache = process.env.NPM_CONFIG_CACHE; |
| 19 | + let tempDir: string; |
| 20 | + let projectDir: string; |
| 21 | + let registry: MockRegistry | undefined; |
| 22 | + |
| 23 | + beforeEach(async () => { |
| 24 | + tempDir = mkdtempSync(path.join(tmpdir(), 'hd-install-e2e-')); |
| 25 | + projectDir = path.join(tempDir, 'project'); |
| 26 | + cpSync(projectFixtureDir, projectDir, { recursive: true }); |
| 27 | + |
| 28 | + const tarballPath = createFixtureTarball(tempDir); |
| 29 | + registry = await startMockRegistry(tarballPath); |
| 30 | + process.env.HD_INSTALL_CATALOG_URL = `${registry.url}/catalog`; |
| 31 | + process.env.HD_INSTALL_NPM_REGISTRY_URL = registry.url; |
| 32 | + process.env.NPM_CONFIG_CACHE = path.join(tempDir, '.npm-cache'); |
| 33 | + process.chdir(projectDir); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(() => { |
| 37 | + registry?.close(); |
| 38 | + process.chdir(originalCwd); |
| 39 | + restoreEnv('HD_INSTALL_CATALOG_URL', originalCatalogUrl); |
| 40 | + restoreEnv('HD_INSTALL_NPM_REGISTRY_URL', originalRegistryOverride); |
| 41 | + restoreEnv('NPM_CONFIG_CACHE', originalNpmCache); |
| 42 | + rmSync(tempDir, { recursive: true, force: true }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('runs npm install through the local proxy against a real fixture project', async () => { |
| 46 | + if (!registry) { |
| 47 | + throw new Error('Mock registry was not started'); |
| 48 | + } |
| 49 | + |
| 50 | + const output = await runCommand('install'); |
| 51 | + equal(output.error, undefined); |
| 52 | + |
| 53 | + const stdout = output.stdout; |
| 54 | + match(stdout, /Install completed\./); |
| 55 | + |
| 56 | + const installedPackagePath = path.join(projectDir, 'node_modules', 'hd-demo-dep', 'index.js'); |
| 57 | + equal(existsSync(installedPackagePath), true); |
| 58 | + equal(readFileSync(installedPackagePath, 'utf8'), "module.exports = 'installed through hd install';\n"); |
| 59 | + |
| 60 | + const lockfile = JSON.parse(readFileSync(path.join(projectDir, 'package-lock.json'), 'utf8')); |
| 61 | + equal(lockfile.packages['node_modules/hd-demo-dep'].version, '1.0.0'); |
| 62 | + match(lockfile.packages['node_modules/hd-demo-dep'].resolved, /\/hd-demo-dep-1\.0\.0-hd-demo-dep-1\.0\.1\.tgz$/); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +function restoreEnv(name: string, value: string | undefined): void { |
| 67 | + if (value === undefined) { |
| 68 | + delete process.env[name]; |
| 69 | + return; |
| 70 | + } |
| 71 | + process.env[name] = value; |
| 72 | +} |
| 73 | + |
| 74 | +function createFixtureTarball(destination: string): string { |
| 75 | + const entries = [ |
| 76 | + createTarEntry('package/package.json', readFileSync(path.join(packageFixtureDir, 'package.json'))), |
| 77 | + createTarEntry('package/index.js', readFileSync(path.join(packageFixtureDir, 'index.js'))), |
| 78 | + ]; |
| 79 | + const tarballPath = path.join(destination, 'hd-demo-dep-1.0.0.tgz'); |
| 80 | + writeFileSync(tarballPath, gzipSync(Buffer.concat([...entries, Buffer.alloc(1024)]))); |
| 81 | + return tarballPath; |
| 82 | +} |
| 83 | + |
| 84 | +function createTarEntry(name: string, content: Buffer): Buffer { |
| 85 | + const header = Buffer.alloc(512); |
| 86 | + writeTarString(header, name, 0, 100); |
| 87 | + writeTarOctal(header, 0o644, 100, 8); |
| 88 | + writeTarOctal(header, 0, 108, 8); |
| 89 | + writeTarOctal(header, 0, 116, 8); |
| 90 | + writeTarOctal(header, content.length, 124, 12); |
| 91 | + writeTarOctal(header, 0, 136, 12); |
| 92 | + header.fill(' ', 148, 156); |
| 93 | + header[156] = '0'.charCodeAt(0); |
| 94 | + writeTarString(header, 'ustar', 257, 6); |
| 95 | + writeTarString(header, '00', 263, 2); |
| 96 | + |
| 97 | + let checksum = 0; |
| 98 | + for (const byte of header) { |
| 99 | + checksum += byte; |
| 100 | + } |
| 101 | + writeTarOctal(header, checksum, 148, 8); |
| 102 | + |
| 103 | + return Buffer.concat([header, content, Buffer.alloc(padToTarBlock(content.length))]); |
| 104 | +} |
| 105 | + |
| 106 | +function writeTarString(header: Buffer, value: string, offset: number, length: number): void { |
| 107 | + header.write(value, offset, Math.min(Buffer.byteLength(value), length), 'utf8'); |
| 108 | +} |
| 109 | + |
| 110 | +function writeTarOctal(header: Buffer, value: number, offset: number, length: number): void { |
| 111 | + const encoded = value.toString(8).padStart(length - 1, '0'); |
| 112 | + header.write(`${encoded}\0`, offset, length, 'ascii'); |
| 113 | +} |
| 114 | + |
| 115 | +function padToTarBlock(size: number): number { |
| 116 | + const remainder = size % 512; |
| 117 | + return remainder === 0 ? 0 : 512 - remainder; |
| 118 | +} |
| 119 | + |
| 120 | +interface MockRegistry { |
| 121 | + url: string; |
| 122 | + close: () => void; |
| 123 | +} |
| 124 | + |
| 125 | +async function startMockRegistry(tarballPath: string): Promise<MockRegistry> { |
| 126 | + let registryUrl = ''; |
| 127 | + const server = createServer((req, res) => { |
| 128 | + handleRegistryRequest(req, res, registryUrl, tarballPath); |
| 129 | + }); |
| 130 | + |
| 131 | + await new Promise<void>((resolve, reject) => { |
| 132 | + server.once('error', reject); |
| 133 | + server.listen(0, '127.0.0.1', resolve); |
| 134 | + }); |
| 135 | + |
| 136 | + const address = server.address(); |
| 137 | + if (!address || typeof address === 'string') { |
| 138 | + throw new Error('Mock registry did not bind to a TCP port'); |
| 139 | + } |
| 140 | + |
| 141 | + registryUrl = `http://127.0.0.1:${address.port}`; |
| 142 | + |
| 143 | + return { |
| 144 | + url: registryUrl, |
| 145 | + close: () => { |
| 146 | + server.closeAllConnections(); |
| 147 | + server.close(); |
| 148 | + }, |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +function handleRegistryRequest( |
| 153 | + req: IncomingMessage, |
| 154 | + res: ServerResponse, |
| 155 | + registryUrl: string, |
| 156 | + tarballPath: string, |
| 157 | +): void { |
| 158 | + const url = new URL(req.url ?? '/', registryUrl); |
| 159 | + const decodedPathname = decodeURIComponent(url.pathname); |
| 160 | + |
| 161 | + if ( |
| 162 | + req.method === 'GET' && |
| 163 | + (decodedPathname === '/hd-demo-dep' || decodedPathname === '/@neverendingsupport/hd-demo-dep') |
| 164 | + ) { |
| 165 | + const isNesPackage = decodedPathname === '/@neverendingsupport/hd-demo-dep'; |
| 166 | + const packageName = isNesPackage ? '@neverendingsupport/hd-demo-dep' : 'hd-demo-dep'; |
| 167 | + const packageVersion = isNesPackage ? '1.0.0-hd-demo-dep-1.0.1' : '1.0.0'; |
| 168 | + const tarballUrl = isNesPackage |
| 169 | + ? `${registryUrl}/%40neverendingsupport/hd-demo-dep/-/hd-demo-dep-1.0.0-hd-demo-dep-1.0.1.tgz` |
| 170 | + : `${registryUrl}/hd-demo-dep/-/hd-demo-dep-1.0.0.tgz`; |
| 171 | + |
| 172 | + sendJson(res, { |
| 173 | + name: packageName, |
| 174 | + 'dist-tags': { latest: packageVersion }, |
| 175 | + versions: { |
| 176 | + [packageVersion]: { |
| 177 | + name: packageName, |
| 178 | + version: packageVersion, |
| 179 | + main: 'index.js', |
| 180 | + dist: { |
| 181 | + tarball: tarballUrl, |
| 182 | + }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }); |
| 186 | + return; |
| 187 | + } |
| 188 | + |
| 189 | + if (req.method === 'GET' && decodedPathname === '/catalog') { |
| 190 | + sendJson(res, { |
| 191 | + results: [ |
| 192 | + { |
| 193 | + component: 'pkg:npm/hd-demo-dep', |
| 194 | + versions: [ |
| 195 | + { |
| 196 | + version: '1.0.0', |
| 197 | + nes: { |
| 198 | + latest: '1.0.0-hd-demo-dep-1.0.1', |
| 199 | + purl: 'pkg:npm/%40neverendingsupport/hd-demo-dep', |
| 200 | + }, |
| 201 | + }, |
| 202 | + ], |
| 203 | + }, |
| 204 | + ], |
| 205 | + totalPages: 1, |
| 206 | + }); |
| 207 | + return; |
| 208 | + } |
| 209 | + |
| 210 | + if ( |
| 211 | + req.method === 'GET' && |
| 212 | + (decodedPathname === '/hd-demo-dep/-/hd-demo-dep-1.0.0.tgz' || |
| 213 | + decodedPathname === '/@neverendingsupport/hd-demo-dep/-/hd-demo-dep-1.0.0-hd-demo-dep-1.0.1.tgz') |
| 214 | + ) { |
| 215 | + res.writeHead(200, { 'content-type': 'application/octet-stream' }); |
| 216 | + res.end(readFileSync(tarballPath)); |
| 217 | + return; |
| 218 | + } |
| 219 | + |
| 220 | + res.writeHead(404, { 'content-type': 'application/json' }); |
| 221 | + res.end(JSON.stringify({ error: 'not found' })); |
| 222 | +} |
| 223 | + |
| 224 | +function sendJson(res: ServerResponse, body: unknown): void { |
| 225 | + res.writeHead(200, { 'content-type': 'application/json' }); |
| 226 | + res.end(JSON.stringify(body)); |
| 227 | +} |
0 commit comments