Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove path and ref from module args also findBin #457

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions .aegir.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
'use strict'

const createServer = require('./src').createServer
const { findBin } = require('./src/utils')

const server = createServer(null, {
ipfsModule: {
path: require.resolve('ipfs'),
ref: require('ipfs')
},
ipfsHttpModule: {
path: require.resolve('ipfs-http-client'),
ref: require('ipfs-http-client')
}
ipfsModule: require('ipfs'),
ipfsHttpModule: require('ipfs-http-client')
}, {
go: {
ipfsBin: findBin('go', true)
ipfsBin: require('go-ipfs-dep').path()
},
js: {
ipfsBin: findBin('js', true)
ipfsBin: require.resolve('ipfs/src/cli/bin.js')
}
}) // using defaults
module.exports = {
Expand Down
7 changes: 5 additions & 2 deletions examples/electron-asar/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const app = electron.app
const ipcMain = electron.ipcMain
const BrowserWindow = electron.BrowserWindow

const { createNode, createServer } = require('ipfsd-ctl')
const { createController, createServer } = require('ipfsd-ctl')

app.on('ready', () => {
const win = new BrowserWindow({
Expand All @@ -24,7 +24,10 @@ ipcMain.on('start', async ({ sender }) => {
try {
const s = createServer()
await s.start()
const node = await createNode({ type: 'go' })
const node = await createController({
type: 'go',
ipfsBin: require('go-ipfs-dep').path()
})
console.log('get id')
sender.send('message', 'get id')

Expand Down
2 changes: 1 addition & 1 deletion examples/electron-asar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"private": true,
"main": "./app.js",
"dependencies": {
"go-ipfs-dep": "~0.4.22",
"go-ipfs-dep": "github:ipfs/npm-go-ipfs-dep#add-path-function-to-detect-binary",
"ipfs": "^0.39.0-rc.2",
"ipfsd-ctl": "file:../.."
},
Expand Down
20 changes: 16 additions & 4 deletions examples/id/id.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
/* eslint no-console: 0 */
'use strict'

const { createNode } = require('../../src')
const { createController } = require('../../src')

async function run () {
const node = await createNode({ type: 'go' })
const node = await createController({
type: 'go',
ipfsBin: require('go-ipfs-dep').path(),
ipfsHttpModule: require('ipfs-http-client')
})
console.log('alice')
console.log(await node.api.id())
await node.stop()

const nodeJs = await createNode({ type: 'js' })
const nodeJs = await createController({
type: 'js',
ipfsBin: require.resolve('ipfs/src/cli/bin.js'),
ipfsHttpModule: require('ipfs-http-client')
})
console.log('alice')
console.log(await nodeJs.api.id())
await nodeJs.stop()

const nodeProc = await createNode({ type: 'proc' })
const nodeProc = await createController({
type: 'proc',
ipfsModule: require('ipfs'),
ipfsHttpModule: require('ipfs-http-client')
})
console.log('bob')
console.log(await nodeProc.api.id())
await nodeProc.stop()
Expand Down
8 changes: 6 additions & 2 deletions examples/remote-disposable/remote-disposable.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
// Start a remote disposable node, and get access to the api
// print the node id, and stop the temporary daemon

const { createNode, createServer } = require('../../src')
const { createController, createServer } = require('../../src')
const server = createServer()

async function run () {
await server.start()
const node = await createNode({ remote: true })
const node = await createController({
remote: true,
type: 'go',
ipfsBin: require('go-ipfs-dep').path()
})

console.log(await node.api.id())
await node.stop()
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
"merge-options": "^2.0.0",
"multiaddr": "^7.2.1",
"nanoid": "^2.1.9",
"resolve-cwd": "^3.0.0",
"temp-write": "^4.0.0"
},
"devDependencies": {
Expand All @@ -71,10 +70,10 @@
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"go-ipfs-dep": "^0.4.22",
"go-ipfs-dep": "github:ipfs/npm-go-ipfs-dep#add-path-function-to-detect-binary",
"husky": "^4.0.10",
"ipfs": "^0.40.0",
"ipfs-http-client": "^42.0.0-pre.0",
"ipfs": "^0.41.0-rc.0",
"ipfs-http-client": "^42.0.0",
"lint-staged": "^10.0.2"
},
"peerDependencies": {
Expand Down
10 changes: 3 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,9 @@ module.exports = {
* - proc - spawn in-process js-ipfs node
* @property {Object} [env] - Additional environment variables, passed to executing shell. Only applies for Daemon controllers.
* @property {Array} [args] - Custom cli args.
* @property {Object} [ipfsHttpModule] - Setup IPFS HTTP client to be used by ctl.
* @property {Object} [ipfsHttpModule.ref] - Reference to a IPFS HTTP Client object. (defaults to the local require(`ipfs-http-client`))
* @property {string} [ipfsHttpModule.path] - Path to a IPFS HTTP Client to be required. (defaults to the local require.resolve('ipfs-http-client'))
* @property {Object} [ipfsModule] - Setup IPFS API to be used by ctl.
* @property {Object} [ipfsModule.ref] - Reference to a IPFS API object. (defaults to the local require(`ipfs`))
* @property {string} [ipfsModule.path] - Path to a IPFS API implementation to be required. (defaults to the local require.resolve('ipfs'))
* @property {String} [ipfsBin] - Path to a IPFS exectutable . (defaults to the local 'js-ipfs/src/bin/cli.js')
* @property {Object} [ipfsHttpModule] - Reference to a IPFS HTTP Client object. (defaults to the local require(`ipfs-http-client`))
* @property {Object} [ipfsModule] - Reference to a IPFS API object. (defaults to the local require(`ipfs`))
* @property {String} [ipfsBin] - Path to a IPFS exectutable
* @property {IpfsOptions} [ipfsOptions] - Options for the IPFS node.
* @property {boolean} [forceKill] - Whether to use SIGKILL to quit a daemon that does not stop after `.stop()` is called. (default true)
* @property {Number} [forceKillTimeout] - How long to wait before force killing a daemon in ms. (default 5000)
Expand Down
2 changes: 1 addition & 1 deletion src/ipfsd-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Client {
_setApi (addr) {
if (addr) {
this.apiAddr = multiaddr(addr)
this.api = (this.opts.ipfsHttpModule.ref)(addr)
this.api = this.opts.ipfsHttpModule(addr)
this.api.apiHost = this.apiAddr.nodeAddress().address
this.api.apiPort = this.apiAddr.nodeAddress().port
}
Expand Down
10 changes: 4 additions & 6 deletions src/ipfsd-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ class Daemon {
constructor (opts) {
/** @type ControllerOptions */
this.opts = opts
// make sure we have real paths
this.opts.ipfsHttpModule.path = fs.realpathSync(this.opts.ipfsHttpModule.path)
this.opts.ipfsBin = fs.realpathSync(this.opts.ipfsBin)

this.path = this.opts.ipfsOptions.repo || (opts.disposable ? tmpDir(opts.type) : defaultRepo(opts.type))
this.exec = this.opts.ipfsBin
this.env = merge({ IPFS_PATH: this.path }, this.opts.env)
Expand All @@ -61,7 +57,7 @@ class Daemon {
*/
_setApi (addr) {
this.apiAddr = multiaddr(addr)
this.api = require(this.opts.ipfsHttpModule.path)(addr)
this.api = this.opts.ipfsHttpModule(addr)
this.api.apiHost = this.apiAddr.nodeAddress().address
this.api.apiPort = this.apiAddr.nodeAddress().port
}
Expand Down Expand Up @@ -254,7 +250,9 @@ class Daemon {
try {
await this.api.stop()
} catch (err) {
if (!killed) throw err // if was killed then ignore error
if (!killed) {
throw err // if was killed then ignore error
}

daemonLog.info('Daemon was force killed')
}
Expand Down
6 changes: 4 additions & 2 deletions src/ipfsd-in-proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class InProc {
return
}

const IPFS = this.opts.ipfsModule.ref
const IPFS = this.opts.ipfsModule

this.api = await IPFS.create(merge({
silent: true,
repo: this.path
Expand All @@ -49,7 +50,7 @@ class InProc {
*/
_setApi (addr) {
this.apiAddr = multiaddr(addr)
this.api = (this.opts.ipfsHttpModule.ref)(addr)
this.api = this.opts.ipfsHttpModule(addr)
this.api.apiHost = this.apiAddr.nodeAddress().address
this.api.apiPort = this.apiAddr.nodeAddress().port
}
Expand Down Expand Up @@ -90,6 +91,7 @@ class InProc {

await this.setExec()
await this.api.init(opts)

this.clean = false
this.initialized = true
return this
Expand Down
4 changes: 0 additions & 4 deletions src/utils.browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ const checkForRunningApi = (path) => {
return null
}

const findBin = (type) => {
}

const tmpDir = (type = '') => {
return `${type}_ipfs_${nanoid()}`
}
Expand All @@ -57,6 +54,5 @@ module.exports = {
repoExists,
defaultRepo,
checkForRunningApi,
findBin,
tmpDir
}
15 changes: 1 addition & 14 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ const path = require('path')
const fs = require('fs-extra')
const debug = require('debug')
const nanoid = require('nanoid')
const resolveCwd = require('resolve-cwd')
const isWindows = os.platform() === 'win32'

const log = debug('ipfsd-ctl:utils')

Expand All @@ -26,7 +24,7 @@ const repoExists = async (repoPath) => {
const defaultRepo = (type) => {
return path.join(
os.homedir(),
type === 'js' ? '.jsipfs' : '.ipfs'
type === 'js' || type === 'proc' ? '.jsipfs' : '.ipfs'
)
}

Expand All @@ -41,16 +39,6 @@ const checkForRunningApi = (repoPath) => {
return api ? api.toString() : null
}

const findBin = (type, required) => {
const resolve = required ? resolveCwd : resolveCwd.silent

if (type === 'js') {
return process.env.IPFS_JS_EXEC || resolve('ipfs/src/cli/bin.js')
}

return process.env.IPFS_GO_EXEC || resolve(`go-ipfs-dep/go-ipfs/${isWindows ? 'ipfs.exe' : 'ipfs'}`)
}

const tmpDir = (type = '') => {
return path.join(os.tmpdir(), `${type}_ipfs_${nanoid()}`)
}
Expand All @@ -60,6 +48,5 @@ module.exports = {
repoExists,
defaultRepo,
checkForRunningApi,
findBin,
tmpDir
}
16 changes: 3 additions & 13 deletions test/browser.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ const expect = chai.expect
chai.use(dirtyChai)

const { isEnvWithDom } = require('ipfs-utils/src/env')
const { findBin, tmpDir, checkForRunningApi, defaultRepo, repoExists, removeRepo } = require('../src/utils')
const { tmpDir, checkForRunningApi, defaultRepo, repoExists, removeRepo } = require('../src/utils')
const { createFactory, createController } = require('../src')

describe('utils browser version', function () {
if (isEnvWithDom) {
it('findBin should return undefined', () => {
expect(findBin()).to.be.undefined()
})

it('tmpDir should return correct path', () => {
expect(tmpDir('js')).to.be.contain('js_ipfs_')
expect(tmpDir('go')).to.be.contain('go_ipfs_')
Expand All @@ -37,10 +33,7 @@ describe('utils browser version', function () {
type: 'proc',
disposable: false,
ipfsOptions: { repo: 'ipfs_test_remove' },
ipfsModule: {
path: require.resolve('ipfs'),
ref: require('ipfs')
}
ipfsModule: require('ipfs')
})
await ctl.init()
await ctl.start()
Expand All @@ -60,10 +53,7 @@ describe('utils browser version', function () {
ipfsOptions: {
repo: 'ipfs_test'
},
ipfsModule: {
path: require.resolve('ipfs'),
ref: require('ipfs')
}
ipfsModule: require('ipfs')
})
expect(await repoExists('ipfs_test')).to.be.true()
await node.stop()
Expand Down
Loading