Skip to content

Commit 753bbcf

Browse files
committed
Merge branch 'main' into dev2
2 parents 7642c4e + c2d67f8 commit 753bbcf

3 files changed

Lines changed: 147 additions & 12 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

‎package-lock.json‎

Lines changed: 41 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/prepare-web.sh‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,18 @@ echo " Version: ${APP_VERSION}"
3939
# --ignore-scripts avoids native module compilation for the host platform
4040
# (node-pty, serialport — not needed, they are esbuild externals).
4141
echo " Installing dependencies ..."
42-
npm ci --legacy-peer-deps --ignore-scripts || {
43-
echo " npm ci failed, falling back to npm install ..."
44-
npm install --legacy-peer-deps --ignore-scripts
45-
}
42+
if ! npm ci --legacy-peer-deps --ignore-scripts; then
43+
# A version bumped by hand in package.json + package-lock.json keeps the OLD
44+
# integrity hash on the lock entry, which makes BOTH `npm ci` and a plain
45+
# `npm install` die with EINTEGRITY. Re-resolve the direct deps from the
46+
# registry, then retry. See build-src/bin/fix-lock-integrity.js.
47+
echo " npm ci failed, re-resolving lockfile integrity ..."
48+
node build-src/bin/fix-lock-integrity.js
49+
npm ci --legacy-peer-deps --ignore-scripts || {
50+
echo " npm ci failed again, falling back to npm install ..."
51+
npm install --legacy-peer-deps --ignore-scripts
52+
}
53+
fi
4654

4755
# Copy @electerm/electerm-react's client sources into src/client/electerm-react
4856
# (gitignored generated dir the vite build imports from). This is the android

0 commit comments

Comments
 (0)