Skip to content

Commit 220c56d

Browse files
billatnpmisaacs
authored andcommitted
feat(promise): converted .resolve to native promise, converted .map and .reduce to native
1 parent cc3ee05 commit 220c56d

23 files changed

+146
-120
lines changed

get.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const BB = require('bluebird')
3+
const util = require('util')
44

55
const figgyPudding = require('figgy-pudding')
66
const fs = require('fs')
@@ -9,7 +9,7 @@ const index = require('./lib/entry-index')
99
const memo = require('./lib/memoization')
1010
const read = require('./lib/content/read')
1111

12-
const writeFile = BB.promisify(fs.writeFile)
12+
const writeFile = util.promisify(fs.writeFile)
1313

1414
const GetOpts = figgyPudding({
1515
integrity: {},

lib/content/read.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
'use strict'
22

33
const BB = require('bluebird')
4+
const util = require('util')
45

56
const contentPath = require('./path')
67
const figgyPudding = require('figgy-pudding')
78
const fs = require('graceful-fs')
89
const PassThrough = require('stream').PassThrough
9-
const pipe = BB.promisify(require('mississippi').pipe)
10+
const pipe = util.promisify(require('mississippi').pipe)
1011
const ssri = require('ssri')
1112
const Y = require('../util/y.js')
1213

13-
const lstat = BB.promisify(fs.lstat)
14-
const readFile = BB.promisify(fs.readFile)
14+
const lstat = util.promisify(fs.lstat)
15+
const readFile = util.promisify(fs.readFile)
1516

1617
const ReadOpts = figgyPudding({
1718
size: {}
@@ -77,7 +78,7 @@ let copyFile
7778
if (fs.copyFile) {
7879
module.exports.copy = copy
7980
module.exports.copy.sync = copySync
80-
copyFile = BB.promisify(fs.copyFile)
81+
copyFile = util.promisify(fs.copyFile)
8182
}
8283

8384
function copy (cache, integrity, dest, opts) {
@@ -97,7 +98,7 @@ function copySync (cache, integrity, dest, opts) {
9798
module.exports.hasContent = hasContent
9899

99100
function hasContent (cache, integrity) {
100-
if (!integrity) { return BB.resolve(false) }
101+
if (!integrity) { return Promise.resolve(false) }
101102
return withContentSri(cache, integrity, (cpath, sri) => {
102103
return lstat(cpath).then((stat) => ({ size: stat.size, sri, stat }))
103104
}).catch((err) => {

lib/content/rm.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

3-
const BB = require('bluebird')
3+
const util = require('util')
44

55
const contentPath = require('./path')
66
const hasContent = require('./read').hasContent
7-
const rimraf = BB.promisify(require('rimraf'))
7+
const rimraf = util.promisify(require('rimraf'))
88

99
module.exports = rm
1010
function rm (cache, integrity) {

lib/content/write.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict'
22

3-
const BB = require('bluebird')
3+
const util = require('util')
44

55
const contentPath = require('./path')
66
const fixOwner = require('../util/fix-owner')
77
const fs = require('graceful-fs')
88
const moveFile = require('../util/move-file')
99
const PassThrough = require('stream').PassThrough
1010
const path = require('path')
11-
const pipe = BB.promisify(require('mississippi').pipe)
12-
const rimraf = BB.promisify(require('rimraf'))
11+
const pipe = util.promisify(require('mississippi').pipe)
12+
const rimraf = util.promisify(require('rimraf'))
1313
const ssri = require('ssri')
1414
const { to } = require('mississippi')
1515
const uniqueFilename = require('unique-filename')
1616
const Y = require('../util/y.js')
1717

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

2020
module.exports = write
2121

@@ -101,7 +101,7 @@ function handleContent (inputStream, cache, opts, errCheck) {
101101
}
102102

103103
function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
104-
return BB.resolve().then(() => {
104+
return Promise.resolve().then(() => {
105105
let integrity
106106
let size
107107
const hashStream = ssri.integrityStream({

lib/entry-index.js

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const BB = require('bluebird')
3+
const util = require('util')
44

55
const contentPath = require('./content/path')
66
const crypto = require('crypto')
@@ -15,9 +15,9 @@ const Y = require('./util/y.js')
1515

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

18-
const appendFile = BB.promisify(fs.appendFile)
19-
const readFile = BB.promisify(fs.readFile)
20-
const readdir = BB.promisify(fs.readdir)
18+
const appendFile = util.promisify(fs.appendFile)
19+
const readFile = util.promisify(fs.readFile)
20+
const readdir = util.promisify(fs.readdir)
2121

2222
module.exports.NotFoundError = class NotFoundError extends Error {
2323
constructor (cache, key) {
@@ -34,6 +34,7 @@ const IndexOpts = figgyPudding({
3434
})
3535

3636
module.exports.insert = insert
37+
3738
function insert (cache, key, integrity, opts) {
3839
opts = IndexOpts(opts)
3940
const bucket = bucketPath(cache, key)
@@ -76,6 +77,7 @@ function insert (cache, key, integrity, opts) {
7677
}
7778

7879
module.exports.insert.sync = insertSync
80+
7981
function insertSync (cache, key, integrity, opts) {
8082
opts = IndexOpts(opts)
8183
const bucket = bucketPath(cache, key)
@@ -102,6 +104,7 @@ function insertSync (cache, key, integrity, opts) {
102104
}
103105

104106
module.exports.find = find
107+
105108
function find (cache, key) {
106109
const bucket = bucketPath(cache, key)
107110
return bucketEntries(bucket).then((entries) => {
@@ -122,6 +125,7 @@ function find (cache, key) {
122125
}
123126

124127
module.exports.find.sync = findSync
128+
125129
function findSync (cache, key) {
126130
const bucket = bucketPath(cache, key)
127131
try {
@@ -142,55 +146,72 @@ function findSync (cache, key) {
142146
}
143147

144148
module.exports.delete = del
149+
145150
function del (cache, key, opts) {
146151
return insert(cache, key, null, opts)
147152
}
148153

149154
module.exports.delete.sync = delSync
155+
150156
function delSync (cache, key, opts) {
151157
return insertSync(cache, key, null, opts)
152158
}
153159

154160
module.exports.lsStream = lsStream
161+
155162
function lsStream (cache) {
156163
const indexDir = bucketDir(cache)
157164
const stream = from.obj()
158165

159166
// "/cachename/*"
160-
readdirOrEmpty(indexDir).map(bucket => {
161-
const bucketPath = path.join(indexDir, bucket)
162-
163-
// "/cachename/<bucket 0xFF>/*"
164-
return readdirOrEmpty(bucketPath).map(subbucket => {
165-
const subbucketPath = path.join(bucketPath, subbucket)
166-
167-
// "/cachename/<bucket 0xFF>/<bucket 0xFF>/*"
168-
return readdirOrEmpty(subbucketPath).map(entry => {
169-
const getKeyToEntry = bucketEntries(
170-
path.join(subbucketPath, entry)
171-
).reduce((acc, entry) => {
172-
acc.set(entry.key, entry)
173-
return acc
174-
}, new Map())
175-
176-
return getKeyToEntry.then((reduced) => {
177-
for (let entry of reduced.values()) {
178-
const formatted = formatEntry(cache, entry)
179-
formatted && stream.push(formatted)
180-
}
181-
}).catch((err) => { if (err.code === 'ENOENT') { return undefined } throw err })
182-
})
167+
readdirOrEmpty(indexDir)
168+
.then((buckets) => {
169+
return Promise.all(buckets.map((bucket) => {
170+
const bucketPath = path.join(indexDir, bucket)
171+
172+
// "/cachename/<bucket 0xFF>/*"
173+
return readdirOrEmpty(bucketPath).then((subbuckets) => {
174+
return Promise.all(subbuckets.map((subbucket) => {
175+
const subbucketPath = path.join(bucketPath, subbucket)
176+
177+
// "/cachename/<bucket 0xFF>/<bucket 0xFF>/*"
178+
return readdirOrEmpty(subbucketPath).then((entries) => {
179+
return Promise.all(entries.map((entry) => {
180+
const getKeyToEntry = bucketEntries(
181+
path.join(subbucketPath, entry)
182+
).then((entries) => {
183+
return entries.reduce((acc, entry) => {
184+
acc.set(entry.key, entry)
185+
return acc
186+
}, new Map())
187+
})
188+
189+
return getKeyToEntry.then((reduced) => {
190+
for (let entry of reduced.values()) {
191+
const formatted = formatEntry(cache, entry)
192+
formatted && stream.push(formatted)
193+
}
194+
}).catch((err) => {
195+
if (err.code === 'ENOENT') { return undefined }
196+
throw err
197+
})
198+
}))
199+
})
200+
}))
201+
})
202+
}))
203+
})
204+
.then(() => {
205+
stream.push(null)
206+
}, err => {
207+
stream.emit('error', err)
183208
})
184-
}).then(() => {
185-
stream.push(null)
186-
}, err => {
187-
stream.emit('error', err)
188-
})
189209

190210
return stream
191211
}
192212

193213
module.exports.ls = ls
214+
194215
function ls (cache) {
195216
return new Promise((resolve, reject) => {
196217
lsStream(cache).on('error', reject).pipe(concat(entries => {
@@ -238,11 +259,13 @@ function _bucketEntries (data, filter) {
238259
}
239260

240261
module.exports._bucketDir = bucketDir
262+
241263
function bucketDir (cache) {
242264
return path.join(cache, `index-v${indexV}`)
243265
}
244266

245267
module.exports._bucketPath = bucketPath
268+
246269
function bucketPath (cache, key) {
247270
const hashed = hashKey(key)
248271
return path.join.apply(path, [bucketDir(cache)].concat(
@@ -251,11 +274,13 @@ function bucketPath (cache, key) {
251274
}
252275

253276
module.exports._hashKey = hashKey
277+
254278
function hashKey (key) {
255279
return hash(key, 'sha256')
256280
}
257281

258282
module.exports._hashEntry = hashEntry
283+
259284
function hashEntry (str) {
260285
return hash(str, 'sha1')
261286
}

lib/util/fix-owner.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
'use strict'
22

3-
const BB = require('bluebird')
3+
const util = require('util')
44

5-
const chownr = BB.promisify(require('chownr'))
6-
const mkdirp = BB.promisify(require('mkdirp'))
5+
const chownr = util.promisify(require('chownr'))
6+
const mkdirp = util.promisify(require('mkdirp'))
77
const inflight = require('promise-inflight')
88
const inferOwner = require('infer-owner')
99

@@ -35,16 +35,16 @@ module.exports.chownr = fixOwner
3535
function fixOwner (cache, filepath) {
3636
if (!process.getuid) {
3737
// This platform doesn't need ownership fixing
38-
return BB.resolve()
38+
return Promise.resolve()
3939
}
4040

4141
getSelf()
4242
if (self.uid !== 0) {
4343
// almost certainly can't chown anyway
44-
return BB.resolve()
44+
return Promise.resolve()
4545
}
4646

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

5050
// No need to override if it's already what we used.
@@ -101,7 +101,7 @@ function mkdirfix (cache, p, cb) {
101101
// we aren't going to use the results, since the cache itself might not
102102
// exist yet. If we mkdirp it, then our current uid/gid will be assumed
103103
// to be correct if it creates the cache folder in the process.
104-
return BB.resolve(inferOwner(cache)).then(() => {
104+
return Promise.resolve(inferOwner(cache)).then(() => {
105105
return mkdirp(p).then(made => {
106106
if (made) {
107107
return fixOwner(cache, made).then(() => made)

lib/util/move-file.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict'
22

33
const fs = require('graceful-fs')
4-
const BB = require('bluebird')
5-
const chmod = BB.promisify(fs.chmod)
6-
const unlink = BB.promisify(fs.unlink)
7-
const stat = BB.promisify(fs.stat)
4+
const util = require('util')
5+
const chmod = util.promisify(fs.chmod)
6+
const unlink = util.promisify(fs.unlink)
7+
const stat = util.promisify(fs.stat)
88
const move = require('move-concurrently')
99
const pinflight = require('promise-inflight')
1010

@@ -43,7 +43,7 @@ function moveFile (src, dest) {
4343
throw err
4444
}
4545
// file doesn't already exist! let's try a rename -> copy fallback
46-
return move(src, dest, { BB, fs })
46+
return move(src, dest, { Promise, fs })
4747
})
4848
})
4949
})

lib/util/tmp.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict'
22

3-
const BB = require('bluebird')
3+
const util = require('util')
44

55
const figgyPudding = require('figgy-pudding')
66
const fixOwner = require('./fix-owner')
77
const path = require('path')
8-
const rimraf = BB.promisify(require('rimraf'))
8+
const rimraf = util.promisify(require('rimraf'))
99
const uniqueFilename = require('unique-filename')
1010

1111
const TmpOpts = figgyPudding({

0 commit comments

Comments
 (0)