|
| 1 | +/** |
| 2 | + * fix-lock-integrity.js |
| 3 | + * |
| 4 | + * Repair stale `integrity` hashes in package-lock.json for direct deps. |
| 5 | + * |
| 6 | + * The failure this exists for: a dependency version bumped by hand in |
| 7 | + * package.json + package-lock.json (the `Ver -> x.y.z` commits) only moves |
| 8 | + * the lock entry's `version` and `resolved` URL. `integrity` keeps the OLD |
| 9 | + * version's hash, and then BOTH install paths abort: |
| 10 | + * |
| 11 | + * npm error code EINTEGRITY |
| 12 | + * npm error integrity checksum failed when using sha512: |
| 13 | + * wanted sha512-<old 5.5.15 hash> but got sha512-<the real 5.5.25 hash> |
| 14 | + * |
| 15 | + * `npm install --package-lock-only` (our `npm run lock`) does NOT repair it — |
| 16 | + * with no node_modules present npm trusts the lockfile as-is. Only |
| 17 | + * re-resolving the package from the registry does. |
| 18 | + * |
| 19 | + * Usage: |
| 20 | + * node build-src/bin/fix-lock-integrity.js |
| 21 | + * |
| 22 | + * Called automatically by scripts/prepare-web.sh when `npm ci` fails. |
| 23 | + * Idempotent: prints "already correct" and touches nothing when in sync. |
| 24 | + * Exits non-zero only if a registry lookup itself failed. |
| 25 | + */ |
| 26 | +import { readFile, writeFile } from 'node:fs/promises' |
| 27 | +import { resolve, dirname } from 'node:path' |
| 28 | +import { fileURLToPath } from 'node:url' |
| 29 | + |
| 30 | +const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..') |
| 31 | +const pkgPath = resolve(root, 'package.json') |
| 32 | +const lockPath = resolve(root, 'package-lock.json') |
| 33 | + |
| 34 | +const pkg = JSON.parse(await readFile(pkgPath, 'utf8')) |
| 35 | +const lock = JSON.parse(await readFile(lockPath, 'utf8')) |
| 36 | + |
| 37 | +const direct = { |
| 38 | + ...pkg.dependencies, |
| 39 | + ...pkg.devDependencies, |
| 40 | + ...pkg.optionalDependencies |
| 41 | +} |
| 42 | + |
| 43 | +let patched = 0 |
| 44 | +let failed = 0 |
| 45 | + |
| 46 | +for (const [name, range] of Object.entries(direct)) { |
| 47 | + const entry = lock.packages?.[`node_modules/${name}`] |
| 48 | + if (!entry?.version || !entry.resolved?.includes('registry.npmjs.org')) { |
| 49 | + continue |
| 50 | + } |
| 51 | + const version = entry.version |
| 52 | + let dist |
| 53 | + try { |
| 54 | + const res = await fetch( |
| 55 | + `https://registry.npmjs.org/${name.replace('/', '%2f')}/${version}` |
| 56 | + ) |
| 57 | + if (!res.ok) { |
| 58 | + throw new Error(`HTTP ${res.status}`) |
| 59 | + } |
| 60 | + dist = (await res.json()).dist |
| 61 | + } catch (err) { |
| 62 | + console.log(` ! ${name}@${version}: registry lookup failed (${err.message})`) |
| 63 | + failed++ |
| 64 | + continue |
| 65 | + } |
| 66 | + if (!dist?.integrity) { |
| 67 | + console.log(` ! ${name}@${version}: registry has no dist.integrity`) |
| 68 | + failed++ |
| 69 | + continue |
| 70 | + } |
| 71 | + if (entry.integrity === dist.integrity && entry.resolved === dist.tarball) { |
| 72 | + continue |
| 73 | + } |
| 74 | + console.log(` ~ ${name}@${version} (wanted ${range})`) |
| 75 | + console.log(` resolved: ${entry.resolved}`) |
| 76 | + console.log(` -> ${dist.tarball}`) |
| 77 | + console.log(` integrity: ${entry.integrity}`) |
| 78 | + console.log(` -> ${dist.integrity}`) |
| 79 | + entry.integrity = dist.integrity |
| 80 | + entry.resolved = dist.tarball |
| 81 | + patched++ |
| 82 | +} |
| 83 | + |
| 84 | +if (patched > 0) { |
| 85 | + await writeFile(lockPath, `${JSON.stringify(lock, null, 2)}\n`) |
| 86 | + console.log(` fixed ${patched} lockfile entry/entries`) |
| 87 | +} else if (failed === 0) { |
| 88 | + console.log(' lockfile integrity already correct for all direct deps') |
| 89 | +} |
| 90 | + |
| 91 | +if (failed > 0) { |
| 92 | + console.log(` ${failed} direct dep(s) could not be verified`) |
| 93 | + process.exit(1) |
| 94 | +} |
0 commit comments