Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
refactor: dont expose new getters, use options
Browse files Browse the repository at this point in the history
refactor: generator to factory
  • Loading branch information
jacobheun authored and alanshaw committed Aug 17, 2018
1 parent 3417b6f commit 289b12e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 53 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,11 +301,11 @@ Modify the default IPFS node config. This object will be *merged* with the defau
| Type | Default |
|------|---------|
| object | [`libp2p-nodejs.js`](https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-nodejs.js) in Node.js, [`libp2p-browser.js`](https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/libp2p-browser.js) in browsers |
| function | [`libp2p generator`](examples/custom-libp2p) |
| function | [`libp2p factory`](examples/custom-libp2p) |

The libp2p option allows you to build your libp2p node by configuration, or via a generator. If you are looking to just modify the below options, using the object format is the quickest way to get the default features of libp2p. If you need to create a more customized libp2p node, such as with custom transports or peer/content routers that need some of the ipfs data on startup, a generator is a great way to achieve this.
The libp2p option allows you to build your libp2p node by configuration, or via a factory. If you are looking to just modify the below options, using the object format is the quickest way to get the default features of libp2p. If you need to create a more customized libp2p node, such as with custom transports or peer/content routers that need some of the ipfs data on startup, a factory is a great way to achieve this.

You can see the generator in action in the [custom libp2p example](examples/custom-libp2p).
You can see the factory in action in the [custom libp2p example](examples/custom-libp2p).

- `modules` (object):
- `transport` (Array<[libp2p.Transport](https://github.com/libp2p/interface-transport)>): An array of Libp2p transport classes/instances to use _instead_ of the defaults. See [libp2p/interface-transport](https://github.com/libp2p/interface-transport) for details.
Expand Down
20 changes: 14 additions & 6 deletions examples/custom-libp2p/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const assert = require('assert')

/**
* Options for the libp2p factory
* @typedef {Object} libp2pFactory~options
* @property {PeerInfo} peerInfo - The PeerInfo of the IPFS node
* @property {PeerBook} peerBook - The PeerBook of the IPFS node
* @property {Object} config - The config of the IPFS node
* @property {Object} options - The options given to the IPFS node
*/

/**
* This is the factory we will use to create our fully customized libp2p node.
*
* @param {*} _ipfsNode The ipfs node. This houses the PeerInfo and PeerBook that modules may need
* @param {*} _ipfsConfig The config that is fetched from the ipfs-repo
* @param {libp2pFactory~options} opts The options to use when generating the libp2p node
* @returns {Libp2p} Our new libp2p node
*/
const libp2pFactory = (_ipfsNode, _ipfsConfig) => {
const libp2pFactory = (opts) => {
// Set convenience variables to clearly showcase some of the useful things that are available
const peerInfo = _ipfsNode.peerInfo
const peerBook = _ipfsNode.peerBook
const bootstrapList = _ipfsConfig.Bootstrap
const peerInfo = opts.peerInfo
const peerBook = opts.peerBook
const bootstrapList = opts.config.Bootstrap

// Create our WebSocketStar transport and give it our PeerId, straight from the ipfs node
const wsstar = new WebSocketStar({
Expand Down
53 changes: 29 additions & 24 deletions src/core/components/libp2p.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,42 @@ module.exports = function libp2p (self) {
return callback(err)
}

const defaultGenerator = (_ipfs, _config) => {
const defaultFactory = (opts) => {
const libp2pDefaults = {
peerInfo: _ipfs._peerInfo,
peerBook: _ipfs._peerInfoBook,
peerInfo: opts.peerInfo,
peerBook: opts.peerBook,
config: {
peerDiscovery: {
mdns: {
enabled: get(_ipfs._options, 'config.Discovery.MDNS.Enabled',
get(_config, 'Discovery.MDNS.Enabled', true))
enabled: get(opts.options, 'config.Discovery.MDNS.Enabled',
get(opts.config, 'Discovery.MDNS.Enabled', true))
},
webRTCStar: {
enabled: get(_ipfs._options, 'config.Discovery.webRTCStar.Enabled',
get(_config, 'Discovery.webRTCStar.Enabled', true))
enabled: get(opts.options, 'config.Discovery.webRTCStar.Enabled',
get(opts.config, 'Discovery.webRTCStar.Enabled', true))
},
bootstrap: {
list: get(_ipfs._options, 'config.Bootstrap',
get(_config, 'Bootstrap', []))
list: get(opts.options, 'config.Bootstrap',
get(opts.config, 'Bootstrap', []))
}
},
relay: {
enabled: get(_ipfs._options, 'relay.enabled',
get(_config, 'relay.enabled', false)),
enabled: get(opts.options, 'relay.enabled',
get(opts.config, 'relay.enabled', false)),
hop: {
enabled: get(_ipfs._options, 'relay.hop.enabled',
get(_config, 'relay.hop.enabled', false)),
active: get(_ipfs._options, 'relay.hop.active',
get(_config, 'relay.hop.active', false))
enabled: get(opts.options, 'relay.hop.enabled',
get(opts.config, 'relay.hop.enabled', false)),
active: get(opts.options, 'relay.hop.active',
get(opts.config, 'relay.hop.active', false))
}
},
EXPERIMENTAL: {
dht: get(_ipfs._options, 'EXPERIMENTAL.dht', false),
pubsub: get(_ipfs._options, 'EXPERIMENTAL.pubsub', false)
dht: get(opts.options, 'EXPERIMENTAL.dht', false),
pubsub: get(opts.options, 'EXPERIMENTAL.pubsub', false)
}
},
connectionManager: get(_ipfs._options, 'connectionManager',
get(_config, 'connectionManager', {}))
connectionManager: get(opts.options, 'connectionManager',
get(opts.config, 'connectionManager', {}))
}

const libp2pOptions = defaultsDeep(
Expand All @@ -62,13 +62,18 @@ module.exports = function libp2p (self) {
return new Node(libp2pOptions)
}

// Always create libp2p via a generator
let libp2pGenerator = get(self._options, 'libp2p', null)
if (typeof libp2pGenerator !== 'function') {
libp2pGenerator = defaultGenerator
// Always create libp2p via a factory
let libp2pFactory = get(self._options, 'libp2p', null)
if (typeof libp2pFactory !== 'function') {
libp2pFactory = defaultFactory
}

self._libp2pNode = libp2pGenerator(self, config)
self._libp2pNode = libp2pFactory({
options: self._options,
config: config,
peerInfo: self._peerInfo,
peerBook: self._peerInfoBook
})

self._libp2pNode.on('peer:discovery', (peerInfo) => {
const dial = () => {
Expand Down
14 changes: 0 additions & 14 deletions src/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,6 @@ class IPFS extends EventEmitter {

boot(this)
}

/**
* @type {PeerBook}
*/
get peerBook () {
return this._peerInfoBook
}

/**
* @type {PeerInfo}
*/
get peerInfo () {
return this._peerInfo
}
}

exports = module.exports = IPFS
Expand Down
12 changes: 6 additions & 6 deletions test/core/libp2p.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,19 @@ describe('libp2p customization', function () {
})
})

describe('generator', () => {
it('should allow for using a libp2p generator', (done) => {
describe('factory', () => {
it('should allow for using a libp2p factory', (done) => {
const ipfs = {
_peerInfo: peerInfo,
_peerBook: peerBook,
config: mockConfig,
_options: {
libp2p: (_ipfs, _ipfsConfig) => {
const wsstar = new WebSocketStar({id: _ipfs._peerInfo.id})
libp2p: (opts) => {
const wsstar = new WebSocketStar({id: opts.peerInfo.id})

return new Libp2p({
peerInfo: _ipfs._peerInfo,
peerBook: _ipfs._peerBook,
peerInfo: opts.peerInfo,
peerBook: opts.peerBook,
modules: {
transport: [
wsstar
Expand Down

0 comments on commit 289b12e

Please sign in to comment.