33const BB = require ( 'bluebird' )
44
55const contentPath = require ( './content/path' )
6+ const figgyPudding = require ( 'figgy-pudding' )
67const finished = BB . promisify ( require ( 'mississippi' ) . finished )
78const fixOwner = require ( './util/fix-owner' )
89const fs = require ( 'graceful-fs' )
@@ -14,10 +15,22 @@ const ssri = require('ssri')
1415
1516BB . promisifyAll ( fs )
1617
18+ const VerifyOpts = figgyPudding ( {
19+ concurrency : {
20+ default : 20
21+ } ,
22+ filter : { } ,
23+ log : {
24+ default : { silly ( ) { } }
25+ } ,
26+ uid : { } ,
27+ gid : { }
28+ } )
29+
1730module . exports = verify
1831function verify ( cache , opts ) {
19- opts = opts || { }
20- opts . log && opts . log . silly ( 'verify' , 'verifying cache at' , cache )
32+ opts = VerifyOpts ( opts )
33+ opts . log . silly ( 'verify' , 'verifying cache at' , cache )
2134 return BB . reduce ( [
2235 markStartTime ,
2336 fixPerms ,
@@ -40,7 +53,7 @@ function verify (cache, opts) {
4053 } )
4154 } , { } ) . tap ( stats => {
4255 stats . runTime . total = stats . endTime - stats . startTime
43- opts . log && opts . log . silly ( 'verify' , 'verification finished for' , cache , 'in' , `${ stats . runTime . total } ms` )
56+ opts . log . silly ( 'verify' , 'verification finished for' , cache , 'in' , `${ stats . runTime . total } ms` )
4457 } )
4558}
4659
@@ -53,7 +66,7 @@ function markEndTime (cache, opts) {
5366}
5467
5568function fixPerms ( cache , opts ) {
56- opts . log && opts . log . silly ( 'verify' , 'fixing cache permissions' )
69+ opts . log . silly ( 'verify' , 'fixing cache permissions' )
5770 return fixOwner . mkdirfix ( cache , opts . uid , opts . gid ) . then ( ( ) => {
5871 // TODO - fix file permissions too
5972 return fixOwner . chownr ( cache , opts . uid , opts . gid )
@@ -70,11 +83,11 @@ function fixPerms (cache, opts) {
7083// 5. If content is not marked as live, rimraf it.
7184//
7285function garbageCollect ( cache , opts ) {
73- opts . log && opts . log . silly ( 'verify' , 'garbage collecting content' )
86+ opts . log . silly ( 'verify' , 'garbage collecting content' )
7487 const indexStream = index . lsStream ( cache )
7588 const liveContent = new Set ( )
7689 indexStream . on ( 'data' , entry => {
77- if ( opts && opts . filter && ! opts . filter ( entry ) ) { return }
90+ if ( opts . filter && ! opts . filter ( entry ) ) { return }
7891 liveContent . add ( entry . integrity . toString ( ) )
7992 } )
8093 return finished ( indexStream ) . then ( ( ) => {
@@ -117,7 +130,7 @@ function garbageCollect (cache, opts) {
117130 } )
118131 } )
119132 }
120- } , { concurrency : opts . concurrency || 20 } ) )
133+ } , { concurrency : opts . concurrency } ) )
121134 } )
122135 } )
123136}
@@ -141,7 +154,7 @@ function verifyContent (filepath, sri) {
141154}
142155
143156function rebuildIndex ( cache , opts ) {
144- opts . log && opts . log . silly ( 'verify' , 'rebuilding index' )
157+ opts . log . silly ( 'verify' , 'rebuilding index' )
145158 return index . ls ( cache ) . then ( entries => {
146159 const stats = {
147160 missingContent : 0 ,
@@ -153,7 +166,7 @@ function rebuildIndex (cache, opts) {
153166 if ( entries . hasOwnProperty ( k ) ) {
154167 const hashed = index . _hashKey ( k )
155168 const entry = entries [ k ]
156- const excluded = opts && opts . filter && ! opts . filter ( entry )
169+ const excluded = opts . filter && ! opts . filter ( entry )
157170 excluded && stats . rejectedEntries ++
158171 if ( buckets [ hashed ] && ! excluded ) {
159172 buckets [ hashed ] . push ( entry )
@@ -170,7 +183,7 @@ function rebuildIndex (cache, opts) {
170183 }
171184 return BB . map ( Object . keys ( buckets ) , key => {
172185 return rebuildBucket ( cache , buckets [ key ] , stats , opts )
173- } , { concurrency : opts . concurrency || 20 } ) . then ( ( ) => stats )
186+ } , { concurrency : opts . concurrency } ) . then ( ( ) => stats )
174187 } )
175188}
176189
@@ -195,13 +208,13 @@ function rebuildBucket (cache, bucket, stats, opts) {
195208}
196209
197210function cleanTmp ( cache , opts ) {
198- opts . log && opts . log . silly ( 'verify' , 'cleaning tmp directory' )
211+ opts . log . silly ( 'verify' , 'cleaning tmp directory' )
199212 return rimraf ( path . join ( cache , 'tmp' ) )
200213}
201214
202215function writeVerifile ( cache , opts ) {
203216 const verifile = path . join ( cache , '_lastverified' )
204- opts . log && opts . log . silly ( 'verify' , 'writing verifile to ' + verifile )
217+ opts . log . silly ( 'verify' , 'writing verifile to ' + verifile )
205218 return fs . writeFileAsync ( verifile , '' + ( + ( new Date ( ) ) ) )
206219}
207220
0 commit comments