Skip to content

Commit c97b3a4

Browse files
committed
fix: basicaly normalize behaviour across the 3 impls
1 parent fe9bece commit c97b3a4

File tree

13 files changed

+426
-556
lines changed

13 files changed

+426
-556
lines changed

src/endpoint/routes.js

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,32 +61,25 @@ module.exports = (server) => {
6161
path: '/spawn',
6262
handler: (request, reply) => {
6363
const payload = request.payload || {}
64-
6564
// TODO: use the ../src/index.js so that the right Factory is picked
6665
const f = new FactoryDaemon({ type: payload.type })
6766

68-
f.spawn(payload.options, (err, ipfsd) => {
67+
f.spawn(payload, (err, ipfsd) => {
6968
if (err) {
7069
return reply(boom.badRequest(err))
7170
}
7271
const id = hat()
73-
const initialized = ipfsd.initialized
7472
nodes[id] = ipfsd
7573

76-
let api = null
77-
78-
if (nodes[id].started) {
79-
api = {
80-
apiAddr: nodes[id].apiAddr
81-
? nodes[id].apiAddr.toString()
82-
: '',
83-
gatewayAddr: nodes[id].gatewayAddr
84-
? nodes[id].gatewayAddr.toString()
85-
: ''
86-
}
87-
}
88-
89-
reply({ id: id, api: api, initialized: initialized })
74+
reply({
75+
_id: id,
76+
apiAddr: ipfsd.apiAddr ? ipfsd.apiAddr.toString() : '',
77+
gatewayAddr: ipfsd.gatewayAddr ? ipfsd.gatewayAddr.toString() : '',
78+
initialized: ipfsd.initialized,
79+
started: ipfsd.started,
80+
_env: ipfsd._env,
81+
path: ipfsd.path
82+
})
9083
})
9184
}
9285
})
@@ -131,10 +124,8 @@ module.exports = (server) => {
131124
}
132125

133126
reply({
134-
api: {
135-
apiAddr: nodes[id].apiAddr.toString(),
136-
gatewayAddr: nodes[id].gatewayAddr.toString()
137-
}
127+
apiAddr: nodes[id].apiAddr.toString(),
128+
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : null
138129
})
139130
})
140131
},

src/factory-client.js

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
'use strict'
22

33
const request = require('superagent')
4+
const merge = require('merge-options')
5+
const defaultConfig = require('./defaults/config.json')
46
const DaemonClient = require('./ipfsd-client')
57

68
/** @ignore @typedef {import("./index").SpawnOptions} SpawnOptions */
@@ -90,29 +92,32 @@ class FactoryClient {
9092
options = {}
9193
}
9294

93-
options = options || {}
94-
95+
const daemonOptions = merge({
96+
exec: this.options.exec,
97+
type: this.options.type,
98+
IpfsApi: this.options.IpfsApi,
99+
disposable: true,
100+
start: options.disposable !== false,
101+
init: options.disposable !== false,
102+
config: defaultConfig
103+
}, options)
104+
105+
if (options.defaultAddrs) {
106+
delete daemonOptions.config.Addresses
107+
}
95108
request
96-
.post(`${this.baseUrl}/spawn`)
97-
.send({ options: options, type: this.options.type })
109+
.post(`${this.baseUrl}/spawn`, daemonOptions)
98110
.end((err, res) => {
99111
if (err) {
100112
return callback(new Error(err.response ? err.response.body.message : err))
101113
}
102-
103-
const apiAddr = res.body.api ? res.body.api.apiAddr : ''
104-
const gatewayAddr = res.body.api ? res.body.api.gatewayAddr : ''
105-
106-
const ipfsd = new DaemonClient(
114+
const node = new DaemonClient(
107115
this.baseUrl,
108-
res.body.id,
109-
res.body.initialized,
110-
apiAddr,
111-
gatewayAddr,
112-
{ IpfsApi: this.options.IpfsApi }
116+
res.body,
117+
daemonOptions
113118
)
114119

115-
callback(null, ipfsd)
120+
callback(null, node)
116121
})
117122
}
118123
}

src/factory-daemon.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const series = require('async/series')
44
const merge = require('merge-options')
5-
const path = require('path')
65
const tmpDir = require('./utils/tmp-dir')
76
const Daemon = require('./ipfsd-daemon')
87
const defaultConfig = require('./defaults/config.json')
@@ -79,19 +78,15 @@ class FactoryDaemon {
7978
callback = options
8079
options = {}
8180
}
82-
const defaultRepo = path.join(
83-
process.env.HOME || process.env.USERPROFILE,
84-
this.options.type === 'js' ? '.jsipfs' : '.ipfs'
85-
)
81+
8682
const daemonOptions = merge({
8783
exec: this.options.exec,
8884
type: this.options.type,
8985
IpfsApi: this.options.IpfsApi,
9086
disposable: true,
9187
start: options.disposable !== false,
9288
init: options.disposable !== false,
93-
config: defaultConfig,
94-
repoPath: defaultRepo
89+
config: defaultConfig
9590
}, options)
9691

9792
if (options.defaultAddrs) {
@@ -101,8 +96,8 @@ class FactoryDaemon {
10196
const node = new Daemon(daemonOptions)
10297

10398
series([
104-
daemonOptions.init && ((cb) => node.init(daemonOptions.initOptions, cb)),
105-
daemonOptions.start && ((cb) => node.start(daemonOptions.args, cb))
99+
daemonOptions.init && (cb => node.init(daemonOptions.initOptions, cb)),
100+
daemonOptions.start && (cb => node.start(daemonOptions.args, cb))
106101
].filter(Boolean),
107102
(err) => {
108103
if (err) { return callback(err) }

src/factory-in-proc.js

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
'use strict'
22

3-
const defaults = require('lodash.defaultsdeep')
4-
const clone = require('lodash.clone')
53
const series = require('async/series')
6-
const path = require('path')
7-
const once = require('once')
4+
const merge = require('merge-options')
85
const tmpDir = require('./utils/tmp-dir')
9-
const repoUtils = require('./utils/repo/nodejs')
106
const InProc = require('./ipfsd-in-proc')
117
const defaultConfig = require('./defaults/config.json')
12-
const defaultOptions = require('./defaults/options.json')
138

149
/** @ignore @typedef {import("./index").SpawnOptions} SpawnOptions */
1510

@@ -38,7 +33,6 @@ class FactoryInProc {
3833
*
3934
* @param {string} type - the type of the node
4035
* @param {function(Error, string): void} callback
41-
* @returns {void}
4236
*/
4337
tmpDir (type, callback) {
4438
callback(null, tmpDir(true))
@@ -49,7 +43,6 @@ class FactoryInProc {
4943
*
5044
* @param {Object} [options={}]
5145
* @param {function(Error, string): void} callback
52-
* @returns {void}
5346
*/
5447
version (options, callback) {
5548
if (typeof options === 'function') {
@@ -58,79 +51,51 @@ class FactoryInProc {
5851
}
5952

6053
const node = new InProc(options)
61-
node.once('ready', () => {
62-
node.version(callback)
63-
})
54+
node.version(callback)
6455
}
6556

6657
/**
6758
* Spawn JSIPFS instances
6859
*
69-
* @param {SpawnOptions} [opts={}] - various config options and ipfs config parameters
60+
* @param {SpawnOptions} [options={}] - various config options and ipfs config parameters
7061
* @param {function(Error, InProc): void} callback - a callback that receives an array with an `ipfs-instance` attached to the node and a `Node`
7162
* @returns {void}
7263
*/
73-
spawn (opts, callback) {
74-
if (typeof opts === 'function') {
75-
callback = opts
76-
opts = defaultOptions
64+
spawn (options, callback) {
65+
if (typeof options === 'function') {
66+
callback = options
67+
options = {}
7768
}
7869

79-
const options = defaults({}, opts, defaultOptions)
80-
options.init = typeof options.init !== 'undefined'
81-
? options.init
82-
: true
83-
84-
if (options.disposable) {
85-
options.config = defaults({}, options.config, defaultConfig)
86-
} else {
87-
const nonDisposableConfig = clone(defaultConfig)
88-
options.init = false
89-
options.start = false
90-
91-
const defaultRepo = path.join(
92-
process.env.HOME || process.env.USERPROFILE || '',
93-
options.isJs ? '.jsipfs' : '.ipfs'
94-
)
95-
96-
options.repoPath = options.repoPath || (process.env.IPFS_PATH || defaultRepo)
97-
options.config = defaults({}, options.config, nonDisposableConfig)
98-
}
70+
const daemonOptions = merge({
71+
exec: this.options.exec,
72+
type: this.options.type,
73+
IpfsApi: this.options.IpfsApi,
74+
disposable: true,
75+
start: options.disposable !== false,
76+
init: options.disposable !== false,
77+
config: defaultConfig
78+
}, options)
9979

10080
if (options.defaultAddrs) {
101-
delete options.config.Addresses
81+
delete daemonOptions.config.Addresses
10282
}
10383

104-
options.type = this.options.type
105-
options.exec = options.exec || this.options.exec
106-
107-
if (typeof options.exec !== 'function') {
84+
if (typeof this.options.exec !== 'function') {
10885
return callback(new Error(`'type' proc requires 'exec' to be a coderef`))
10986
}
11087

111-
const node = new InProc(options)
112-
const callbackOnce = once((err) => {
113-
if (err) {
114-
return callback(err)
115-
}
116-
callback(null, node)
117-
})
118-
node.once('error', callbackOnce)
88+
const node = new InProc(daemonOptions)
11989

12090
series([
121-
(cb) => node.once('ready', cb),
122-
(cb) => repoUtils.repoExists(node.path, (err, initialized) => {
123-
if (err) { return cb(err) }
124-
node.initialized = initialized
125-
cb()
126-
}),
127-
(cb) => options.init
128-
? node.init(cb)
129-
: cb(),
130-
(cb) => options.start
131-
? node.start(options.args, cb)
132-
: cb()
133-
], callbackOnce)
91+
daemonOptions.init && (cb => node.init(daemonOptions.initOptions, cb)),
92+
daemonOptions.start && (cb => node.start(daemonOptions.args, cb))
93+
].filter(Boolean),
94+
(err) => {
95+
if (err) { return callback(err) }
96+
97+
callback(null, node)
98+
})
13499
}
135100
}
136101

0 commit comments

Comments
 (0)