Skip to content

Commit

Permalink
feat(promise): replaced .reduce and .mapSeries
Browse files Browse the repository at this point in the history
  • Loading branch information
billatnpm authored and isaacs committed Sep 15, 2019
1 parent 74b939e commit 478f5cb
Showing 1 changed file with 34 additions and 27 deletions.
61 changes: 34 additions & 27 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,32 @@ module.exports = verify
function verify (cache, opts) {
opts = VerifyOpts(opts)
opts.log.silly('verify', 'verifying cache at', cache)
return BB.reduce([

const steps = [
markStartTime,
fixPerms,
garbageCollect,
rebuildIndex,
cleanTmp,
writeVerifile,
markEndTime
], (stats, step, i) => {
]

return steps.reduce((promise, step, i) => {
const label = step.name || `step #${i}`
const start = new Date()
return BB.resolve(step(cache, opts)).then((s) => {
s && Object.keys(s).forEach(k => {
stats[k] = s[k]
return promise.then((stats) => {
return step(cache, opts).then((s) => {
s && Object.keys(s).forEach(k => {
stats[k] = s[k]
})
const end = new Date()
if (!stats.runTime) { stats.runTime = {} }
stats.runTime[label] = end - start
return BB.resolve(stats)
})
const end = new Date()
if (!stats.runTime) { stats.runTime = {} }
stats.runTime[label] = end - start
return stats
})
}, {})
}, BB.resolve({}))
.then((stats) => {
stats.runTime.total = stats.endTime - stats.startTime
opts.log.silly('verify', 'verification finished for', cache, 'in', `${stats.runTime.total}ms`)
Expand All @@ -61,11 +66,11 @@ function verify (cache, opts) {
}

function markStartTime (cache, opts) {
return { startTime: new Date() }
return BB.resolve({ startTime: new Date() })
}

function markEndTime (cache, opts) {
return { endTime: new Date() }
return BB.resolve({ endTime: new Date() })
}

function fixPerms (cache, opts) {
Expand Down Expand Up @@ -200,22 +205,24 @@ function rebuildBucket (cache, bucket, stats, opts) {
return truncate(bucket._path).then(() => {
// This needs to be serialized because cacache explicitly
// lets very racy bucket conflicts clobber each other.
return BB.mapSeries(bucket, entry => {
const content = contentPath(cache, entry.integrity)
return stat(content).then(() => {
return index.insert(cache, entry.key, entry.integrity, {
metadata: entry.metadata,
size: entry.size
}).then(() => { stats.totalEntries++ })
}).catch((err) => {
if (err.code === 'ENOENT') {
stats.rejectedEntries++
stats.missingContent++
return
}
throw err
return bucket.reduce((promise, entry) => {
return promise.then(() => {
const content = contentPath(cache, entry.integrity)
return stat(content).then(() => {
return index.insert(cache, entry.key, entry.integrity, {
metadata: entry.metadata,
size: entry.size
}).then(() => { stats.totalEntries++ })
}).catch((err) => {
if (err.code === 'ENOENT') {
stats.rejectedEntries++
stats.missingContent++
return
}
throw err
})
})
})
}, BB.resolve())
})
}

Expand Down

0 comments on commit 478f5cb

Please sign in to comment.