Skip to content

Commit 478f5cb

Browse files
billatnpmisaacs
authored andcommitted
feat(promise): replaced .reduce and .mapSeries
1 parent 74b939e commit 478f5cb

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

lib/verify.js

+34-27
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,32 @@ module.exports = verify
3232
function verify (cache, opts) {
3333
opts = VerifyOpts(opts)
3434
opts.log.silly('verify', 'verifying cache at', cache)
35-
return BB.reduce([
35+
36+
const steps = [
3637
markStartTime,
3738
fixPerms,
3839
garbageCollect,
3940
rebuildIndex,
4041
cleanTmp,
4142
writeVerifile,
4243
markEndTime
43-
], (stats, step, i) => {
44+
]
45+
46+
return steps.reduce((promise, step, i) => {
4447
const label = step.name || `step #${i}`
4548
const start = new Date()
46-
return BB.resolve(step(cache, opts)).then((s) => {
47-
s && Object.keys(s).forEach(k => {
48-
stats[k] = s[k]
49+
return promise.then((stats) => {
50+
return step(cache, opts).then((s) => {
51+
s && Object.keys(s).forEach(k => {
52+
stats[k] = s[k]
53+
})
54+
const end = new Date()
55+
if (!stats.runTime) { stats.runTime = {} }
56+
stats.runTime[label] = end - start
57+
return BB.resolve(stats)
4958
})
50-
const end = new Date()
51-
if (!stats.runTime) { stats.runTime = {} }
52-
stats.runTime[label] = end - start
53-
return stats
5459
})
55-
}, {})
60+
}, BB.resolve({}))
5661
.then((stats) => {
5762
stats.runTime.total = stats.endTime - stats.startTime
5863
opts.log.silly('verify', 'verification finished for', cache, 'in', `${stats.runTime.total}ms`)
@@ -61,11 +66,11 @@ function verify (cache, opts) {
6166
}
6267

6368
function markStartTime (cache, opts) {
64-
return { startTime: new Date() }
69+
return BB.resolve({ startTime: new Date() })
6570
}
6671

6772
function markEndTime (cache, opts) {
68-
return { endTime: new Date() }
73+
return BB.resolve({ endTime: new Date() })
6974
}
7075

7176
function fixPerms (cache, opts) {
@@ -200,22 +205,24 @@ function rebuildBucket (cache, bucket, stats, opts) {
200205
return truncate(bucket._path).then(() => {
201206
// This needs to be serialized because cacache explicitly
202207
// lets very racy bucket conflicts clobber each other.
203-
return BB.mapSeries(bucket, entry => {
204-
const content = contentPath(cache, entry.integrity)
205-
return stat(content).then(() => {
206-
return index.insert(cache, entry.key, entry.integrity, {
207-
metadata: entry.metadata,
208-
size: entry.size
209-
}).then(() => { stats.totalEntries++ })
210-
}).catch((err) => {
211-
if (err.code === 'ENOENT') {
212-
stats.rejectedEntries++
213-
stats.missingContent++
214-
return
215-
}
216-
throw err
208+
return bucket.reduce((promise, entry) => {
209+
return promise.then(() => {
210+
const content = contentPath(cache, entry.integrity)
211+
return stat(content).then(() => {
212+
return index.insert(cache, entry.key, entry.integrity, {
213+
metadata: entry.metadata,
214+
size: entry.size
215+
}).then(() => { stats.totalEntries++ })
216+
}).catch((err) => {
217+
if (err.code === 'ENOENT') {
218+
stats.rejectedEntries++
219+
stats.missingContent++
220+
return
221+
}
222+
throw err
223+
})
217224
})
218-
})
225+
}, BB.resolve())
219226
})
220227
}
221228

0 commit comments

Comments
 (0)