Skip to content

Commit 69848c6

Browse files
committed
wip: reworking factory instantiation
1 parent 77c70a6 commit 69848c6

File tree

12 files changed

+91
-101
lines changed

12 files changed

+91
-101
lines changed

README.md

+31-29
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
> Control an ipfs node daemon using either Node.js or the browser
1414
1515
```
16-
17-
1816
+-----+
1917
| H |
2018
| T |
@@ -32,7 +30,6 @@
3230
| +-----------------------+ | +-----------------------|---- | |
3331
| | | +----------------------+ |
3432
+-----------------------------+ +-----------------------------+
35-
3633
```
3734

3835
## Table of Contents
@@ -60,13 +57,10 @@ IPFS daemons are already easy to start and stop, but this module is here to do i
6057
// Start a disposable node, and get access to the api
6158
// print the node id, and stop the temporary daemon
6259

63-
// IPFS_PATH will point to /tmp/ipfs_***** and will be
64-
// cleaned up when the process exits.
60+
const controllerFactory = require('ipfsd-ctl')
61+
const daemonFactory = controllerFactory()
6562

66-
const daemonFactory = require('ipfsd-ctl')
67-
const local = daemonFactory.localController
68-
69-
local.spawn(function (err, ipfsd) {
63+
daemonFactory.spawn(function (err, ipfsd) {
7064
const ipfsCtl = ipfsd.ctl
7165
const ipfsCtrl = ipfsd.ctrl
7266
ipfsCtl.id(function (err, id) {
@@ -82,21 +76,18 @@ local.spawn(function (err, ipfsd) {
8276
// Start a remote disposable node, and get access to the api
8377
// print the node id, and stop the temporary daemon
8478

85-
// IPFS_PATH will point to /tmp/ipfs_***** and will be
86-
// cleaned up when the process exits.
87-
88-
const daemonFactory = require('ipfsd-ctl')
89-
const server = daemonFactory.server
79+
const controllerFactory = require('ipfsd-ctl')
80+
const daemonFactory = controllerFactory()
9081

91-
server.start((err) => {
82+
const port = 9999
83+
daemonFactory.start(port, (err) => {
9284
if (err) {
9385
throw err
9486
}
9587

96-
const remote = daemonFactory.remoteController(port || 9999)
97-
remote.spawn(function (err, controller) {
98-
const ipfsCtl = controller.ctl
99-
const ipfsCtrl = controller.ctrl
88+
daemonFactory.spawn(function (err, ipfsd) {
89+
const ipfsCtl = ipfsd.ctl
90+
const ipfsCtrl = ipfsd.ctrl
10091
ipfsCtl.id(function (err, id) {
10192
console.log(id)
10293
ipfsCtrl.stopDaemon()
@@ -138,16 +129,25 @@ module.exports = {
138129

139130
#### Create factory
140131

141-
- `daemonFactory.localController` - create a local controller
142-
- `daemonFactory.remoteController([port])` - create a remote controller, usable from browsers
143-
- These methods return a factory that exposes the `spawn` method, which allows spawning and controlling ipfs nodes
144-
- `daemonFactory.server` - exposes `start` and `stop` methods to start and stop the bundled http server that is required to run the remote controller.
132+
```js
133+
const controllerFactory = require('ipfsd-ctl')
134+
const daemonFactory = controllerFactory()
135+
```
136+
137+
> Create a factory that will expose the `daemonFactory.spawn` method
138+
139+
- These method return a factory that exposes the `spawn` method, which allows spawning and controlling ipfs nodes
140+
141+
142+
> `daemonFactory.server`
143+
144+
- exposes `start` and `stop` methods to start and stop the bundled http server that is required to run the remote controller.
145145

146146
#### Spawn nodes
147147

148148
> Spawn either a js-ipfs or go-ipfs node through `localController` or `remoteController`
149149
150-
`spawn([options], cb)`
150+
`spawn([options], callback)`
151151

152152
- `options` - is an optional object with various options and ipfs config parameters
153153
- `js` bool (default false) - spawn a js or go node (default go)
@@ -158,10 +158,11 @@ module.exports = {
158158
- `args` - array of cmd line arguments to be passed to ipfs daemon
159159
- `config` - ipfs configuration options
160160

161-
- `cb(err, {ctl: <ipfs-api instance>, ctrl: <Node (ctrl) instance>})` - a callback that receives an object with two members:
162-
- `ctl` - an [ipfs-api](https://github.com/ipfs/js-ipfs-api) instance attached to the newly created ipfs node
163-
- `ctrl` - an instance of a daemon controller object
164-
161+
- `callback` - is a function with the signature `cb(err, ipfsd)` where:
162+
- `err` - is the error set if spawning the node is unsuccessful
163+
- `ipfsd` - is an object with two properties:
164+
- `ctl` - an [ipfs-api](https://github.com/ipfs/js-ipfs-api) instance attached to the newly created ipfs node
165+
- `ctrl` - an instance of a daemon controller object
165166

166167
### IPFS Client (ctl)
167168

@@ -170,14 +171,15 @@ module.exports = {
170171

171172
### IPFS Daemon Controller (ctrl)
172173

174+
> The IPFS daemon controller that allows interacting with the spawned IPFS process
173175
174176
#### `apiAddr` (getter)
175177

176178
> Get the address (multiaddr) of connected IPFS API.
177179
178180
- returns multiaddr
179181

180-
#### `gatewayAddr` (getter)
182+
#### `gatewayAddr` (getter)
181183

182184
> Get the address (multiaddr) of connected IPFS HTTP Gateway.
183185

examples/id/id.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/* eslint no-console: 0 */
22
'use strict'
33

4-
const factory = require('ipfsd-ctl')
5-
const localController = factory.localController
4+
const controllerFactory = require('ipfsd-ctl')
5+
const daemonFactory = controllerFactory()
66

7-
localController.spawn(function (err, ipfsd) {
7+
daemonFactory.spawn(function (err, ipfsd) {
88
if (err) {
99
throw err
1010
}
@@ -21,19 +21,19 @@ localController.spawn(function (err, ipfsd) {
2121
})
2222
})
2323

24-
localController.spawn(function (err, ipfsd) {
24+
daemonFactory.spawn(function (err, ipfsd) {
2525
if (err) {
2626
throw err
2727
}
2828

29-
const ipfs = ipfsd.ctl
30-
const node = ipfsd.ctrl
31-
ipfs.id(function (err, id) {
29+
const ipfsCtl = ipfsd.ctl
30+
const ipfsCtrl = ipfsd.ctrl
31+
ipfsCtl.id(function (err, id) {
3232
if (err) {
3333
throw err
3434
}
3535
console.log('bob')
3636
console.log(id)
37-
node.stopDaemon()
37+
ipfsCtrl.stopDaemon()
3838
})
3939
})

examples/local-disposable/local-disposable.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,43 @@
44
// Start a disposable node, and get access to the api
55
// print the node id
66

7-
// IPFS_PATH will point to /tmp/ipfs_***** and will be
8-
// cleaned up when the process exits.
9-
10-
const factory = require('ipfsd-ctl')
11-
const localController = factory.localController
7+
const controllerFactory = require('ipfsd-ctl')
8+
const daemonFactory = controllerFactory()
129

1310
// start a go daemon
14-
localController.spawn((err, ipfsd) => {
11+
daemonFactory.spawn((err, ipfsd) => {
1512
if (err) {
1613
throw err
1714
}
1815

19-
const ipfs = ipfsd.ctl
20-
const node = ipfsd.ctrl
21-
ipfs.id(function (err, id) {
16+
const ipfsCtl = ipfsd.ctl
17+
const ipfsCtrl = ipfsd.ctrl
18+
ipfsCtl.id(function (err, id) {
2219
if (err) {
2320
throw err
2421
}
2522

2623
console.log('go-ipfs')
2724
console.log(id)
28-
node.stopDaemon()
25+
ipfsCtrl.stopDaemon()
2926
})
3027
})
3128

3229
// start a js daemon
33-
localController.spawn({ isJs: true }, (err, ipfsd) => {
30+
daemonFactory.spawn({ isJs: true }, (err, ipfsd) => {
3431
if (err) {
3532
throw err
3633
}
3734

38-
const ipfs = ipfsd.ctl
39-
const node = ipfsd.ctrl
40-
ipfs.id(function (err, id) {
35+
const ipfsCtl = ipfsd.ctl
36+
const ipfsCtrl = ipfsd.ctrl
37+
ipfsCtl.id(function (err, id) {
4138
if (err) {
4239
throw err
4340
}
4441

4542
console.log('js-ipfs')
4643
console.log(id)
47-
node.stopDaemon()
44+
ipfsCtrl.stopDaemon()
4845
})
4946
})

examples/local/local.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* eslint no-console: 0 */
22
'use strict'
33

4-
const factory = require('ipfsd-ctl')
5-
const localController = factory.localController
4+
const controllerFactory = require('ipfsd-ctl')
5+
const daemonFactory = controllerFactory()
66

77
// opens an api connection to local running go-ipfs node
8-
localController.spawn({ disposable: false }, (err, ipfsd) => {
8+
daemonFactory.spawn({ disposable: false }, (err, ipfsd) => {
99
if (err) {
1010
throw err
1111
}
@@ -24,7 +24,7 @@ localController.spawn({ disposable: false }, (err, ipfsd) => {
2424
})
2525

2626
// opens an api connection to local running js-ipfs node
27-
localController.spawn({ isJs: true, disposable: false }, (err, ipfsd) => {
27+
daemonFactory.spawn({ isJs: true, disposable: false }, (err, ipfsd) => {
2828
if (err) {
2929
throw err
3030
}

examples/remote-disposable/remote-disposable.js

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,29 @@
44
// Start a remote disposable node, and get access to the api
55
// print the node id, and stop the temporary daemon
66

7-
// IPFS_PATH will point to /tmp/ipfs_***** and will be
8-
// cleaned up when the process exits.
9-
10-
const ipfsd = require('ipfsd-ctl')
11-
const server = ipfsd.server
7+
const controllerFactory = require('ipfsd-ctl')
8+
const daemonFactory = controllerFactory({ remote: true })
9+
const server = controllerFactory.server
1210

1311
server.start((err) => {
1412
if (err) {
1513
throw err
1614
}
1715

18-
const remoteController = ipfsd.remoteController()
19-
remoteController.spawn(function (err, controller) {
16+
daemonFactory.spawn(function (err, ipfsd) {
2017
if (err) {
2118
throw err
2219
}
2320

24-
const ipfs = controller.ctl
25-
const node = controller.ctrl
26-
ipfs.id(function (err, id) {
21+
const ipfsCtl = ipfsd.ctl
22+
const ipfsCtrl = ipfsd.ctrl
23+
ipfsCtl.id(function (err, id) {
2724
if (err) {
2825
throw err
2926
}
3027

3128
console.log(id)
32-
node.stopDaemon(() => server.stop())
29+
ipfsCtrl.stopDaemon(() => server.stop())
3330
})
3431
})
3532
})

package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
],
6262
"license": "MIT",
6363
"dependencies": {
64-
"@ljharb/eslint-config": "^12.2.1",
6564
"async": "^2.6.0",
6665
"debug": "^3.1.0",
6766
"detect-node": "^2.0.3",
@@ -91,7 +90,7 @@
9190
"aegir": "^12.2.0",
9291
"chai": "^4.1.2",
9392
"dirty-chai": "^2.0.1",
94-
"ipfs": "^0.27.0",
93+
"ipfs": "~0.27.0",
9594
"is-running": "1.0.5",
9695
"mkdirp": "^0.5.1",
9796
"multihashes": "~0.4.12",

src/daemon.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,10 @@ class Node {
3838
const isJs = truthy(process.env.IPFS_JS)
3939

4040
this.opts = opts || { isJs: isJs || false }
41-
process.env.IPFS_JS = this.opts.isJs
4241

4342
this.path = this.opts.disposable ? tempDir(isJs) : (this.opts.repoPath || tempDir(isJs))
4443
this.disposable = this.opts.disposable
45-
this.exec = process.env.IPFS_EXEC || findIpfsExecutable(this.opts.isJs, rootPath)
44+
this.exec = this.opts.executable || process.env.IPFS_EXEC || findIpfsExecutable(this.opts.isJs, rootPath)
4645
this.subprocess = null
4746
this.initialized = fs.existsSync(path)
4847
this.clean = true
@@ -115,7 +114,6 @@ class Node {
115114

116115
if (initOpts.directory && initOpts.directory !== this.path) {
117116
this.path = initOpts.directory
118-
this.env.IPFS_PATH = this.path
119117
}
120118

121119
this._run(['init', '-b', keySize], { env: this.env }, (err, result) => {

src/index.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@
22

33
const localController = require('./local')
44
const remote = require('./remote')
5+
const isNode = require('detect-node')
6+
const defaults = require('lodash.defaultsdeep')
57

6-
module.exports = {
7-
localController,
8-
remoteController: remote.remoteController,
9-
server: remote.server
8+
function controllerFactory (opts) {
9+
const options = defaults({}, opts, { remote: !isNode })
10+
11+
if (options.remote) {
12+
return remote.remoteController(options.port)
13+
}
14+
15+
return localController
1016
}
17+
18+
controllerFactory.server = remote.server
19+
20+
module.exports = controllerFactory

src/local.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const IpfsDaemonController = {
5454
* - `disposable` bool - a new repo is created and initialized for each invocation
5555
* - `config` - ipfs configuration options
5656
* - `args` - array of cmd line arguments to be passed to ipfs daemon
57+
* - `executable` - path to the desired IPFS executable to spawn
5758
*
5859
* @param {Object} [opts={}] - various config options and ipfs config parameters
5960
* @param {Function} callback(err, [`ipfs-api instance`, `Node (ctrl) instance`]) - a callback that receives an array with an `ipfs-instance` attached to the node and a `Node`

src/remote/server.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,16 @@ const Hapi = require('hapi')
44
const routes = require('./routes')
55

66
let server = null
7-
exports.start = function start (port, host, cb) {
7+
exports.start = function start (port, cb) {
88
if (typeof port === 'function') {
99
cb = port
1010
port = 9999
1111
}
1212

13-
if (typeof host === 'function') {
14-
cb = host
15-
host = 'localhost'
16-
}
17-
1813
port = port || 9999
19-
host = host || 'localhost'
2014

2115
server = new Hapi.Server()
22-
server.connection({ port, host, routes: { cors: true } })
16+
server.connection({ port, host: 'localhost', routes: { cors: true } })
2317

2418
routes(server)
2519
server.start(cb)

0 commit comments

Comments
 (0)