Skip to content

Commit

Permalink
feat: complete rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Oct 16, 2019
1 parent 8ea97a6 commit bbda5a9
Show file tree
Hide file tree
Showing 30 changed files with 479 additions and 895 deletions.
21 changes: 11 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"fs": false,
"fs-extra": false,
"joi": false,
"./src/utils/repo/nodejs.js": "./src/utils/repo/browser.js",
"./src/utils/repo.js": "./src/utils/repo.browser.js",
"./src/utils/exec.js": false,
"./src/utils/find-ipfs-executable.js": false,
"./src/utils/tmp-dir.js": "./src/utils/tmp-dir-browser.js",
"./src/utils/tmp-dir.js": "./src/utils/tmp-dir.browser.js",
"./src/utils/run.js": false,
"./src/factory-daemon.js": false,
"./src/ipfsd-daemon.js": false,
Expand Down Expand Up @@ -62,29 +62,30 @@
"daemon"
],
"dependencies": {
"@hapi/boom": "^7.4.7",
"@hapi/boom": "^8.0.1",
"@hapi/hapi": "^18.3.2",
"@hapi/joi": "^15.1.1",
"@hapi/joi": "^16.1.7",
"debug": "^4.1.1",
"execa": "^2.0.4",
"execa": "^3.1.0",
"fs-extra": "^8.1.0",
"hat": "~0.0.3",
"ipfs-http-client": "^38.2.0",
"ipfs-http-client": "^39.0.0",
"ipfs-utils": "^0.4.0",
"merge-options": "^1.0.1",
"multiaddr": "^6.1.1",
"safe-json-stringify": "^1.2.0",
"superagent": "^5.0.5"
"superagent": "^5.0.5",
"tempy": "^0.3.0"
},
"devDependencies": {
"aegir": "^20.3.2",
"aegir": "^20.4.1",
"chai": "^4.2.0",
"delay": "^4.3.0",
"detect-port": "^1.3.0",
"dirty-chai": "^2.0.1",
"go-ipfs-dep": "~0.4.22",
"husky": "^3.0.8",
"ipfs": "~0.38.0",
"husky": "^3.0.9",
"ipfs": "~0.39.0-rc.1",
"is-running": "^2.1.0",
"lint-staged": "^9.4.2",
"proxyquire": "^2.1.3",
Expand Down
55 changes: 27 additions & 28 deletions src/endpoint/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
const hat = require('hat')
const Joi = require('@hapi/joi')
const boom = require('@hapi/boom')
const merge = require('merge-options')
const FactoryDaemon = require('../factory-daemon')
const tmpDir = require('../utils/tmp-dir')

const routeConfig = {
const routeOptions = {
validate: {
query: {
query: Joi.object({
id: Joi.string().alphanum().required()
}
})
}
}

Expand All @@ -30,7 +29,7 @@ module.exports = (server) => {
handler: async (request) => {
const type = request.query.type || 'go'
try {
return { tmpDir: await tmpDir(type === 'js') }
return { tmpDir: await tmpDir(type) }
} catch (err) {
throw boom.badRequest(err.message)
}
Expand Down Expand Up @@ -67,10 +66,10 @@ module.exports = (server) => {
const payload = request.payload || {}

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

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

Expand All @@ -80,7 +79,7 @@ module.exports = (server) => {
gatewayAddr: ipfsd.gatewayAddr ? ipfsd.gatewayAddr.toString() : '',
initialized: ipfsd.initialized,
started: ipfsd.started,
_env: ipfsd._env,
_env: ipfsd.env,
path: ipfsd.path
}
} catch (err) {
Expand All @@ -100,7 +99,7 @@ module.exports = (server) => {
const payload = request.payload || {}

try {
await nodes[id].init(payload.initOpts)
await nodes[id].init(payload.opts)

return {
initialized: nodes[id].initialized
Expand All @@ -109,7 +108,7 @@ module.exports = (server) => {
throw boom.badRequest(err.message)
}
},
config: routeConfig
options: routeOptions
})

/*
Expand All @@ -120,11 +119,9 @@ module.exports = (server) => {
path: '/start',
handler: async (request) => {
const id = request.query.id
const payload = request.payload || {}
const flags = payload.flags || []

try {
await nodes[id].start(flags)
await nodes[id].start()

return {
apiAddr: nodes[id].apiAddr.toString(),
Expand All @@ -134,7 +131,7 @@ module.exports = (server) => {
throw boom.badRequest(err.message)
}
},
config: routeConfig
options: routeOptions
})

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

return { apiAddr: nodes[id].apiAddr.toString() }
},
config: routeConfig
options: routeOptions
})

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

return { getawayAddr: nodes[id].gatewayAddr.toString() }
},
config: routeConfig
options: routeOptions
})

/*
Expand All @@ -185,7 +182,7 @@ module.exports = (server) => {
throw boom.badRequest(err.message)
}
},
config: routeConfig
options: routeOptions
})

/*
Expand All @@ -206,7 +203,7 @@ module.exports = (server) => {
throw boom.badRequest(err.message)
}
},
config: routeConfig
options: routeOptions
})

/*
Expand All @@ -230,7 +227,7 @@ module.exports = (server) => {
throw boom.badRequest(err.message)
}
},
config: routeConfig
options: routeOptions
})

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

return { pid: nodes[id].pid }
},
config: routeConfig
options: routeOptions
})

/*
Expand All @@ -267,13 +264,14 @@ module.exports = (server) => {
throw boom.badRequest(err.message)
}
},
config: merge(routeConfig, {
options: {
validate: {
query: {
query: Joi.object({
id: Joi.string().alphanum().required(),
key: Joi.string().optional()
}
})
}
})
}
})

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

return h.response().code(200)
},
config: merge(routeConfig, {
options: {
validate: {
payload: {
query: Joi.object({
id: Joi.string().alphanum().required(),
key: Joi.string(),
value: Joi.any()
}
})
}
})
}
})
}
92 changes: 53 additions & 39 deletions src/factory-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,108 @@ const request = require('superagent')
const DaemonClient = require('./ipfsd-client')
const merge = require('merge-options')
const defaultConfig = require('./defaults/config.json')
// const findBin = require('./utils/find-ipfs-executable')

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

/**
* Exposes the same Factory API but uses a remote endpoint to create the Daemons/Nodes
* @param {Object} options
*/
class FactoryClient {
constructor (options) {
options = options || {}
if (!options.host) { options.host = 'localhost' }
if (!options.port) { options.port = 43134 }
if (!options.type) { options.type = 'go' }
if (typeof options.host === 'number') {
options.port = options.host
options.host = 'localhost'
}

this.options = options

if (options.type === 'proc') {
throw new Error('\'proc\' is not supported in client mode')
}
/**
* @param {FactoryOptions} options
*/
constructor (options = {}) {
/** @type FactoryOptions */
this.options = merge({
host: 'localhost',
port: 43134,
secure: false,
type: 'go',
defaultAddrs: false,
disposable: true,
env: process.env,
args: [],
ipfsHttp: {
path: require.resolve('ipfs-http-client'),
ref: require('ipfs-http-client')
},
ipfsApi: {
path: require.resolve('ipfs'),
ref: require('ipfs')
}
// ipfsBin: findBin(options.type || 'go')
}, options)

this.baseUrl = `${options.secure ? 'https://' : 'http://'}${options.host}:${options.port}`
this.baseUrl = `${this.options.secure ? 'https://' : 'http://'}${this.options.host}:${this.options.port}`
}

/**
* 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 (isJS) {
async tmpDir () {
const res = await request
.get(`${this.baseUrl}/util/tmp-dir`)
.query({ type: isJS ? 'js' : 'go' })
.query({ type: this.options.type })

return res.body.tmpDir
}

/**
* Get the version of the IPFS Daemon.
*
* @param {Object} [options={}]
* @returns {Promise}
*/
async version (options = {}) {
options = options || { type: this.options.type }

async version () {
const res = await request
.get(`${this.baseUrl}/version`)
.query(options)
.query({ type: this.options.type })

return res.body.version
}

/**
* Spawn a remote daemon using ipfs-http-client
*
* @param {SpawnOptions} [options={}]
* @param {IpfsOptions} [options={}] - Same as js-ipfs https://github.com/ipfs/js-ipfs#ipfs-constructor
* @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,
const ipfsOptions = merge({
start: this.options.disposable !== false,
init: this.options.disposable !== false,
config: defaultConfig
}, options)
},
this.options.ipfsOptions,
options)

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

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

const ipfsd = new DaemonClient(
this.baseUrl,
res.body,
daemonOptions
merge(
{
ipfsOptions
},
this.options
)
)

return ipfsd
Expand Down
Loading

0 comments on commit bbda5a9

Please sign in to comment.