Skip to content

Commit

Permalink
feat(promise): removed bluebird specific .catch calls
Browse files Browse the repository at this point in the history
  • Loading branch information
billatnpm authored and isaacs committed Sep 15, 2019
1 parent 1d56da1 commit 28aeeac
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 50 deletions.
2 changes: 1 addition & 1 deletion get.js
Expand Up @@ -160,7 +160,7 @@ function getStream (cache, key, opts) {
memoStream,
stream
)
}).catch(err => stream.emit('error', err))
}).catch((err) => stream.emit('error', err))
return stream
}

Expand Down
6 changes: 3 additions & 3 deletions lib/content/read.js
Expand Up @@ -64,7 +64,7 @@ function readStream (cache, integrity, opts) {
}),
stream
)
}).catch(err => {
}).catch((err) => {
stream.emit('error', err)
})
return stream
Expand Down Expand Up @@ -96,7 +96,7 @@ function hasContent (cache, integrity) {
if (!integrity) { return BB.resolve(false) }
return withContentSri(cache, integrity, (cpath, sri) => {
return lstatAsync(cpath).then(stat => ({ size: stat.size, sri, stat }))
}).catch(err => {
}).catch((err) => {
if (err.code === 'ENOENT') { return false }
if (err.code === 'EPERM') {
if (process.platform !== 'win32') {
Expand Down Expand Up @@ -142,7 +142,7 @@ function withContentSri (cache, integrity, fn) {
return BB.any(sri[sri.pickAlgorithm()].map(meta => {
return withContentSri(cache, meta, fn)
}, { concurrency: 1 }))
.catch(err => {
.catch((err) => {
if ([].some.call(err, e => e.code === 'ENOENT')) {
throw Object.assign(
new Error('No matching content found for ' + sri.toString()),
Expand Down
2 changes: 1 addition & 1 deletion lib/content/write.js
Expand Up @@ -119,7 +119,7 @@ function pipeToTmp (inputStream, cache, tmpTarget, opts, errCheck) {
errCheck()
return pipe(inputStream, hashStream, outStream).then(() => {
return { integrity, size }
}).catch(err => {
}).catch((err) => {
return rimraf(tmpTarget).then(() => { throw err })
})
})
Expand Down
20 changes: 13 additions & 7 deletions lib/entry-index.js
Expand Up @@ -62,7 +62,11 @@ function insert (cache, key, integrity, opts) {
)
}).then(
() => fixOwner.chownr(cache, bucket)
).catch({ code: 'ENOENT' }, () => {
).catch((err) => {
if (err.code === 'ENOENT') {
return undefined
}
throw err
// There's a class of race conditions that happen when things get deleted
// during fixOwner, or between the two mkdirfix/chownr calls.
//
Expand Down Expand Up @@ -110,7 +114,7 @@ function find (cache, key) {
return latest
}
}, null)
}).catch(err => {
}).catch((err) => {
if (err.code === 'ENOENT') {
return null
} else {
Expand Down Expand Up @@ -176,7 +180,7 @@ function lsStream (cache) {
const formatted = formatEntry(cache, entry)
formatted && stream.push(formatted)
}
}).catch({ code: 'ENOENT' }, nop)
}).catch((err) => { if (err.code === 'ENOENT') { return undefined } throw err })
})
})
}).then(() => {
Expand Down Expand Up @@ -280,9 +284,11 @@ function formatEntry (cache, entry) {

function readdirOrEmpty (dir) {
return readdirAsync(dir)
.catch({ code: 'ENOENT' }, () => [])
.catch({ code: 'ENOTDIR' }, () => [])
}
.catch((err) => {
if (err.code === 'ENOENT' || err.code === 'ENOTDIR') {
return []
}

function nop () {
throw err
})
}
15 changes: 11 additions & 4 deletions lib/util/fix-owner.js
Expand Up @@ -58,7 +58,12 @@ function fixOwner (cache, filepath) {
filepath,
typeof uid === 'number' ? uid : self.uid,
typeof gid === 'number' ? gid : self.gid
).catch({ code: 'ENOENT' }, () => null)
).catch((err) => {
if (err.code === 'ENOENT') {
return null
}
throw err
})
)
})
}
Expand Down Expand Up @@ -101,9 +106,11 @@ function mkdirfix (cache, p, cb) {
if (made) {
return fixOwner(cache, made).then(() => made)
}
}).catch({ code: 'EEXIST' }, () => {
// There's a race in mkdirp!
return fixOwner(cache, p).then(() => null)
}).catch((err) => {
if (err.code === 'EEXIST') {
return fixOwner(cache, p).then(() => null)
}
throw err
})
})
}
Expand Down
9 changes: 4 additions & 5 deletions lib/util/move-file.js
Expand Up @@ -4,8 +4,9 @@ const fs = require('graceful-fs')
const BB = require('bluebird')
const chmod = BB.promisify(fs.chmod)
const unlink = BB.promisify(fs.unlink)
let move
let pinflight
const stat = BB.promisify(fs.stat)
const move = require('move-concurrently')
const pinflight = require('promise-inflight')

module.exports = moveFile
function moveFile (src, dest) {
Expand Down Expand Up @@ -35,15 +36,13 @@ function moveFile (src, dest) {
// content should never change for any reason, so make it read-only
return Promise.all([unlink(src), process.platform !== 'win32' && chmod(dest, '0444')])
}).catch(() => {
if (!pinflight) { pinflight = require('promise-inflight') }
return pinflight('cacache-move-file:' + dest, () => {
return BB.promisify(fs.stat)(dest).catch(err => {
return stat(dest).catch(err => {
if (err.code !== 'ENOENT') {
// Something else is wrong here. Bail bail bail
throw err
}
// file doesn't already exist! let's try a rename -> copy fallback
if (!move) { move = require('move-concurrently') }
return move(src, dest, { BB, fs })
})
})
Expand Down
19 changes: 14 additions & 5 deletions lib/verify.js
Expand Up @@ -145,13 +145,18 @@ function verifyContent (filepath, sri) {
return ssri.checkStream(
fs.createReadStream(filepath),
sri
).catch(err => {
).catch((err) => {
if (err.code !== 'EINTEGRITY') { throw err }
return rimraf(filepath).then(() => {
contentInfo.valid = false
})
}).then(() => contentInfo)
}).catch({ code: 'ENOENT' }, () => ({ size: 0, valid: false }))
}).catch((err) => {
if (err.code === 'ENOENT') {
return { size: 0, valid: false }
}
throw err
})
}

function rebuildIndex (cache, opts) {
Expand Down Expand Up @@ -199,9 +204,13 @@ function rebuildBucket (cache, bucket, stats, opts) {
metadata: entry.metadata,
size: entry.size
}).then(() => { stats.totalEntries++ })
}).catch({ code: 'ENOENT' }, () => {
stats.rejectedEntries++
stats.missingContent++
}).catch((err) => {
if (err.code === 'ENOENT') {
stats.rejectedEntries++
stats.missingContent++
return
}
throw err
})
})
})
Expand Down
4 changes: 2 additions & 2 deletions test/content.rm.js
Expand Up @@ -23,7 +23,7 @@ test('removes a content entry', function (t) {
fs.statAsync(contentPath(CACHE, 'sha1-deadbeef'))
)).then(() => {
throw new Error('expected an error')
}).catch(err => {
}).catch((err) => {
t.ok(err, 'fs.stat failed on rmed content')
t.equal('ENOENT', err.code, 'file does not exist anymore')
})
Expand All @@ -36,7 +36,7 @@ test('works fine if entry missing', function (t) {
fs.statAsync(contentPath(CACHE, 'sha1-deadbeef'))
)).then(() => {
throw new Error('expected an error')
}).catch(err => {
}).catch((err) => {
t.ok(err, 'fs.stat failed on rmed content')
t.equal('ENOENT', err.code, 'file does not exist anymore')
})
Expand Down
10 changes: 5 additions & 5 deletions test/get.js
Expand Up @@ -154,11 +154,11 @@ test('get.copy', t => {
test('ENOENT if not found', t => {
return get(CACHE, KEY).then(() => {
throw new Error('lookup should fail')
}).catch(err => {
}).catch((err) => {
t.ok(err, 'got an error')
t.equal(err.code, 'ENOENT', 'error code is ENOENT')
return get.info(CACHE, KEY)
}).catch(err => {
}).catch((err) => {
t.ok(err, 'got an error')
t.equal(err.code, 'ENOENT', 'error code is ENOENT')
})
Expand Down Expand Up @@ -205,7 +205,7 @@ test('memoizes data on bulk read', t => {
}, 'memoized data fetched by default')
return get(CACHE, KEY, { memoize: false }).then(() => {
throw new Error('expected get to fail')
}).catch(err => {
}).catch((err) => {
t.ok(err, 'got an error from unmemoized get')
t.equal(err.code, 'ENOENT', 'cached content not found')
t.deepEqual(memo.get(CACHE, KEY), {
Expand Down Expand Up @@ -299,10 +299,10 @@ test('memoizes data on stream read', t => {
return Promise.all([
streamGet(false, CACHE, KEY, {
memoize: false
}).catch(err => err),
}).catch((err) => err),
streamGet(true, CACHE, INTEGRITY, {
memoize: false
}).catch(err => err)
}).catch((err) => err)
]).then(([keyErr, digestErr]) => {
t.equal(keyErr.code, 'ENOENT', 'key get memoization bypassed')
t.equal(keyErr.code, 'ENOENT', 'digest get memoization bypassed')
Expand Down
2 changes: 1 addition & 1 deletion test/index.find.js
Expand Up @@ -57,7 +57,7 @@ test('index.find cache miss', function (t) {
test('index.find no cache', function (t) {
return fs.statAsync(CACHE).then(() => {
throw new Error('expected cache directory')
}).catch(err => {
}).catch((err) => {
t.assert(err, 'cache directory does not exist')
return index.find(CACHE, 'whatever')
}).then(info => {
Expand Down
8 changes: 4 additions & 4 deletions test/put.js
Expand Up @@ -105,7 +105,7 @@ test('optionally memoizes data on stream insertion', t => {
test('errors if integrity errors', t => {
return put(CACHE, KEY, CONTENT, {
integrity: 'sha1-BaDDigEST'
}).catch(err => {
}).catch((err) => {
t.equal(err.code, 'EINTEGRITY', 'got error from bad integrity')
})
})
Expand All @@ -116,12 +116,12 @@ test('signals error if error writing to cache', t => {
size: 2
}).then(() => {
throw new Error('expected error')
}).catch(err => err),
}).catch((err) => err),
pipe(fromString(CONTENT), put.stream(CACHE, KEY, {
size: 2
})).then(() => {
throw new Error('expected error')
}).catch(err => err)
}).catch((err) => err)
]).then(([bulkErr, streamErr]) => {
t.equal(bulkErr.code, 'EBADSIZE', 'got error from bulk write')
t.equal(streamErr.code, 'EBADSIZE', 'got error from stream write')
Expand All @@ -138,7 +138,7 @@ test('errors if input stream errors', t => {
stream, putter
).then(() => {
throw new Error('expected error')
}).catch(err => {
}).catch((err) => {
t.ok(err, 'got an error')
t.ok(!int, 'no integrity returned')
t.match(
Expand Down
24 changes: 18 additions & 6 deletions test/rm.js
Expand Up @@ -36,8 +36,12 @@ test('rm.entry removes entries, not content', t => {
return get(CACHE, KEY)
}).then(res => {
throw new Error('unexpected success')
}).catch({ code: 'ENOENT' }, err => {
t.match(err.message, KEY, 'entry no longer accessible')
}).catch((err) => {
if (err.code === 'ENOENT') {
t.match(err.message, KEY, 'entry no longer accessible')
return
}
throw err
}).then(() => {
return fs.readFileAsync(contentPath(CACHE, INTEGRITY))
}).then(data => {
Expand All @@ -58,14 +62,22 @@ test('rm.content removes content, not entries', t => {
return get(CACHE, KEY)
}).then(res => {
throw new Error('unexpected success')
}).catch({ code: 'ENOENT' }, err => {
t.match(err.message, /no such file/, 'entry no longer accessible')
}).catch((err) => {
if (err.code === 'ENOENT') {
t.match(err.message, /no such file/, 'entry no longer accessible')
return
}
throw err
}).then(() => {
return fs.readFileAsync(contentPath(CACHE, INTEGRITY))
}).then(() => {
throw new Error('unexpected success')
}).catch({ code: 'ENOENT' }, err => {
t.match(err.message, /no such file/, 'content gone')
}).catch((err) => {
if (err.code === 'ENOENT') {
t.match(err.message, /no such file/, 'content gone')
return
}
throw err
})
})

Expand Down
8 changes: 4 additions & 4 deletions test/util.move-file.js
Expand Up @@ -23,7 +23,7 @@ test('move a file', function (t) {
return fs.readFileAsync('dest', 'utf8')
}).then(data => {
t.equal(data, 'foo', 'file data correct')
return fs.statAsync('src').catch(err => {
return fs.statAsync('src').catch((err) => {
t.ok(err, 'src read error')
t.equal(err.code, 'ENOENT', 'src does not exist')
})
Expand All @@ -40,7 +40,7 @@ test('does not clobber existing files', function (t) {
return fs.readFileAsync('dest', 'utf8')
}).then(data => {
t.equal(data, 'bar', 'conflicting file left intact')
return fs.statAsync('src').catch(err => {
return fs.statAsync('src').catch((err) => {
t.ok(err, 'src read error')
t.equal(err.code, 'ENOENT', 'src file still deleted')
})
Expand Down Expand Up @@ -74,7 +74,7 @@ test('does not error if destination file is open', function (t) {
return fs.readFileAsync('dest', 'utf8')
}).then(data => {
t.equal(data, 'bar', 'destination left intact')
return fs.statAsync('src').catch(err => {
return fs.statAsync('src').catch((err) => {
t.ok(err, 'src read error')
t.equal(err.code, 'ENOENT', 'src does not exist')
})
Expand All @@ -93,7 +93,7 @@ test('errors if dest is not writable', {
return fs.chmodAsync('dest', parseInt('400', 8)).then(() => {
return moveFile('src', path.join('dest', 'file')).then(() => {
throw new Error('move succeeded and should not have')
}).catch(err => {
}).catch((err) => {
t.ok(err, 'error was returned')
t.equal(err.code, 'EACCES', 'error is about permissions')
return fs.readFileAsync('src', 'utf8')
Expand Down
8 changes: 6 additions & 2 deletions test/verify.js
Expand Up @@ -142,8 +142,12 @@ test('removes corrupted content', t => {
}, 'reported correct collection counts')
return fs.statAsync(cpath).then(() => {
throw new Error('expected a failure')
}).catch({ code: 'ENOENT' }, err => {
t.match(err.message, /no such file/, 'content no longer in cache')
}).catch((err) => {
if (err.code === 'ENOENT') {
t.match(err.message, /no such file/, 'content no longer in cache')
return
}
throw err
})
})
})
Expand Down

0 comments on commit 28aeeac

Please sign in to comment.