Skip to content

Commit

Permalink
chore: refactor to async/await
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jul 4, 2019
1 parent 0fd13d1 commit b7d8faf
Show file tree
Hide file tree
Showing 29 changed files with 1,218 additions and 1,659 deletions.
10 changes: 8 additions & 2 deletions .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ module.exports = {
},
hooks: {
browser: {
pre: server.start.bind(server),
post: server.stop.bind(server)
pre: (callback) => {
server.start()
.then(() => callback(), callback)
},
post: (callback) => {
server.stop()
.then(() => callback(), callback)
}
}
}
}
93 changes: 40 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,12 @@ npm install --save ipfsd-ctl
const IPFSFactory = require('ipfsd-ctl')
const f = IPFSFactory.create()

f.spawn(function (err, ipfsd) {
if (err) { throw err }
const ipfs = await f.spawn()
const id = await ipfsd.api.id()

ipfsd.api.id(function (err, id) {
if (err) { throw err }
console.log(id)

console.log(id)
ipfsd.stop()
})
})
await ipfsd.stop()
```

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

server.start((err) => {
if (err) { throw err }
await server.start()
const ipfsd = await f.spawn()
const id = await ipfsd.api.id()

f.spawn((err, ipfsd) => {
if (err) { throw err }
console.log(id)

ipfsd.api.id(function (err, id) {
if (err) { throw err }

console.log(id)
ipfsd.stop(server.stop)
})
})
})
await ipfsd.stop()
await server.stop()
```

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

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

#### Spawn a daemon with `f.spawn([options], callback)`
#### Spawn a daemon with `f.spawn([options]) : Promise`

Spawn the daemon

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

- `callback` - is a function with the signature `function (err, ipfsd)` where:
- `err` - is the error set if spawning the node is unsuccessful
- `ipfsd` - is the daemon controller instance:
- `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
Returns a promise that resolves to:

- `ipfsd` - is the daemon controller instance:
- `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

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

#### Get daemon version with `f.version(callback)`
#### Get daemon version with `f.version() : Promise`

Get the version without spawning a daemon

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

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

server.start((err) => {
if (err) { throw err }
await server.start()

console.log('endpoint is running')
console.log('endpoint is running')

server.stop((err) => {
if (err) { throw err }
await server.stop()

console.log('endpoint has stopped')
})
})
console.log('endpoint has stopped')
```

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

Is the node started. Returns a boolean.

#### `init([initOpts], callback)`
#### `init([initOpts]) : Promise`

Initialize a repo.

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

`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.
Returns a promise that resolves to a daemon controller instance.

#### `ipfsd.cleanup(callback)`
#### `ipfsd.cleanup() : Promise`

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

`callback` is a function with the signature `function(err)`.
Returns a promise that resolves when the cleanup is complete.

#### `ipfsd.start(flags, callback)`
#### `ipfsd.start(flags) : Promise`

Start the daemon.

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

`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.
Returns a promiset hat resolves to an instance of `ipfs-http-client`.


#### `ipfsd.stop([timeout, callback])`
#### `ipfsd.stop([timeout]) : Promise`

Stop the daemon.

`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.
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.

Returns a promise that resolves when the daemon has stopped.

#### `ipfsd.killProcess([timeout, callback])`
#### `ipfsd.killProcess([timeout]) : Promise`

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.

Note: timeout is ignored for `proc` nodes

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

`callback` is a function with the signature `function()` called once the process is killed
Returns a promise that resolves once the process is killed

#### `ipfsd.pid(callback)`
#### `ipfsd.pid() : Promise`

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

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

#### `ipfsd.getConfig([key], callback)`
#### `ipfsd.getConfig([key]) : Promise`

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

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

`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
Returns a promise that resolves to `Object|string` on success.

#### `ipfsd.setConfig(key, value, callback)`
#### `ipfsd.setConfig(key, value) : Promise`

Set a config value.

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

`value` - the config value to change/set

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

#### `ipfsd.version(callback)`
#### `ipfsd.version() : Promise`

Get the version of ipfs

`callback` is a function with the signature `function(err, version)`
Returns a promise that resolves to the `version`

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

Expand Down
23 changes: 9 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
"coverage": "aegir coverage --timeout 50000"
},
"browser": {
"hapi": false,
"@hapi/hapi": false,
"glob": false,
"fs": false,
"fs-extra": false,
"joi": false,
"./src/utils/repo/nodejs.js": "./src/utils/repo/browser.js",
"./src/utils/exec.js": false,
Expand Down Expand Up @@ -61,40 +62,34 @@
"daemon"
],
"dependencies": {
"async": "^2.6.2",
"base-x": "^3.0.5",
"@hapi/hapi": "^18.3.1",
"boom": "^7.3.0",
"debug": "^4.1.1",
"detect-node": "^2.0.4",
"dexie": "^2.0.4",
"execa": "^1.0.0",
"hapi": "^16.6.2",
"execa": "^2.0.2",
"fs-extra": "^8.1.0",
"hat": "~0.0.3",
"ipfs-http-client": "ipfs/js-ipfs-http-client#master",
"ipfs-http-client": "^32.0.1",
"joi": "^14.3.1",
"libp2p-crypto": "~0.16.1",
"lodash.clone": "^4.5.0",
"lodash.defaults": "^4.2.0",
"lodash.defaultsdeep": "^4.6.0",
"multiaddr": "^6.1.0",
"once": "^1.4.0",
"protons": "^1.0.1",
"rimraf": "^2.6.3",
"safe-json-parse": "^4.0.0",
"safe-json-stringify": "^1.2.0",
"superagent": "^5.0.5"
},
"devDependencies": {
"aegir": "^19.0.3",
"chai": "^4.2.0",
"delay": "^4.3.0",
"detect-port": "^1.3.0",
"dirty-chai": "^2.0.1",
"go-ipfs-dep": "~0.4.21",
"husky": "^2.4.1",
"husky": "^3.0.0",
"ipfs": "~0.36.3",
"is-running": "^2.1.0",
"lint-staged": "^8.1.7",
"mkdirp": "~0.5.1",
"lint-staged": "^9.0.2",
"proxyquire": "^2.1.0",
"superagent-mocker": "~0.5.2"
},
Expand Down
Loading

0 comments on commit b7d8faf

Please sign in to comment.