Skip to content

Commit 74b939e

Browse files
billatnpmisaacs
authored andcommitted
feat(promise): individually promisifing functions as needed
1 parent 28aeeac commit 74b939e

23 files changed

+222
-195
lines changed

get.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
'use strict'
22

3+
const BB = require('bluebird')
4+
35
const figgyPudding = require('figgy-pudding')
46
const fs = require('fs')
7+
const { pipe, pipeline, through } = require('mississippi')
58
const index = require('./lib/entry-index')
69
const memo = require('./lib/memoization')
7-
const pipe = require('mississippi').pipe
8-
const pipeline = require('mississippi').pipeline
910
const read = require('./lib/content/read')
10-
const through = require('mississippi').through
11+
12+
const writeFile = BB.promisify(fs.writeFile)
1113

1214
const GetOpts = figgyPudding({
1315
integrity: {},
@@ -38,19 +40,19 @@ function getData (byDigest, cache, key, opts) {
3840
}
3941
return (
4042
byDigest ? Promise.resolve(null) : index.find(cache, key, opts)
41-
).then(entry => {
43+
).then((entry) => {
4244
if (!entry && !byDigest) {
4345
throw new index.NotFoundError(cache, key)
4446
}
4547
return read(cache, byDigest ? key : entry.integrity, {
4648
integrity: opts.integrity,
4749
size: opts.size
48-
}).then(data => byDigest ? data : {
50+
}).then((data) => byDigest ? data : {
4951
metadata: entry.metadata,
5052
data: data,
5153
size: entry.size,
5254
integrity: entry.integrity
53-
}).then(res => {
55+
}).then((res) => {
5456
if (opts.memoize && byDigest) {
5557
memo.put.byDigest(cache, key, res, opts)
5658
} else if (opts.memoize) {
@@ -124,7 +126,7 @@ function getStream (cache, key, opts) {
124126
stream.write(memoized.data, () => stream.end())
125127
return stream
126128
}
127-
index.find(cache, key).then(entry => {
129+
index.find(cache, key).then((entry) => {
128130
if (!entry) {
129131
return stream.emit(
130132
'error', new index.NotFoundError(cache, key)
@@ -220,7 +222,7 @@ function copy (byDigest, cache, key, dest, opts) {
220222
if (read.copy) {
221223
return (
222224
byDigest ? Promise.resolve(null) : index.find(cache, key, opts)
223-
).then(entry => {
225+
).then((entry) => {
224226
if (!entry && !byDigest) {
225227
throw new index.NotFoundError(cache, key)
226228
}
@@ -233,8 +235,8 @@ function copy (byDigest, cache, key, dest, opts) {
233235
})
234236
})
235237
} else {
236-
return getData(byDigest, cache, key, opts).then(res => {
237-
return fs.writeFileAsync(dest, byDigest ? res : res.data)
238+
return getData(byDigest, cache, key, opts).then((res) => {
239+
return writeFile(dest, byDigest ? res : res.data)
238240
.then(() => byDigest ? key : {
239241
metadata: res.metadata,
240242
size: res.size,

lib/content/read.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ const pipe = BB.promisify(require('mississippi').pipe)
1010
const ssri = require('ssri')
1111
const Y = require('../util/y.js')
1212

13-
const lstatAsync = BB.promisify(fs.lstat)
14-
const readFileAsync = BB.promisify(fs.readFile)
13+
const lstat = BB.promisify(fs.lstat)
14+
const readFile = BB.promisify(fs.readFile)
1515

1616
const ReadOpts = figgyPudding({
1717
size: {}
@@ -21,7 +21,7 @@ module.exports = read
2121
function read (cache, integrity, opts) {
2222
opts = ReadOpts(opts)
2323
return withContentSri(cache, integrity, (cpath, sri) => {
24-
return readFileAsync(cpath, null).then(data => {
24+
return readFile(cpath, null).then((data) => {
2525
if (typeof opts.size === 'number' && opts.size !== data.length) {
2626
throw sizeError(opts.size, data.length)
2727
} else if (ssri.checkData(data, sri)) {
@@ -54,7 +54,7 @@ function readStream (cache, integrity, opts) {
5454
opts = ReadOpts(opts)
5555
const stream = new PassThrough()
5656
withContentSri(cache, integrity, (cpath, sri) => {
57-
return lstatAsync(cpath).then(stat => ({ cpath, sri, stat }))
57+
return lstat(cpath).then((stat) => ({ cpath, sri, stat }))
5858
}).then(({ cpath, sri, stat }) => {
5959
return pipe(
6060
fs.createReadStream(cpath),
@@ -70,17 +70,17 @@ function readStream (cache, integrity, opts) {
7070
return stream
7171
}
7272

73-
let copyFileAsync
73+
let copyFile
7474
if (fs.copyFile) {
7575
module.exports.copy = copy
7676
module.exports.copy.sync = copySync
77-
copyFileAsync = BB.promisify(fs.copyFile)
77+
copyFile = BB.promisify(fs.copyFile)
7878
}
7979

8080
function copy (cache, integrity, dest, opts) {
8181
opts = ReadOpts(opts)
8282
return withContentSri(cache, integrity, (cpath, sri) => {
83-
return copyFileAsync(cpath, dest)
83+
return copyFile(cpath, dest)
8484
})
8585
}
8686

@@ -95,7 +95,7 @@ module.exports.hasContent = hasContent
9595
function hasContent (cache, integrity) {
9696
if (!integrity) { return BB.resolve(false) }
9797
return withContentSri(cache, integrity, (cpath, sri) => {
98-
return lstatAsync(cpath).then(stat => ({ size: stat.size, sri, stat }))
98+
return lstat(cpath).then((stat) => ({ size: stat.size, sri, stat }))
9999
}).catch((err) => {
100100
if (err.code === 'ENOENT') { return false }
101101
if (err.code === 'EPERM') {

lib/content/rm.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const rimraf = BB.promisify(require('rimraf'))
88

99
module.exports = rm
1010
function rm (cache, integrity) {
11-
return hasContent(cache, integrity).then(content => {
11+
return hasContent(cache, integrity).then((content) => {
1212
if (content) {
1313
const sri = content.sri
1414
if (sri) {

lib/content/write.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const path = require('path')
1111
const pipe = BB.promisify(require('mississippi').pipe)
1212
const rimraf = BB.promisify(require('rimraf'))
1313
const ssri = require('ssri')
14-
const to = require('mississippi').to
14+
const { to } = require('mississippi')
1515
const uniqueFilename = require('unique-filename')
1616
const Y = require('../util/y.js')
1717

18-
const writeFileAsync = BB.promisify(fs.writeFile)
18+
const writeFile = BB.promisify(fs.writeFile)
1919

2020
module.exports = write
2121

@@ -37,7 +37,7 @@ function write (cache, data, opts) {
3737
}
3838
return makeTmp(cache, opts)
3939
.then((tmp) => {
40-
return writeFileAsync(
40+
return writeFile(
4141
tmp.target, data, { flag: 'wx' }
4242
).then(() => moveToDestination(tmp, cache, sri, opts))
4343
.then((result) => makeTmpDisposer(tmp, result))
@@ -69,7 +69,7 @@ function writeStream (cache, opts) {
6969
e.code = 'ENODATA'
7070
return ret.emit('error', e)
7171
}
72-
allDone.then(res => {
72+
allDone.then((res) => {
7373
res.integrity && ret.emit('integrity', res.integrity)
7474
res.size !== null && ret.emit('size', res.size)
7575
cb()
@@ -90,7 +90,7 @@ function handleContent (inputStream, cache, opts, errCheck) {
9090
errCheck()
9191
return pipeToTmp(
9292
inputStream, cache, tmp.target, opts, errCheck
93-
).then(res => {
93+
).then((res) => {
9494
return moveToDestination(
9595
tmp, cache, res.integrity, opts, errCheck
9696
).then(() => res)

lib/entry-index.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ const figgyPudding = require('figgy-pudding')
88
const fixOwner = require('./util/fix-owner')
99
const fs = require('graceful-fs')
1010
const hashToSegments = require('./util/hash-to-segments')
11-
const ms = require('mississippi')
11+
const { concat, from } = require('mississippi')
1212
const path = require('path')
1313
const ssri = require('ssri')
1414
const Y = require('./util/y.js')
1515

1616
const indexV = require('../package.json')['cache-version'].index
1717

18-
const appendFileAsync = BB.promisify(fs.appendFile)
19-
const readFileAsync = BB.promisify(fs.readFile)
20-
const readdirAsync = BB.promisify(fs.readdir)
21-
const concat = ms.concat
22-
const from = ms.from
18+
const appendFile = BB.promisify(fs.appendFile)
19+
const readFile = BB.promisify(fs.readFile)
20+
const readdir = BB.promisify(fs.readdir)
2321

2422
module.exports.NotFoundError = class NotFoundError extends Error {
2523
constructor (cache, key) {
@@ -57,7 +55,7 @@ function insert (cache, key, integrity, opts) {
5755
// question. So, we just slap the length in there and verify it on read.
5856
//
5957
// Thanks to @isaacs for the whiteboarding session that ended up with this.
60-
return appendFileAsync(
58+
return appendFile(
6159
bucket, `\n${hashEntry(stringified)}\t${stringified}`
6260
)
6361
}).then(
@@ -106,7 +104,7 @@ function insertSync (cache, key, integrity, opts) {
106104
module.exports.find = find
107105
function find (cache, key) {
108106
const bucket = bucketPath(cache, key)
109-
return bucketEntries(bucket).then(entries => {
107+
return bucketEntries(bucket).then((entries) => {
110108
return entries.reduce((latest, next) => {
111109
if (next && next.key === key) {
112110
return formatEntry(cache, next)
@@ -175,7 +173,7 @@ function lsStream (cache) {
175173
return acc
176174
}, new Map())
177175

178-
return getKeyToEntry.then(reduced => {
176+
return getKeyToEntry.then((reduced) => {
179177
for (let entry of reduced.values()) {
180178
const formatted = formatEntry(cache, entry)
181179
formatted && stream.push(formatted)
@@ -205,9 +203,9 @@ function ls (cache) {
205203
}
206204

207205
function bucketEntries (bucket, filter) {
208-
return readFileAsync(
206+
return readFile(
209207
bucket, 'utf8'
210-
).then(data => _bucketEntries(data, filter))
208+
).then((data) => _bucketEntries(data, filter))
211209
}
212210

213211
function bucketEntriesSync (bucket, filter) {
@@ -283,7 +281,7 @@ function formatEntry (cache, entry) {
283281
}
284282

285283
function readdirOrEmpty (dir) {
286-
return readdirAsync(dir)
284+
return readdir(dir)
287285
.catch((err) => {
288286
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
289287
return []

lib/util/fix-owner.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function fixOwner (cache, filepath) {
4444
return BB.resolve()
4545
}
4646

47-
return BB.resolve(inferOwner(cache)).then(owner => {
47+
return BB.resolve(inferOwner(cache)).then((owner) => {
4848
const { uid, gid } = owner
4949

5050
// No need to override if it's already what we used.

lib/verify.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ const path = require('path')
1313
const rimraf = BB.promisify(require('rimraf'))
1414
const ssri = require('ssri')
1515

16-
BB.promisifyAll(fs)
16+
const stat = BB.promisify(fs.stat)
17+
const truncate = BB.promisify(fs.truncate)
18+
const writeFile = BB.promisify(fs.writeFile)
19+
const readFile = BB.promisify(fs.readFile)
1720

1821
const VerifyOpts = figgyPudding({
1922
concurrency: {
@@ -40,7 +43,7 @@ function verify (cache, opts) {
4043
], (stats, step, i) => {
4144
const label = step.name || `step #${i}`
4245
const start = new Date()
43-
return BB.resolve(step(cache, opts)).then(s => {
46+
return BB.resolve(step(cache, opts)).then((s) => {
4447
s && Object.keys(s).forEach(k => {
4548
stats[k] = s[k]
4649
})
@@ -96,7 +99,7 @@ function garbageCollect (cache, opts) {
9699
follow: false,
97100
nodir: true,
98101
nosort: true
99-
}).then(files => {
102+
}).then((files) => {
100103
return BB.resolve({
101104
verifiedContent: 0,
102105
reclaimedCount: 0,
@@ -109,7 +112,7 @@ function garbageCollect (cache, opts) {
109112
const algo = split[split.length - 4]
110113
const integrity = ssri.fromHex(digest, algo)
111114
if (liveContent.has(integrity.toString())) {
112-
return verifyContent(f, integrity).then(info => {
115+
return verifyContent(f, integrity).then((info) => {
113116
if (!info.valid) {
114117
stats.reclaimedCount++
115118
stats.badContentCount++
@@ -123,7 +126,7 @@ function garbageCollect (cache, opts) {
123126
} else {
124127
// No entries refer to this content. We can delete.
125128
stats.reclaimedCount++
126-
return fs.statAsync(f).then(s => {
129+
return stat(f).then((s) => {
127130
return rimraf(f).then(() => {
128131
stats.reclaimedSize += s.size
129132
return stats
@@ -137,9 +140,9 @@ function garbageCollect (cache, opts) {
137140
}
138141

139142
function verifyContent (filepath, sri) {
140-
return fs.statAsync(filepath).then(stat => {
143+
return stat(filepath).then((s) => {
141144
const contentInfo = {
142-
size: stat.size,
145+
size: s.size,
143146
valid: true
144147
}
145148
return ssri.checkStream(
@@ -161,7 +164,7 @@ function verifyContent (filepath, sri) {
161164

162165
function rebuildIndex (cache, opts) {
163166
opts.log.silly('verify', 'rebuilding index')
164-
return index.ls(cache).then(entries => {
167+
return index.ls(cache).then((entries) => {
165168
const stats = {
166169
missingContent: 0,
167170
rejectedEntries: 0,
@@ -194,12 +197,12 @@ function rebuildIndex (cache, opts) {
194197
}
195198

196199
function rebuildBucket (cache, bucket, stats, opts) {
197-
return fs.truncateAsync(bucket._path).then(() => {
200+
return truncate(bucket._path).then(() => {
198201
// This needs to be serialized because cacache explicitly
199202
// lets very racy bucket conflicts clobber each other.
200203
return BB.mapSeries(bucket, entry => {
201204
const content = contentPath(cache, entry.integrity)
202-
return fs.statAsync(content).then(() => {
205+
return stat(content).then(() => {
203206
return index.insert(cache, entry.key, entry.integrity, {
204207
metadata: entry.metadata,
205208
size: entry.size
@@ -225,15 +228,16 @@ function writeVerifile (cache, opts) {
225228
const verifile = path.join(cache, '_lastverified')
226229
opts.log.silly('verify', 'writing verifile to ' + verifile)
227230
try {
228-
return fs.writeFileAsync(verifile, '' + (+(new Date())))
231+
return writeFile(verifile, '' + (+(new Date())))
229232
} finally {
230233
fixOwner.chownr.sync(cache, verifile)
231234
}
232235
}
233236

234237
module.exports.lastRun = lastRun
238+
235239
function lastRun (cache) {
236-
return fs.readFileAsync(
240+
return readFile(
237241
path.join(cache, '_lastverified'), 'utf8'
238-
).then(data => new Date(+data))
242+
).then((data) => new Date(+data))
239243
}

put.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const figgyPudding = require('figgy-pudding')
44
const index = require('./lib/entry-index')
55
const memo = require('./lib/memoization')
66
const write = require('./lib/content/write')
7-
const to = require('mississippi').to
7+
const { to } = require('mississippi')
88

99
const PutOpts = figgyPudding({
1010
algorithms: {
@@ -25,10 +25,10 @@ const PutOpts = figgyPudding({
2525
module.exports = putData
2626
function putData (cache, key, data, opts) {
2727
opts = PutOpts(opts)
28-
return write(cache, data, opts).then(res => {
28+
return write(cache, data, opts).then((res) => {
2929
return index.insert(
3030
cache, key, res.integrity, opts.concat({ size: res.size })
31-
).then(entry => {
31+
).then((entry) => {
3232
if (opts.memoize) {
3333
memo.put(cache, entry, data, opts)
3434
}
@@ -62,7 +62,7 @@ function putStream (cache, key, opts) {
6262
})
6363
}, cb => {
6464
contentStream.end(() => {
65-
index.insert(cache, key, integrity, opts.concat({ size })).then(entry => {
65+
index.insert(cache, key, integrity, opts.concat({ size })).then((entry) => {
6666
if (opts.memoize) {
6767
memo.put(cache, entry, Buffer.concat(memoData, memoTotal), opts)
6868
}

0 commit comments

Comments
 (0)