Skip to content

Commit

Permalink
feat: change to ipfs-http-client (#311)
Browse files Browse the repository at this point in the history
feat: change to ipfs-http-client
  • Loading branch information
hugomrdias committed Nov 28, 2018
1 parent 7df2805 commit 412c18f
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 27 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Install one or both of the following modules:
- `go` - spawn go-ipfs daemon
- `js` - spawn js-ipfs daemon
- `proc` - spawn in-process js-ipfs instance. Needs to be called also with exec. Example: `DaemonFactory.create({type: 'proc', exec: require('ipfs') })`.
- `IpfsApi` - A custom IPFS API constructor to use instead of the packaged one
- `IpfsClient` - A custom IPFS API constructor to use instead of the packaged one

**example:** See [Usage](#usage)

Expand All @@ -128,7 +128,7 @@ Spawn the daemon
- `callback` - is a function with the signature `function (err, ipfsd)` where:
- `err` - is the error set if spawning the node is unsuccessful
- `ipfsd` - is the daemon controller instance:
- `api` - a property of `ipfsd`, an instance of [ipfs-api](https://github.com/ipfs/js-ipfs-api) attached to the newly created ipfs node
- `api` - a property of `ipfsd`, an instance of [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client) attached to the newly created ipfs node

**example:** See [Usage](#usage)

Expand Down Expand Up @@ -213,7 +213,7 @@ Start the daemon.

`flags` - Flags array to be passed to the `ipfs daemon` command.

`callback` is a function with the signature `function(err, ipfsApi)` that receives an instance of `Error` on failure or an instance of `ipfs-api` on success.
`callback` is a function with the signature `function(err, ipfsClient)` that receives an instance of `Error` on failure or an instance of `ipfs-http-client` on success.


#### `ipfsd.stop([timeout, callback])`
Expand Down Expand Up @@ -264,7 +264,7 @@ Get the version of ipfs

### IPFS HTTP Client - `ipfsd.api`

An instance of [ipfs-api](https://github.com/ipfs/js-ipfs-api#api) that is used to interact with the daemon.
An instance of [ipfs-http-client](https://github.com/ipfs/js-ipfs-http-client#api) that is used to interact with the daemon.

This instance is returned for each successfully started IPFS daemon, when either `df.spawn({start: true})` (the default) is called, or `ipfsd.start()` is invoked in the case of nodes that were spawned with `df.spawn({start: false})`.

Expand Down
84 changes: 84 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"execa": "^1.0.0",
"hapi": "^16.6.2",
"hat": "~0.0.3",
"ipfs-api": "^26.1.0",
"ipfs-http-client": "^27.0.0",
"joi": "^14.0.4",
"libp2p-crypto": "~0.14.0",
"lodash.clone": "^4.5.0",
Expand Down
4 changes: 2 additions & 2 deletions src/factory-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class FactoryClient {
}

/**
* Spawn a remote daemon using ipfs-api
* Spawn a remote daemon using ipfs-http-client
*
* @param {SpawnOptions} [options={}]
* @param {function(Error, DaemonClient)} callback
Expand Down Expand Up @@ -109,7 +109,7 @@ class FactoryClient {
res.body.initialized,
apiAddr,
gatewayAddr,
{ IpfsApi: this.options.IpfsApi }
{ IpfsClient: this.options.IpfsClient }
)

callback(null, ipfsd)
Expand Down
6 changes: 3 additions & 3 deletions src/factory-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class FactoryDaemon {
options = {}
}
options = Object.assign(
{ IpfsApi: this.options.IpfsApi },
{ IpfsClient: this.options.IpfsClient },
options,
{ type: this.options.type, exec: this.options.exec }
)
Expand All @@ -75,7 +75,7 @@ class FactoryDaemon {
* Spawn an IPFS node, either js-ipfs or go-ipfs
*
* @param {SpawnOptions} [options={}] - Various config options and ipfs config parameters
* @param {function(Error, Daemon): void} callback - Callback receives Error or a Daemon instance, Daemon has a `api` property which is an `ipfs-api` instance.
* @param {function(Error, Daemon): void} callback - Callback receives Error or a Daemon instance, Daemon has a `api` property which is an `ipfs-http-client` instance.
* @returns {void}
*/
spawn (options, callback) {
Expand All @@ -87,7 +87,7 @@ class FactoryDaemon {
// TODO this options parsing is daunting. Refactor and move to a separate
// func documenting what it is trying to do.
options = defaultsDeep(
{ IpfsApi: this.options.IpfsApi },
{ IpfsClient: this.options.IpfsClient },
options,
defaultOptions
)
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const Server = require('./endpoint/server')
* - go - spawn go-ipfs daemon
* - js - spawn js-ipfs daemon
* - proc - spawn in-process js-ipfs instance. Needs to be called also with exec. Example: `IPFSFactory.create({type: 'proc', exec: require('ipfs') })`.
* @param {Object} IpfsApi - A custom IPFS API constructor to use instead of the packaged one `js-ipfs-api`.
* @param {Object} IpfsClient - A custom IPFS API constructor to use instead of the packaged one `js-ipfs-http-client`.
* @returns {(FactoryDaemon|FactoryClient|FactoryInProc)}
*/
const create = (opts) => {
Expand Down
12 changes: 6 additions & 6 deletions src/ipfsd-client.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

const request = require('superagent')
const IpfsApi = require('ipfs-api')
const IpfsClient = require('ipfs-http-client')
const multiaddr = require('multiaddr')

function createApi (apiAddr, gwAddr, IpfsApi) {
function createApi (apiAddr, gwAddr, IpfsClient) {
let api
if (apiAddr) {
api = IpfsApi(apiAddr)
api = IpfsClient(apiAddr)
api.apiHost = multiaddr(apiAddr).nodeAddress().address
api.apiPort = multiaddr(apiAddr).nodeAddress().port
}
Expand Down Expand Up @@ -39,7 +39,7 @@ class Client {
this._gwAddr = multiaddr(gwAddrs)
this.initialized = initialized
this.started = false
this.api = createApi(apiAddr, gwAddrs, this.options.IpfsApi || IpfsApi)
this.api = createApi(apiAddr, gwAddrs, this.options.IpfsClient || IpfsClient)
}

/**
Expand Down Expand Up @@ -128,7 +128,7 @@ class Client {
* Start the daemon.
*
* @param {Array<string>} [flags=[]] - Flags to be passed to the `ipfs daemon` command.
* @param {function(Error, IpfsApi)} cb
* @param {function(Error, IpfsClient)} cb
* @returns {undefined}
*/
start (flags, cb) {
Expand All @@ -151,7 +151,7 @@ class Client {
const apiAddr = res.body.api ? res.body.api.apiAddr : ''
const gatewayAddr = res.body.api ? res.body.api.gatewayAddr : ''

this.api = createApi(apiAddr, gatewayAddr, this.options.IpfsApi || IpfsApi)
this.api = createApi(apiAddr, gatewayAddr, this.options.IpfsClient || IpfsClient)
return cb(null, this.api)
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/ipfsd-daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const fs = require('fs')
const waterfall = require('async/waterfall')
const series = require('async/series')
const IpfsApi = require('ipfs-api')
const IpfsClient = require('ipfs-http-client')
const multiaddr = require('multiaddr')
const rimraf = require('rimraf')
const path = require('path')
Expand Down Expand Up @@ -73,7 +73,7 @@ class Daemon {
this._apiAddr = null
this._gatewayAddr = null
this._started = false
/** @member {IpfsApi} */
/** @member {IpfsClient} */
this.api = null
this.bits = this.opts.initOptions ? this.opts.initOptions.bits : null
this._env = Object.assign({}, process.env, this.opts.env)
Expand Down Expand Up @@ -210,7 +210,7 @@ class Daemon {
* Start the daemon.
*
* @param {Array<string>} [flags=[]] - Flags to be passed to the `ipfs daemon` command.
* @param {function(Error, IpfsApi): void} callback
* @param {function(Error, IpfsClient): void} callback
* @return {void}
*/
start (flags, callback) {
Expand All @@ -228,7 +228,7 @@ class Daemon {

const setApiAddr = (addr) => {
this._apiAddr = multiaddr(addr)
this.api = (this.opts.IpfsApi || IpfsApi)(addr)
this.api = (this.opts.IpfsClient || IpfsClient)(addr)
this.api.apiHost = this.apiAddr.nodeAddress().address
this.api.apiPort = this.apiAddr.nodeAddress().port
}
Expand Down
2 changes: 1 addition & 1 deletion src/ipfsd-in-proc.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class InProc extends EventEmitter {
* Start the daemon.
*
* @param {Array<string>} [flags=[]] - Flags to be passed to the `ipfs daemon` command.
* @param {function(Error, IpfsApi)} callback
* @param {function(Error, IpfsClient)} callback
* @returns {undefined}
*/
start (flags, callback) {
Expand Down
4 changes: 2 additions & 2 deletions test/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('ipfsd.api for Daemons', () => {
})
},
(cb) => {
api.util.addFromFs(path.join(__dirname, 'fixtures/'), {
api.addFromFs(path.join(__dirname, 'fixtures/'), {
recursive: true
}, (err, res) => {
expect(err).to.not.exist()
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('ipfsd.api for Daemons', () => {
expect(ipfsd.gatewayAddr).to.not.be.null()
expect(multiaddr.isMultiaddr(ipfsd.gatewayAddr)).to.equal(true)

// Check for props in ipfs-api instance
// Check for props in ipfs-http-client instance
expect(ipfsd.api).to.have.property('apiHost')
expect(ipfsd.api).to.have.property('apiPort')
expect(ipfsd.api).to.have.property('gatewayHost')
Expand Down
6 changes: 3 additions & 3 deletions test/custom-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const IpfsApi = require('ipfs-api')
const IpfsClient = require('ipfs-http-client')
const IpfsFactory = require('../src')

describe('custom API', function () {
Expand All @@ -19,14 +19,14 @@ describe('custom API', function () {
const f = IpfsFactory.create({
type: 'js',
initOptions: { bits: 512 },
IpfsApi: () => mockApi
IpfsClient: () => mockApi
})

f.spawn((err, ipfsd) => {
if (err) return done(err)
expect(ipfsd.api).to.equal(mockApi)
// Restore a real API so that the node can be stopped properly
ipfsd.api = IpfsApi(ipfsd.apiAddr)
ipfsd.api = IpfsClient(ipfsd.apiAddr)
ipfsd.stop(done)
})
})
Expand Down

0 comments on commit 412c18f

Please sign in to comment.