@@ -22,8 +22,6 @@ const defaultOpts = {
22
22
strict : false ,
23
23
}
24
24
25
- const ssriOpts = ( opts = { } ) => ( { ...defaultOpts , ...opts } )
26
-
27
25
const getOptString = options => ! options || ! options . length
28
26
? ''
29
27
: `?${ options . join ( '?' ) } `
@@ -44,27 +42,21 @@ class IntegrityStream extends MiniPass {
44
42
this [ _getOptions ] ( )
45
43
46
44
// options used for calculating stream. can't be changed.
47
- const { algorithms = defaultOpts . algorithms } = opts
45
+ const algorithms = opts ? .algorithms || defaultOpts . algorithms
48
46
this . algorithms = Array . from (
49
47
new Set ( algorithms . concat ( this . algorithm ? [ this . algorithm ] : [ ] ) )
50
48
)
51
49
this . hashes = this . algorithms . map ( crypto . createHash )
52
50
}
53
51
54
52
[ _getOptions ] ( ) {
55
- const {
56
- integrity,
57
- size,
58
- options,
59
- } = { ...defaultOpts , ...this . opts }
60
-
61
53
// For verification
62
- this . sri = integrity ? parse ( integrity , this . opts ) : null
63
- this . expectedSize = size
54
+ this . sri = this . opts ?. integrity ? parse ( this . opts ?. integrity , this . opts ) : null
55
+ this . expectedSize = this . opts ?. size
64
56
this . goodSri = this . sri ? ! ! Object . keys ( this . sri ) . length : false
65
57
this . algorithm = this . goodSri ? this . sri . pickAlgorithm ( this . opts ) : null
66
58
this . digests = this . goodSri ? this . sri [ this . algorithm ] : null
67
- this . optString = getOptString ( options )
59
+ this . optString = getOptString ( this . opts ?. options )
68
60
}
69
61
70
62
on ( ev , handler ) {
@@ -141,8 +133,7 @@ class Hash {
141
133
}
142
134
143
135
constructor ( hash , opts ) {
144
- opts = ssriOpts ( opts )
145
- const strict = ! ! opts . strict
136
+ const strict = opts ?. strict
146
137
this . source = hash . trim ( )
147
138
148
139
// set default values so that we make V8 happy to
@@ -161,7 +152,7 @@ class Hash {
161
152
if ( ! match ) {
162
153
return
163
154
}
164
- if ( strict && ! SPEC_ALGORITHMS . some ( a => a === match [ 1 ] ) ) {
155
+ if ( strict && ! SPEC_ALGORITHMS . includes ( match [ 1 ] ) ) {
165
156
return
166
157
}
167
158
this . algorithm = match [ 1 ]
@@ -182,14 +173,13 @@ class Hash {
182
173
}
183
174
184
175
toString ( opts ) {
185
- opts = ssriOpts ( opts )
186
- if ( opts . strict ) {
176
+ if ( opts ?. strict ) {
187
177
// Strict mode enforces the standard as close to the foot of the
188
178
// letter as it can.
189
179
if ( ! (
190
180
// The spec has very restricted productions for algorithms.
191
181
// https://www.w3.org/TR/CSP2/#source-list-syntax
192
- SPEC_ALGORITHMS . some ( x => x === this . algorithm ) &&
182
+ SPEC_ALGORITHMS . includes ( this . algorithm ) &&
193
183
// Usually, if someone insists on using a "different" base64, we
194
184
// leave it as-is, since there's multiple standards, and the
195
185
// specified is not a URL-safe variant.
@@ -224,9 +214,8 @@ class Integrity {
224
214
}
225
215
226
216
toString ( opts ) {
227
- opts = ssriOpts ( opts )
228
- let sep = opts . sep || ' '
229
- if ( opts . strict ) {
217
+ let sep = opts ?. sep || ' '
218
+ if ( opts ?. strict ) {
230
219
// Entries must be separated by whitespace, according to spec.
231
220
sep = sep . replace ( / \S + / g, ' ' )
232
221
}
@@ -238,7 +227,6 @@ class Integrity {
238
227
}
239
228
240
229
concat ( integrity , opts ) {
241
- opts = ssriOpts ( opts )
242
230
const other = typeof integrity === 'string'
243
231
? integrity
244
232
: stringify ( integrity , opts )
@@ -252,7 +240,6 @@ class Integrity {
252
240
// add additional hashes to an integrity value, but prevent
253
241
// *changing* an existing integrity hash.
254
242
merge ( integrity , opts ) {
255
- opts = ssriOpts ( opts )
256
243
const other = parse ( integrity , opts )
257
244
for ( const algo in other ) {
258
245
if ( this [ algo ] ) {
@@ -268,7 +255,6 @@ class Integrity {
268
255
}
269
256
270
257
match ( integrity , opts ) {
271
- opts = ssriOpts ( opts )
272
258
const other = parse ( integrity , opts )
273
259
if ( ! other ) {
274
260
return false
@@ -286,8 +272,7 @@ class Integrity {
286
272
}
287
273
288
274
pickAlgorithm ( opts ) {
289
- opts = ssriOpts ( opts )
290
- const pickAlgorithm = opts . pickAlgorithm
275
+ const pickAlgorithm = opts ?. pickAlgorithm || defaultOpts . pickAlgorithm ;
291
276
const keys = Object . keys ( this )
292
277
return keys . reduce ( ( acc , algo ) => {
293
278
return pickAlgorithm ( acc , algo ) || acc
@@ -300,7 +285,6 @@ function parse (sri, opts) {
300
285
if ( ! sri ) {
301
286
return null
302
287
}
303
- opts = ssriOpts ( opts )
304
288
if ( typeof sri === 'string' ) {
305
289
return _parse ( sri , opts )
306
290
} else if ( sri . algorithm && sri . digest ) {
@@ -315,7 +299,7 @@ function parse (sri, opts) {
315
299
function _parse ( integrity , opts ) {
316
300
// 3.4.3. Parse metadata
317
301
// https://w3c.github.io/webappsec-subresource-integrity/#parse-metadata
318
- if ( opts . single ) {
302
+ if ( opts ? .single ) {
319
303
return new Hash ( integrity , opts )
320
304
}
321
305
const hashes = integrity . trim ( ) . split ( / \s + / ) . reduce ( ( acc , string ) => {
@@ -334,7 +318,6 @@ function _parse (integrity, opts) {
334
318
335
319
module . exports . stringify = stringify
336
320
function stringify ( obj , opts ) {
337
- opts = ssriOpts ( opts )
338
321
if ( obj . algorithm && obj . digest ) {
339
322
return Hash . prototype . toString . call ( obj , opts )
340
323
} else if ( typeof obj === 'string' ) {
@@ -346,8 +329,7 @@ function stringify (obj, opts) {
346
329
347
330
module . exports . fromHex = fromHex
348
331
function fromHex ( hexDigest , algorithm , opts ) {
349
- opts = ssriOpts ( opts )
350
- const optString = getOptString ( opts . options )
332
+ const optString = getOptString ( opts ?. options )
351
333
return parse (
352
334
`${ algorithm } -${
353
335
Buffer . from ( hexDigest , 'hex' ) . toString ( 'base64' )
@@ -357,9 +339,8 @@ function fromHex (hexDigest, algorithm, opts) {
357
339
358
340
module . exports . fromData = fromData
359
341
function fromData ( data , opts ) {
360
- opts = ssriOpts ( opts )
361
- const algorithms = opts . algorithms
362
- const optString = getOptString ( opts . options )
342
+ const algorithms = opts ?. algorithms || defaultOpts . algorithms
343
+ const optString = getOptString ( opts ?. options )
363
344
return algorithms . reduce ( ( acc , algo ) => {
364
345
const digest = crypto . createHash ( algo ) . update ( data ) . digest ( 'base64' )
365
346
const hash = new Hash (
@@ -382,7 +363,6 @@ function fromData (data, opts) {
382
363
383
364
module . exports . fromStream = fromStream
384
365
function fromStream ( stream , opts ) {
385
- opts = ssriOpts ( opts )
386
366
const istream = integrityStream ( opts )
387
367
return new Promise ( ( resolve , reject ) => {
388
368
stream . pipe ( istream )
@@ -399,10 +379,9 @@ function fromStream (stream, opts) {
399
379
400
380
module . exports . checkData = checkData
401
381
function checkData ( data , sri , opts ) {
402
- opts = ssriOpts ( opts )
403
382
sri = parse ( sri , opts )
404
383
if ( ! sri || ! Object . keys ( sri ) . length ) {
405
- if ( opts . error ) {
384
+ if ( opts ? .error ) {
406
385
throw Object . assign (
407
386
new Error ( 'No valid integrity hashes to check against' ) , {
408
387
code : 'EINTEGRITY' ,
@@ -416,7 +395,8 @@ function checkData (data, sri, opts) {
416
395
const digest = crypto . createHash ( algorithm ) . update ( data ) . digest ( 'base64' )
417
396
const newSri = parse ( { algorithm, digest } )
418
397
const match = newSri . match ( sri , opts )
419
- if ( match || ! opts . error ) {
398
+ opts = opts || Object . create ( null )
399
+ if ( match || ! ( opts . error ) ) {
420
400
return match
421
401
} else if ( typeof opts . size === 'number' && ( data . length !== opts . size ) ) {
422
402
/* eslint-disable-next-line max-len */
@@ -440,7 +420,7 @@ function checkData (data, sri, opts) {
440
420
441
421
module . exports . checkStream = checkStream
442
422
function checkStream ( stream , sri , opts ) {
443
- opts = ssriOpts ( opts )
423
+ opts = opts || Object . create ( null )
444
424
opts . integrity = sri
445
425
sri = parse ( sri , opts )
446
426
if ( ! sri || ! Object . keys ( sri ) . length ) {
@@ -465,15 +445,14 @@ function checkStream (stream, sri, opts) {
465
445
}
466
446
467
447
module . exports . integrityStream = integrityStream
468
- function integrityStream ( opts = { } ) {
448
+ function integrityStream ( opts = Object . create ( null ) ) {
469
449
return new IntegrityStream ( opts )
470
450
}
471
451
472
452
module . exports . create = createIntegrity
473
453
function createIntegrity ( opts ) {
474
- opts = ssriOpts ( opts )
475
- const algorithms = opts . algorithms
476
- const optString = getOptString ( opts . options )
454
+ const algorithms = opts ?. algorithms || defaultOpts . algorithms
455
+ const optString = getOptString ( opts ?. options )
477
456
478
457
const hashes = algorithms . map ( crypto . createHash )
479
458
0 commit comments