fix: better message on integrity checksum error

This commit is contained in:
Zoltan Kochan
2019-11-10 14:56:57 +02:00
parent 25b2904b07
commit a03a4911db
3 changed files with 36 additions and 7 deletions

View File

@@ -148,7 +148,7 @@ test('fail when shasum from lockfile does not match with the actual one', async
}, await testDefaults({}, {}, { fetchRetries: 0 }))
t.fail('installation should have failed')
} catch (err) {
t.equal(err.code, 'EINTEGRITY')
t.equal(err.code, 'ERR_PNPM_TARBALL_INTEGRITY')
}
})

View File

@@ -29,6 +29,29 @@ class TarballFetchError extends PnpmError {
}
}
class TarballIntegrityError extends PnpmError {
public readonly found: string
public readonly expected: string
public readonly algorithm: string
public readonly sri: string
public readonly url: string
constructor (opts: {
found: string,
expected: string,
algorithm: string,
sri: string,
url: string,
}) {
super('TARBALL_INTEGRITY', `Got unexpected checksum for "${opts.url}". Wanted "${opts.expected}". Got "${opts.found}".`)
this.found = opts.found
this.expected = opts.expected
this.algorithm = opts.algorithm
this.sri = opts.sri
this.url = opts.url
}
}
export interface HttpResponse {
body: string
}
@@ -173,7 +196,7 @@ export default (
const tempLocation = pathTemp(opts.unpackTo)
const ignore = gotOpts.fsIsCaseSensitive ? opts.ignore : createIgnorer(url, opts.ignore)
Promise.all([
opts.integrity && safeCheckStream(res.body, opts.integrity) || true,
opts.integrity && safeCheckStream(res.body, opts.integrity, url) || true,
unpackStream.local(res.body, tempLocation, {
generateIntegrity: opts.generatePackageIntegrity,
ignore,
@@ -241,12 +264,18 @@ function createIgnorer (tarballUrl: string, ignore?: (filename: string) => boole
}
}
async function safeCheckStream (stream: any, integrity: string): Promise<true | Error> { // tslint:disable-line:no-any
async function safeCheckStream (stream: any, integrity: string, url: string): Promise<true | Error> { // tslint:disable-line:no-any
try {
await ssri.checkStream(stream, integrity)
return true
} catch (err) {
return err
return new TarballIntegrityError({
algorithm: err['algorithm'],
expected: err['expected'],
found: err['found'],
sri: err['sri'],
url,
})
}
}

View File

@@ -256,9 +256,9 @@ test('fail when integrity check fails two times in a row', async t => {
})
t.fail('should have failed')
} catch (err) {
t.equal(err.message, 'sha1-HssnaJydJVE+rbyZFKc/VAi+enY= integrity checksum failed when using sha1: ' +
'wanted sha1-HssnaJydJVE+rbyZFKc/VAi+enY= but got sha512-VuFL1iPaIxJK/k3gTxStIkc6+wSiDwlLdnCWNZyapsVLobu/0onvGOZolASZpfBFiDJYrOIGiDzgLIULTW61Vg== sha1-ACjKMFA7S6uRFXSDFfH4aT+4B4Y=. (1194 bytes)')
t.equal(err['code'], 'EINTEGRITY')
t.equal(err.message, 'Got unexpected checksum for "http://example.com/foo.tgz". Wanted "sha1-HssnaJydJVE+rbyZFKc/VAi+enY=". ' +
'Got "sha512-VuFL1iPaIxJK/k3gTxStIkc6+wSiDwlLdnCWNZyapsVLobu/0onvGOZolASZpfBFiDJYrOIGiDzgLIULTW61Vg== sha1-ACjKMFA7S6uRFXSDFfH4aT+4B4Y=".')
t.equal(err['code'], 'ERR_PNPM_TARBALL_INTEGRITY')
t.equal(err['resource'], 'http://example.com/foo.tgz')
t.equal(err['attempts'], 2)