-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Switches to a Promise-based API and removes callback stuff
- Loading branch information
Showing
1 changed file
with
21 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} |