Skip to content

Commit

Permalink
feat: add download url and size for ShasumNotMatchError (#400)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed May 10, 2022
1 parent f692927 commit dad3dcf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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

0 comments on commit dad3dcf

Please sign in to comment.