@@ -29,19 +29,39 @@ class IntegrityStream extends MiniPass {
29
29
this . #getOptions( )
30
30
31
31
// options used for calculating stream. can't be changed.
32
- const algorithms = opts ?. algorithms || DEFAULT_ALGORITHMS
33
- this . algorithms = Array . from (
34
- new Set ( algorithms . concat ( this . algorithm ? [ this . algorithm ] : [ ] ) )
35
- )
32
+ if ( opts ?. algorithms ) {
33
+ this . algorithms = Array . from (
34
+ new Set ( opts . algorithms . concat ( this . algorithm ? [ this . algorithm ] : [ ] ) )
35
+ )
36
+ } else {
37
+ this . algorithms = DEFAULT_ALGORITHMS
38
+
39
+ if ( this . algorithm !== null && this . algorithm !== DEFAULT_ALGORITHMS [ 0 ] ) {
40
+ this . algorithms . push ( this . algorithm )
41
+ }
42
+ }
43
+
36
44
this . hashes = this . algorithms . map ( crypto . createHash )
37
45
}
38
46
39
47
#getOptions ( ) {
40
48
// For verification
41
49
this . sri = this . opts ?. integrity ? parse ( this . opts ?. integrity , this . opts ) : null
42
50
this . expectedSize = this . opts ?. size
43
- this . goodSri = this . sri ? ! ! Object . keys ( this . sri ) . length : false
44
- this . algorithm = this . goodSri ? this . sri . pickAlgorithm ( this . opts ) : null
51
+
52
+ if ( ! this . sri ) {
53
+ this . algorithm = null
54
+ } else if ( this . sri . isIntegrity ) {
55
+ this . goodSri = ! this . sri . isEmpty ( )
56
+
57
+ if ( this . goodSri ) {
58
+ this . algorithm = this . sri . pickAlgorithm ( this . opts )
59
+ }
60
+ } else if ( this . sri . isHash ) {
61
+ this . goodSri = true
62
+ this . algorithm = this . sri . algorithm
63
+ }
64
+
45
65
this . digests = this . goodSri ? this . sri [ this . algorithm ] : null
46
66
this . optString = getOptString ( this . opts ?. options )
47
67
}
@@ -159,6 +179,24 @@ class Hash {
159
179
return this . toString ( )
160
180
}
161
181
182
+ match ( integrity , opts ) {
183
+ const other = parse ( integrity , opts )
184
+ if ( ! other ) {
185
+ return false
186
+ }
187
+ if ( other instanceof Integrity ) {
188
+ const algo = other . pickAlgorithm ( opts )
189
+ const foundHash = other [ algo ] . find ( hash => hash . digest === this . digest )
190
+
191
+ if ( foundHash ) {
192
+ return foundHash
193
+ }
194
+
195
+ return false
196
+ }
197
+ return other . digest === this . digest ? other : false
198
+ }
199
+
162
200
toString ( opts ) {
163
201
if ( opts ?. strict ) {
164
202
// Strict mode enforces the standard as close to the foot of the
@@ -399,7 +437,7 @@ function fromStream (stream, opts) {
399
437
sri = s
400
438
} )
401
439
istream . on ( 'end' , ( ) => resolve ( sri ) )
402
- istream . on ( 'data' , ( ) => { } )
440
+ istream . resume ( )
403
441
} )
404
442
}
405
443
@@ -466,7 +504,7 @@ function checkStream (stream, sri, opts) {
466
504
verified = s
467
505
} )
468
506
checker . on ( 'end' , ( ) => resolve ( verified ) )
469
- checker . on ( 'data' , ( ) => { } )
507
+ checker . resume ( )
470
508
} )
471
509
}
472
510
0 commit comments