Skip to content

Commit 9c457a0

Browse files
billatnpmisaacs
authored andcommitted
feat(promise): removed .fromNode, removed .join
1 parent 6b995d3 commit 9c457a0

File tree

9 files changed

+188
-166
lines changed

9 files changed

+188
-166
lines changed

lib/entry-index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,9 @@ function lsStream (cache) {
190190

191191
module.exports.ls = ls
192192
function ls (cache) {
193-
return BB.fromNode(cb => {
194-
lsStream(cache).on('error', cb).pipe(concat(entries => {
195-
cb(null, entries.reduce((acc, xs) => {
193+
return new Promise((resolve, reject) => {
194+
lsStream(cache).on('error', reject).pipe(concat(entries => {
195+
resolve(entries.reduce((acc, xs) => {
196196
acc[xs.key] = xs
197197
return acc
198198
}, {}))

lib/util/move-file.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ function moveFile (src, dest) {
1818
// content their own way.
1919
//
2020
// Note that, as the name suggests, this strictly only supports file moves.
21-
return BB.fromNode(cb => {
21+
return new Promise((resolve, reject) => {
2222
fs.link(src, dest, err => {
2323
if (err) {
2424
if (err.code === 'EEXIST' || err.code === 'EBUSY') {
2525
// file already exists, so whatever
2626
} else if (err.code === 'EPERM' && process.platform === 'win32') {
2727
// file handle stayed open even past graceful-fs limits
2828
} else {
29-
return cb(err)
29+
return reject(err)
3030
}
3131
}
32-
return cb()
32+
return resolve()
3333
})
3434
}).then(() => {
3535
// content should never change for any reason, so make it read-only
36-
return BB.join(unlink(src), process.platform !== 'win32' && chmod(dest, '0444'))
36+
return Promise.all([unlink(src), process.platform !== 'win32' && chmod(dest, '0444')])
3737
}).catch(() => {
3838
if (!pinflight) { pinflight = require('promise-inflight') }
3939
return pinflight('cacache-move-file:' + dest, () => {

test/content.read.js

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,13 @@ test('read.stream: returns a stream with cache content data', function (t) {
5252
stream.on('error', function (e) { throw e })
5353
let buf = ''
5454
stream.on('data', function (data) { buf += data })
55-
return BB.join(
55+
return Promise.all([
5656
finished(stream).then(() => Buffer.from(buf)),
57-
read(CACHE, INTEGRITY, { size: CONTENT.length }),
58-
(fromStream, fromBulk) => {
57+
read(CACHE, INTEGRITY, { size: CONTENT.length })])
58+
.then(([fromStream, fromBulk]) => {
5959
t.deepEqual(fromStream, CONTENT, 'stream data checks out')
6060
t.deepEqual(fromBulk, CONTENT, 'promise data checks out')
61-
}
62-
)
61+
})
6362
})
6463

6564
test('read: allows hashAlgorithm configuration', function (t) {
@@ -74,14 +73,13 @@ test('read: allows hashAlgorithm configuration', function (t) {
7473
stream.on('error', function (e) { throw e })
7574
let buf = ''
7675
stream.on('data', function (data) { buf += data })
77-
return BB.join(
76+
return Promise.all([
7877
finished(stream).then(() => Buffer.from(buf)),
79-
read(CACHE, INTEGRITY),
80-
(fromStream, fromBulk) => {
78+
read(CACHE, INTEGRITY)])
79+
.then(([fromStream, fromBulk]) => {
8180
t.deepEqual(fromStream, CONTENT, 'stream used algorithm')
8281
t.deepEqual(fromBulk, CONTENT, 'promise used algorithm')
83-
}
84-
)
82+
})
8583
})
8684

8785
test('read: errors if content missing', function (t) {
@@ -92,14 +90,23 @@ test('read: errors if content missing', function (t) {
9290
stream.on('end', function () {
9391
throw new Error('end was called even though stream errored')
9492
})
95-
return BB.join(
96-
finished(stream).catch({ code: 'ENOENT' }, err => err),
97-
read(CACHE, 'sha512-whatnot').catch({ code: 'ENOENT' }, err => err),
98-
(streamErr, bulkErr) => {
93+
return Promise.all([
94+
finished(stream).catch((err) => {
95+
if (err.code === 'ENOENT') {
96+
return err
97+
}
98+
throw err
99+
}),
100+
read(CACHE, 'sha512-whatnot').catch((err) => {
101+
if (err.code === 'ENOENT') {
102+
return err
103+
}
104+
throw err
105+
})])
106+
.then(([streamErr, bulkErr]) => {
99107
t.equal(streamErr.code, 'ENOENT', 'stream got the right error')
100108
t.equal(bulkErr.code, 'ENOENT', 'bulk got the right error')
101-
}
102-
)
109+
})
103110
})
104111

105112
test('read: errors if content fails checksum', function (t) {
@@ -113,14 +120,23 @@ test('read: errors if content fails checksum', function (t) {
113120
stream.on('end', function () {
114121
throw new Error('end was called even though stream errored')
115122
})
116-
return BB.join(
117-
finished(stream).catch({ code: 'EINTEGRITY' }, err => err),
118-
read(CACHE, INTEGRITY).catch({ code: 'EINTEGRITY' }, err => err),
119-
(streamErr, bulkErr) => {
123+
return Promise.all([
124+
finished(stream).catch((err) => {
125+
if (err.code === 'EINTEGRITY') {
126+
return err
127+
}
128+
throw err
129+
}),
130+
read(CACHE, INTEGRITY).catch((err) => {
131+
if (err.code === 'EINTEGRITY') {
132+
return err
133+
}
134+
throw err
135+
})])
136+
.then(([streamErr, bulkErr]) => {
120137
t.equal(streamErr.code, 'EINTEGRITY', 'stream got the right error')
121138
t.equal(bulkErr.code, 'EINTEGRITY', 'bulk got the right error')
122-
}
123-
)
139+
})
124140
})
125141

126142
test('read: errors if content size does not match size option', function (t) {
@@ -134,24 +150,33 @@ test('read: errors if content size does not match size option', function (t) {
134150
stream.on('end', function () {
135151
throw new Error('end was called even though stream errored')
136152
})
137-
return BB.join(
138-
finished(stream).catch({ code: 'EBADSIZE' }, err => err),
153+
return Promise.all([
154+
finished(stream).catch((err) => {
155+
if (err.code === 'EBADSIZE') {
156+
return err
157+
}
158+
throw err
159+
}),
139160
read(CACHE, INTEGRITY, {
140161
size: CONTENT.length
141-
}).catch({ code: 'EBADSIZE' }, err => err),
142-
(streamErr, bulkErr) => {
162+
}).catch((err) => {
163+
if (err.code === 'EBADSIZE') {
164+
return err
165+
}
166+
throw err
167+
})])
168+
.then(([streamErr, bulkErr]) => {
143169
t.equal(streamErr.code, 'EBADSIZE', 'stream got the right error')
144170
t.equal(bulkErr.code, 'EBADSIZE', 'bulk got the right error')
145-
}
146-
)
171+
})
147172
})
148173

149174
test('hasContent: tests content existence', t => {
150175
const fixture = new Tacks(CacheContent({
151176
'sha1-deadbeef': ''
152177
}))
153178
fixture.create(CACHE)
154-
return BB.join(
179+
return Promise.all([
155180
read.hasContent(CACHE, 'sha1-deadbeef')
156181
.then(content => {
157182
t.ok(content.sri, 'returned sri for this content')
@@ -166,7 +191,7 @@ test('hasContent: tests content existence', t => {
166191
.then(content => {
167192
t.equal(content, false, 'multi-content hash failures work ok')
168193
})
169-
)
194+
])
170195
})
171196

172197
test('hasContent.sync: checks content existence synchronously', t => {

test/get.js

Lines changed: 46 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -104,23 +104,22 @@ test('basic stream get', t => {
104104
}))
105105
fixture.create(CACHE)
106106
return index.insert(CACHE, KEY, INTEGRITY, opts()).then(() => {
107-
return BB.join(
107+
return Promise.all([
108108
streamGet(false, CACHE, KEY),
109-
streamGet(true, CACHE, INTEGRITY),
110-
(byKey, byDigest) => {
111-
t.deepEqual(byKey, {
112-
data: CONTENT,
113-
integrity: INTEGRITY,
114-
metadata: METADATA,
115-
size: SIZE
116-
}, 'got all expected data and fields from key fetch')
117-
t.deepEqual(
118-
byDigest.data,
119-
CONTENT,
120-
'got correct data from digest fetch'
121-
)
122-
}
123-
)
109+
streamGet(true, CACHE, INTEGRITY)
110+
]).then(([byKey, byDigest]) => {
111+
t.deepEqual(byKey, {
112+
data: CONTENT,
113+
integrity: INTEGRITY,
114+
metadata: METADATA,
115+
size: SIZE
116+
}, 'got all expected data and fields from key fetch')
117+
t.deepEqual(
118+
byDigest.data,
119+
CONTENT,
120+
'got correct data from digest fetch'
121+
)
122+
})
124123
})
125124
})
126125

@@ -225,18 +224,17 @@ test('memoizes data on stream read', t => {
225224
}))
226225
fixture.create(CACHE)
227226
return index.insert(CACHE, KEY, INTEGRITY, opts()).then(ENTRY => {
228-
return BB.join(
227+
return Promise.all([
229228
streamGet(false, CACHE, KEY),
230-
streamGet(true, CACHE, INTEGRITY),
231-
() => {
232-
t.deepEqual(memo.get(CACHE, KEY), null, 'no memoization by key!')
233-
t.deepEqual(
234-
memo.get.byDigest(CACHE, INTEGRITY),
235-
null,
236-
'no memoization by digest!'
237-
)
238-
}
239-
).then(() => {
229+
streamGet(true, CACHE, INTEGRITY)
230+
]).then(() => {
231+
t.deepEqual(memo.get(CACHE, KEY), null, 'no memoization by key!')
232+
t.deepEqual(
233+
memo.get.byDigest(CACHE, INTEGRITY),
234+
null,
235+
'no memoization by digest!'
236+
)
237+
}).then(() => {
240238
memo.clearMemoized()
241239
return streamGet(true, CACHE, INTEGRITY, {
242240
memoize: true
@@ -281,36 +279,34 @@ test('memoizes data on stream read', t => {
281279
}).then(() => {
282280
return rimraf(CACHE)
283281
}).then(() => {
284-
return BB.join(
282+
return Promise.all([
285283
streamGet(false, CACHE, KEY),
286-
streamGet(true, CACHE, INTEGRITY),
287-
(byKey, byDigest) => {
288-
t.deepEqual(byKey, {
289-
metadata: METADATA,
290-
data: CONTENT,
291-
integrity: INTEGRITY,
292-
size: SIZE
293-
}, 'key fetch fulfilled by memoization cache')
294-
t.deepEqual(
295-
byDigest.data,
296-
CONTENT,
297-
'digest fetch fulfilled by memoization cache'
298-
)
299-
}
300-
)
284+
streamGet(true, CACHE, INTEGRITY)
285+
]).then(([byKey, byDigest]) => {
286+
t.deepEqual(byKey, {
287+
metadata: METADATA,
288+
data: CONTENT,
289+
integrity: INTEGRITY,
290+
size: SIZE
291+
}, 'key fetch fulfilled by memoization cache')
292+
t.deepEqual(
293+
byDigest.data,
294+
CONTENT,
295+
'digest fetch fulfilled by memoization cache'
296+
)
297+
})
301298
}).then(() => {
302-
return BB.join(
299+
return Promise.all([
303300
streamGet(false, CACHE, KEY, {
304301
memoize: false
305302
}).catch(err => err),
306303
streamGet(true, CACHE, INTEGRITY, {
307304
memoize: false
308-
}).catch(err => err),
309-
(keyErr, digestErr) => {
310-
t.equal(keyErr.code, 'ENOENT', 'key get memoization bypassed')
311-
t.equal(keyErr.code, 'ENOENT', 'digest get memoization bypassed')
312-
}
313-
)
305+
}).catch(err => err)
306+
]).then(([keyErr, digestErr]) => {
307+
t.equal(keyErr.code, 'ENOENT', 'key get memoization bypassed')
308+
t.equal(keyErr.code, 'ENOENT', 'digest get memoization bypassed')
309+
})
314310
})
315311
})
316312
})

test/index.find.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ test('index.find key case-sensitivity', function (t) {
8181
}
8282
}))
8383
fixture.create(CACHE)
84-
return BB.join(
84+
return Promise.all([
8585
index.find(CACHE, 'JSONStream').then(info => {
8686
t.ok(info, 'found an entry for JSONStream')
8787
t.equal(info.key, 'JSONStream', 'fetched the correct entry')
@@ -93,7 +93,7 @@ test('index.find key case-sensitivity', function (t) {
9393
index.find(CACHE, 'jsonStream').then(info => {
9494
t.ok(!info, 'no entry for jsonStream')
9595
})
96-
)
96+
])
9797
})
9898

9999
test('index.find path-breaking characters', function (t) {
@@ -180,8 +180,8 @@ test('index.find garbled data in index file', function (t) {
180180
})
181181
const fixture = new Tacks(CacheIndex({
182182
'whatever': '\n' +
183-
`${index._hashEntry(stringified)}\t${stringified}` +
184-
'\n{"key": "' + key + '"\noway'
183+
`${index._hashEntry(stringified)}\t${stringified}` +
184+
'\n{"key": "' + key + '"\noway'
185185
}))
186186
fixture.create(CACHE)
187187
return index.find(CACHE, key).then(info => {

0 commit comments

Comments
 (0)