diff --git a/index.js b/index.js index 8e88057..0358a8e 100644 --- a/index.js +++ b/index.js @@ -93,7 +93,11 @@ class Integrity { } pickAlgorithm (opts) { const pickAlgorithm = (opts && opts.pickAlgorithm) || getPrioritizedHash - return Object.keys(this).reduce((acc, algo) => { + const keys = Object.keys(this) + if (!keys.length) { + throw new Error(`No algorithms available for ${this}`) + } + return keys.reduce((acc, algo) => { return pickAlgorithm(acc, algo) || acc }) } diff --git a/test/integrity.js b/test/integrity.js index 526bbfa..9db0737 100644 --- a/test/integrity.js +++ b/test/integrity.js @@ -93,6 +93,9 @@ test('pickAlgorithm()', t => { 'sha384', 'custom pickAlgorithm function accepted' ) + t.throws(() => { + ssri.parse('').pickAlgorithm() + }, /No algorithms available/, 'SRIs without algorithms are invalid') t.done() })