@@ -33,6 +33,21 @@ function read (cache, integrity, opts) {
33
33
} )
34
34
}
35
35
36
+ module . exports . sync = readSync
37
+ function readSync ( cache , integrity , opts ) {
38
+ opts = ReadOpts ( opts )
39
+ return withContentSriSync ( cache , integrity , ( cpath , sri ) => {
40
+ const data = fs . readFileSync ( cpath )
41
+ if ( typeof opts . size === 'number' && opts . size !== data . length ) {
42
+ throw sizeError ( opts . size , data . length )
43
+ } else if ( ssri . checkData ( data , sri ) ) {
44
+ return data
45
+ } else {
46
+ throw integrityError ( sri , cpath )
47
+ }
48
+ } )
49
+ }
50
+
36
51
module . exports . stream = readStream
37
52
module . exports . readStream = readStream
38
53
function readStream ( cache , integrity , opts ) {
@@ -112,6 +127,28 @@ function withContentSri (cache, integrity, fn) {
112
127
} )
113
128
}
114
129
130
+ function withContentSriSync ( cache , integrity , fn ) {
131
+ const sri = ssri . parse ( integrity )
132
+ // If `integrity` has multiple entries, pick the first digest
133
+ // with available local data.
134
+ const algo = sri . pickAlgorithm ( )
135
+ const digests = sri [ algo ]
136
+ if ( digests . length <= 1 ) {
137
+ const cpath = contentPath ( cache , digests [ 0 ] )
138
+ return fn ( cpath , digests [ 0 ] )
139
+ } else {
140
+ let lastErr = null
141
+ for ( const meta of sri [ sri . pickAlgorithm ( ) ] ) {
142
+ try {
143
+ return withContentSriSync ( cache , meta , fn )
144
+ } catch ( err ) {
145
+ lastErr = err
146
+ }
147
+ }
148
+ if ( lastErr ) { throw lastErr }
149
+ }
150
+ }
151
+
115
152
function sizeError ( expected , found ) {
116
153
var err = new Error ( Y `Bad data size: expected inserted data to be ${ expected } bytes, but got ${ found } instead` )
117
154
err . expected = expected
0 commit comments