diff --git a/get.js b/get.js index ef62507..13f0576 100644 --- a/get.js +++ b/get.js @@ -1,39 +1,36 @@ 'use strict' -var index = require('./lib/entry-index') -var finished = require('mississippi').finished -var pipe = require('mississippi').pipe -var read = require('./lib/content/read') -var through = require('mississippi').through +const Promise = require('bluebird') -module.exports = function get (cache, key, opts, cb) { - return getData(false, cache, key, opts, cb) +const index = require('./lib/entry-index') +const finished = Promise.promisify(require('mississippi').finished) +const pipe = require('mississippi').pipe +const read = require('./lib/content/read') +const through = require('mississippi').through + +module.exports = function get (cache, key, opts) { + return getData(false, cache, key, opts) } -module.exports.byDigest = function getByDigest (cache, digest, opts, cb) { - return getData(true, cache, digest, opts, cb) +module.exports.byDigest = function getByDigest (cache, digest, opts) { + return getData(true, cache, digest, opts) } -function getData (byDigest, cache, key, opts, cb) { - if (!cb) { - cb = opts - opts = null - } +function getData (byDigest, cache, key, opts) { opts = opts || {} - var src = (byDigest ? getStream.byDigest : getStream)(cache, key, opts) - var data = '' - var meta + const src = (byDigest ? getStream.byDigest : getStream)(cache, key, opts) + let data = '' + let meta src.on('data', function (d) { data += d }) src.on('metadata', function (m) { meta = m }) - finished(src, function (err) { - cb(err, data, meta) - }) + return finished(src).then(() => ({ data, meta })) } module.exports.stream = getStream module.exports.stream.byDigest = read.readStream function getStream (cache, key, opts) { - var stream = through() - index.find(cache, key, function (err, data) { - if (err) { return stream.emit('error', err) } + const stream = through() + index.find(cache, key).catch(err => { + stream.emit('error', err) + }).then(data => { if (!data) { return stream.emit( 'error', index.notFoundError(cache, key) @@ -52,6 +49,6 @@ function getStream (cache, key, opts) { } module.exports.info = info -function info (cache, key, cb) { - index.find(cache, key, cb) +function info (cache, key) { + return index.find(cache, key) } diff --git a/put.js b/put.js index beeb7cb..5fec78d 100644 --- a/put.js +++ b/put.js @@ -1,10 +1,12 @@ 'use strict' -var index = require('./lib/entry-index') -var pipe = require('mississippi').pipe -var putContent = require('./lib/content/put-stream') -var through = require('mississippi').through -var to = require('mississippi').to +const Promise = require('bluebird') + +const index = require('./lib/entry-index') +const pipe = Promise.promisify(require('mississippi').pipe) +const putContent = require('./lib/content/put-stream') +const through = require('mississippi').through +const to = require('mississippi').to module.exports = putData function putData (cache, key, data, opts, cb) { @@ -13,32 +15,30 @@ function putData (cache, key, data, opts, cb) { opts = null } opts = opts || {} - var src = through() - var meta - var dest = putStream(cache, key, opts) + const src = through() + let meta + const dest = putStream(cache, key, opts) dest.on('metadata', function (m) { meta = m }) - pipe(src, dest, function (err) { - cb(err, meta) - }) + const ret = pipe(src, dest).then(() => meta) src.write(data, function () { src.end() }) + return ret } module.exports.stream = putStream function putStream (cache, key, opts) { opts = opts || {} - var digest - var contentStream = putContent(cache, opts).on('digest', function (d) { + let digest + const contentStream = putContent(cache, opts).on('digest', function (d) { digest = d }) - var errored = false - var stream = to(function (chunk, enc, cb) { + let errored = false + const stream = to(function (chunk, enc, cb) { contentStream.write(chunk, enc, cb) }, function (cb) { contentStream.end(function () { - index.insert(cache, key, digest, opts, function (err, entry) { - if (err) { return cb(err) } + index.insert(cache, key, digest, opts).then(entry => { stream.emit('digest', digest) stream.emit('metadata', entry) cb() diff --git a/rm.js b/rm.js index 1a648b7..16de2b8 100644 --- a/rm.js +++ b/rm.js @@ -1,20 +1,22 @@ 'use strict' -var rmContent = require('./lib/content/rm') -var index = require('./lib/entry-index') -var rimraf = require('rimraf') +const Promise = require('bluebird') + +const rmContent = require('./lib/content/rm') +const index = require('./lib/entry-index') +const rimraf = Promise.promisify(require('rimraf')) module.exports.all = all -function all (cache, cb) { - rimraf(cache, cb) +function all (cache) { + return rimraf(cache) } module.exports.entry = entry -function entry (cache, key, cb) { - index.delete(cache, key, cb) +function entry (cache, key) { + return index.delete(cache, key) } module.exports.content = content -function content (cache, address, cb) { - rmContent(cache, address, cb) +function content (cache, address) { + return rmContent(cache, address) }