Skip to content

Commit

Permalink
feat(read): switched to promises
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Switches to a Promise-based API and removes callback stuff
  • Loading branch information
zkat committed Feb 27, 2017
1 parent b9797c5 commit a869362
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions lib/content/read.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
'use strict'

var checksumStream = require('checksum-stream')
var contentPath = require('./path')
var dezalgo = require('dezalgo')
var fs = require('graceful-fs')
var pipe = require('mississippi').pipe
const Promise = require('bluebird')

const checksumStream = require('checksum-stream')
const contentPath = require('./path')
const fs = require('graceful-fs')
const pipe = require('mississippi').pipe

Promise.promisifyAll(fs)

module.exports.readStream = readStream
function readStream (cache, address, opts) {
opts = opts || {}
var stream = checksumStream({
const stream = checksumStream({
digest: address,
algorithm: opts.hashAlgorithm || 'sha1'
})
var cpath = contentPath(cache, address)
hasContent(cache, address, function (err, exists) {
if (err) { return stream.emit('error', err) }
const cpath = contentPath(cache, address)
hasContent(cache, address).then(exists => {
if (!exists) {
err = new Error('content not found')
const err = new Error('content not found')
err.code = 'ENOENT'
err.cache = cache
err.digest = address
return stream.emit('error', err)
} else {
pipe(fs.createReadStream(cpath), stream)
}
}).catch(err => {
stream.emit('error', err)
})
return stream
}

module.exports.hasContent = hasContent
function hasContent (cache, address, cb) {
cb = dezalgo(cb)
if (!address) { return cb(null, false) }
fs.lstat(contentPath(cache, address), function (err) {
if (!address) { return Promise.resolve(false) }
return fs.lstatAsync(
contentPath(cache, address)
).then(() => true).catch(err => {
if (err && err.code === 'ENOENT') {
return cb(null, false)
return Promise.resolve(false)
} else if (err && process.platform === 'win32' && err.code === 'EPERM') {
return cb(null, false)
} else if (err) {
return cb(err)
return Promise.resolve(false)
} else {
return cb(null, true)
throw err
}
})
}

0 comments on commit a869362

Please sign in to comment.