Skip to content

Commit

Permalink
refactor(read): open the content file directly
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 3, 2017
1 parent 2956ec3 commit 32d75e4
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions lib/content/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,21 @@ const Promise = require('bluebird')
const checksumStream = require('checksum-stream')
const contentPath = require('./path')
const fs = require('graceful-fs')
const pipe = require('mississippi').pipe
const pipeline = require('mississippi').pipeline

Promise.promisifyAll(fs)

module.exports.readStream = readStream
function readStream (cache, address, opts) {
opts = opts || {}
const stream = checksumStream({
digest: address,
algorithm: opts.hashAlgorithm || 'sha512'
})
const cpath = contentPath(cache, address, opts.hashAlgorithm || 'sha512')
hasContent(cache, address, opts.hashAlgorithm).then(exists => {
if (!exists) {
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
return pipeline(
fs.createReadStream(cpath), checksumStream({
digest: address,
algorithm: opts.hashAlgorithm || 'sha512',
size: opts.size
})
)
}

module.exports.hasContent = hasContent
Expand Down

0 comments on commit 32d75e4

Please sign in to comment.