Skip to content

Commit dad3dcf

Browse files
authored
feat: add download url and size for ShasumNotMatchError (#400)
1 parent f692927 commit dad3dcf

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

lib/download/npm.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,7 @@ async function getTarballStream(tarballUrl, pkg, options) {
484484
stream.on('data', chunk => {
485485
options.totalTarballSize += chunk.length;
486486
});
487+
stream.tarballUrl = tarballUrl;
487488
return stream;
488489
}
489490
} catch (err) {
@@ -512,6 +513,7 @@ async function getTarballStream(tarballUrl, pkg, options) {
512513
result.res.on('data', chunk => {
513514
options.totalTarballSize += chunk.length;
514515
});
516+
result.res.tarballUrl = tarballUrl;
515517
return result.res;
516518
}
517519

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

591593
const stream = fs.createReadStream(tarballFile);
592594
stream.tarballFile = tarballFile;
595+
stream.tarballUrl = tarballUrl;
593596
return stream;
594597
}
595598

@@ -698,6 +701,7 @@ function checkShasumAndUngzip(ungzipDir, readstream, pkg, useTarFormat, options)
698701
return new Promise((resolve, reject) => {
699702
const shasum = pkg.dist.shasum;
700703
const hash = crypto.createHash('sha1');
704+
let tarballSize = 0;
701705
const opts = {
702706
cwd: ungzipDir,
703707
strip: 1,
@@ -760,12 +764,15 @@ function checkShasumAndUngzip(ungzipDir, readstream, pkg, useTarFormat, options)
760764
resolve();
761765
}
762766

763-
readstream.on('data', buf => hash.update(buf));
767+
readstream.on('data', buf => {
768+
tarballSize += buf.length;
769+
hash.update(buf);
770+
});
764771
readstream.on('end', () => {
765772
// this will be fire before extracter `env` event fire.
766773
const realShasum = hash.digest('hex');
767774
if (realShasum !== shasum) {
768-
const err = new Error(`real sha1:${realShasum} not equal to remote:${shasum}`);
775+
const err = new Error(`real sha1:${realShasum} not equal to remote:${shasum}, download url ${readstream.tarballUrl || ''}, download size ${tarballSize}`);
769776
err.name = 'ShasumNotMatchError';
770777
handleCallback(err);
771778
}

test/download.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ describe('test/download.test.js', () => {
9090
throw new Error('should not run this');
9191
} catch (err) {
9292
assert(err.name === 'ShasumNotMatchError');
93-
assert(/real sha1:7f5098d60307b4ef7240c3d693cb20a9473c6074 not equal to remote:00098d60307b4ef7240c3d693cb20a9473c111/.test(err.message));
93+
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\)/
94+
.test(err.message));
9495
}
9596
});
9697

0 commit comments

Comments
 (0)