1
1
'use strict'
2
2
3
3
const crypto = require ( 'crypto' )
4
- const figgyPudding = require ( 'figgy-pudding' )
5
4
const MiniPass = require ( 'minipass' )
6
5
7
6
const SPEC_ALGORITHMS = [ 'sha256' , 'sha384' , 'sha512' ]
@@ -11,17 +10,17 @@ const SRI_REGEX = /^([^-]+)-([^?]+)([?\S*]*)$/
11
10
const STRICT_SRI_REGEX = / ^ ( [ ^ - ] + ) - ( [ A - Z a - z 0 - 9 + / = ] { 44 , 88 } ) ( \? [ \x21 - \x7E ] * ) * $ /
12
11
const VCHAR_REGEX = / ^ [ \x21 - \x7E ] + $ /
13
12
14
- const SsriOpts = figgyPudding ( {
15
- algorithms : { default : [ 'sha512' ] } ,
16
- error : { default : false } ,
17
- integrity : { } ,
18
- options : { default : [ ] } ,
19
- pickAlgorithm : { default : ( ) => getPrioritizedHash } ,
20
- sep : { default : ' ' } ,
21
- single : { default : false } ,
22
- size : { } ,
23
- strict : { default : false }
24
- } )
13
+ const defaultOpts = {
14
+ algorithms : [ 'sha512' ] ,
15
+ error : false ,
16
+ options : [ ] ,
17
+ pickAlgorithm : getPrioritizedHash ,
18
+ sep : ' ' ,
19
+ single : false ,
20
+ strict : false
21
+ }
22
+
23
+ const ssriOpts = ( opts = { } ) => ( { ... defaultOpts , ... opts } )
25
24
26
25
const getOptString = options => ! options || ! options . length ? ''
27
26
: `?${ options . join ( '?' ) } `
@@ -101,7 +100,7 @@ class IntegrityStream extends MiniPass {
101
100
class Hash {
102
101
get isHash ( ) { return true }
103
102
constructor ( hash , opts ) {
104
- opts = SsriOpts ( opts )
103
+ opts = ssriOpts ( opts )
105
104
const strict = ! ! opts . strict
106
105
this . source = hash . trim ( )
107
106
@@ -138,7 +137,7 @@ class Hash {
138
137
}
139
138
140
139
toString ( opts ) {
141
- opts = SsriOpts ( opts )
140
+ opts = ssriOpts ( opts )
142
141
if ( opts . strict ) {
143
142
// Strict mode enforces the standard as close to the foot of the
144
143
// letter as it can.
@@ -173,7 +172,7 @@ class Integrity {
173
172
}
174
173
175
174
toString ( opts ) {
176
- opts = SsriOpts ( opts )
175
+ opts = ssriOpts ( opts )
177
176
let sep = opts . sep || ' '
178
177
if ( opts . strict ) {
179
178
// Entries must be separated by whitespace, according to spec.
@@ -187,7 +186,7 @@ class Integrity {
187
186
}
188
187
189
188
concat ( integrity , opts ) {
190
- opts = SsriOpts ( opts )
189
+ opts = ssriOpts ( opts )
191
190
const other = typeof integrity === 'string'
192
191
? integrity
193
192
: stringify ( integrity , opts )
@@ -201,7 +200,7 @@ class Integrity {
201
200
// add additional hashes to an integrity value, but prevent
202
201
// *changing* an existing integrity hash.
203
202
merge ( integrity , opts ) {
204
- opts = SsriOpts ( opts )
203
+ opts = ssriOpts ( opts )
205
204
const other = parse ( integrity , opts )
206
205
for ( const algo in other ) {
207
206
if ( this [ algo ] ) {
@@ -217,7 +216,7 @@ class Integrity {
217
216
}
218
217
219
218
match ( integrity , opts ) {
220
- opts = SsriOpts ( opts )
219
+ opts = ssriOpts ( opts )
221
220
const other = parse ( integrity , opts )
222
221
const algo = other . pickAlgorithm ( opts )
223
222
return (
@@ -232,7 +231,7 @@ class Integrity {
232
231
}
233
232
234
233
pickAlgorithm ( opts ) {
235
- opts = SsriOpts ( opts )
234
+ opts = ssriOpts ( opts )
236
235
const pickAlgorithm = opts . pickAlgorithm
237
236
const keys = Object . keys ( this )
238
237
if ( ! keys . length ) {
@@ -248,7 +247,7 @@ class Integrity {
248
247
249
248
module . exports . parse = parse
250
249
function parse ( sri , opts ) {
251
- opts = SsriOpts ( opts )
250
+ opts = ssriOpts ( opts )
252
251
if ( typeof sri === 'string' ) {
253
252
return _parse ( sri , opts )
254
253
} else if ( sri . algorithm && sri . digest ) {
@@ -279,7 +278,7 @@ function _parse (integrity, opts) {
279
278
280
279
module . exports . stringify = stringify
281
280
function stringify ( obj , opts ) {
282
- opts = SsriOpts ( opts )
281
+ opts = ssriOpts ( opts )
283
282
if ( obj . algorithm && obj . digest ) {
284
283
return Hash . prototype . toString . call ( obj , opts )
285
284
} else if ( typeof obj === 'string' ) {
@@ -291,7 +290,7 @@ function stringify (obj, opts) {
291
290
292
291
module . exports . fromHex = fromHex
293
292
function fromHex ( hexDigest , algorithm , opts ) {
294
- opts = SsriOpts ( opts )
293
+ opts = ssriOpts ( opts )
295
294
const optString = getOptString ( opts . options )
296
295
return parse (
297
296
`${ algorithm } -${
@@ -302,7 +301,7 @@ function fromHex (hexDigest, algorithm, opts) {
302
301
303
302
module . exports . fromData = fromData
304
303
function fromData ( data , opts ) {
305
- opts = SsriOpts ( opts )
304
+ opts = ssriOpts ( opts )
306
305
const algorithms = opts . algorithms
307
306
const optString = getOptString ( opts . options )
308
307
return algorithms . reduce ( ( acc , algo ) => {
@@ -325,7 +324,7 @@ function fromData (data, opts) {
325
324
326
325
module . exports . fromStream = fromStream
327
326
function fromStream ( stream , opts ) {
328
- opts = SsriOpts ( opts )
327
+ opts = ssriOpts ( opts )
329
328
const istream = integrityStream ( opts )
330
329
return new Promise ( ( resolve , reject ) => {
331
330
stream . pipe ( istream )
@@ -340,7 +339,7 @@ function fromStream (stream, opts) {
340
339
341
340
module . exports . checkData = checkData
342
341
function checkData ( data , sri , opts ) {
343
- opts = SsriOpts ( opts )
342
+ opts = ssriOpts ( opts )
344
343
sri = parse ( sri , opts )
345
344
if ( ! Object . keys ( sri ) . length ) {
346
345
if ( opts . error ) {
@@ -379,10 +378,9 @@ function checkData (data, sri, opts) {
379
378
380
379
module . exports . checkStream = checkStream
381
380
function checkStream ( stream , sri , opts ) {
382
- opts = SsriOpts ( opts )
383
- const checker = integrityStream ( opts . concat ( {
384
- integrity : sri
385
- } ) )
381
+ opts = ssriOpts ( opts )
382
+ opts . integrity = sri
383
+ const checker = integrityStream ( opts )
386
384
return new Promise ( ( resolve , reject ) => {
387
385
stream . pipe ( checker )
388
386
stream . on ( 'error' , reject )
@@ -396,12 +394,12 @@ function checkStream (stream, sri, opts) {
396
394
397
395
module . exports . integrityStream = integrityStream
398
396
function integrityStream ( opts ) {
399
- return new IntegrityStream ( SsriOpts ( opts ) )
397
+ return new IntegrityStream ( ssriOpts ( opts ) )
400
398
}
401
399
402
400
module . exports . create = createIntegrity
403
401
function createIntegrity ( opts ) {
404
- opts = SsriOpts ( opts )
402
+ opts = ssriOpts ( opts )
405
403
const algorithms = opts . algorithms
406
404
const optString = getOptString ( opts . options )
407
405
0 commit comments