Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add download url and size for ShasumNotMatchError #400

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/download/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,7 @@ async function getTarballStream(tarballUrl, pkg, options) {
stream.on('data', chunk => {
options.totalTarballSize += chunk.length;
});
stream.tarballUrl = tarballUrl;
return stream;
}
} catch (err) {
Expand Down Expand Up @@ -512,6 +513,7 @@ async function getTarballStream(tarballUrl, pkg, options) {
result.res.on('data', chunk => {
options.totalTarballSize += chunk.length;
});
result.res.tarballUrl = tarballUrl;
return result.res;
}

Expand Down Expand Up @@ -590,6 +592,7 @@ async function getTarballStream(tarballUrl, pkg, options) {

const stream = fs.createReadStream(tarballFile);
stream.tarballFile = tarballFile;
stream.tarballUrl = tarballUrl;
return stream;
}

Expand Down Expand Up @@ -698,6 +701,7 @@ function checkShasumAndUngzip(ungzipDir, readstream, pkg, useTarFormat, options)
return new Promise((resolve, reject) => {
const shasum = pkg.dist.shasum;
const hash = crypto.createHash('sha1');
let tarballSize = 0;
const opts = {
cwd: ungzipDir,
strip: 1,
Expand Down Expand Up @@ -760,12 +764,15 @@ function checkShasumAndUngzip(ungzipDir, readstream, pkg, useTarFormat, options)
resolve();
}

readstream.on('data', buf => hash.update(buf));
readstream.on('data', buf => {
tarballSize += buf.length;
hash.update(buf);
});
readstream.on('end', () => {
// this will be fire before extracter `env` event fire.
const realShasum = hash.digest('hex');
if (realShasum !== shasum) {
const err = new Error(`real sha1:${realShasum} not equal to remote:${shasum}`);
const err = new Error(`real sha1:${realShasum} not equal to remote:${shasum}, download url ${readstream.tarballUrl || ''}, download size ${tarballSize}`);
err.name = 'ShasumNotMatchError';
handleCallback(err);
}
Expand Down
3 changes: 2 additions & 1 deletion test/download.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ describe('test/download.test.js', () => {
throw new Error('should not run this');
} catch (err) {
assert(err.name === 'ShasumNotMatchError');
assert(/real sha1:7f5098d60307b4ef7240c3d693cb20a9473c6074 not equal to remote:00098d60307b4ef7240c3d693cb20a9473c111/.test(err.message));
assert(/real sha1:7f5098d60307b4ef7240c3d693cb20a9473c6074 not equal to remote:00098d60307b4ef7240c3d693cb20a9473c111, download url https:\/\/registry.npmmirror.com\/pedding\/-\/pedding-1.0.0.tgz, download size 2107 \(pedding@1.0.0\)/
.test(err.message));
}
});

Expand Down