1
1
'use strict'
2
2
3
- const promisify = require ( 'promisify-es6' )
4
3
const IPFS = require ( 'ipfs' )
5
- const remote = require ( 'ipfs-api ' )
4
+ const remote = require ( 'ipfs-http-client ' )
6
5
const mfs = require ( './mfs' )
7
6
const log = require ( 'debug' ) ( 'ipfs:blob-store:create' )
8
7
const defaultOptions = {
@@ -11,43 +10,31 @@ const defaultOptions = {
11
10
baseDir : '/'
12
11
}
13
12
14
- module . exports = promisify ( ( opts , callback ) => {
15
- if ( typeof opts === 'function' ) {
16
- callback = opts
17
- opts = defaultOptions
18
- }
19
-
13
+ module . exports = async ( opts ) => {
20
14
const options = Object . assign ( { } , defaultOptions , opts )
21
15
22
16
if ( options . ipfs ) {
23
17
log ( 'Using pre-configured IPFS instance' )
24
- return setImmediate ( ( ) => callback ( null , mfs ( options ) ) )
18
+ return mfs ( options )
25
19
}
26
20
27
21
if ( options . host && options . port ) {
28
22
log ( `Connecting to remote IPFS at ${ options . host } :${ options . port } ` )
29
23
options . ipfs = remote ( options . host , options . port )
30
24
31
- return setImmediate ( ( ) => callback ( null , mfs ( options ) ) )
25
+ return mfs ( options )
32
26
}
33
27
34
28
log ( `Starting an IPFS instance` )
35
- callback = once ( callback )
36
-
37
- options . ipfs = new IPFS ( )
38
- options . ipfs . once ( 'ready' , ( ) => callback ( null , mfs ( options ) ) )
39
- options . ipfs . once ( 'error' , ( error ) => callback ( error ) )
40
- } )
41
29
42
- function once ( cb ) {
43
- let called = false
44
-
45
- return function ( ) {
46
- if ( called ) {
47
- return
48
- }
30
+ options . ipfs = await getIPFSReadyNode ( )
31
+ return mfs ( options )
32
+ }
49
33
50
- called = true
51
- cb . apply ( null , arguments )
52
- }
34
+ function getIPFSReadyNode ( ) {
35
+ return new Promise ( ( resolve , reject ) => {
36
+ const ipfs = new IPFS ( )
37
+ ipfs . once ( 'ready' , ( ) => resolve ( ipfs ) )
38
+ ipfs . once ( 'error' , reject )
39
+ } )
53
40
}
0 commit comments