Skip to content

Commit

Permalink
fix(pickAlgorithm): error if pickAlgorithm() is used in an empty Inte…
Browse files Browse the repository at this point in the history
…grity
  • Loading branch information
zkat committed Apr 12, 2017
1 parent 99e5e1f commit fab470e
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand Down
3 changes: 3 additions & 0 deletions test/integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down

0 comments on commit fab470e

Please sign in to comment.