Skip to content

Commit

Permalink
fix: fix non-disposable and normalise behaviour
Browse files Browse the repository at this point in the history
closes #305
closes #276
closes #354
closes #330
closes #329
  • Loading branch information
hugomrdias committed Sep 17, 2019
1 parent a62bd97 commit f95d011
Show file tree
Hide file tree
Showing 18 changed files with 574 additions and 804 deletions.
5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@
"@hapi/joi": "^15.1.1",
"debug": "^4.1.1",
"detect-node": "^2.0.4",
"dexie": "^2.0.4",
"execa": "^2.0.4",
"fs-extra": "^8.1.0",
"hat": "~0.0.3",
"ipfs-http-client": "^35.1.0",
"lodash.clone": "^4.5.0",
"lodash.defaults": "^4.2.0",
"lodash.defaultsdeep": "^4.6.1",
"merge-options": "^1.0.1",
"multiaddr": "^7.0.0",
"safe-json-stringify": "^1.2.0",
"superagent": "^5.0.5"
Expand Down
6 changes: 0 additions & 6 deletions src/defaults/options.json

This file was deleted.

56 changes: 23 additions & 33 deletions src/endpoint/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const hat = require('hat')
const Joi = require('@hapi/joi')
const boom = require('@hapi/boom')
const defaults = require('lodash.defaultsdeep')
const merge = require('merge-options')
const FactoryDaemon = require('../factory-daemon')
const tmpDir = require('../utils/tmp-dir')

Expand All @@ -27,11 +27,13 @@ module.exports = (server) => {
server.route({
method: 'GET',
path: '/util/tmp-dir',
handler: (request) => {
handler: async (request) => {
const type = request.query.type || 'go'
const path = tmpDir(type === 'js')

return { tmpDir: path }
try {
return { tmpDir: await tmpDir(type === 'js') }
} catch (err) {
throw boom.badRequest(err.message)
}
}
})

Expand Down Expand Up @@ -68,28 +70,18 @@ module.exports = (server) => {
const f = new FactoryDaemon({ type: payload.type })

try {
const ipfsd = await f.spawn(payload.options)
const ipfsd = await f.spawn(payload)
const id = hat()
const initialized = ipfsd.initialized
nodes[id] = ipfsd

let api = null

if (nodes[id].started) {
api = {
apiAddr: nodes[id].apiAddr
? nodes[id].apiAddr.toString()
: '',
gatewayAddr: nodes[id].gatewayAddr
? nodes[id].gatewayAddr.toString()
: ''
}
}

return {
id,
api,
initialized
_id: id,
apiAddr: ipfsd.apiAddr ? ipfsd.apiAddr.toString() : '',
gatewayAddr: ipfsd.gatewayAddr ? ipfsd.gatewayAddr.toString() : '',
initialized: ipfsd.initialized,
started: ipfsd.started,
_env: ipfsd._env,
path: ipfsd.path
}
} catch (err) {
throw boom.badRequest(err.message)
Expand Down Expand Up @@ -135,10 +127,8 @@ module.exports = (server) => {
await nodes[id].start(flags)

return {
api: {
apiAddr: nodes[id].apiAddr.toString(),
gatewayAddr: nodes[id].gatewayAddr.toString()
}
apiAddr: nodes[id].apiAddr.toString(),
gatewayAddr: nodes[id].gatewayAddr ? nodes[id].gatewayAddr.toString() : ''
}
} catch (err) {
throw boom.badRequest(err.message)
Expand Down Expand Up @@ -206,7 +196,7 @@ module.exports = (server) => {
path: '/stop',
handler: async (request, h) => {
const id = request.query.id
const timeout = request.payload.timeout
const timeout = request.payload && request.payload.timeout

try {
await nodes[id].stop(timeout)
Expand All @@ -230,7 +220,7 @@ module.exports = (server) => {
path: '/kill',
handler: async (request, h) => {
const id = request.query.id
const timeout = request.payload.timeout
const timeout = request.payload && request.payload.timeout

try {
await nodes[id].killProcess(timeout)
Expand Down Expand Up @@ -277,13 +267,13 @@ module.exports = (server) => {
throw boom.badRequest(err.message)
}
},
config: defaults({}, {
config: merge(routeConfig, {
validate: {
query: {
key: Joi.string().optional()
}
}
}, routeConfig)
})
})

/*
Expand All @@ -305,13 +295,13 @@ module.exports = (server) => {

return h.response().code(200)
},
config: defaults({}, {
config: merge(routeConfig, {
validate: {
payload: {
key: Joi.string(),
value: Joi.any()
}
}
}, routeConfig)
})
})
}
32 changes: 22 additions & 10 deletions src/factory-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

const request = require('superagent')
const DaemonClient = require('./ipfsd-client')
const merge = require('merge-options')
const defaultConfig = require('./defaults/config.json')

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

Expand Down Expand Up @@ -33,12 +35,14 @@ class FactoryClient {
* Utility method to get a temporary directory
* useful in browsers to be able to generate temp
* repos manually
* @param {boolean} isJS
*
* @returns {Promise}
*/
async tmpDir () {
async tmpDir (isJS) {
const res = await request
.get(`${this.baseUrl}/util/tmp-dir`)
.query({ type: isJS ? 'js' : 'go' })

return res.body.tmpDir
}
Expand Down Expand Up @@ -66,20 +70,28 @@ class FactoryClient {
* @return {Promise}
*/
async spawn (options = {}) {
const daemonOptions = merge({
exec: this.options.exec,
type: this.options.type,
IpfsClient: this.options.IpfsClient,
disposable: true,
start: options.disposable !== false,
init: options.disposable !== false,
config: defaultConfig
}, options)

if (options.defaultAddrs) {
delete daemonOptions.config.Addresses
}

const res = await request
.post(`${this.baseUrl}/spawn`)
.send({ options: options, type: this.options.type })

const apiAddr = res.body.api ? res.body.api.apiAddr : ''
const gatewayAddr = res.body.api ? res.body.api.gatewayAddr : ''
.send(daemonOptions)

const ipfsd = new DaemonClient(
this.baseUrl,
res.body.id,
res.body.initialized,
apiAddr,
gatewayAddr,
{ IpfsClient: this.options.IpfsClient }
res.body,
daemonOptions
)

return ipfsd
Expand Down
67 changes: 19 additions & 48 deletions src/factory-daemon.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
'use strict'

const defaultsDeep = require('lodash.defaultsdeep')
const clone = require('lodash.clone')
const path = require('path')
const tmpDir = require('./utils/tmp-dir')
const Daemon = require('./ipfsd-daemon')
const merge = require('merge-options')
const defaultConfig = require('./defaults/config.json')
const defaultOptions = require('./defaults/options.json')

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

Expand All @@ -22,7 +19,7 @@ class FactoryDaemon {
if (options && options.type === 'proc') {
throw new Error('This Factory does not know how to spawn in proc nodes')
}
this.options = Object.assign({}, { type: 'go' }, options)
this.options = merge({ type: 'go' }, options)
}

/**
Expand All @@ -36,7 +33,7 @@ class FactoryDaemon {
* @returns {Promise}
*/
tmpDir (type) {
return tmpDir(type === 'js')
return Promise.resolve(tmpDir(type === 'js'))
}

/**
Expand Down Expand Up @@ -68,57 +65,31 @@ class FactoryDaemon {
* Spawn an IPFS node, either js-ipfs or go-ipfs
*
* @param {SpawnOptions} [options={}] - Various config options and ipfs config parameters
* @param {function(Error, Daemon): void} callback - Callback receives Error or a Daemon instance, Daemon has a `api` property which is an `ipfs-http-client` instance.
* @returns {void}
* @returns {Promise<Daemon>}
*/
async spawn (options = {}) {
// TODO this options parsing is daunting. Refactor and move to a separate
// func documenting what it is trying to do.
options = defaultsDeep(
{ IpfsClient: this.options.IpfsClient },
options,
defaultOptions
)

options.init = typeof options.init !== 'undefined'
? options.init
: true

if (!options.disposable) {
const nonDisposableConfig = clone(defaultConfig)
options.init = false
options.start = false

const defaultRepo = path.join(
process.env.HOME || process.env.USERPROFILE,
options.isJs
? '.jsipfs'
: '.ipfs'
)

options.repoPath = options.repoPath ||
(process.env.IPFS_PATH || defaultRepo)
options.config = defaultsDeep({}, options.config, nonDisposableConfig)
} else {
options.config = defaultsDeep({}, options.config, defaultConfig)
}
const daemonOptions = merge({
exec: this.options.exec,
type: this.options.type,
IpfsClient: this.options.IpfsClient,
disposable: true,
start: options.disposable !== false,
init: options.disposable !== false,
config: defaultConfig
}, options)

if (options.defaultAddrs) {
delete options.config.Addresses
delete daemonOptions.config.Addresses
}

options.type = this.options.type
options.exec = options.exec || this.options.exec
options.initOptions = defaultsDeep({}, this.options.initOptions, options.initOptions)

const node = new Daemon(options)
const node = new Daemon(daemonOptions)

if (options.init) {
await node.init(options.initOptions)
if (daemonOptions.init) {
await node.init(daemonOptions.initOptions)
}

if (options.start) {
await node.start(options.args)
if (daemonOptions.start) {
await node.start(daemonOptions.args)
}

return node
Expand Down
Loading

0 comments on commit f95d011

Please sign in to comment.