Skip to content

Commit

Permalink
feat(promise): removed .fromNode, removed .join
Browse files Browse the repository at this point in the history
  • Loading branch information
billatnpm authored and isaacs committed Sep 15, 2019
1 parent 6b995d3 commit 9c457a0
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 166 deletions.
6 changes: 3 additions & 3 deletions lib/entry-index.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ function lsStream (cache) {

module.exports.ls = ls
function ls (cache) {
return BB.fromNode(cb => {
lsStream(cache).on('error', cb).pipe(concat(entries => {
cb(null, entries.reduce((acc, xs) => {
return new Promise((resolve, reject) => {
lsStream(cache).on('error', reject).pipe(concat(entries => {
resolve(entries.reduce((acc, xs) => {
acc[xs.key] = xs
return acc
}, {}))
Expand Down
8 changes: 4 additions & 4 deletions lib/util/move-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ function moveFile (src, dest) {
// content their own way.
//
// Note that, as the name suggests, this strictly only supports file moves.
return BB.fromNode(cb => {
return new Promise((resolve, reject) => {
fs.link(src, dest, err => {
if (err) {
if (err.code === 'EEXIST' || err.code === 'EBUSY') {
// file already exists, so whatever
} else if (err.code === 'EPERM' && process.platform === 'win32') {
// file handle stayed open even past graceful-fs limits
} else {
return cb(err)
return reject(err)
}
}
return cb()
return resolve()
})
}).then(() => {
// content should never change for any reason, so make it read-only
return BB.join(unlink(src), process.platform !== 'win32' && chmod(dest, '0444'))
return Promise.all([unlink(src), process.platform !== 'win32' && chmod(dest, '0444')])
}).catch(() => {
if (!pinflight) { pinflight = require('promise-inflight') }
return pinflight('cacache-move-file:' + dest, () => {
Expand Down
85 changes: 55 additions & 30 deletions test/content.read.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,13 @@ test('read.stream: returns a stream with cache content data', function (t) {
stream.on('error', function (e) { throw e })
let buf = ''
stream.on('data', function (data) { buf += data })
return BB.join(
return Promise.all([
finished(stream).then(() => Buffer.from(buf)),
read(CACHE, INTEGRITY, { size: CONTENT.length }),
(fromStream, fromBulk) => {
read(CACHE, INTEGRITY, { size: CONTENT.length })])
.then(([fromStream, fromBulk]) => {
t.deepEqual(fromStream, CONTENT, 'stream data checks out')
t.deepEqual(fromBulk, CONTENT, 'promise data checks out')
}
)
})
})

test('read: allows hashAlgorithm configuration', function (t) {
Expand All @@ -74,14 +73,13 @@ test('read: allows hashAlgorithm configuration', function (t) {
stream.on('error', function (e) { throw e })
let buf = ''
stream.on('data', function (data) { buf += data })
return BB.join(
return Promise.all([
finished(stream).then(() => Buffer.from(buf)),
read(CACHE, INTEGRITY),
(fromStream, fromBulk) => {
read(CACHE, INTEGRITY)])
.then(([fromStream, fromBulk]) => {
t.deepEqual(fromStream, CONTENT, 'stream used algorithm')
t.deepEqual(fromBulk, CONTENT, 'promise used algorithm')
}
)
})
})

test('read: errors if content missing', function (t) {
Expand All @@ -92,14 +90,23 @@ test('read: errors if content missing', function (t) {
stream.on('end', function () {
throw new Error('end was called even though stream errored')
})
return BB.join(
finished(stream).catch({ code: 'ENOENT' }, err => err),
read(CACHE, 'sha512-whatnot').catch({ code: 'ENOENT' }, err => err),
(streamErr, bulkErr) => {
return Promise.all([
finished(stream).catch((err) => {
if (err.code === 'ENOENT') {
return err
}
throw err
}),
read(CACHE, 'sha512-whatnot').catch((err) => {
if (err.code === 'ENOENT') {
return err
}
throw err
})])
.then(([streamErr, bulkErr]) => {
t.equal(streamErr.code, 'ENOENT', 'stream got the right error')
t.equal(bulkErr.code, 'ENOENT', 'bulk got the right error')
}
)
})
})

test('read: errors if content fails checksum', function (t) {
Expand All @@ -113,14 +120,23 @@ test('read: errors if content fails checksum', function (t) {
stream.on('end', function () {
throw new Error('end was called even though stream errored')
})
return BB.join(
finished(stream).catch({ code: 'EINTEGRITY' }, err => err),
read(CACHE, INTEGRITY).catch({ code: 'EINTEGRITY' }, err => err),
(streamErr, bulkErr) => {
return Promise.all([
finished(stream).catch((err) => {
if (err.code === 'EINTEGRITY') {
return err
}
throw err
}),
read(CACHE, INTEGRITY).catch((err) => {
if (err.code === 'EINTEGRITY') {
return err
}
throw err
})])
.then(([streamErr, bulkErr]) => {
t.equal(streamErr.code, 'EINTEGRITY', 'stream got the right error')
t.equal(bulkErr.code, 'EINTEGRITY', 'bulk got the right error')
}
)
})
})

test('read: errors if content size does not match size option', function (t) {
Expand All @@ -134,24 +150,33 @@ test('read: errors if content size does not match size option', function (t) {
stream.on('end', function () {
throw new Error('end was called even though stream errored')
})
return BB.join(
finished(stream).catch({ code: 'EBADSIZE' }, err => err),
return Promise.all([
finished(stream).catch((err) => {
if (err.code === 'EBADSIZE') {
return err
}
throw err
}),
read(CACHE, INTEGRITY, {
size: CONTENT.length
}).catch({ code: 'EBADSIZE' }, err => err),
(streamErr, bulkErr) => {
}).catch((err) => {
if (err.code === 'EBADSIZE') {
return err
}
throw err
})])
.then(([streamErr, bulkErr]) => {
t.equal(streamErr.code, 'EBADSIZE', 'stream got the right error')
t.equal(bulkErr.code, 'EBADSIZE', 'bulk got the right error')
}
)
})
})

test('hasContent: tests content existence', t => {
const fixture = new Tacks(CacheContent({
'sha1-deadbeef': ''
}))
fixture.create(CACHE)
return BB.join(
return Promise.all([
read.hasContent(CACHE, 'sha1-deadbeef')
.then(content => {
t.ok(content.sri, 'returned sri for this content')
Expand All @@ -166,7 +191,7 @@ test('hasContent: tests content existence', t => {
.then(content => {
t.equal(content, false, 'multi-content hash failures work ok')
})
)
])
})

test('hasContent.sync: checks content existence synchronously', t => {
Expand Down
96 changes: 46 additions & 50 deletions test/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,22 @@ test('basic stream get', t => {
}))
fixture.create(CACHE)
return index.insert(CACHE, KEY, INTEGRITY, opts()).then(() => {
return BB.join(
return Promise.all([
streamGet(false, CACHE, KEY),
streamGet(true, CACHE, INTEGRITY),
(byKey, byDigest) => {
t.deepEqual(byKey, {
data: CONTENT,
integrity: INTEGRITY,
metadata: METADATA,
size: SIZE
}, 'got all expected data and fields from key fetch')
t.deepEqual(
byDigest.data,
CONTENT,
'got correct data from digest fetch'
)
}
)
streamGet(true, CACHE, INTEGRITY)
]).then(([byKey, byDigest]) => {
t.deepEqual(byKey, {
data: CONTENT,
integrity: INTEGRITY,
metadata: METADATA,
size: SIZE
}, 'got all expected data and fields from key fetch')
t.deepEqual(
byDigest.data,
CONTENT,
'got correct data from digest fetch'
)
})
})
})

Expand Down Expand Up @@ -225,18 +224,17 @@ test('memoizes data on stream read', t => {
}))
fixture.create(CACHE)
return index.insert(CACHE, KEY, INTEGRITY, opts()).then(ENTRY => {
return BB.join(
return Promise.all([
streamGet(false, CACHE, KEY),
streamGet(true, CACHE, INTEGRITY),
() => {
t.deepEqual(memo.get(CACHE, KEY), null, 'no memoization by key!')
t.deepEqual(
memo.get.byDigest(CACHE, INTEGRITY),
null,
'no memoization by digest!'
)
}
).then(() => {
streamGet(true, CACHE, INTEGRITY)
]).then(() => {
t.deepEqual(memo.get(CACHE, KEY), null, 'no memoization by key!')
t.deepEqual(
memo.get.byDigest(CACHE, INTEGRITY),
null,
'no memoization by digest!'
)
}).then(() => {
memo.clearMemoized()
return streamGet(true, CACHE, INTEGRITY, {
memoize: true
Expand Down Expand Up @@ -281,36 +279,34 @@ test('memoizes data on stream read', t => {
}).then(() => {
return rimraf(CACHE)
}).then(() => {
return BB.join(
return Promise.all([
streamGet(false, CACHE, KEY),
streamGet(true, CACHE, INTEGRITY),
(byKey, byDigest) => {
t.deepEqual(byKey, {
metadata: METADATA,
data: CONTENT,
integrity: INTEGRITY,
size: SIZE
}, 'key fetch fulfilled by memoization cache')
t.deepEqual(
byDigest.data,
CONTENT,
'digest fetch fulfilled by memoization cache'
)
}
)
streamGet(true, CACHE, INTEGRITY)
]).then(([byKey, byDigest]) => {
t.deepEqual(byKey, {
metadata: METADATA,
data: CONTENT,
integrity: INTEGRITY,
size: SIZE
}, 'key fetch fulfilled by memoization cache')
t.deepEqual(
byDigest.data,
CONTENT,
'digest fetch fulfilled by memoization cache'
)
})
}).then(() => {
return BB.join(
return Promise.all([
streamGet(false, CACHE, KEY, {
memoize: false
}).catch(err => err),
streamGet(true, CACHE, INTEGRITY, {
memoize: false
}).catch(err => err),
(keyErr, digestErr) => {
t.equal(keyErr.code, 'ENOENT', 'key get memoization bypassed')
t.equal(keyErr.code, 'ENOENT', 'digest get memoization bypassed')
}
)
}).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
8 changes: 4 additions & 4 deletions test/index.find.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ test('index.find key case-sensitivity', function (t) {
}
}))
fixture.create(CACHE)
return BB.join(
return Promise.all([
index.find(CACHE, 'JSONStream').then(info => {
t.ok(info, 'found an entry for JSONStream')
t.equal(info.key, 'JSONStream', 'fetched the correct entry')
Expand All @@ -93,7 +93,7 @@ test('index.find key case-sensitivity', function (t) {
index.find(CACHE, 'jsonStream').then(info => {
t.ok(!info, 'no entry for jsonStream')
})
)
])
})

test('index.find path-breaking characters', function (t) {
Expand Down Expand Up @@ -180,8 +180,8 @@ test('index.find garbled data in index file', function (t) {
})
const fixture = new Tacks(CacheIndex({
'whatever': '\n' +
`${index._hashEntry(stringified)}\t${stringified}` +
'\n{"key": "' + key + '"\noway'
`${index._hashEntry(stringified)}\t${stringified}` +
'\n{"key": "' + key + '"\noway'
}))
fixture.create(CACHE)
return index.find(CACHE, key).then(info => {
Expand Down
Loading

0 comments on commit 9c457a0

Please sign in to comment.