|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | +import { execFileSync, spawn } from 'node:child_process'; |
| 3 | +import { |
| 4 | + existsSync, |
| 5 | + lstatSync, |
| 6 | + mkdirSync, |
| 7 | + readdirSync, |
| 8 | + readFileSync, |
| 9 | + realpathSync, |
| 10 | + writeFileSync, |
| 11 | +} from 'node:fs'; |
| 12 | +// eslint-disable-next-line no-restricted-imports -- Containment checks require host-native filesystem paths, not Vite identifiers. |
| 13 | +import { basename, dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'; |
| 14 | +import { pathToFileURL } from 'node:url'; |
| 15 | +import { parseArgs } from 'node:util'; |
| 16 | + |
| 17 | +const { values } = parseArgs({ |
| 18 | + options: { |
| 19 | + repo: { type: 'string' }, |
| 20 | + out: { type: 'string' }, |
| 21 | + archives: { type: 'string' }, |
| 22 | + help: { type: 'boolean', short: 'h' }, |
| 23 | + }, |
| 24 | +}); |
| 25 | +if (values.help) { |
| 26 | + console.log( |
| 27 | + 'Usage: node measure-install.mjs --repo <Void checkout> --out <new directory> [--archives <captured cohort>]', |
| 28 | + ); |
| 29 | +} else { |
| 30 | + try { |
| 31 | + await measure(); |
| 32 | + } catch (error) { |
| 33 | + console.error(error.message); |
| 34 | + process.exitCode = 1; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +async function measure() { |
| 39 | + assert(values.repo && values.out, '--repo and --out are required; use --help.'); |
| 40 | + const repository = realpathSync(resolve(values.repo)); |
| 41 | + // Resolve existing ancestors too: /var and /private/var alias on macOS. |
| 42 | + let ancestor = resolve(values.out); |
| 43 | + const suffix = []; |
| 44 | + while (!existsSync(ancestor)) { |
| 45 | + suffix.unshift(basename(ancestor)); |
| 46 | + const parent = dirname(ancestor); |
| 47 | + assert.notEqual(parent, ancestor, 'Cannot resolve output directory.'); |
| 48 | + ancestor = parent; |
| 49 | + } |
| 50 | + const output = join(realpathSync(ancestor), ...suffix); |
| 51 | + const within = relative(repository, output); |
| 52 | + assert( |
| 53 | + within.startsWith('..' + sep) || within === '..' || isAbsolute(within), |
| 54 | + '--out must be outside the checkout.', |
| 55 | + ); |
| 56 | + assert( |
| 57 | + !existsSync(output), |
| 58 | + '--out must be a new directory; existing results are never overwritten.', |
| 59 | + ); |
| 60 | + const registryModule = join(repository, '.github/scripts/packed-registry.mjs'); |
| 61 | + assert(existsSync(registryModule), 'Not a Void checkout: packed registry helper missing.'); |
| 62 | + const { inspectPackage, startPackedRegistry } = await import(pathToFileURL(registryModule)); |
| 63 | + const publicPackages = readdirSync(join(repository, 'packages'), { withFileTypes: true }) |
| 64 | + .filter((entry) => entry.isDirectory()) |
| 65 | + .map((entry) => ({ |
| 66 | + cwd: join(repository, 'packages', entry.name), |
| 67 | + manifest: JSON.parse( |
| 68 | + readFileSync(join(repository, 'packages', entry.name, 'package.json'), 'utf8'), |
| 69 | + ), |
| 70 | + })) |
| 71 | + .filter((pkg) => pkg.manifest.private !== true); |
| 72 | + const archives = values.archives |
| 73 | + ? realpathSync(resolve(values.archives)) |
| 74 | + : join(output, 'archives'); |
| 75 | + mkdirSync(output, { recursive: true }); |
| 76 | + if (!values.archives) { |
| 77 | + mkdirSync(archives); |
| 78 | + for (const { cwd, manifest } of publicPackages) { |
| 79 | + execFileSync( |
| 80 | + 'vp', |
| 81 | + ['pm', 'pack', '--out', join(archives, `${manifest.name.replaceAll('/', '-')}.tgz`)], |
| 82 | + { cwd, stdio: 'pipe' }, |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + const packages = readdirSync(archives) |
| 87 | + .filter((name) => name.endsWith('.tgz')) |
| 88 | + .map((name) => inspectPackage(join(archives, name))); |
| 89 | + for (const { manifest } of publicPackages) { |
| 90 | + assert( |
| 91 | + packages.some((pkg) => pkg.manifest.name === manifest.name), |
| 92 | + `Incomplete cohort: missing ${manifest.name}.`, |
| 93 | + ); |
| 94 | + } |
| 95 | + const version = packages.find((pkg) => pkg.manifest.name === 'void')?.manifest.version; |
| 96 | + assert(version, 'Captured cohort must include void.'); |
| 97 | + assert( |
| 98 | + packages.some((pkg) => pkg.manifest.name === '@void/platform'), |
| 99 | + 'Captured cohort must include @void/platform.', |
| 100 | + ); |
| 101 | + for (const pkg of packages) { |
| 102 | + if (pkg.manifest.name === 'void' || pkg.manifest.name.startsWith('@void/')) { |
| 103 | + assert.equal( |
| 104 | + pkg.manifest.version, |
| 105 | + version, |
| 106 | + `${pkg.manifest.name}: inconsistent release cohort version.`, |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + const registry = await startPackedRegistry(packages); |
| 111 | + try { |
| 112 | + const config = join(output, '.npmrc'); |
| 113 | + writeFileSync( |
| 114 | + join(output, 'package.json'), |
| 115 | + JSON.stringify({ private: true, dependencies: { void: version } }, null, 2) + '\n', |
| 116 | + ); |
| 117 | + writeFileSync( |
| 118 | + config, |
| 119 | + `registry=${registry.origin}\naudit=false\nfund=false\nengine-strict=true\n`, |
| 120 | + ); |
| 121 | + const environment = { ...process.env }; |
| 122 | + for (const key of Object.keys(environment)) { |
| 123 | + if ( |
| 124 | + /^(VOID_|VP_|CLOUDFLARE_|CF_|WRANGLER_|NPM_TOKEN$|NODE_AUTH_TOKEN$|npm_config_)/i.test(key) |
| 125 | + ) { |
| 126 | + delete environment[key]; |
| 127 | + } |
| 128 | + } |
| 129 | + environment.npm_config_userconfig = config; |
| 130 | + console.log(`Installing captured cohort: void@${version}, npm --omit=dev`); |
| 131 | + await new Promise((done, fail) => { |
| 132 | + const child = spawn('npm', ['install', '--omit=dev', '--no-audit', '--no-fund'], { |
| 133 | + cwd: output, |
| 134 | + env: environment, |
| 135 | + stdio: 'inherit', |
| 136 | + timeout: 300_000, |
| 137 | + }); |
| 138 | + child.on('error', fail); |
| 139 | + child.on('exit', (code, signal) => |
| 140 | + code === 0 ? done() : fail(new Error(`npm install failed: ${signal ?? code}`)), |
| 141 | + ); |
| 142 | + }); |
| 143 | + execFileSync( |
| 144 | + process.execPath, |
| 145 | + [ |
| 146 | + '--input-type=module', |
| 147 | + '-e', |
| 148 | + ` |
| 149 | + import { findPackageJSON } from 'node:module'; |
| 150 | + import { readFileSync } from 'node:fs'; |
| 151 | + import { pathToFileURL } from 'node:url'; |
| 152 | + const file = findPackageJSON('@void/platform', import.meta.resolve('void')); |
| 153 | + const entry = JSON.parse(readFileSync(file, 'utf8')).exports['./installer'].import; |
| 154 | + const { resolvePlatformRuntime } = await import(new URL(entry, pathToFileURL(file))); |
| 155 | + if (resolvePlatformRuntime().manifest.platformVersion !== ${JSON.stringify(version)}) throw new Error('Packaged runtime version mismatch.'); |
| 156 | + `, |
| 157 | + ], |
| 158 | + { cwd: output, env: environment, stdio: 'inherit' }, |
| 159 | + ); |
| 160 | + |
| 161 | + let uniqueBytes = 0; |
| 162 | + let logicalBytes = 0; |
| 163 | + const seen = new Set(); |
| 164 | + const runtimes = []; |
| 165 | + const largeFiles = []; |
| 166 | + const runtimeNames = new Set([ |
| 167 | + 'cf', |
| 168 | + 'miniflare', |
| 169 | + 'workerd', |
| 170 | + 'wrangler', |
| 171 | + '@cloudflare/runtime-types', |
| 172 | + '@cloudflare/vite-plugin', |
| 173 | + ]); |
| 174 | + function walk(directory) { |
| 175 | + for (const entry of readdirSync(directory, { withFileTypes: true })) { |
| 176 | + const file = join(directory, entry.name); |
| 177 | + if (entry.isDirectory()) { |
| 178 | + walk(file); |
| 179 | + } else if (entry.isFile()) { |
| 180 | + const stat = lstatSync(file); |
| 181 | + const inode = `${stat.dev}:${stat.ino}`; |
| 182 | + logicalBytes += stat.size; |
| 183 | + if (!seen.has(inode)) { |
| 184 | + seen.add(inode); |
| 185 | + uniqueBytes += stat.size; |
| 186 | + } |
| 187 | + if (stat.size > 10 * 1024 * 1024) { |
| 188 | + largeFiles.push({ path: relative(output, file), bytes: stat.size, inode }); |
| 189 | + } |
| 190 | + if (entry.name === 'package.json') { |
| 191 | + // Bundled fixtures can contain non-manifest package.json files. |
| 192 | + let manifest; |
| 193 | + try { |
| 194 | + manifest = JSON.parse(readFileSync(file, 'utf8')); |
| 195 | + } catch { |
| 196 | + continue; |
| 197 | + } |
| 198 | + if (runtimeNames.has(manifest.name)) { |
| 199 | + runtimes.push({ |
| 200 | + name: manifest.name, |
| 201 | + version: manifest.version, |
| 202 | + path: relative(output, file), |
| 203 | + }); |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + walk(join(output, 'node_modules')); |
| 210 | + let diskMiB = null; |
| 211 | + if (process.platform !== 'win32') { |
| 212 | + const du = execFileSync('du', ['-sk', join(output, 'node_modules')], { encoding: 'utf8' }); |
| 213 | + diskMiB = Number(du.trim().split(/\s+/)[0]) / 1024; |
| 214 | + } |
| 215 | + const result = { |
| 216 | + capturedAt: new Date().toISOString(), |
| 217 | + repositoryCommit: execFileSync('git', ['rev-parse', 'HEAD'], { |
| 218 | + cwd: repository, |
| 219 | + encoding: 'utf8', |
| 220 | + }).trim(), |
| 221 | + version, |
| 222 | + node: process.version, |
| 223 | + npm: execFileSync('npm', ['--version'], { env: environment, encoding: 'utf8' }).trim(), |
| 224 | + platform: process.platform, |
| 225 | + arch: process.arch, |
| 226 | + archives, |
| 227 | + install: 'npm install --omit=dev (fresh local cohort, no consumer overrides)', |
| 228 | + uniqueBytes, |
| 229 | + uniqueMiB: uniqueBytes / 1024 / 1024, |
| 230 | + logicalBytes, |
| 231 | + logicalMiB: logicalBytes / 1024 / 1024, |
| 232 | + diskMiB, |
| 233 | + runtimes, |
| 234 | + largeFiles, |
| 235 | + }; |
| 236 | + writeFileSync(join(output, 'footprint.json'), JSON.stringify(result, null, 2) + '\n'); |
| 237 | + console.log(JSON.stringify(result, null, 2)); |
| 238 | + } finally { |
| 239 | + await registry.close(); |
| 240 | + } |
| 241 | +} |
0 commit comments