|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { spawn } from 'node:child_process'; |
| 3 | +import { createHash } from 'node:crypto'; |
| 4 | +import { once } from 'node:events'; |
| 5 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 6 | +import { createServer } from 'node:http'; |
| 7 | +import { tmpdir } from 'node:os'; |
| 8 | +import { join } from 'node:path'; |
| 9 | +import { gzipSync } from 'node:zlib'; |
| 10 | + |
| 11 | +// Small, valid archives keep this test offline. The installed files are never run. |
| 12 | +function archive(files) { |
| 13 | + const blocks = []; |
| 14 | + for (const [name, contents] of Object.entries(files)) { |
| 15 | + const body = Buffer.from(contents); |
| 16 | + const header = Buffer.alloc(512); |
| 17 | + header.write(name); |
| 18 | + header.write('0000755\0', 100); |
| 19 | + header.write(body.length.toString(8).padStart(11, '0') + '\0', 124); |
| 20 | + header.fill(' ', 148, 156); |
| 21 | + header.write('0', 156); |
| 22 | + const checksum = header.reduce((sum, byte) => sum + byte, 0); |
| 23 | + header.write(checksum.toString(8).padStart(6, '0') + '\0 ', 148); |
| 24 | + blocks.push(header, body, Buffer.alloc((512 - (body.length % 512)) % 512)); |
| 25 | + } |
| 26 | + return gzipSync(Buffer.concat([...blocks, Buffer.alloc(1024)])); |
| 27 | +} |
| 28 | + |
| 29 | +const version = '99.0.0'; |
| 30 | +const musl = process.platform === 'linux' && !process.report.getReport().header.glibcVersionRuntime; |
| 31 | +const platform = `${process.platform}-${process.arch}${musl ? '-musl' : ''}`; |
| 32 | +const nodeRoot = `node-v${version}-${platform}`; |
| 33 | +const nodeArchive = archive({ [`${nodeRoot}/bin/node`]: '#!/bin/sh\nexit 0\n' }); |
| 34 | +const npmArchive = archive({ |
| 35 | + 'package/package.json': JSON.stringify({ |
| 36 | + name: 'npm', |
| 37 | + version, |
| 38 | + bin: { npm: 'bin/npm-cli.js', npx: 'bin/npx-cli.js' }, |
| 39 | + }), |
| 40 | + 'package/bin/npm-cli.js': '// fixture\n', |
| 41 | + 'package/bin/npx-cli.js': '// fixture\n', |
| 42 | +}); |
| 43 | +let downloads = 0; |
| 44 | +const server = createServer((request, response) => { |
| 45 | + if (request.url.endsWith('/SHASUMS256.txt.asc')) { |
| 46 | + response.writeHead(404).end(); |
| 47 | + return; |
| 48 | + } |
| 49 | + if (request.url.endsWith('/SHASUMS256.txt')) { |
| 50 | + response.end(`${createHash('sha256').update(nodeArchive).digest('hex')} ${nodeRoot}.tar.gz\n`); |
| 51 | + return; |
| 52 | + } |
| 53 | + const body = request.url.endsWith(`/${nodeRoot}.tar.gz`) |
| 54 | + ? nodeArchive |
| 55 | + : request.url === `/npm/-/npm-${version}.tgz` |
| 56 | + ? npmArchive |
| 57 | + : undefined; |
| 58 | + if (!body) { |
| 59 | + response.writeHead(404).end(); |
| 60 | + return; |
| 61 | + } |
| 62 | + downloads++; |
| 63 | + if (process.argv[2] === 'known') response.setHeader('Content-Length', body.length); |
| 64 | + response.flushHeaders(); |
| 65 | + // Exercise multiple progress redraws; only the completed screen is snapshotted. |
| 66 | + let offset = 0; |
| 67 | + const timer = setInterval(() => { |
| 68 | + const end = Math.min(offset + Math.ceil(body.length / 8), body.length); |
| 69 | + response.write(body.subarray(offset, end)); |
| 70 | + offset = end; |
| 71 | + if (offset === body.length) response.end(); |
| 72 | + }, 50); |
| 73 | + response.on('close', () => clearInterval(timer)); |
| 74 | +}); |
| 75 | +server.listen(0, '127.0.0.1'); |
| 76 | +await once(server, 'listening'); |
| 77 | +const mirror = `http://127.0.0.1:${server.address().port}`; |
| 78 | +// A separate home prevents mock installs from changing the runner's shared runtime seed. |
| 79 | +const home = mkdtempSync(join(tmpdir(), 'vp-download-progress-')); |
| 80 | +try { |
| 81 | + for (const tool of ['node', 'npm']) { |
| 82 | + console.log(`Before ${tool} download: preserve this output.`); |
| 83 | + const child = spawn('vp', ['env', 'install', `${tool}@${version}`], { |
| 84 | + stdio: 'inherit', |
| 85 | + env: { ...process.env, VP_HOME: home, VP_NODE_DIST_MIRROR: mirror, npm_config_registry: mirror }, |
| 86 | + }); |
| 87 | + const [code, signal] = await once(child, 'exit'); |
| 88 | + assert.equal(signal, null); |
| 89 | + assert.equal(code, 0); |
| 90 | + console.log(`After ${tool} download.`); |
| 91 | + } |
| 92 | + assert.equal(downloads, 2); |
| 93 | +} finally { |
| 94 | + server.close(); |
| 95 | + server.closeAllConnections(); |
| 96 | + rmSync(home, { recursive: true, force: true }); |
| 97 | +} |
0 commit comments