Skip to content

Commit b3356c6

Browse files
committed
refactor: use async-await
This is part of the Awesome Endeavour: Async Iterators: ipfs/js-ipfs#1670
1 parent a616b10 commit b3356c6

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@
4444
"devDependencies": {
4545
"abstract-blob-store": "^3.3.4",
4646
"aegir": "^17.0.1",
47-
"ipfs": "~0.33.0",
47+
"ipfs": "~0.34.4",
4848
"ipfsd-ctl": "~0.40.0",
4949
"tape": "^4.9.1",
5050
"which": "^1.3.1"
5151
},
5252
"dependencies": {
5353
"debug": "^4.1.0",
54-
"ipfs": "~0.33.0",
55-
"ipfs-api": "^26.1.2",
56-
"promisify-es6": "^1.0.3"
54+
"ipfs": "~0.34.4",
55+
"ipfs-http-client": "^30.1.0"
5756
}
5857
}

src/create.js

+13-26
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
43
const IPFS = require('ipfs')
5-
const remote = require('ipfs-api')
4+
const remote = require('ipfs-http-client')
65
const mfs = require('./mfs')
76
const log = require('debug')('ipfs:blob-store:create')
87
const defaultOptions = {
@@ -11,43 +10,31 @@ const defaultOptions = {
1110
baseDir: '/'
1211
}
1312

14-
module.exports = promisify((opts, callback) => {
15-
if (typeof opts === 'function') {
16-
callback = opts
17-
opts = defaultOptions
18-
}
19-
13+
module.exports = async (opts) => {
2014
const options = Object.assign({}, defaultOptions, opts)
2115

2216
if (options.ipfs) {
2317
log('Using pre-configured IPFS instance')
24-
return setImmediate(() => callback(null, mfs(options)))
18+
return mfs(options)
2519
}
2620

2721
if (options.host && options.port) {
2822
log(`Connecting to remote IPFS at ${options.host}:${options.port}`)
2923
options.ipfs = remote(options.host, options.port)
3024

31-
return setImmediate(() => callback(null, mfs(options)))
25+
return mfs(options)
3226
}
3327

3428
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-
})
4129

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+
}
4933

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+
})
5340
}

0 commit comments

Comments
 (0)