diff --git a/src/factory.js b/src/factory.js index 4ab990a2..00c48fee 100644 --- a/src/factory.js +++ b/src/factory.js @@ -7,6 +7,7 @@ const ControllerDaemon = require('./ipfsd-daemon') const ControllerRemote = require('./ipfsd-client') const ControllerProc = require('./ipfsd-in-proc') const testsConfig = require('./config') +const handleFactoryOptions = require('./options') /** @typedef {import("./index").ControllerOptions} ControllerOptions */ /** @typedef {import("./index").ControllerOptionsOverrides} ControllerOptionsOverrides */ @@ -99,11 +100,7 @@ class Factory { * @returns {Promise} */ async spawn (options = { }) { - const type = options.type || this.opts.type - const opts = merge( - this.overrides[type], - options - ) + const { type, opts } = await handleFactoryOptions(this, options) // IPFS options defaults const ipfsOptions = merge( diff --git a/src/options.js b/src/options.js new file mode 100644 index 00000000..fa037c20 --- /dev/null +++ b/src/options.js @@ -0,0 +1,43 @@ +'use strict' + +const { isNode } = require('ipfs-utils/src/env') + +/** + * handle ControllerOptions + * @param {Factory} ctx + * @param {ControllerOptions} options + * @returns {Promise} + */ +const handleFactoryOptions = async (ctx, options = { }) => { + const type = options.type || ctx.opts.type + const opts = merge( + ctx.overrides[type], + options + ) + + if (typeof opts.ipfsHttpModule === 'undefined') { + opts.ipfsHttpModule = require('ipfs-http-client') + } + + if (isNode && (typeof opts.ipfsModule === 'undefined' || typeof opts.ipfsBin === 'undefined')) { + if (type === 'js') { + if (typeof opts.ipfsModule === 'undefined') { + opts.ipfsModule = require('ipfs') + } + if (typeof opts.ipfsBin === 'undefined') { + opts.ipfsBin = require.resolve('ipfs/src/cli/bin.js') + } + } else if (type === 'go') { + if (typeof opts.ipfsBin === 'undefined') { + opts.ipfsBin = require('go-ipfs-dep').path() + } + } + } + + return { + type, + opts + } +} + +module.exports = handleFactoryOptions