@@ -177,6 +177,10 @@ class Integrity {
177
177
return this . toString ( )
178
178
}
179
179
180
+ isEmpty ( ) {
181
+ return Object . keys ( this ) . length === 0
182
+ }
183
+
180
184
toString ( opts ) {
181
185
opts = ssriOpts ( opts )
182
186
let sep = opts . sep || ' '
@@ -240,11 +244,6 @@ class Integrity {
240
244
opts = ssriOpts ( opts )
241
245
const pickAlgorithm = opts . pickAlgorithm
242
246
const keys = Object . keys ( this )
243
- if ( ! keys . length ) {
244
- throw new Error ( `No algorithms available for ${
245
- JSON . stringify ( this . toString ( ) )
246
- } `)
247
- }
248
247
return keys . reduce ( ( acc , algo ) => {
249
248
return pickAlgorithm ( acc , algo ) || acc
250
249
} )
@@ -253,6 +252,7 @@ class Integrity {
253
252
254
253
module . exports . parse = parse
255
254
function parse ( sri , opts ) {
255
+ if ( ! sri ) return null
256
256
opts = ssriOpts ( opts )
257
257
if ( typeof sri === 'string' ) {
258
258
return _parse ( sri , opts )
@@ -271,7 +271,7 @@ function _parse (integrity, opts) {
271
271
if ( opts . single ) {
272
272
return new Hash ( integrity , opts )
273
273
}
274
- return integrity . trim ( ) . split ( / \s + / ) . reduce ( ( acc , string ) => {
274
+ const hashes = integrity . trim ( ) . split ( / \s + / ) . reduce ( ( acc , string ) => {
275
275
const hash = new Hash ( string , opts )
276
276
if ( hash . algorithm && hash . digest ) {
277
277
const algo = hash . algorithm
@@ -280,6 +280,7 @@ function _parse (integrity, opts) {
280
280
}
281
281
return acc
282
282
} , new Integrity ( ) )
283
+ return hashes . isEmpty ( ) ? null : hashes
283
284
}
284
285
285
286
module . exports . stringify = stringify
@@ -347,7 +348,7 @@ module.exports.checkData = checkData
347
348
function checkData ( data , sri , opts ) {
348
349
opts = ssriOpts ( opts )
349
350
sri = parse ( sri , opts )
350
- if ( ! Object . keys ( sri ) . length ) {
351
+ if ( ! sri || ! Object . keys ( sri ) . length ) {
351
352
if ( opts . error ) {
352
353
throw Object . assign (
353
354
new Error ( 'No valid integrity hashes to check against' ) , {
@@ -386,6 +387,14 @@ module.exports.checkStream = checkStream
386
387
function checkStream ( stream , sri , opts ) {
387
388
opts = ssriOpts ( opts )
388
389
opts . integrity = sri
390
+ sri = parse ( sri , opts )
391
+ if ( ! sri || ! Object . keys ( sri ) . length ) {
392
+ return Promise . reject ( Object . assign (
393
+ new Error ( 'No valid integrity hashes to check against' ) , {
394
+ code : 'EINTEGRITY'
395
+ }
396
+ ) )
397
+ }
389
398
const checker = integrityStream ( opts )
390
399
return new Promise ( ( resolve , reject ) => {
391
400
stream . pipe ( checker )
0 commit comments