diff --git a/lib/download/npm.js b/lib/download/npm.js index 6c6cd1d6..9d9211e8 100644 --- a/lib/download/npm.js +++ b/lib/download/npm.js @@ -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) { @@ -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; } @@ -590,6 +592,7 @@ async function getTarballStream(tarballUrl, pkg, options) { const stream = fs.createReadStream(tarballFile); stream.tarballFile = tarballFile; + stream.tarballUrl = tarballUrl; return stream; } @@ -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, @@ -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); } diff --git a/test/download.test.js b/test/download.test.js index 7875f3ad..46ab78f4 100644 --- a/test/download.test.js +++ b/test/download.test.js @@ -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)); } });