Skip to content

Commit c8ddf48

Browse files
committed
feat(parse): allow parsing straight into a single IntegrityMetadata object
1 parent b97a796 commit c8ddf48

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ will be an `Integrity` instance that has this shape:
9292
}
9393
```
9494

95+
If `opts.single` is truthy, a single `IntegrityMetadata` object will be
96+
returned. That is, a single object that looks like `{algorithm, digest,
97+
options}`, as opposed to a larger object with multiple of these.
98+
9599
If `opts.strict` is truthy, the resulting object will be filtered such that
96100
it strictly follows the Subresource Integrity spec, throwing away any entries
97101
with any invalid components. This also means a restricted set of algorithms

Diff for: index.js

+3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ function parse (sri, opts) {
102102
function _parse (integrity, opts) {
103103
// 3.4.3. Parse metadata
104104
// https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata
105+
if (opts.single) {
106+
return new IntegrityMetadata(integrity, opts)
107+
}
105108
return integrity.trim().split(/\s+/).reduce((acc, string) => {
106109
const metadata = new IntegrityMetadata(string, opts)
107110
if (metadata.algorithm && metadata.digest) {

Diff for: test/parse.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function hash (data, algorithm) {
1212
return crypto.createHash(algorithm).update(data).digest('base64')
1313
}
1414

15-
test('parses single-entry inegrity string', t => {
15+
test('parses single-entry integrity string', t => {
1616
const sha = hash(TEST_DATA, 'sha512')
1717
const integrity = `sha512-${sha}`
1818
t.deepEqual(ssri.parse(integrity), {
@@ -26,6 +26,18 @@ test('parses single-entry inegrity string', t => {
2626
t.done()
2727
})
2828

29+
test('can parse single-entry string directly into IntegrityMetadata', t => {
30+
const sha = hash(TEST_DATA, 'sha512')
31+
const integrity = `sha512-${sha}`
32+
t.deepEqual(ssri.parse(integrity, {single: true}), {
33+
source: integrity,
34+
digest: sha,
35+
algorithm: 'sha512',
36+
options: []
37+
}, 'single entry parsed into single IntegrityMetadata instance')
38+
t.done()
39+
})
40+
2941
test('accepts IntegrityMetadata-likes as input', t => {
3042
const algorithm = 'sha512'
3143
const digest = hash(TEST_DATA, 'sha512')

0 commit comments

Comments
 (0)