Skip to content

Commit aab5ea5

Browse files
build: embed binary checksums in the npm package (electron#30611)
* build: embed binary checksums in the npm package * Update docs/tutorial/installation.md Co-authored-by: Jeremy Rose <[email protected]> * refactor: replace reduce with loop Co-authored-by: Jeremy Rose <[email protected]>
1 parent 7093cd7 commit aab5ea5

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ compile_commands.json
2626
# npm package
2727
/npm/dist
2828
/npm/path.txt
29+
/npm/checksums.json
2930

3031
.npmrc
3132

docs/tutorial/installation.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ ELECTRON_CUSTOM_DIR="{{ version }}"
9090
The above configuration will download from URLs such as
9191
`https://npm.taobao.org/mirrors/electron/8.0.0/electron-v8.0.0-linux-x64.zip`.
9292

93+
If your mirror serves artifacts with different checksums to the official
94+
Electron release you may have to set `ELECTRON_USE_REMOTE_CHECKSUMS=1` to
95+
force Electron to use the remote `SHASUMS256.txt` file to verify the checksum
96+
instead of the embedded checksums.
97+
9398
#### Cache
9499

95100
Alternatively, you can override the local cache. `@electron/get` will cache

npm/install.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ downloadArtifact({
4141
artifactName: 'electron',
4242
force: process.env.force_no_cache === 'true',
4343
cacheRoot: process.env.electron_config_cache,
44+
checksums: process.env.electron_use_remote_checksums ? undefined : require('./checksums.json'),
4445
platform,
4546
arch
4647
}).then(extractFile).catch(err => {

npm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"postinstall": "node install.js"
99
},
1010
"dependencies": {
11-
"@electron/get": "^1.0.1",
11+
"@electron/get": "^1.13.0",
1212
"@types/node": "^14.6.2",
1313
"extract-zip": "^1.0.3"
1414
},

script/release/publish-to-npm.js

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,27 @@ new Promise((resolve, reject) => {
103103

104104
return release;
105105
})
106+
.then(async (release) => {
107+
const checksumsAsset = release.assets.find((asset) => asset.name === 'SHASUMS256.txt');
108+
if (!checksumsAsset) {
109+
throw new Error(`cannot find SHASUMS256.txt from v${rootPackageJson.version} release assets`);
110+
}
111+
112+
const checksumsContent = await getAssetContents(
113+
rootPackageJson.version.indexOf('nightly') > 0 ? 'nightlies' : 'electron',
114+
checksumsAsset.id
115+
);
116+
117+
const checksumsObject = {};
118+
for (const line of checksumsContent.trim().split('\n')) {
119+
const [checksum, file] = line.split(' *');
120+
checksumsObject[file] = checksum;
121+
}
122+
123+
fs.writeFileSync(path.join(tempDir, 'checksums.json'), JSON.stringify(checksumsObject, null, 2));
124+
125+
return release;
126+
})
106127
.then(async (release) => {
107128
const currentBranch = await getCurrentBranch();
108129

@@ -145,10 +166,26 @@ new Promise((resolve, reject) => {
145166
// test that the package can install electron prebuilt from github release
146167
const tarballPath = path.join(tempDir, `${rootPackageJson.name}-${rootPackageJson.version}.tgz`);
147168
return new Promise((resolve, reject) => {
148-
childProcess.execSync(`npm install ${tarballPath} --force --silent`, {
169+
const result = childProcess.spawnSync('npm', ['install', tarballPath, '--force', '--silent'], {
149170
env: Object.assign({}, process.env, { electron_config_cache: tempDir }),
150-
cwd: tempDir
171+
cwd: tempDir,
172+
stdio: 'inherit'
151173
});
174+
if (result.status !== 0) {
175+
return reject(new Error(`npm install failed with status ${result.status}`));
176+
}
177+
try {
178+
const electronPath = require(path.resolve(tempDir, 'node_modules', rootPackageJson.name));
179+
if (typeof electronPath !== 'string') {
180+
return reject(new Error(`path to electron binary (${electronPath}) returned by the ${rootPackageJson.name} module is not a string`));
181+
}
182+
if (!fs.existsSync(electronPath)) {
183+
return reject(new Error(`path to electron binary (${electronPath}) returned by the ${rootPackageJson.name} module does not exist on disk`));
184+
}
185+
} catch (e) {
186+
console.error(e);
187+
return reject(new Error(`loading the generated ${rootPackageJson.name} module failed with an error`));
188+
}
152189
resolve(tarballPath);
153190
});
154191
})
@@ -181,6 +218,6 @@ new Promise((resolve, reject) => {
181218
}
182219
})
183220
.catch((err) => {
184-
console.error(`Error: ${err}`);
221+
console.error('Error:', err);
185222
process.exit(1);
186223
});

0 commit comments

Comments
 (0)