Skip to content
This repository has been archived by the owner on Jul 3, 2019. It is now read-only.

Commit

Permalink
feat(api): converted external api
Browse files Browse the repository at this point in the history
BREAKING CHANGE: this means we are going all in on promises now
  • Loading branch information
zkat committed Feb 27, 2017
1 parent 1670a49 commit 7bf032f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 51 deletions.
47 changes: 22 additions & 25 deletions get.js
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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)
}
34 changes: 17 additions & 17 deletions put.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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()
Expand Down
20 changes: 11 additions & 9 deletions rm.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 7bf032f

Please sign in to comment.