Skip to content

Commit

Permalink
feat(promise): removed .map, replaced with p-map. removed .try
Browse files Browse the repository at this point in the history
  • Loading branch information
billatnpm authored and isaacs committed Sep 15, 2019
1 parent 478f5cb commit cc3ee05
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
16 changes: 15 additions & 1 deletion lib/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const ReadOpts = figgyPudding({
})

module.exports = read

function read (cache, integrity, opts) {
opts = ReadOpts(opts)
return withContentSri(cache, integrity, (cpath, sri) => {
Expand All @@ -34,6 +35,7 @@ function read (cache, integrity, opts) {
}

module.exports.sync = readSync

function readSync (cache, integrity, opts) {
opts = ReadOpts(opts)
return withContentSriSync(cache, integrity, (cpath, sri) => {
Expand All @@ -50,6 +52,7 @@ function readSync (cache, integrity, opts) {

module.exports.stream = readStream
module.exports.readStream = readStream

function readStream (cache, integrity, opts) {
opts = ReadOpts(opts)
const stream = new PassThrough()
Expand Down Expand Up @@ -92,6 +95,7 @@ function copySync (cache, integrity, dest, opts) {
}

module.exports.hasContent = hasContent

function hasContent (cache, integrity) {
if (!integrity) { return BB.resolve(false) }
return withContentSri(cache, integrity, (cpath, sri) => {
Expand All @@ -109,6 +113,7 @@ function hasContent (cache, integrity) {
}

module.exports.hasContent.sync = hasContentSync

function hasContentSync (cache, integrity) {
if (!integrity) { return false }
return withContentSriSync(cache, integrity, (cpath, sri) => {
Expand All @@ -129,12 +134,13 @@ function hasContentSync (cache, integrity) {
}

function withContentSri (cache, integrity, fn) {
return BB.try(() => {
const tryFn = () => {
const sri = ssri.parse(integrity)
// If `integrity` has multiple entries, pick the first digest
// with available local data.
const algo = sri.pickAlgorithm()
const digests = sri[algo]

if (digests.length <= 1) {
const cpath = contentPath(cache, digests[0])
return fn(cpath, digests[0])
Expand All @@ -153,6 +159,14 @@ function withContentSri (cache, integrity, fn) {
}
})
}
}

return new Promise((resolve, reject) => {
try {
tryFn().then(resolve).catch(reject)
} catch (err) {
reject(err)
}
})
}

Expand Down
5 changes: 3 additions & 2 deletions lib/verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const BB = require('bluebird')

const pMap = require('p-map')
const contentPath = require('./content/path')
const figgyPudding = require('figgy-pudding')
const finished = BB.promisify(require('mississippi').finished)
Expand Down Expand Up @@ -111,7 +112,7 @@ function garbageCollect (cache, opts) {
reclaimedSize: 0,
badContentCount: 0,
keptSize: 0
}).then((stats) => BB.map(files, (f) => {
}).then((stats) => pMap(files, (f) => {
const split = f.split(/[/\\]/)
const digest = split.slice(split.length - 3).join('')
const algo = split[split.length - 4]
Expand Down Expand Up @@ -195,7 +196,7 @@ function rebuildIndex (cache, opts) {
}
}
}
return BB.map(Object.keys(buckets), key => {
return pMap(Object.keys(buckets), key => {
return rebuildBucket(cache, buckets[key], stats, opts)
}, { concurrency: opts.concurrency }).then(() => stats)
})
Expand Down
25 changes: 23 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"mississippi": "^3.0.0",
"mkdirp": "^0.5.1",
"move-concurrently": "^1.0.1",
"p-map": "^3.0.0",
"promise-inflight": "^1.0.1",
"rimraf": "^2.6.3",
"ssri": "^6.0.1",
Expand Down

0 comments on commit cc3ee05

Please sign in to comment.