Skip to content
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
7 changes: 2 additions & 5 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -99,11 +100,7 @@ class Factory {
* @returns {Promise<ControllerDaemon>}
*/
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(
Expand Down
43 changes: 43 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
'use strict'

const { isNode } = require('ipfs-utils/src/env')

/**
* handle ControllerOptions
* @param {Factory} ctx
* @param {ControllerOptions} options
* @returns {Promise<ControllerOptions>}
*/
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