diff --git a/README.md b/README.md index 20e37fb..4649b4a 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,10 @@ will be an `Integrity` instance that has this shape: } ``` +If `opts.single` is truthy, a single `IntegrityMetadata` object will be +returned. That is, a single object that looks like `{algorithm, digest, +options}`, as opposed to a larger object with multiple of these. + If `opts.strict` is truthy, the resulting object will be filtered such that it strictly follows the Subresource Integrity spec, throwing away any entries with any invalid components. This also means a restricted set of algorithms diff --git a/index.js b/index.js index 5f0786b..d5b63cd 100644 --- a/index.js +++ b/index.js @@ -102,6 +102,9 @@ function parse (sri, opts) { function _parse (integrity, opts) { // 3.4.3. Parse metadata // https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata + if (opts.single) { + return new IntegrityMetadata(integrity, opts) + } return integrity.trim().split(/\s+/).reduce((acc, string) => { const metadata = new IntegrityMetadata(string, opts) if (metadata.algorithm && metadata.digest) { diff --git a/test/parse.js b/test/parse.js index f48a166..67e953f 100644 --- a/test/parse.js +++ b/test/parse.js @@ -12,7 +12,7 @@ function hash (data, algorithm) { return crypto.createHash(algorithm).update(data).digest('base64') } -test('parses single-entry inegrity string', t => { +test('parses single-entry integrity string', t => { const sha = hash(TEST_DATA, 'sha512') const integrity = `sha512-${sha}` t.deepEqual(ssri.parse(integrity), { @@ -26,6 +26,18 @@ test('parses single-entry inegrity string', t => { t.done() }) +test('can parse single-entry string directly into IntegrityMetadata', t => { + const sha = hash(TEST_DATA, 'sha512') + const integrity = `sha512-${sha}` + t.deepEqual(ssri.parse(integrity, {single: true}), { + source: integrity, + digest: sha, + algorithm: 'sha512', + options: [] + }, 'single entry parsed into single IntegrityMetadata instance') + t.done() +}) + test('accepts IntegrityMetadata-likes as input', t => { const algorithm = 'sha512' const digest = hash(TEST_DATA, 'sha512')