Skip to content

Commit

Permalink
fix: only require http api client if it has not been specified (#450)
Browse files Browse the repository at this point in the history
If we configure which http api module to use, don't try to require
`ipfs-http-client`, because that then means the containing project
needs to specify a dependency on `ipfs-http-client` when the
containing project might actually be `ipfs-http-client`...


Co-Authored-By: Hugo Dias <[email protected]>

Co-authored-by: Hugo Dias <[email protected]>
  • Loading branch information
achingbrain and hugomrdias authored Jan 31, 2020
1 parent 025c06f commit 6b21d5b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 50 deletions.
9 changes: 8 additions & 1 deletion .aegir.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const createServer = require('./src').createServer

const server = createServer() // using defaults
module.exports = {
bundlesize: { maxSize: '930kB' },
bundlesize: { maxSize: '35kB' },
karma: {
files: [{
pattern: 'test/fixtures/**/*',
Expand All @@ -16,5 +16,12 @@ module.exports = {
hooks: {
pre: () => server.start(),
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'
}
}
}
20 changes: 1 addition & 19 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,13 @@ os:
- windows

script:
- npm install ipfs
- npm install ipfs-http-client
- npm install go-ipfs-dep
- npx nyc -s npm run test:node -- --timeout 60000
after_success: npx nyc report --reporter=text-lcov > coverage.lcov && npx codecov

jobs:
include:
- stage: check
script:
- npm install ipfs
- npm install ipfs-http-client
- npm install go-ipfs-dep
- npx aegir build --bundlesize
- npx aegir commitlint --travis
- npx aegir dep-check
Expand All @@ -42,37 +36,25 @@ jobs:
addons:
chrome: stable
script:
- npm install ipfs
- npm install ipfs-http-client
- npm install go-ipfs-dep
- npx aegir test -t browser -t webworker --bail --timeout 60000

- stage: test
name: firefox
addons:
firefox: latest
script:
- npm install ipfs
- npm install ipfs-http-client
- npm install go-ipfs-dep
- npx aegir test -t browser -t webworker --bail --timeout 60000 -- --browsers FirefoxHeadless

- stage: test
name: electron-main
os: osx
script:
- npm install ipfs
- npm install ipfs-http-client
- npm install go-ipfs-dep
- npx aegir test -t electron-main --bail --timeout 60000

- stage: test
name: electron-renderer
os: osx
script:
- npm install ipfs
- npm install ipfs-http-client
- npm install go-ipfs-dep
- npx aegir test -t electron-renderer --bail --timeout 60000
notifications:
email: false
email: false
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"dirty-chai": "^2.0.1",
"go-ipfs-dep": "^0.4.22",
"husky": "^4.0.10",
"ipfs": "^0.40.0",
"ipfs-http-client": "^42.0.0-pre.0",
"lint-staged": "^10.0.2"
},
"peerDependencies": {
Expand Down
85 changes: 55 additions & 30 deletions src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ const defaults = {
type: 'go',
env: {},
args: [],
ipfsHttpModule: {
path: require.resolve('ipfs-http-client'),
ref: require('ipfs-http-client')
},
ipfsModule: {},
ipfsOptions: {},
forceKill: true,
forceKillTimeout: 5000
Expand All @@ -51,14 +46,6 @@ class Factory {
proc: merge(this.opts, { type: 'proc' })
}, overrides)

if (!this.overrides.js.ipfsBin) {
this.overrides.js.ipfsBin = findBin('js', this.opts.type === 'js')
}

if (!this.overrides.go.ipfsBin) {
this.overrides.go.ipfsBin = findBin('go', this.opts.type === 'go')
}

/** @type ControllerDaemon[] */
this.controllers = []
}
Expand Down Expand Up @@ -87,18 +74,33 @@ class Factory {
}

async _spawnRemote (options) {
const opts = {
json: {
...options,
ipfsModule: undefined,
ipfsHttpModule: undefined,
// avoid recursive spawning
remote: false
}
}

if (options.ipfsModule && options.ipfsModule.path) {
opts.json.ipfsModule = {
path: options.ipfsModule.path
// n.b. no ref property - do not send code refs over http
}
}

if (options.ipfsHttpModule && options.ipfsHttpModule.path) {
opts.json.ipfsHttpModule = {
path: options.ipfsHttpModule.path
// n.b. no ref property - do not send code refs over http
}
}

const res = await ky.post(
`${options.endpoint}/spawn`,
{
json: {
...options,
// avoid recursive spawning
remote: false,
// do not send code refs over http
ipfsModule: { ...options.ipfsModule, ref: undefined },
ipfsHttpModule: { ...options.ipfsHttpModule, ref: undefined }
}
}
opts
).json()
return new ControllerRemote(
options.endpoint,
Expand All @@ -116,16 +118,39 @@ class Factory {
const type = options.type || this.opts.type
const opts = merge(
this.overrides[type],
// conditionally include ipfs based on which type of daemon we will spawn when none has been specifed
(type === 'js' || type === 'proc') ? {
ipfsModule: {
path: require.resolve('ipfs'),
ref: require('ipfs')
}
} : {},
options
)

// conditionally include ipfs based on which type of daemon we will spawn when none has been specified
if ((opts.type === 'js' || opts.type === 'proc') && !opts.ipfsModule) {
opts.ipfsModule = {}
}

if (opts.ipfsModule) {
if (!opts.ipfsModule.path) {
opts.ipfsModule.path = require.resolve('ipfs')
}

if (!opts.ipfsModule.ref) {
opts.ipfsModule.ref = require('ipfs')
}
}

// only include the http api client if it has not been specified as an option
// for example if we are testing the http api client itself we should not try
// to require 'ipfs-http-client'
if (!opts.ipfsHttpModule) {
opts.ipfsHttpModule = {
path: require.resolve('ipfs-http-client'),
ref: require('ipfs-http-client')
}
}

// find ipfs binary if not specified
if (opts.type !== 'proc' && !opts.ipfsBin) {
opts.ipfsBin = findBin(opts.type, true)
}

// IPFS options defaults
const ipfsOptions = merge(
{
Expand Down

0 comments on commit 6b21d5b

Please sign in to comment.