Skip to content

Commit

Permalink
feat(promise): converted .resolve to native promise, converted .map a…
Browse files Browse the repository at this point in the history
…nd .reduce to native
  • Loading branch information
billatnpm authored and isaacs committed Sep 15, 2019
1 parent cc3ee05 commit 220c56d
Show file tree
Hide file tree
Showing 23 changed files with 146 additions and 120 deletions.
4 changes: 2 additions & 2 deletions get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const BB = require('bluebird')
const util = require('util')

const figgyPudding = require('figgy-pudding')
const fs = require('fs')
Expand All @@ -9,7 +9,7 @@ const index = require('./lib/entry-index')
const memo = require('./lib/memoization')
const read = require('./lib/content/read')

const writeFile = BB.promisify(fs.writeFile)
const writeFile = util.promisify(fs.writeFile)

const GetOpts = figgyPudding({
integrity: {},
Expand Down
11 changes: 6 additions & 5 deletions lib/content/read.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
'use strict'

const BB = require('bluebird')
const util = require('util')

const contentPath = require('./path')
const figgyPudding = require('figgy-pudding')
const fs = require('graceful-fs')
const PassThrough = require('stream').PassThrough
const pipe = BB.promisify(require('mississippi').pipe)
const pipe = util.promisify(require('mississippi').pipe)
const ssri = require('ssri')
const Y = require('../util/y.js')

const lstat = BB.promisify(fs.lstat)
const readFile = BB.promisify(fs.readFile)
const lstat = util.promisify(fs.lstat)
const readFile = util.promisify(fs.readFile)

const ReadOpts = figgyPudding({
size: {}
Expand Down Expand Up @@ -77,7 +78,7 @@ let copyFile
if (fs.copyFile) {
module.exports.copy = copy
module.exports.copy.sync = copySync
copyFile = BB.promisify(fs.copyFile)
copyFile = util.promisify(fs.copyFile)
}

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

function hasContent (cache, integrity) {
if (!integrity) { return BB.resolve(false) }
if (!integrity) { return Promise.resolve(false) }
return withContentSri(cache, integrity, (cpath, sri) => {
return lstat(cpath).then((stat) => ({ size: stat.size, sri, stat }))
}).catch((err) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/content/rm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const BB = require('bluebird')
const util = require('util')

const contentPath = require('./path')
const hasContent = require('./read').hasContent
const rimraf = BB.promisify(require('rimraf'))
const rimraf = util.promisify(require('rimraf'))

module.exports = rm
function rm (cache, integrity) {
Expand Down
10 changes: 5 additions & 5 deletions lib/content/write.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
'use strict'

const BB = require('bluebird')
const util = require('util')

const contentPath = require('./path')
const fixOwner = require('../util/fix-owner')
const fs = require('graceful-fs')
const moveFile = require('../util/move-file')
const PassThrough = require('stream').PassThrough
const path = require('path')
const pipe = BB.promisify(require('mississippi').pipe)
const rimraf = BB.promisify(require('rimraf'))
const pipe = util.promisify(require('mississippi').pipe)
const rimraf = util.promisify(require('rimraf'))
const ssri = require('ssri')
const { to } = require('mississippi')
const uniqueFilename = require('unique-filename')
const Y = require('../util/y.js')

const writeFile = BB.promisify(fs.writeFile)
const writeFile = util.promisify(fs.writeFile)

module.exports = write

Expand Down Expand Up @@ -101,7 +101,7 @@ function handleContent (inputStream, cache, opts, errCheck) {
}

function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
return BB.resolve().then(() => {
return Promise.resolve().then(() => {
let integrity
let size
const hashStream = ssri.integrityStream({
Expand Down
89 changes: 57 additions & 32 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const BB = require('bluebird')
const util = require('util')

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

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

const appendFile = BB.promisify(fs.appendFile)
const readFile = BB.promisify(fs.readFile)
const readdir = BB.promisify(fs.readdir)
const appendFile = util.promisify(fs.appendFile)
const readFile = util.promisify(fs.readFile)
const readdir = util.promisify(fs.readdir)

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

module.exports.insert = insert

function insert (cache, key, integrity, opts) {
opts = IndexOpts(opts)
const bucket = bucketPath(cache, key)
Expand Down Expand Up @@ -76,6 +77,7 @@ function insert (cache, key, integrity, opts) {
}

module.exports.insert.sync = insertSync

function insertSync (cache, key, integrity, opts) {
opts = IndexOpts(opts)
const bucket = bucketPath(cache, key)
Expand All @@ -102,6 +104,7 @@ function insertSync (cache, key, integrity, opts) {
}

module.exports.find = find

function find (cache, key) {
const bucket = bucketPath(cache, key)
return bucketEntries(bucket).then((entries) => {
Expand All @@ -122,6 +125,7 @@ function find (cache, key) {
}

module.exports.find.sync = findSync

function findSync (cache, key) {
const bucket = bucketPath(cache, key)
try {
Expand All @@ -142,55 +146,72 @@ function findSync (cache, key) {
}

module.exports.delete = del

function del (cache, key, opts) {
return insert(cache, key, null, opts)
}

module.exports.delete.sync = delSync

function delSync (cache, key, opts) {
return insertSync(cache, key, null, opts)
}

module.exports.lsStream = lsStream

function lsStream (cache) {
const indexDir = bucketDir(cache)
const stream = from.obj()

// "/cachename/*"
readdirOrEmpty(indexDir).map(bucket => {
const bucketPath = path.join(indexDir, bucket)

// "/cachename/<bucket 0xFF>/*"
return readdirOrEmpty(bucketPath).map(subbucket => {
const subbucketPath = path.join(bucketPath, subbucket)

// "/cachename/<bucket 0xFF>/<bucket 0xFF>/*"
return readdirOrEmpty(subbucketPath).map(entry => {
const getKeyToEntry = bucketEntries(
path.join(subbucketPath, entry)
).reduce((acc, entry) => {
acc.set(entry.key, entry)
return acc
}, new Map())

return getKeyToEntry.then((reduced) => {
for (let entry of reduced.values()) {
const formatted = formatEntry(cache, entry)
formatted && stream.push(formatted)
}
}).catch((err) => { if (err.code === 'ENOENT') { return undefined } throw err })
})
readdirOrEmpty(indexDir)
.then((buckets) => {
return Promise.all(buckets.map((bucket) => {
const bucketPath = path.join(indexDir, bucket)

// "/cachename/<bucket 0xFF>/*"
return readdirOrEmpty(bucketPath).then((subbuckets) => {
return Promise.all(subbuckets.map((subbucket) => {
const subbucketPath = path.join(bucketPath, subbucket)

// "/cachename/<bucket 0xFF>/<bucket 0xFF>/*"
return readdirOrEmpty(subbucketPath).then((entries) => {
return Promise.all(entries.map((entry) => {
const getKeyToEntry = bucketEntries(
path.join(subbucketPath, entry)
).then((entries) => {
return entries.reduce((acc, entry) => {
acc.set(entry.key, entry)
return acc
}, new Map())
})

return getKeyToEntry.then((reduced) => {
for (let entry of reduced.values()) {
const formatted = formatEntry(cache, entry)
formatted && stream.push(formatted)
}
}).catch((err) => {
if (err.code === 'ENOENT') { return undefined }
throw err
})
}))
})
}))
})
}))
})
.then(() => {
stream.push(null)
}, err => {
stream.emit('error', err)
})
}).then(() => {
stream.push(null)
}, err => {
stream.emit('error', err)
})

return stream
}

module.exports.ls = ls

function ls (cache) {
return new Promise((resolve, reject) => {
lsStream(cache).on('error', reject).pipe(concat(entries => {
Expand Down Expand Up @@ -238,11 +259,13 @@ function _bucketEntries (data, filter) {
}

module.exports._bucketDir = bucketDir

function bucketDir (cache) {
return path.join(cache, `index-v${indexV}`)
}

module.exports._bucketPath = bucketPath

function bucketPath (cache, key) {
const hashed = hashKey(key)
return path.join.apply(path, [bucketDir(cache)].concat(
Expand All @@ -251,11 +274,13 @@ function bucketPath (cache, key) {
}

module.exports._hashKey = hashKey

function hashKey (key) {
return hash(key, 'sha256')
}

module.exports._hashEntry = hashEntry

function hashEntry (str) {
return hash(str, 'sha1')
}
Expand Down
14 changes: 7 additions & 7 deletions lib/util/fix-owner.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict'

const BB = require('bluebird')
const util = require('util')

const chownr = BB.promisify(require('chownr'))
const mkdirp = BB.promisify(require('mkdirp'))
const chownr = util.promisify(require('chownr'))
const mkdirp = util.promisify(require('mkdirp'))
const inflight = require('promise-inflight')
const inferOwner = require('infer-owner')

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

getSelf()
if (self.uid !== 0) {
// almost certainly can't chown anyway
return BB.resolve()
return Promise.resolve()
}

return BB.resolve(inferOwner(cache)).then((owner) => {
return Promise.resolve(inferOwner(cache)).then((owner) => {
const { uid, gid } = owner

// No need to override if it's already what we used.
Expand Down Expand Up @@ -101,7 +101,7 @@ function mkdirfix (cache, p, cb) {
// we aren't going to use the results, since the cache itself might not
// exist yet. If we mkdirp it, then our current uid/gid will be assumed
// to be correct if it creates the cache folder in the process.
return BB.resolve(inferOwner(cache)).then(() => {
return Promise.resolve(inferOwner(cache)).then(() => {
return mkdirp(p).then(made => {
if (made) {
return fixOwner(cache, made).then(() => made)
Expand Down
10 changes: 5 additions & 5 deletions lib/util/move-file.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const fs = require('graceful-fs')
const BB = require('bluebird')
const chmod = BB.promisify(fs.chmod)
const unlink = BB.promisify(fs.unlink)
const stat = BB.promisify(fs.stat)
const util = require('util')
const chmod = util.promisify(fs.chmod)
const unlink = util.promisify(fs.unlink)
const stat = util.promisify(fs.stat)
const move = require('move-concurrently')
const pinflight = require('promise-inflight')

Expand Down Expand Up @@ -43,7 +43,7 @@ function moveFile (src, dest) {
throw err
}
// file doesn't already exist! let's try a rename -> copy fallback
return move(src, dest, { BB, fs })
return move(src, dest, { Promise, fs })
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions lib/util/tmp.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
'use strict'

const BB = require('bluebird')
const util = require('util')

const figgyPudding = require('figgy-pudding')
const fixOwner = require('./fix-owner')
const path = require('path')
const rimraf = BB.promisify(require('rimraf'))
const rimraf = util.promisify(require('rimraf'))
const uniqueFilename = require('unique-filename')

const TmpOpts = figgyPudding({
Expand Down
Loading

0 comments on commit 220c56d

Please sign in to comment.