1
1
'use strict'
2
2
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' )
8
4
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 )
11
13
}
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 )
14
16
}
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 ) {
20
18
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
24
22
src . on ( 'data' , function ( d ) { data += d } )
25
23
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 } ) )
29
25
}
30
26
31
27
module . exports . stream = getStream
32
28
module . exports . stream . byDigest = read . readStream
33
29
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 => {
37
34
if ( ! data ) {
38
35
return stream . emit (
39
36
'error' , index . notFoundError ( cache , key )
@@ -52,6 +49,6 @@ function getStream (cache, key, opts) {
52
49
}
53
50
54
51
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 )
57
54
}
0 commit comments