Skip to content

Commit 7bf032f

Browse files
committed
feat(api): converted external api
BREAKING CHANGE: this means we are going all in on promises now
1 parent 1670a49 commit 7bf032f

File tree

3 files changed

+50
-51
lines changed

3 files changed

+50
-51
lines changed

Diff for: get.js

+22-25
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
'use strict'
22

3-
var index = require('./lib/entry-index')
4-
var finished = require('mississippi').finished
5-
var pipe = require('mississippi').pipe
6-
var read = require('./lib/content/read')
7-
var through = require('mississippi').through
3+
const Promise = require('bluebird')
84

9-
module.exports = function get (cache, key, opts, cb) {
10-
return getData(false, cache, key, opts, cb)
5+
const index = require('./lib/entry-index')
6+
const finished = Promise.promisify(require('mississippi').finished)
7+
const pipe = require('mississippi').pipe
8+
const read = require('./lib/content/read')
9+
const through = require('mississippi').through
10+
11+
module.exports = function get (cache, key, opts) {
12+
return getData(false, cache, key, opts)
1113
}
12-
module.exports.byDigest = function getByDigest (cache, digest, opts, cb) {
13-
return getData(true, cache, digest, opts, cb)
14+
module.exports.byDigest = function getByDigest (cache, digest, opts) {
15+
return getData(true, cache, digest, opts)
1416
}
15-
function getData (byDigest, cache, key, opts, cb) {
16-
if (!cb) {
17-
cb = opts
18-
opts = null
19-
}
17+
function getData (byDigest, cache, key, opts) {
2018
opts = opts || {}
21-
var src = (byDigest ? getStream.byDigest : getStream)(cache, key, opts)
22-
var data = ''
23-
var meta
19+
const src = (byDigest ? getStream.byDigest : getStream)(cache, key, opts)
20+
let data = ''
21+
let meta
2422
src.on('data', function (d) { data += d })
2523
src.on('metadata', function (m) { meta = m })
26-
finished(src, function (err) {
27-
cb(err, data, meta)
28-
})
24+
return finished(src).then(() => ({ data, meta }))
2925
}
3026

3127
module.exports.stream = getStream
3228
module.exports.stream.byDigest = read.readStream
3329
function getStream (cache, key, opts) {
34-
var stream = through()
35-
index.find(cache, key, function (err, data) {
36-
if (err) { return stream.emit('error', err) }
30+
const stream = through()
31+
index.find(cache, key).catch(err => {
32+
stream.emit('error', err)
33+
}).then(data => {
3734
if (!data) {
3835
return stream.emit(
3936
'error', index.notFoundError(cache, key)
@@ -52,6 +49,6 @@ function getStream (cache, key, opts) {
5249
}
5350

5451
module.exports.info = info
55-
function info (cache, key, cb) {
56-
index.find(cache, key, cb)
52+
function info (cache, key) {
53+
return index.find(cache, key)
5754
}

Diff for: put.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict'
22

3-
var index = require('./lib/entry-index')
4-
var pipe = require('mississippi').pipe
5-
var putContent = require('./lib/content/put-stream')
6-
var through = require('mississippi').through
7-
var to = require('mississippi').to
3+
const Promise = require('bluebird')
4+
5+
const index = require('./lib/entry-index')
6+
const pipe = Promise.promisify(require('mississippi').pipe)
7+
const putContent = require('./lib/content/put-stream')
8+
const through = require('mississippi').through
9+
const to = require('mississippi').to
810

911
module.exports = putData
1012
function putData (cache, key, data, opts, cb) {
@@ -13,32 +15,30 @@ function putData (cache, key, data, opts, cb) {
1315
opts = null
1416
}
1517
opts = opts || {}
16-
var src = through()
17-
var meta
18-
var dest = putStream(cache, key, opts)
18+
const src = through()
19+
let meta
20+
const dest = putStream(cache, key, opts)
1921
dest.on('metadata', function (m) { meta = m })
20-
pipe(src, dest, function (err) {
21-
cb(err, meta)
22-
})
22+
const ret = pipe(src, dest).then(() => meta)
2323
src.write(data, function () {
2424
src.end()
2525
})
26+
return ret
2627
}
2728

2829
module.exports.stream = putStream
2930
function putStream (cache, key, opts) {
3031
opts = opts || {}
31-
var digest
32-
var contentStream = putContent(cache, opts).on('digest', function (d) {
32+
let digest
33+
const contentStream = putContent(cache, opts).on('digest', function (d) {
3334
digest = d
3435
})
35-
var errored = false
36-
var stream = to(function (chunk, enc, cb) {
36+
let errored = false
37+
const stream = to(function (chunk, enc, cb) {
3738
contentStream.write(chunk, enc, cb)
3839
}, function (cb) {
3940
contentStream.end(function () {
40-
index.insert(cache, key, digest, opts, function (err, entry) {
41-
if (err) { return cb(err) }
41+
index.insert(cache, key, digest, opts).then(entry => {
4242
stream.emit('digest', digest)
4343
stream.emit('metadata', entry)
4444
cb()

Diff for: rm.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
'use strict'
22

3-
var rmContent = require('./lib/content/rm')
4-
var index = require('./lib/entry-index')
5-
var rimraf = require('rimraf')
3+
const Promise = require('bluebird')
4+
5+
const rmContent = require('./lib/content/rm')
6+
const index = require('./lib/entry-index')
7+
const rimraf = Promise.promisify(require('rimraf'))
68

79
module.exports.all = all
8-
function all (cache, cb) {
9-
rimraf(cache, cb)
10+
function all (cache) {
11+
return rimraf(cache)
1012
}
1113

1214
module.exports.entry = entry
13-
function entry (cache, key, cb) {
14-
index.delete(cache, key, cb)
15+
function entry (cache, key) {
16+
return index.delete(cache, key)
1517
}
1618

1719
module.exports.content = content
18-
function content (cache, address, cb) {
19-
rmContent(cache, address, cb)
20+
function content (cache, address) {
21+
return rmContent(cache, address)
2022
}

0 commit comments

Comments
 (0)