Skip to content

Commit bbda5a9

Browse files
committed
feat: complete rewrite
1 parent 8ea97a6 commit bbda5a9

30 files changed

+479
-895
lines changed

package.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
"fs": false,
2626
"fs-extra": false,
2727
"joi": false,
28-
"./src/utils/repo/nodejs.js": "./src/utils/repo/browser.js",
28+
"./src/utils/repo.js": "./src/utils/repo.browser.js",
2929
"./src/utils/exec.js": false,
3030
"./src/utils/find-ipfs-executable.js": false,
31-
"./src/utils/tmp-dir.js": "./src/utils/tmp-dir-browser.js",
31+
"./src/utils/tmp-dir.js": "./src/utils/tmp-dir.browser.js",
3232
"./src/utils/run.js": false,
3333
"./src/factory-daemon.js": false,
3434
"./src/ipfsd-daemon.js": false,
@@ -62,29 +62,30 @@
6262
"daemon"
6363
],
6464
"dependencies": {
65-
"@hapi/boom": "^7.4.7",
65+
"@hapi/boom": "^8.0.1",
6666
"@hapi/hapi": "^18.3.2",
67-
"@hapi/joi": "^15.1.1",
67+
"@hapi/joi": "^16.1.7",
6868
"debug": "^4.1.1",
69-
"execa": "^2.0.4",
69+
"execa": "^3.1.0",
7070
"fs-extra": "^8.1.0",
7171
"hat": "~0.0.3",
72-
"ipfs-http-client": "^38.2.0",
72+
"ipfs-http-client": "^39.0.0",
7373
"ipfs-utils": "^0.4.0",
7474
"merge-options": "^1.0.1",
7575
"multiaddr": "^6.1.1",
7676
"safe-json-stringify": "^1.2.0",
77-
"superagent": "^5.0.5"
77+
"superagent": "^5.0.5",
78+
"tempy": "^0.3.0"
7879
},
7980
"devDependencies": {
80-
"aegir": "^20.3.2",
81+
"aegir": "^20.4.1",
8182
"chai": "^4.2.0",
8283
"delay": "^4.3.0",
8384
"detect-port": "^1.3.0",
8485
"dirty-chai": "^2.0.1",
8586
"go-ipfs-dep": "~0.4.22",
86-
"husky": "^3.0.8",
87-
"ipfs": "~0.38.0",
87+
"husky": "^3.0.9",
88+
"ipfs": "~0.39.0-rc.1",
8889
"is-running": "^2.1.0",
8990
"lint-staged": "^9.4.2",
9091
"proxyquire": "^2.1.3",

src/endpoint/routes.js

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
const hat = require('hat')
44
const Joi = require('@hapi/joi')
55
const boom = require('@hapi/boom')
6-
const merge = require('merge-options')
76
const FactoryDaemon = require('../factory-daemon')
87
const tmpDir = require('../utils/tmp-dir')
98

10-
const routeConfig = {
9+
const routeOptions = {
1110
validate: {
12-
query: {
11+
query: Joi.object({
1312
id: Joi.string().alphanum().required()
14-
}
13+
})
1514
}
1615
}
1716

@@ -30,7 +29,7 @@ module.exports = (server) => {
3029
handler: async (request) => {
3130
const type = request.query.type || 'go'
3231
try {
33-
return { tmpDir: await tmpDir(type === 'js') }
32+
return { tmpDir: await tmpDir(type) }
3433
} catch (err) {
3534
throw boom.badRequest(err.message)
3635
}
@@ -67,10 +66,10 @@ module.exports = (server) => {
6766
const payload = request.payload || {}
6867

6968
// TODO: use the ../src/index.js so that the right Factory is picked
70-
const f = new FactoryDaemon({ type: payload.type })
69+
const f = new FactoryDaemon(payload)
7170

7271
try {
73-
const ipfsd = await f.spawn(payload)
72+
const ipfsd = await f.spawn(payload.ipfsOptions)
7473
const id = hat()
7574
nodes[id] = ipfsd
7675

@@ -80,7 +79,7 @@ module.exports = (server) => {
8079
gatewayAddr: ipfsd.gatewayAddr ? ipfsd.gatewayAddr.toString() : '',
8180
initialized: ipfsd.initialized,
8281
started: ipfsd.started,
83-
_env: ipfsd._env,
82+
_env: ipfsd.env,
8483
path: ipfsd.path
8584
}
8685
} catch (err) {
@@ -100,7 +99,7 @@ module.exports = (server) => {
10099
const payload = request.payload || {}
101100

102101
try {
103-
await nodes[id].init(payload.initOpts)
102+
await nodes[id].init(payload.opts)
104103

105104
return {
106105
initialized: nodes[id].initialized
@@ -109,7 +108,7 @@ module.exports = (server) => {
109108
throw boom.badRequest(err.message)
110109
}
111110
},
112-
config: routeConfig
111+
options: routeOptions
113112
})
114113

115114
/*
@@ -120,11 +119,9 @@ module.exports = (server) => {
120119
path: '/start',
121120
handler: async (request) => {
122121
const id = request.query.id
123-
const payload = request.payload || {}
124-
const flags = payload.flags || []
125122

126123
try {
127-
await nodes[id].start(flags)
124+
await nodes[id].start()
128125

129126
return {
130127
apiAddr: nodes[id].apiAddr.toString(),
@@ -134,7 +131,7 @@ module.exports = (server) => {
134131
throw boom.badRequest(err.message)
135132
}
136133
},
137-
config: routeConfig
134+
options: routeOptions
138135
})
139136

140137
/*
@@ -148,7 +145,7 @@ module.exports = (server) => {
148145

149146
return { apiAddr: nodes[id].apiAddr.toString() }
150147
},
151-
config: routeConfig
148+
options: routeOptions
152149
})
153150

154151
/*
@@ -163,7 +160,7 @@ module.exports = (server) => {
163160

164161
return { getawayAddr: nodes[id].gatewayAddr.toString() }
165162
},
166-
config: routeConfig
163+
options: routeOptions
167164
})
168165

169166
/*
@@ -185,7 +182,7 @@ module.exports = (server) => {
185182
throw boom.badRequest(err.message)
186183
}
187184
},
188-
config: routeConfig
185+
options: routeOptions
189186
})
190187

191188
/*
@@ -206,7 +203,7 @@ module.exports = (server) => {
206203
throw boom.badRequest(err.message)
207204
}
208205
},
209-
config: routeConfig
206+
options: routeOptions
210207
})
211208

212209
/*
@@ -230,7 +227,7 @@ module.exports = (server) => {
230227
throw boom.badRequest(err.message)
231228
}
232229
},
233-
config: routeConfig
230+
options: routeOptions
234231
})
235232

236233
/*
@@ -244,7 +241,7 @@ module.exports = (server) => {
244241

245242
return { pid: nodes[id].pid }
246243
},
247-
config: routeConfig
244+
options: routeOptions
248245
})
249246

250247
/*
@@ -267,13 +264,14 @@ module.exports = (server) => {
267264
throw boom.badRequest(err.message)
268265
}
269266
},
270-
config: merge(routeConfig, {
267+
options: {
271268
validate: {
272-
query: {
269+
query: Joi.object({
270+
id: Joi.string().alphanum().required(),
273271
key: Joi.string().optional()
274-
}
272+
})
275273
}
276-
})
274+
}
277275
})
278276

279277
/*
@@ -295,13 +293,14 @@ module.exports = (server) => {
295293

296294
return h.response().code(200)
297295
},
298-
config: merge(routeConfig, {
296+
options: {
299297
validate: {
300-
payload: {
298+
query: Joi.object({
299+
id: Joi.string().alphanum().required(),
301300
key: Joi.string(),
302301
value: Joi.any()
303-
}
302+
})
304303
}
305-
})
304+
}
306305
})
307306
}

src/factory-client.js

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,94 +4,108 @@ const request = require('superagent')
44
const DaemonClient = require('./ipfsd-client')
55
const merge = require('merge-options')
66
const defaultConfig = require('./defaults/config.json')
7+
// const findBin = require('./utils/find-ipfs-executable')
78

8-
/** @ignore @typedef {import("./index").SpawnOptions} SpawnOptions */
9+
/** @ignore @typedef {import("./index").IpfsOptions} IpfsOptions */
10+
/** @ignore @typedef {import("./index").FactoryOptions} FactoryOptions */
911

1012
/**
1113
* Exposes the same Factory API but uses a remote endpoint to create the Daemons/Nodes
12-
* @param {Object} options
1314
*/
1415
class FactoryClient {
15-
constructor (options) {
16-
options = options || {}
17-
if (!options.host) { options.host = 'localhost' }
18-
if (!options.port) { options.port = 43134 }
19-
if (!options.type) { options.type = 'go' }
20-
if (typeof options.host === 'number') {
21-
options.port = options.host
22-
options.host = 'localhost'
23-
}
24-
25-
this.options = options
26-
27-
if (options.type === 'proc') {
28-
throw new Error('\'proc\' is not supported in client mode')
29-
}
16+
/**
17+
* @param {FactoryOptions} options
18+
*/
19+
constructor (options = {}) {
20+
/** @type FactoryOptions */
21+
this.options = merge({
22+
host: 'localhost',
23+
port: 43134,
24+
secure: false,
25+
type: 'go',
26+
defaultAddrs: false,
27+
disposable: true,
28+
env: process.env,
29+
args: [],
30+
ipfsHttp: {
31+
path: require.resolve('ipfs-http-client'),
32+
ref: require('ipfs-http-client')
33+
},
34+
ipfsApi: {
35+
path: require.resolve('ipfs'),
36+
ref: require('ipfs')
37+
}
38+
// ipfsBin: findBin(options.type || 'go')
39+
}, options)
3040

31-
this.baseUrl = `${options.secure ? 'https://' : 'http://'}${options.host}:${options.port}`
41+
this.baseUrl = `${this.options.secure ? 'https://' : 'http://'}${this.options.host}:${this.options.port}`
3242
}
3343

3444
/**
3545
* Utility method to get a temporary directory
3646
* useful in browsers to be able to generate temp
3747
* repos manually
38-
* @param {boolean} isJS
3948
*
4049
* @returns {Promise}
4150
*/
42-
async tmpDir (isJS) {
51+
async tmpDir () {
4352
const res = await request
4453
.get(`${this.baseUrl}/util/tmp-dir`)
45-
.query({ type: isJS ? 'js' : 'go' })
54+
.query({ type: this.options.type })
4655

4756
return res.body.tmpDir
4857
}
4958

5059
/**
5160
* Get the version of the IPFS Daemon.
5261
*
53-
* @param {Object} [options={}]
5462
* @returns {Promise}
5563
*/
56-
async version (options = {}) {
57-
options = options || { type: this.options.type }
58-
64+
async version () {
5965
const res = await request
6066
.get(`${this.baseUrl}/version`)
61-
.query(options)
67+
.query({ type: this.options.type })
6268

6369
return res.body.version
6470
}
6571

6672
/**
6773
* Spawn a remote daemon using ipfs-http-client
6874
*
69-
* @param {SpawnOptions} [options={}]
75+
* @param {IpfsOptions} [options={}] - Same as js-ipfs https://github.com/ipfs/js-ipfs#ipfs-constructor
7076
* @return {Promise}
7177
*/
7278
async spawn (options = {}) {
73-
const daemonOptions = merge({
74-
exec: this.options.exec,
75-
type: this.options.type,
76-
IpfsClient: this.options.IpfsClient,
77-
disposable: true,
78-
start: options.disposable !== false,
79-
init: options.disposable !== false,
79+
const ipfsOptions = merge({
80+
start: this.options.disposable !== false,
81+
init: this.options.disposable !== false,
8082
config: defaultConfig
81-
}, options)
83+
},
84+
this.options.ipfsOptions,
85+
options)
8286

83-
if (options.defaultAddrs) {
84-
delete daemonOptions.config.Addresses
87+
if (this.options.defaultAddrs) {
88+
delete ipfsOptions.config.Addresses
8589
}
8690

8791
const res = await request
8892
.post(`${this.baseUrl}/spawn`)
89-
.send(daemonOptions)
93+
.send(merge(
94+
{
95+
ipfsOptions
96+
},
97+
this.options
98+
))
9099

91100
const ipfsd = new DaemonClient(
92101
this.baseUrl,
93102
res.body,
94-
daemonOptions
103+
merge(
104+
{
105+
ipfsOptions
106+
},
107+
this.options
108+
)
95109
)
96110

97111
return ipfsd

0 commit comments

Comments
 (0)