diff --git a/.aegir.js b/.aegir.js index 48a04e99..6724bfc1 100644 --- a/.aegir.js +++ b/.aegir.js @@ -2,19 +2,23 @@ const createServer = require('./src').createServer -const server = createServer(null, { - ipfsModule: require('ipfs'), - ipfsHttpModule: require('ipfs-http-client') -}, { - go: { - ipfsBin: require('go-ipfs-dep').path() - }, - js: { - ipfsBin: require.resolve('ipfs/src/cli/bin.js') +const server = createServer(null, + { + ipfsModule: require('ipfs'), + ipfsHttpModule: require('ipfs-http-client') + }, + { + go: { + ipfsBin: require('go-ipfs-dep').path() + }, + js: { + ipfsBin: require.resolve('ipfs/src/cli/bin.js') + } } -}) // using defaults +) + module.exports = { - bundlesize: { maxSize: '35kB' }, + bundlesize: { maxSize: '33kB' }, karma: { files: [{ pattern: 'test/fixtures/**/*', @@ -24,14 +28,14 @@ module.exports = { }] }, hooks: { - pre: () => server.start(), + pre: async () => { + await server.start() + return { + env: { + IPFSD_CTL_SERVER: `http://localhost:${server.port}` + } + } + }, post: () => server.stop() - }, - webpack: process.env.NODE_ENV === 'test' ? undefined : { - externals: { - ipfs: 'ipfs', - 'ipfs-http-client': 'ipfs-http-client', - 'go-ipfs-dep': 'go-ipfs-dep' - } } } diff --git a/README.md b/README.md index 6200c744..01121ab5 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ const server = Ctl.createServer(port, { const factory = Ctl.createFactory({ ipfsHttpModule: require('ipfs-http-client'), remote: true, - endpoint: `http://localhost:${port}` + endpoint: `http://localhost:${port}` // or you can set process.env.IPFSD_CTL_SERVER to http://localhost:9090 }) await server.start() diff --git a/src/endpoint/server.js b/src/endpoint/server.js index f085a6b5..b9ba924f 100644 --- a/src/endpoint/server.js +++ b/src/endpoint/server.js @@ -1,6 +1,7 @@ 'use strict' const Hapi = require('@hapi/hapi') +const getPort = require('aegir/utils/get-port') const routes = require('./routes') /** @@ -15,10 +16,9 @@ class Server { * @param {function} createFactory */ constructor (options, createFactory) { - options = options || { port: 43134 } - + this.options = options || { port: 43134 } this.server = null - this.port = options.port + this.port = this.options.port this.createFactory = createFactory } @@ -28,6 +28,7 @@ class Server { * @returns {Promise} */ async start () { + this.port = await getPort(this.options.port, 'localhost') this.server = new Hapi.Server({ port: this.port, host: 'localhost', diff --git a/src/factory.js b/src/factory.js index ee429436..91cbf6f3 100644 --- a/src/factory.js +++ b/src/factory.js @@ -14,7 +14,7 @@ const testsConfig = require('./config') const defaults = { remote: !isNode, - endpoint: 'http://localhost:43134', + endpoint: process.env.IPFSD_CTL_SERVER || 'http://localhost:43134', disposable: true, test: false, type: 'go', diff --git a/src/index.js b/src/index.js index b770f532..f9cdd2e6 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,7 @@ 'use strict' -const Server = require('./endpoint/server') const Factory = require('./factory') +const Server = require('./endpoint/server') /** @typedef {import("./ipfsd-daemon")} Controller */