Skip to content

Commit 92ae1ce

Browse files
committed
chore: refactor to async/await
1 parent 0fd13d1 commit 92ae1ce

28 files changed

+1119
-1662
lines changed

README.md

+40-53
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,12 @@ npm install --save ipfsd-ctl
4040
const IPFSFactory = require('ipfsd-ctl')
4141
const f = IPFSFactory.create()
4242

43-
f.spawn(function (err, ipfsd) {
44-
if (err) { throw err }
43+
const ipfs = await f.spawn()
44+
const id = await ipfsd.api.id()
4545

46-
ipfsd.api.id(function (err, id) {
47-
if (err) { throw err }
46+
console.log(id)
4847

49-
console.log(id)
50-
ipfsd.stop()
51-
})
52-
})
48+
await ipfsd.stop()
5349
```
5450

5551
**Spawn an IPFS daemon from the Browser using the provided remote endpoint**
@@ -64,20 +60,14 @@ const port = 9090
6460
const server = IPFSFactory.createServer(port)
6561
const f = IPFSFactory.create({ remote: true, port: port })
6662

67-
server.start((err) => {
68-
if (err) { throw err }
63+
await server.start()
64+
const ipfsd = await f.spawn()
65+
const id = await ipfsd.api.id()
6966

70-
f.spawn((err, ipfsd) => {
71-
if (err) { throw err }
67+
console.log(id)
7268

73-
ipfsd.api.id(function (err, id) {
74-
if (err) { throw err }
75-
76-
console.log(id)
77-
ipfsd.stop(server.stop)
78-
})
79-
})
80-
})
69+
await ipfsd.stop()
70+
await server.stop()
8171
```
8272

8373
## Disposable vs non Disposable nodes
@@ -112,7 +102,7 @@ Install one or both of the following modules:
112102

113103
**example:** See [Usage](#usage)
114104

115-
#### Spawn a daemon with `f.spawn([options], callback)`
105+
#### Spawn a daemon with `f.spawn([options]) : Promise`
116106

117107
Spawn the daemon
118108

@@ -126,14 +116,14 @@ Spawn the daemon
126116
- `args` - array of cmd line arguments to be passed to ipfs daemon
127117
- `config` - ipfs configuration options
128118

129-
- `callback` - is a function with the signature `function (err, ipfsd)` where:
130-
- `err` - is the error set if spawning the node is unsuccessful
131-
- `ipfsd` - is the daemon controller instance:
132-
- `api` - a property of `ipfsd`, an instance of [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client) attached to the newly created ipfs node
119+
Returns a promise that resolves to:
120+
121+
- `ipfsd` - is the daemon controller instance:
122+
- `api` - a property of `ipfsd`, an instance of [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client) attached to the newly created ipfs node
133123

134124
**example:** See [Usage](#usage)
135125

136-
#### Get daemon version with `f.version(callback)`
126+
#### Get daemon version with `f.version() : Promise`
137127

138128
Get the version without spawning a daemon
139129

@@ -158,17 +148,13 @@ const IPFSFactory = require('ipfsd-ctl')
158148

159149
const server = IPFSFactory.createServer({ port: 12345 })
160150

161-
server.start((err) => {
162-
if (err) { throw err }
151+
await server.start()
163152

164-
console.log('endpoint is running')
153+
console.log('endpoint is running')
165154

166-
server.stop((err) => {
167-
if (err) { throw err }
155+
await server.stop()
168156

169-
console.log('endpoint has stopped')
170-
})
171-
})
157+
console.log('endpoint has stopped')
172158
```
173159

174160
### IPFS Daemon Controller - `ipfsd`
@@ -191,7 +177,7 @@ Get the current repo path. Returns string.
191177

192178
Is the node started. Returns a boolean.
193179

194-
#### `init([initOpts], callback)`
180+
#### `init([initOpts]) : Promise`
195181

196182
Initialize a repo.
197183

@@ -200,68 +186,69 @@ Initialize a repo.
200186
- `directory` (default IPFS_PATH if defined, or ~/.ipfs for go-ipfs and ~/.jsipfs for js-ipfs) - The location of the repo.
201187
- `pass` (optional) - The passphrase of the key chain.
202188

203-
`callback` is a function with the signature `function (err, ipfsd)` where `err` is an Error in case something goes wrong and `ipfsd` is the daemon controller instance.
189+
Returns a promise that resolves to a daemon controller instance.
204190

205-
#### `ipfsd.cleanup(callback)`
191+
#### `ipfsd.cleanup() : Promise`
206192

207193
Delete the repo that was being used. If the node was marked as `disposable` this will be called automatically when the process is exited.
208194

209-
`callback` is a function with the signature `function(err)`.
195+
Returns a promise that resolves when the cleanup is complete.
210196

211-
#### `ipfsd.start(flags, callback)`
197+
#### `ipfsd.start(flags) : Promise`
212198

213199
Start the daemon.
214200

215201
`flags` - Flags array to be passed to the `ipfs daemon` command.
216202

217-
`callback` is a function with the signature `function(err, ipfsClient)` that receives an instance of `Error` on failure or an instance of `ipfs-http-client` on success.
203+
Returns a promiset hat resolves to an instance of `ipfs-http-client`.
218204

219-
220-
#### `ipfsd.stop([timeout, callback])`
205+
#### `ipfsd.stop([timeout]) : Promise`
221206

222207
Stop the daemon.
223208

224-
`callback` is a function with the signature `function(err)` callback - function that receives an instance of `Error` on failure. Use timeout to specify the grace period in ms before hard stopping the daemon. Otherwise, a grace period of `10500` ms will be used for disposable nodes and `10500 * 3` ms for non disposable nodes.
209+
Use `timeout` to specify the grace period in ms before hard stopping the daemon. Otherwise, a grace period of `10500` ms will be used for disposable nodes and `10500 * 3` ms for non disposable nodes.
210+
211+
Returns a promise that resolves when the daemon has stopped.
225212

226-
#### `ipfsd.killProcess([timeout, callback])`
213+
#### `ipfsd.killProcess([timeout]) : Promise`
227214

228215
Kill the `ipfs daemon` process. Use timeout to specify the grace period in ms before hard stopping the daemon. Otherwise, a grace period of `10500` ms will be used for disposable nodes and `10500 * 3` ms for non disposable nodes.
229216

230217
Note: timeout is ignored for `proc` nodes
231218

232219
First a `SIGTERM` is sent, after 10.5 seconds `SIGKILL` is sent if the process hasn't exited yet.
233220

234-
`callback` is a function with the signature `function()` called once the process is killed
221+
Returns a promise that resolves once the process is killed
235222

236-
#### `ipfsd.pid(callback)`
223+
#### `ipfsd.pid() : Promise`
237224

238225
Get the pid of the `ipfs daemon` process. Returns the pid number
239226

240-
`callback` is a function with the signature `function(err, pid)` that receives the `pid` of the running daemon or an `Error` instance on failure
227+
Returns a promiset that resolves to the `pid` of the running daemon.
241228

242-
#### `ipfsd.getConfig([key], callback)`
229+
#### `ipfsd.getConfig([key]) : Promise`
243230

244231
Returns the output of an `ipfs config` command. If no `key` is passed, the whole config is returned as an object.
245232

246233
`key` (optional) - A specific config to retrieve.
247234

248-
`callback` is a function with the signature `function(err, (Object|string))` that receives an object or string on success or an `Error` instance on failure
235+
Returns a promise that resolves to `Object|string` on success.
249236

250-
#### `ipfsd.setConfig(key, value, callback)`
237+
#### `ipfsd.setConfig(key, value) : Promise`
251238

252239
Set a config value.
253240

254241
`key` - the key of the config entry to change/set
255242

256243
`value` - the config value to change/set
257244

258-
`callback` is a function with the signature `function(err)` callback - function that receives an `Error` instance on failure
245+
Returns a promise that resolves on success.
259246

260-
#### `ipfsd.version(callback)`
247+
#### `ipfsd.version() : Promise`
261248

262249
Get the version of ipfs
263250

264-
`callback` is a function with the signature `function(err, version)`
251+
Returns a promise that resolves to the `version`
265252

266253
### IPFS HTTP Client - `ipfsd.api`
267254

package.json

+7-13
Original file line numberDiff line numberDiff line change
@@ -61,40 +61,34 @@
6161
"daemon"
6262
],
6363
"dependencies": {
64-
"async": "^2.6.2",
65-
"base-x": "^3.0.5",
6664
"boom": "^7.3.0",
6765
"debug": "^4.1.1",
6866
"detect-node": "^2.0.4",
6967
"dexie": "^2.0.4",
70-
"execa": "^1.0.0",
71-
"hapi": "^16.6.2",
68+
"execa": "^2.0.2",
69+
"fs-extra": "^8.1.0",
70+
"hapi": "^18.1.0",
7271
"hat": "~0.0.3",
73-
"ipfs-http-client": "ipfs/js-ipfs-http-client#master",
72+
"ipfs-http-client": "^32.0.1",
7473
"joi": "^14.3.1",
75-
"libp2p-crypto": "~0.16.1",
7674
"lodash.clone": "^4.5.0",
7775
"lodash.defaults": "^4.2.0",
7876
"lodash.defaultsdeep": "^4.6.0",
7977
"multiaddr": "^6.1.0",
80-
"once": "^1.4.0",
81-
"protons": "^1.0.1",
82-
"rimraf": "^2.6.3",
83-
"safe-json-parse": "^4.0.0",
8478
"safe-json-stringify": "^1.2.0",
8579
"superagent": "^5.0.5"
8680
},
8781
"devDependencies": {
8882
"aegir": "^19.0.3",
8983
"chai": "^4.2.0",
84+
"delay": "^4.3.0",
9085
"detect-port": "^1.3.0",
9186
"dirty-chai": "^2.0.1",
9287
"go-ipfs-dep": "~0.4.21",
93-
"husky": "^2.4.1",
88+
"husky": "^3.0.0",
9489
"ipfs": "~0.36.3",
9590
"is-running": "^2.1.0",
96-
"lint-staged": "^8.1.7",
97-
"mkdirp": "~0.5.1",
91+
"lint-staged": "^9.0.2",
9892
"proxyquire": "^2.1.0",
9993
"superagent-mocker": "~0.5.2"
10094
},

0 commit comments

Comments
 (0)