Skip to content

Commit a869362

Browse files
committed
feat(read): switched to promises
BREAKING CHANGE: Switches to a Promise-based API and removes callback stuff
1 parent b9797c5 commit a869362

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

Diff for: lib/content/read.js

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
11
'use strict'
22

3-
var checksumStream = require('checksum-stream')
4-
var contentPath = require('./path')
5-
var dezalgo = require('dezalgo')
6-
var fs = require('graceful-fs')
7-
var pipe = require('mississippi').pipe
3+
const Promise = require('bluebird')
4+
5+
const checksumStream = require('checksum-stream')
6+
const contentPath = require('./path')
7+
const fs = require('graceful-fs')
8+
const pipe = require('mississippi').pipe
9+
10+
Promise.promisifyAll(fs)
811

912
module.exports.readStream = readStream
1013
function readStream (cache, address, opts) {
1114
opts = opts || {}
12-
var stream = checksumStream({
15+
const stream = checksumStream({
1316
digest: address,
1417
algorithm: opts.hashAlgorithm || 'sha1'
1518
})
16-
var cpath = contentPath(cache, address)
17-
hasContent(cache, address, function (err, exists) {
18-
if (err) { return stream.emit('error', err) }
19+
const cpath = contentPath(cache, address)
20+
hasContent(cache, address).then(exists => {
1921
if (!exists) {
20-
err = new Error('content not found')
22+
const err = new Error('content not found')
2123
err.code = 'ENOENT'
2224
err.cache = cache
2325
err.digest = address
2426
return stream.emit('error', err)
2527
} else {
2628
pipe(fs.createReadStream(cpath), stream)
2729
}
30+
}).catch(err => {
31+
stream.emit('error', err)
2832
})
2933
return stream
3034
}
3135

3236
module.exports.hasContent = hasContent
3337
function hasContent (cache, address, cb) {
34-
cb = dezalgo(cb)
35-
if (!address) { return cb(null, false) }
36-
fs.lstat(contentPath(cache, address), function (err) {
38+
if (!address) { return Promise.resolve(false) }
39+
return fs.lstatAsync(
40+
contentPath(cache, address)
41+
).then(() => true).catch(err => {
3742
if (err && err.code === 'ENOENT') {
38-
return cb(null, false)
43+
return Promise.resolve(false)
3944
} else if (err && process.platform === 'win32' && err.code === 'EPERM') {
40-
return cb(null, false)
41-
} else if (err) {
42-
return cb(err)
45+
return Promise.resolve(false)
4346
} else {
44-
return cb(null, true)
47+
throw err
4548
}
4649
})
4750
}

0 commit comments

Comments
 (0)