Compare commits

..

3 Commits

Author SHA1 Message Date
Jacob Heun
8e95df7f22 chore: release version v0.25.6 2019-08-20 16:12:04 +02:00
Jacob Heun
67cbec17a6 chore: update contributors 2019-08-20 16:12:03 +02:00
Jacob Heun
2f6fea9756 fix: config validation (#405)
* test: backport content provide test update

* fix: avoid using superstruct interface
2019-08-20 15:53:04 +02:00
35 changed files with 340 additions and 646 deletions

View File

@@ -1,6 +1,7 @@
'use strict'
const pull = require('pull-stream')
const parallel = require('async/parallel')
const WebSocketStarRendezvous = require('libp2p-websocket-star-rendezvous')
const sigServer = require('libp2p-webrtc-star/src/sig-server')
@@ -14,46 +15,68 @@ const {
let wrtcRendezvous
let wsRendezvous
let node
let peerInfo
const before = async () => {
[wrtcRendezvous, wsRendezvous, peerInfo] = await Promise.all([
sigServer.start({
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
}),
WebSocketStarRendezvous.start({
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
refreshPeerListIntervalMS: 1000,
strictMultiaddr: false,
cryptoChallenge: true
}),
getPeerRelay()
])
node = new Node({
peerInfo,
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: true
const before = (done) => {
parallel([
(cb) => {
sigServer.start({
port: WRTC_RENDEZVOUS_MULTIADDR.nodeAddress().port
// cryptoChallenge: true TODO: needs https://github.com/libp2p/js-libp2p-webrtc-star/issues/128
})
.then(server => {
wrtcRendezvous = server
cb()
})
.catch(cb)
},
(cb) => {
WebSocketStarRendezvous.start({
port: WS_RENDEZVOUS_MULTIADDR.nodeAddress().port,
refreshPeerListIntervalMS: 1000,
strictMultiaddr: false,
cryptoChallenge: true
}, (err, _server) => {
if (err) {
return cb(err)
}
wsRendezvous = _server
cb()
})
},
(cb) => {
getPeerRelay((err, peerInfo) => {
if (err) {
return done(err)
}
}
}
})
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
await node.start()
node = new Node({
peerInfo,
config: {
relay: {
enabled: true,
hop: {
enabled: true,
active: true
}
}
}
})
node.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
node.start(cb)
})
}
], done)
}
const after = () => {
return Promise.all([
wrtcRendezvous.stop(),
wsRendezvous.stop(),
node.stop()
])
const after = (done) => {
setTimeout(() =>
parallel([
(cb) => wrtcRendezvous.stop().then(cb).catch(cb),
...[node, wsRendezvous].map((s) => (cb) => s.stop(cb)),
], done),
2000
)
}
module.exports = {

View File

@@ -1,70 +1,10 @@
<a name="0.26.0"></a>
# [0.26.0](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.3...v0.26.0) (2019-08-07)
<a name="0.26.0-rc.3"></a>
# [0.26.0-rc.3](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.2...v0.26.0-rc.3) (2019-08-06)
<a name="0.25.6"></a>
## [0.25.6](https://github.com/libp2p/js-libp2p/compare/v0.25.5...v0.25.6) (2019-08-20)
### Bug Fixes
* promisified methods ([#398](https://github.com/libp2p/js-libp2p/issues/398)) ([ff7a6c8](https://github.com/libp2p/js-libp2p/commit/ff7a6c8))
<a name="0.26.0-rc.2"></a>
# [0.26.0-rc.2](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.1...v0.26.0-rc.2) (2019-08-01)
### Bug Fixes
* dont override methods of created instance ([#394](https://github.com/libp2p/js-libp2p/issues/394)) ([3e95e6f](https://github.com/libp2p/js-libp2p/commit/3e95e6f))
* pubsub default config ([#393](https://github.com/libp2p/js-libp2p/issues/393)) ([f4f3f0f](https://github.com/libp2p/js-libp2p/commit/f4f3f0f))
### Chores
* update switch ([#395](https://github.com/libp2p/js-libp2p/issues/395)) ([684f283](https://github.com/libp2p/js-libp2p/commit/684f283))
### BREAKING CHANGES
* switch configuration has changed.
'blacklistTTL' is now 'denyTTL' and 'blackListAttempts' is now 'denyAttempts'
<a name="0.26.0-rc.1"></a>
# [0.26.0-rc.1](https://github.com/libp2p/js-libp2p/compare/v0.26.0-rc.0...v0.26.0-rc.1) (2019-07-31)
<a name="0.26.0-rc.0"></a>
# [0.26.0-rc.0](https://github.com/libp2p/js-libp2p/compare/v0.25.5...v0.26.0-rc.0) (2019-07-31)
### Bug Fixes
* make subscribe comply with ipfs interface ([#389](https://github.com/libp2p/js-libp2p/issues/389)) ([9554b05](https://github.com/libp2p/js-libp2p/commit/9554b05))
### Features
* integrate gossipsub by default ([#365](https://github.com/libp2p/js-libp2p/issues/365)) ([791f39a](https://github.com/libp2p/js-libp2p/commit/791f39a))
* promisify all api methods that accept callbacks ([#381](https://github.com/libp2p/js-libp2p/issues/381)) ([df6ef45](https://github.com/libp2p/js-libp2p/commit/df6ef45))
### BREAKING CHANGES
* new configuration for deciding the implementation of pubsub to be used.
In this context, the experimental flags were also removed. See the README for the latest usage.
* The ipfs interface specified that options
should be provided after the handler, not before.
https://github.com/ipfs/interface-js-ipfs-core/blob/v0.109.0/SPEC/PUBSUB.md#pubsubsubscribe
This corrects the order of parameters. See the jsdocs examples
for subscribe to see how it should be used.
* config validation ([#405](https://github.com/libp2p/js-libp2p/issues/405)) ([2f6fea9](https://github.com/libp2p/js-libp2p/commit/2f6fea9))

View File

@@ -94,8 +94,6 @@ npm install --save libp2p
## Usage
**IMPORTANT NOTE**: We are currently on the way of migrating all our `libp2p` modules to use `async await` and `async iterators`, instead of callbacks and `pull-streams`. As a consequence, when you start a new libp2p project, we must check which versions of the modules you should use. For now, it is required to use the modules using callbacks with `libp2p`, while we are working on getting the remaining modules ready for a full migration. For more details, you can have a look at [libp2p/js-libp2p#266](https://github.com/libp2p/js-libp2p/issues/266).
### [Tutorials and Examples](/examples)
You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarios.
@@ -121,7 +119,6 @@ const MPLEX = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const MulticastDNS = require('libp2p-mdns')
const DHT = require('libp2p-kad-dht')
const GossipSub = require('libp2p-gossipsub')
const defaultsDeep = require('@nodeutils/defaults-deep')
const Protector = require('libp2p-pnet')
const DelegatedPeerRouter = require('libp2p-delegated-peer-routing')
@@ -157,8 +154,7 @@ class Node extends Libp2p {
peerDiscovery: [
MulticastDNS
],
dht: DHT, // DHT enables PeerRouting, ContentRouting and DHT itself components
pubsub: GossipSub
dht: DHT // DHT enables PeerRouting, ContentRouting and DHT itself components
},
// libp2p config options (typically found on a config.json)
@@ -191,8 +187,9 @@ class Node extends Libp2p {
timeout: 10e3
}
},
pubsub: {
enabled: true
// Enable/Disable Experimental features
EXPERIMENTAL: { // Experimental features ("behind a flag")
pubsub: false
}
}
}
@@ -207,8 +204,6 @@ class Node extends Libp2p {
### API
**IMPORTANT NOTE**: All the methods listed in the API section that take a callback are also now Promisified. Libp2p is migrating away from callbacks to async/await, and in a future release (that will be announced in advance), callback support will be removed entirely. You can follow progress of the async/await endeavor at https://github.com/ipfs/js-ipfs/issues/1670.
#### Create a Node - `Libp2p.createLibp2p(options, callback)`
> Behaves exactly like `new Libp2p(options)`, but doesn't require a PeerInfo. One will be generated instead

View File

@@ -17,10 +17,7 @@
},
"dependencies": {
"detect-dom-ready": "^1.0.2",
"libp2p": "../../../",
"libp2p-bootstrap": "~0.9.7",
"libp2p-gossipsub": "~0.0.4",
"libp2p-kad-dht": "^0.15.3",
"libp2p-mplex": "~0.8.5",
"libp2p-secio": "~0.11.1",
"libp2p-spdy": "~0.13.3",

View File

@@ -8,8 +8,8 @@ const SPDY = require('libp2p-spdy')
const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap')
const DHT = require('libp2p-kad-dht')
const Gossipsub = require('libp2p-gossipsub')
const libp2p = require('libp2p')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../../../../')
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json
const bootstrapList = [
@@ -26,9 +26,9 @@ const bootstrapList = [
]
class Node extends libp2p {
constructor ({ peerInfo }) {
const wrtcStar = new WebRTCStar({ id: peerInfo.id })
const wsstar = new WebSocketStar({ id: peerInfo.id })
constructor (_options) {
const wrtcStar = new WebRTCStar({ id: _options.peerInfo.id })
const wsstar = new WebSocketStar({ id: _options.peerInfo.id })
const defaults = {
modules: {
@@ -49,8 +49,7 @@ class Node extends libp2p {
wsstar.discovery,
Bootstrap
],
dht: DHT,
pubsub: Gossipsub
dht: DHT
},
config: {
peerDiscovery: {
@@ -77,8 +76,8 @@ class Node extends libp2p {
dht: {
enabled: false
},
pubsub: {
enabled: false
EXPERIMENTAL: {
pubsub: false
}
},
connectionManager: {
@@ -87,7 +86,7 @@ class Node extends libp2p {
}
}
super({ ...defaults, peerInfo })
super(defaultsDeep(_options, defaults))
}
}

View File

@@ -10,11 +10,9 @@ function createNode (callback) {
}
const peerIdStr = peerInfo.id.toB58String()
const webrtcAddr = `/dns4/star-signal.cloud.ipfs.team/tcp/443/wss/p2p-webrtc-star/p2p/${peerIdStr}`
const wsAddr = `/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star`
const ma = `/dns4/star-signal.cloud.ipfs.team/tcp/443/wss/p2p-webrtc-star/p2p/${peerIdStr}`
peerInfo.multiaddrs.add(webrtcAddr)
peerInfo.multiaddrs.add(wsAddr)
peerInfo.multiaddrs.add(ma)
const node = new Node({
peerInfo

View File

@@ -46,9 +46,6 @@ domReady(() => {
myPeerDiv.append(idDiv)
console.log('Node is listening o/')
node.peerInfo.multiaddrs.toArray().forEach(ma => {
console.log(ma.toString())
})
// NOTE: to stop the node
// node.stop((err) => {})

View File

@@ -20,3 +20,5 @@ Then simply go into the folder [1](./1) and execute the following
> npm start
# open your browser in port :9090
```
[Version Published on IPFS](http://ipfs.io/ipfs/Qmbc1J7ehw1dNYachbkCWPto4RsnVvqCKNVzmYEod2gXcy)

View File

@@ -7,10 +7,10 @@ const Mplex = require('libp2p-mplex')
const SECIO = require('libp2p-secio')
const PeerInfo = require('peer-info')
const MulticastDNS = require('libp2p-mdns')
const Gossipsub = require('libp2p-gossipsub')
const defaultsDeep = require('@nodeutils/defaults-deep')
const waterfall = require('async/waterfall')
const parallel = require('async/parallel')
const series = require('async/series')
class MyBundle extends libp2p {
constructor (_options) {
@@ -19,8 +19,7 @@ class MyBundle extends libp2p {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
peerDiscovery: [ MulticastDNS ],
pubsub: Gossipsub
peerDiscovery: [ MulticastDNS ]
},
config: {
peerDiscovery: {
@@ -28,6 +27,9 @@ class MyBundle extends libp2p {
interval: 2000,
enabled: true
}
},
EXPERIMENTAL: {
pubsub: true
}
}
}

View File

@@ -1,6 +1,6 @@
{
"name": "libp2p",
"version": "0.26.0",
"version": "0.25.6",
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js",
@@ -48,8 +48,9 @@
"err-code": "^1.1.2",
"fsm-event": "^2.1.0",
"libp2p-connection-manager": "^0.1.0",
"libp2p-floodsub": "^0.16.1",
"libp2p-ping": "^0.8.5",
"libp2p-switch": "^0.43.0",
"libp2p-switch": "^0.42.12",
"libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7",
"multiaddr": "^6.1.0",
@@ -57,12 +58,11 @@
"peer-book": "^0.9.1",
"peer-id": "^0.12.2",
"peer-info": "^0.15.1",
"promisify-es6": "^1.0.3",
"superstruct": "^0.6.0"
},
"devDependencies": {
"@nodeutils/defaults-deep": "^1.1.0",
"aegir": "^20.0.0",
"aegir": "^19.0.3",
"chai": "^4.2.0",
"chai-checkmark": "^1.0.1",
"cids": "^0.7.1",
@@ -73,9 +73,7 @@
"libp2p-circuit": "^0.3.7",
"libp2p-delegated-content-routing": "^0.2.2",
"libp2p-delegated-peer-routing": "^0.2.2",
"libp2p-floodsub": "~0.17.0",
"libp2p-gossipsub": "~0.0.4",
"libp2p-kad-dht": "^0.15.3",
"libp2p-kad-dht": "^0.15.2",
"libp2p-mdns": "^0.12.3",
"libp2p-mplex": "^0.8.4",
"libp2p-secio": "^0.11.1",
@@ -83,9 +81,8 @@
"libp2p-tcp": "^0.13.0",
"libp2p-webrtc-star": "^0.16.1",
"libp2p-websocket-star": "~0.10.2",
"libp2p-websocket-star-rendezvous": "~0.4.1",
"libp2p-websocket-star-rendezvous": "~0.3.0",
"lodash.times": "^4.3.2",
"merge-options": "^1.0.1",
"nock": "^10.0.6",
"pull-goodbye": "0.0.2",
"pull-mplex": "^0.1.2",
@@ -98,7 +95,6 @@
"Aditya Bose <13054902+adbose@users.noreply.github.com>",
"Alan Shaw <alan.shaw@protocol.ai>",
"Alan Shaw <alan@tableflip.io>",
"Alex Potsides <alex@achingbrain.net>",
"Andrew Nesbitt <andrewnez@gmail.com>",
"Chris Bratlien <chrisbratlien@gmail.com>",
"Chris Dostert <chrisdostert@users.noreply.github.com>",

View File

@@ -4,30 +4,33 @@ const { struct, superstruct } = require('superstruct')
const { optional, list } = struct
// Define custom types
const s = superstruct()
const transport = s.union([
s.interface({
createListener: 'function',
dial: 'function'
}),
'function'
])
const s = superstruct({
types: {
transport: value => {
if (value.length === 0) return 'ERROR_EMPTY'
value.forEach(i => {
if (!i.dial) return 'ERR_NOT_A_TRANSPORT'
})
return true
},
protector: value => {
if (!value.protect) return 'ERR_NOT_A_PROTECTOR'
return true
}
}
})
const modulesSchema = s({
connEncryption: optional(list([s('object|function')])),
// this is hacky to simulate optional because interface doesnt work correctly with it
// change to optional when fixed upstream
connProtector: s.union(['undefined', s.interface({ protect: 'function' })]),
connProtector: s('undefined|protector'),
contentRouting: optional(list(['object'])),
dht: optional(s('null|function|object')),
pubsub: optional(s('null|function|object')),
peerDiscovery: optional(list([s('object|function')])),
peerRouting: optional(list(['object'])),
streamMuxer: optional(list([s('object|function')])),
transport: s.intersection([[transport], s.interface({
length (v) {
return v > 0 ? true : 'ERROR_EMPTY'
}
})])
transport: 'transport'
})
const configSchema = s({
@@ -60,10 +63,12 @@ const configSchema = s({
timeout: 10e3
}
}),
// Pubsub config
pubsub: s('object?', {
// Pubsub defaults
enabled: true
// Experimental config
EXPERIMENTAL: s({
pubsub: 'boolean'
}, {
// Experimental defaults
pubsub: false
})
}, {})

View File

@@ -3,7 +3,6 @@
const tryEach = require('async/tryEach')
const parallel = require('async/parallel')
const errCode = require('err-code')
const promisify = require('promisify-es6')
module.exports = (node) => {
const routers = node._modules.contentRouting || []
@@ -25,7 +24,7 @@ module.exports = (node) => {
* @param {function(Error, Result<Array>)} callback
* @returns {void}
*/
findProviders: promisify((key, options, callback) => {
findProviders: (key, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
@@ -61,7 +60,7 @@ module.exports = (node) => {
results = results || []
callback(null, results)
})
}),
},
/**
* Iterates over all content routers in parallel to notify it is
@@ -71,7 +70,7 @@ module.exports = (node) => {
* @param {function(Error)} callback
* @returns {void}
*/
provide: promisify((key, callback) => {
provide: (key, callback) => {
if (!routers.length) {
return callback(errCode(new Error('No content routers available'), 'NO_ROUTERS_AVAILABLE'))
}
@@ -79,6 +78,6 @@ module.exports = (node) => {
parallel(routers.map((router) => {
return (cb) => router.provide(key, cb)
}), callback)
})
}
}
}

View File

@@ -2,20 +2,19 @@
const nextTick = require('async/nextTick')
const errCode = require('err-code')
const promisify = require('promisify-es6')
const { messages, codes } = require('./errors')
module.exports = (node) => {
return {
put: promisify((key, value, callback) => {
put: (key, value, callback) => {
if (!node._dht) {
return nextTick(callback, errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED))
}
node._dht.put(key, value, callback)
}),
get: promisify((key, options, callback) => {
},
get: (key, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
@@ -26,8 +25,8 @@ module.exports = (node) => {
}
node._dht.get(key, options, callback)
}),
getMany: promisify((key, nVals, options, callback) => {
},
getMany: (key, nVals, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
@@ -38,6 +37,6 @@ module.exports = (node) => {
}
node._dht.getMany(key, nVals, options, callback)
})
}
}
}

View File

@@ -4,13 +4,12 @@ const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const errCode = require('err-code')
const promisify = require('promisify-es6')
module.exports = (node) => {
/*
* Helper method to check the data type of peer and convert it to PeerInfo
*/
return promisify(function (peer, callback) {
return function (peer, callback) {
let p
// PeerInfo
if (PeerInfo.isPeerInfo(peer)) {
@@ -63,5 +62,5 @@ module.exports = (node) => {
}
callback(null, p)
})
}
}

View File

@@ -6,7 +6,6 @@ const debug = require('debug')
const log = debug('libp2p')
log.error = debug('libp2p:error')
const errCode = require('err-code')
const promisify = require('promisify-es6')
const each = require('async/each')
const series = require('async/series')
@@ -69,7 +68,7 @@ class Libp2p extends EventEmitter {
// Attach stream multiplexers
if (this._modules.streamMuxer) {
const muxers = this._modules.streamMuxer
let muxers = this._modules.streamMuxer
muxers.forEach((muxer) => this._switch.connection.addStreamMuxer(muxer))
// If muxer exists
@@ -99,7 +98,7 @@ class Libp2p extends EventEmitter {
// Attach crypto channels
if (this._modules.connEncryption) {
const cryptos = this._modules.connEncryption
let cryptos = this._modules.connEncryption
cryptos.forEach((crypto) => {
this._switch.connection.crypto(crypto.tag, crypto.encrypt)
})
@@ -122,9 +121,9 @@ class Libp2p extends EventEmitter {
})
}
// start pubsub
if (this._modules.pubsub && this._config.pubsub.enabled !== false) {
this.pubsub = pubsub(this, this._modules.pubsub)
// enable/disable pubsub
if (this._config.EXPERIMENTAL.pubsub) {
this.pubsub = pubsub(this)
}
// Attach remaining APIs
@@ -187,11 +186,6 @@ class Libp2p extends EventEmitter {
})
this._peerDiscovered = this._peerDiscovered.bind(this)
// promisify all instance methods
;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
this[method] = promisify(this[method], { context: this })
})
}
/**
@@ -300,13 +294,6 @@ class Libp2p extends EventEmitter {
})
}
/**
* Disconnects from the given peer
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
* @param {function(Error)} callback
* @returns {void}
*/
hangUp (peer, callback) {
this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) }
@@ -315,13 +302,6 @@ class Libp2p extends EventEmitter {
})
}
/**
* Pings the provided peer
*
* @param {PeerInfo|PeerId|Multiaddr|string} peer The peer to ping
* @param {function(Error, Ping)} callback
* @returns {void}
*/
ping (peer, callback) {
if (!this.isStarted()) {
return callback(notStarted('ping', this.state._state))
@@ -415,8 +395,8 @@ class Libp2p extends EventEmitter {
}
},
(cb) => {
if (this.pubsub) {
return this.pubsub.start(cb)
if (this._floodSub) {
return this._floodSub.start(cb)
}
cb()
},
@@ -454,8 +434,8 @@ class Libp2p extends EventEmitter {
)
},
(cb) => {
if (this.pubsub) {
return this.pubsub.stop(cb)
if (this._floodSub) {
return this._floodSub.stop(cb)
}
cb()
},
@@ -577,7 +557,7 @@ module.exports = Libp2p
* @param {function(Error, Libp2p)} callback
* @returns {void}
*/
module.exports.createLibp2p = promisify((options, callback) => {
module.exports.createLibp2p = (options, callback) => {
if (options.peerInfo) {
return nextTick(callback, null, new Libp2p(options))
}
@@ -586,4 +566,4 @@ module.exports.createLibp2p = promisify((options, callback) => {
options.peerInfo = peerInfo
callback(null, new Libp2p(options))
})
})
}

View File

@@ -2,7 +2,6 @@
const tryEach = require('async/tryEach')
const errCode = require('err-code')
const promisify = require('promisify-es6')
module.exports = (node) => {
const routers = node._modules.peerRouting || []
@@ -22,7 +21,7 @@ module.exports = (node) => {
* @param {function(Error, Result<Array>)} callback
* @returns {void}
*/
findPeer: promisify((id, options, callback) => {
findPeer: (id, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
@@ -54,6 +53,6 @@ module.exports = (node) => {
results = results || []
callback(null, results)
})
})
}
}
}

View File

@@ -2,128 +2,82 @@
const nextTick = require('async/nextTick')
const { messages, codes } = require('./errors')
const promisify = require('promisify-es6')
const FloodSub = require('libp2p-floodsub')
const errCode = require('err-code')
module.exports = (node, Pubsub) => {
const pubsub = new Pubsub(node, { emitSelf: true })
module.exports = (node) => {
const floodSub = new FloodSub(node)
node._floodSub = floodSub
return {
/**
* Subscribe the given handler to a pubsub topic
*
* @param {string} topic
* @param {function} handler The handler to subscribe
* @param {object|null} [options]
* @param {function} [callback] An optional callback
*
* @returns {Promise|void} A promise is returned if no callback is provided
*
* @example <caption>Subscribe a handler to a topic</caption>
*
* // `null` must be passed for options until subscribe is no longer using promisify
* const handler = (message) => { }
* await libp2p.subscribe(topic, handler, null)
*
* @example <caption>Use a callback instead of the Promise api</caption>
*
* // `options` may be passed or omitted when supplying a callback
* const handler = (message) => { }
* libp2p.subscribe(topic, handler, callback)
*/
subscribe: promisify((topic, handler, options, callback) => {
subscribe: (topic, options, handler, callback) => {
if (typeof options === 'function') {
callback = options
callback = handler
handler = options
options = {}
}
if (!node.isStarted() && !pubsub.started) {
if (!node.isStarted() && !floodSub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
}
function subscribe (cb) {
if (pubsub.listenerCount(topic) === 0) {
pubsub.subscribe(topic)
if (floodSub.listenerCount(topic) === 0) {
floodSub.subscribe(topic)
}
pubsub.on(topic, handler)
floodSub.on(topic, handler)
nextTick(cb)
}
subscribe(callback)
}),
},
/**
* Unsubscribes from a pubsub topic
*
* @param {string} topic
* @param {function|null} handler The handler to unsubscribe from
* @param {function} [callback] An optional callback
*
* @returns {Promise|void} A promise is returned if no callback is provided
*
* @example <caption>Unsubscribe a topic for all handlers</caption>
*
* // `null` must be passed until unsubscribe is no longer using promisify
* await libp2p.unsubscribe(topic, null)
*
* @example <caption>Unsubscribe a topic for 1 handler</caption>
*
* await libp2p.unsubscribe(topic, handler)
*
* @example <caption>Use a callback instead of the Promise api</caption>
*
* libp2p.unsubscribe(topic, handler, callback)
*/
unsubscribe: promisify((topic, handler, callback) => {
if (!node.isStarted() && !pubsub.started) {
unsubscribe: (topic, handler, callback) => {
if (!node.isStarted() && !floodSub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
}
if (!handler) {
pubsub.removeAllListeners(topic)
if (!handler && !callback) {
floodSub.removeAllListeners(topic)
} else {
pubsub.removeListener(topic, handler)
floodSub.removeListener(topic, handler)
}
if (pubsub.listenerCount(topic) === 0) {
pubsub.unsubscribe(topic)
if (floodSub.listenerCount(topic) === 0) {
floodSub.unsubscribe(topic)
}
if (typeof callback === 'function') {
return nextTick(() => callback())
nextTick(() => callback())
}
},
return Promise.resolve()
}),
publish: promisify((topic, data, callback) => {
if (!node.isStarted() && !pubsub.started) {
publish: (topic, data, callback) => {
if (!node.isStarted() && !floodSub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
}
try {
data = Buffer.from(data)
} catch (err) {
return nextTick(callback, errCode(new Error('data must be convertible to a Buffer'), 'ERR_DATA_IS_NOT_VALID'))
if (!Buffer.isBuffer(data)) {
return nextTick(callback, errCode(new Error('data must be a Buffer'), 'ERR_DATA_IS_NOT_A_BUFFER'))
}
pubsub.publish(topic, data, callback)
}),
floodSub.publish(topic, data, callback)
},
ls: promisify((callback) => {
if (!node.isStarted() && !pubsub.started) {
ls: (callback) => {
if (!node.isStarted() && !floodSub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
}
const subscriptions = Array.from(pubsub.subscriptions)
const subscriptions = Array.from(floodSub.subscriptions)
nextTick(() => callback(null, subscriptions))
}),
},
peers: promisify((topic, callback) => {
if (!node.isStarted() && !pubsub.started) {
peers: (topic, callback) => {
if (!node.isStarted() && !floodSub.started) {
return nextTick(callback, errCode(new Error(messages.NOT_STARTED_YET), codes.PUBSUB_NOT_STARTED))
}
@@ -132,19 +86,15 @@ module.exports = (node, Pubsub) => {
topic = null
}
const peers = Array.from(pubsub.peers.values())
const peers = Array.from(floodSub.peers.values())
.filter((peer) => topic ? peer.topics.has(topic) : true)
.map((peer) => peer.info.id.toB58String())
nextTick(() => callback(null, peers))
}),
setMaxListeners (n) {
return pubsub.setMaxListeners(n)
},
start: promisify((cb) => pubsub.start(cb)),
stop: promisify((cb) => pubsub.stop(cb))
setMaxListeners (n) {
return floodSub.setMaxListeners(n)
}
}
}

View File

@@ -43,7 +43,12 @@ describe('circuit relay', () => {
let peerRelay
before('get peer relay', async () => {
peerRelay = await getPeerRelay()
peerRelay = await new Promise(resolve => {
getPeerRelay((err, peer) => {
expect(err).to.not.exist()
resolve(peer)
})
})
})
before('create the browser nodes', async () => {

View File

@@ -16,7 +16,7 @@ const tryEcho = require('./utils/try-echo')
const echo = require('./utils/echo')
describe('circuit relay', () => {
const handlerSpies = []
let handlerSpies = []
let relayNode1
let relayNode2
let nodeWS1

View File

@@ -33,7 +33,7 @@ describe('configuration', () => {
expect(() => {
validateConfig({
modules: {
transport: [WS]
transport: [ WS ]
}
})
}).to.throw()
@@ -52,7 +52,7 @@ describe('configuration', () => {
validateConfig({
peerInfo,
modules: {
transport: []
transport: [ ]
}
})
}).to.throw('ERROR_EMPTY')
@@ -62,8 +62,8 @@ describe('configuration', () => {
const options = {
peerInfo,
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
transport: [ WS ],
peerDiscovery: [ Bootstrap ],
dht: DHT
}
}
@@ -74,16 +74,16 @@ describe('configuration', () => {
minPeers: 25
},
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
transport: [ WS ],
peerDiscovery: [ Bootstrap ],
dht: DHT
},
config: {
peerDiscovery: {
autoDial: true
},
pubsub: {
enabled: true
EXPERIMENTAL: {
pubsub: false
},
dht: {
kBucketSize: 20,
@@ -112,8 +112,8 @@ describe('configuration', () => {
const options = {
peerInfo,
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
transport: [ WS ],
peerDiscovery: [ Bootstrap ],
dht: DHT
},
config: {
@@ -132,8 +132,8 @@ describe('configuration', () => {
minPeers: 25
},
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
transport: [ WS ],
peerDiscovery: [ Bootstrap ],
dht: DHT
},
config: {
@@ -144,8 +144,8 @@ describe('configuration', () => {
enabled: true
}
},
pubsub: {
enabled: true
EXPERIMENTAL: {
pubsub: false
},
dht: {
kBucketSize: 20,
@@ -174,22 +174,22 @@ describe('configuration', () => {
const options = {
peerInfo,
switch: {
denyTTL: 60e3,
denyAttempts: 5,
blacklistTTL: 60e3,
blackListAttempts: 5,
maxParallelDials: 100,
maxColdCalls: 50,
dialTimeout: 30e3
},
modules: {
transport: [WS],
peerDiscovery: []
transport: [ WS ],
peerDiscovery: [ ]
}
}
expect(validateConfig(options)).to.deep.include({
switch: {
denyTTL: 60e3,
denyAttempts: 5,
blacklistTTL: 60e3,
blackListAttempts: 5,
maxParallelDials: 100,
maxColdCalls: 50,
dialTimeout: 30e3
@@ -204,10 +204,10 @@ describe('configuration', () => {
const options = {
peerInfo,
modules: {
transport: [WS],
peerDiscovery: [Bootstrap],
peerRouting: [peerRouter],
contentRouting: [contentRouter],
transport: [ WS ],
peerDiscovery: [ Bootstrap ],
peerRouting: [ peerRouter ],
contentRouting: [ contentRouter ],
dht: DHT
},
config: {
@@ -221,8 +221,8 @@ describe('configuration', () => {
}
expect(validateConfig(options).modules).to.deep.include({
peerRouting: [peerRouter],
contentRouting: [contentRouter]
peerRouting: [ peerRouter ],
contentRouting: [ contentRouter ]
})
})
@@ -230,7 +230,7 @@ describe('configuration', () => {
const options = {
peerInfo,
modules: {
transport: [WS]
transport: [ WS ]
},
config: {
dht: {
@@ -269,8 +269,8 @@ describe('configuration', () => {
dht: DHT
},
config: {
pubsub: {
enabled: true
EXPERIMENTAL: {
pubsub: false
},
peerDiscovery: {
autoDial: true

View File

@@ -151,7 +151,7 @@ describe('.contentRouting', () => {
nodeA = new Node({
peerInfo,
modules: {
contentRouting: [delegate]
contentRouting: [ delegate ]
},
config: {
dht: {
@@ -301,7 +301,7 @@ describe('.contentRouting', () => {
nodeA = new Node({
peerInfo,
modules: {
contentRouting: [delegate]
contentRouting: [ delegate ]
},
config: {
relay: {

View File

@@ -19,8 +19,8 @@ describe('libp2p creation', () => {
it('should be able to start and stop successfully', (done) => {
createNode([], {
config: {
pubsub: {
enabled: true
EXPERIMENTAL: {
pubsub: true
},
dht: {
enabled: true
@@ -29,10 +29,10 @@ describe('libp2p creation', () => {
}, (err, node) => {
expect(err).to.not.exist()
const sw = node._switch
const cm = node.connectionManager
const dht = node._dht
const pub = node.pubsub
let sw = node._switch
let cm = node.connectionManager
let dht = node._dht
let pub = node._floodSub
sinon.spy(sw, 'start')
sinon.spy(cm, 'start')
@@ -77,13 +77,13 @@ describe('libp2p creation', () => {
it('should not create disabled modules', (done) => {
createNode([], {
config: {
pubsub: {
enabled: false
EXPERIMENTAL: {
pubsub: false
}
}
}, (err, node) => {
expect(err).to.not.exist()
expect(node._pubsub).to.not.exist()
expect(node._floodSub).to.not.exist()
done()
})
})
@@ -113,7 +113,7 @@ describe('libp2p creation', () => {
this.timeout(10e3)
createLibp2p({
modules: {
transport: [WS]
transport: [ WS ]
}
}, (err, libp2p) => {
expect(err).to.not.exist()
@@ -130,7 +130,7 @@ describe('libp2p creation', () => {
createLibp2p({
peerInfo,
modules: {
transport: [WS]
transport: [ WS ]
}
}, (err, libp2p) => {
expect(err).to.not.exist()

View File

@@ -60,9 +60,9 @@ describe('libp2p state machine (fsm)', () => {
node.once('stop', done)
// stop the stopped node
node.stop(() => {})
node.stop()
})
node.start(() => {})
node.start()
})
it('should callback with an error when it occurs on stop', (done) => {
@@ -79,7 +79,7 @@ describe('libp2p state machine (fsm)', () => {
expect(2).checks(done)
sinon.stub(node._switch, 'stop').callsArgWith(0, error)
node.start(() => {})
node.start()
})
it('should noop when starting a started node', (done) => {
@@ -89,17 +89,17 @@ describe('libp2p state machine (fsm)', () => {
})
node.once('start', () => {
node.once('stop', done)
node.stop(() => {})
node.stop()
})
// start the started node
node.start(() => {})
node.start()
})
node.start(() => {})
node.start()
})
it('should error on start with no transports', (done) => {
const transports = node._modules.transport
let transports = node._modules.transport
node._modules.transport = null
node.on('stop', () => {
@@ -115,7 +115,7 @@ describe('libp2p state machine (fsm)', () => {
expect(2).checks(done)
node.start(() => {})
node.start()
})
it('should not start if the switch fails to start', (done) => {
@@ -150,7 +150,7 @@ describe('libp2p state machine (fsm)', () => {
})
})
node.stop(() => {})
node.stop()
})
it('should not dial (fsm) when the node is stopped', (done) => {
@@ -162,7 +162,7 @@ describe('libp2p state machine (fsm)', () => {
})
})
node.stop(() => {})
node.stop()
})
})
})

View File

@@ -6,7 +6,6 @@ require('./stream-muxing.node')
require('./peer-discovery.node')
require('./peer-routing.node')
require('./ping.node')
require('./promisify.node')
require('./pubsub.node')
require('./content-routing.node')
require('./circuit-relay.node')

View File

@@ -72,7 +72,7 @@ describe('peer discovery', () => {
stop: sinon.stub().callsArg(0)
}
const options = { modules: { peerDiscovery: [mockDiscovery] } }
const options = { modules: { peerDiscovery: [ mockDiscovery ] } }
createNode(['/ip4/0.0.0.0/tcp/0'], options, (err, node) => {
expect(err).to.not.exist()
@@ -95,7 +95,7 @@ describe('peer discovery', () => {
const MockDiscovery = sinon.stub().returns(mockDiscovery)
const options = { modules: { peerDiscovery: [MockDiscovery] } }
const options = { modules: { peerDiscovery: [ MockDiscovery ] } }
createNode(['/ip4/0.0.0.0/tcp/0'], options, (err, node) => {
expect(err).to.not.exist()
@@ -120,7 +120,7 @@ describe('peer discovery', () => {
const enabled = sinon.stub().returns(true)
const options = {
modules: { peerDiscovery: [mockDiscovery] },
modules: { peerDiscovery: [ mockDiscovery ] },
config: {
peerDiscovery: {
mockDiscovery: {
@@ -156,7 +156,7 @@ describe('peer discovery', () => {
const disabled = sinon.stub().returns(false)
const options = {
modules: { peerDiscovery: [mockDiscovery] },
modules: { peerDiscovery: [ mockDiscovery ] },
config: {
peerDiscovery: {
mockDiscovery: {
@@ -192,7 +192,7 @@ describe('peer discovery', () => {
MockDiscovery.tag = 'mockDiscovery'
const options = {
modules: { peerDiscovery: [MockDiscovery] },
modules: { peerDiscovery: [ MockDiscovery ] },
config: {
peerDiscovery: {
mockDiscovery: {
@@ -228,7 +228,7 @@ describe('peer discovery', () => {
}
const options = {
modules: { peerDiscovery: [mockDiscovery] },
modules: { peerDiscovery: [ mockDiscovery ] },
config: {
peerDiscovery: {
mockDiscovery: { enabled: true }
@@ -294,7 +294,7 @@ describe('peer discovery', () => {
})
it('find peers', function (done) {
const expectedPeers = new Set([
let expectedPeers = new Set([
nodeB.peerInfo.id.toB58String(),
nodeC.peerInfo.id.toB58String()
])
@@ -332,7 +332,7 @@ describe('peer discovery', () => {
it('find peers', function (done) {
this.timeout(20e3)
const expectedPeers = new Set([
let expectedPeers = new Set([
nodeB.peerInfo.id.toB58String(),
nodeC.peerInfo.id.toB58String()
])
@@ -374,7 +374,7 @@ describe('peer discovery', () => {
})
it('find peers', function (done) {
const expectedPeers = new Set([
let expectedPeers = new Set([
nodeB.peerInfo.id.toB58String(),
nodeC.peerInfo.id.toB58String()
])
@@ -421,7 +421,7 @@ describe('peer discovery', () => {
})
it('find peers through the dht', function (done) {
const expectedPeers = new Set([
let expectedPeers = new Set([
nodeB.peerInfo.id.toB58String(),
nodeC.peerInfo.id.toB58String()
])

View File

@@ -105,7 +105,7 @@ describe('.peerRouting', () => {
})
createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
peerRouting: [delegate]
peerRouting: [ delegate ]
},
config: {
dht: {
@@ -211,7 +211,7 @@ describe('.peerRouting', () => {
})
createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
peerRouting: [delegate]
peerRouting: [ delegate ]
}
}, (err, node) => {
expect(err).to.not.exist()

View File

@@ -24,7 +24,7 @@ describe('private network', () => {
config = {
peerInfo,
modules: {
transport: [WS],
transport: [ WS ],
dht: DHT
}
}
@@ -50,14 +50,14 @@ describe('private network', () => {
it('should create a libp2p node with a provided protector', () => {
let node
const protector = {
let protector = {
psk: '123',
tag: '/psk/1.0.0',
protect: () => { }
}
expect(() => {
const options = defaultsDeep(config, {
let options = defaultsDeep(config, {
modules: {
connProtector: protector
}
@@ -71,7 +71,7 @@ describe('private network', () => {
it('should throw an error if the protector does not have a protect method', () => {
expect(() => {
const options = defaultsDeep(config, {
let options = defaultsDeep(config, {
modules: {
connProtector: { }
}

View File

@@ -1,87 +0,0 @@
/* eslint-env mocha */
'use strict'
/**
* This test suite is intended to validate compatability of
* the promisified api, until libp2p has been fully migrated to
* async/await. Once the migration is complete and all tests
* are using async/await, this file can be removed.
*/
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const promisify = require('promisify-es6')
const createNode = promisify(require('./utils/create-node'))
const { createPeerInfo } = require('./utils/create-node')
const Node = require('./utils/bundle-nodejs')
const pull = require('pull-stream')
const Ping = require('libp2p-ping')
/**
* As libp2p is currently promisified, when extending libp2p,
* method arguments must be passed to `super` to ensure the
* promisify callbacks are properly resolved
*/
class AsyncLibp2p extends Node {
async start (...args) {
await super.start(...args)
}
async stop (...args) {
await super.start(...args)
}
}
async function createAsyncNode () {
const peerInfo = await promisify(createPeerInfo)()
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
return new AsyncLibp2p({ peerInfo })
}
describe('promisified libp2p', () => {
let libp2p
let otherNode
const ECHO_PROTO = '/echo/1.0.0'
before('Create and Start', async () => {
[libp2p, otherNode] = await Promise.all([
createNode('/ip4/0.0.0.0/tcp/0'),
createAsyncNode()
])
return [libp2p, otherNode].map(node => {
node.handle(ECHO_PROTO, (_, conn) => pull(conn, conn))
return node.start()
})
})
after('Stop', () => {
return [libp2p, otherNode].map(node => node.stop())
})
afterEach('Hang up', () => {
return libp2p.hangUp(otherNode.peerInfo)
})
it('dial', async () => {
const stream = await libp2p.dial(otherNode.peerInfo)
expect(stream).to.not.exist()
expect(libp2p._switch.connection.getAll()).to.have.length(1)
})
it('dialFSM', async () => {
const connectionFSM = await libp2p.dialFSM(otherNode.peerInfo, ECHO_PROTO)
expect(connectionFSM).to.exist()
})
it('dialProtocol', async () => {
const stream = await libp2p.dialProtocol(otherNode.peerInfo, ECHO_PROTO)
expect(stream).to.exist()
})
it('ping', async () => {
const ping = await libp2p.ping(otherNode.peerInfo)
expect(ping).to.be.an.instanceOf(Ping)
})
})

View File

@@ -11,31 +11,23 @@ const parallel = require('async/parallel')
const series = require('async/series')
const _times = require('lodash.times')
const Floodsub = require('libp2p-floodsub')
const mergeOptions = require('merge-options')
const { codes } = require('../src/errors')
const createNode = require('./utils/create-node')
function startTwo (options, callback) {
if (typeof options === 'function') {
callback = options
options = {}
}
function startTwo (callback) {
const tasks = _times(2, () => (cb) => {
createNode('/ip4/0.0.0.0/tcp/0', mergeOptions({
createNode('/ip4/0.0.0.0/tcp/0', {
config: {
peerDiscovery: {
mdns: {
enabled: false
}
},
pubsub: {
enabled: true
EXPERIMENTAL: {
pubsub: true
}
}
}, options), (err, node) => {
}, (err, node) => {
expect(err).to.not.exist()
node.start((err) => cb(err, node))
})
@@ -55,46 +47,17 @@ function stopTwo (nodes, callback) {
], callback)
}
// There is a vast test suite on PubSub through js-ipfs
// https://github.com/ipfs/interface-ipfs-core/blob/master/js/src/pubsub.js
// and libp2p-floodsub itself
// https://github.com/libp2p/js-libp2p-floodsub/tree/master/test
// TODO: consider if all or some of those should come here
describe('.pubsub', () => {
describe('.pubsub on (default)', () => {
describe('.pubsub on (default)', (done) => {
it('start two nodes and send one message, then unsubscribe', (done) => {
// Check the final series error, and the publish handler
expect(2).checks(done)
let nodes
const data = 'test'
const handler = (msg) => {
// verify the data is correct and mark the expect
expect(msg.data.toString()).to.eql(data).mark()
}
series([
// Start the nodes
(cb) => startTwo((err, _nodes) => {
nodes = _nodes
cb(err)
}),
// subscribe on the first
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, null, cb),
// Wait a moment before publishing
(cb) => setTimeout(cb, 500),
// publish on the second
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
// Wait a moment before unsubscribing
(cb) => setTimeout(cb, 500),
// unsubscribe on the first
(cb) => nodes[0].pubsub.unsubscribe('pubsub', handler, cb),
// Stop both nodes
(cb) => stopTwo(nodes, cb)
], (err) => {
// Verify there was no error, and mark the expect
expect(err).to.not.exist().mark()
})
})
it('start two nodes and send one message, then unsubscribe without handler', (done) => {
// Check the final series error, and the publish handler
expect(3).checks(done)
let nodes
const data = Buffer.from('test')
const handler = (msg) => {
@@ -109,7 +72,7 @@ describe('.pubsub', () => {
cb(err)
}),
// subscribe on the first
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, {}, cb),
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
// Wait a moment before publishing
(cb) => setTimeout(cb, 500),
// publish on the second
@@ -120,81 +83,6 @@ describe('.pubsub', () => {
(cb) => nodes[1].pubsub.peers('pubsub', cb),
// Wait a moment before unsubscribing
(cb) => setTimeout(cb, 500),
// unsubscribe from all
(cb) => nodes[0].pubsub.unsubscribe('pubsub', null, cb),
// Verify unsubscribed
(cb) => {
nodes[0].pubsub.ls((err, topics) => {
expect(topics.length).to.eql(0).mark()
cb(err)
})
},
// Stop both nodes
(cb) => stopTwo(nodes, cb)
], (err) => {
// Verify there was no error, and mark the expect
expect(err).to.not.exist().mark()
})
})
it('publish should fail if data is not a buffer nor a string', (done) => {
createNode('/ip4/0.0.0.0/tcp/0', {
config: {
peerDiscovery: {
mdns: {
enabled: false
}
},
pubsub: {
enabled: true
}
}
}, (err, node) => {
expect(err).to.not.exist()
node.start((err) => {
expect(err).to.not.exist()
node.pubsub.publish('pubsub', 10, (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID')
done()
})
})
})
})
})
describe('.pubsub on using floodsub', () => {
it('start two nodes and send one message, then unsubscribe', (done) => {
// Check the final series error, and the publish handler
expect(2).checks(done)
let nodes
const data = Buffer.from('test')
const handler = (msg) => {
// verify the data is correct and mark the expect
expect(msg.data).to.eql(data).mark()
}
series([
// Start the nodes
(cb) => startTwo({
modules: {
pubsub: Floodsub
}
}, (err, _nodes) => {
nodes = _nodes
cb(err)
}),
// subscribe on the first
(cb) => nodes[0].pubsub.subscribe('pubsub', handler, cb),
// Wait a moment before publishing
(cb) => setTimeout(cb, 500),
// publish on the second
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
// Wait a moment before unsubscribing
(cb) => setTimeout(cb, 500),
// unsubscribe on the first
(cb) => nodes[0].pubsub.unsubscribe('pubsub', handler, cb),
// Stop both nodes
@@ -217,11 +105,7 @@ describe('.pubsub', () => {
series([
// Start the nodes
(cb) => startTwo({
modules: {
pubsub: Floodsub
}
}, (err, _nodes) => {
(cb) => startTwo((err, _nodes) => {
nodes = _nodes
cb(err)
}),
@@ -233,8 +117,12 @@ describe('.pubsub', () => {
(cb) => nodes[1].pubsub.publish('pubsub', data, cb),
// Wait a moment before unsubscribing
(cb) => setTimeout(cb, 500),
// unsubscribe from all
(cb) => nodes[0].pubsub.unsubscribe('pubsub', null, cb),
// unsubscribe on the first
(cb) => {
nodes[0].pubsub.unsubscribe('pubsub')
// Wait a moment to make sure the ubsubscribe-from-all worked
setTimeout(cb, 500)
},
// Verify unsubscribed
(cb) => {
nodes[0].pubsub.ls((err, topics) => {
@@ -257,12 +145,9 @@ describe('.pubsub', () => {
enabled: false
}
},
pubsub: {
enabled: true
EXPERIMENTAL: {
pubsub: true
}
},
modules: {
pubsub: Floodsub
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -270,9 +155,9 @@ describe('.pubsub', () => {
node.start((err) => {
expect(err).to.not.exist()
node.pubsub.publish('pubsub', 10, (err) => {
node.pubsub.publish('pubsub', 'datastr', (err) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_DATA_IS_NOT_VALID')
expect(err.code).to.equal('ERR_DATA_IS_NOT_A_BUFFER')
done()
})
@@ -289,6 +174,9 @@ describe('.pubsub', () => {
mdns: {
enabled: false
}
},
EXPERIMENTAL: {
pubsub: false
}
}
}, (err, node) => {
@@ -310,8 +198,8 @@ describe('.pubsub', () => {
enabled: false
}
},
pubsub: {
enabled: true
EXPERIMENTAL: {
pubsub: true
}
}
}, (err, node) => {

View File

@@ -36,7 +36,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [SPDY]
streamMuxer: [ SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -46,7 +46,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [SPDY]
streamMuxer: [ SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -72,7 +72,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [Mplex]
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -82,7 +82,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [Mplex]
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -108,7 +108,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [pMplex]
streamMuxer: [ pMplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -118,7 +118,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [pMplex]
streamMuxer: [ pMplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -146,7 +146,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [Mplex]
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -156,7 +156,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [SPDY, Mplex]
streamMuxer: [ SPDY, Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -184,7 +184,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [Mplex]
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -194,7 +194,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [pMplex]
streamMuxer: [ pMplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -222,7 +222,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [SPDY, Mplex]
streamMuxer: [ SPDY, Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -232,7 +232,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [Mplex, SPDY]
streamMuxer: [ Mplex, SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -260,7 +260,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [SPDY, pMplex]
streamMuxer: [ SPDY, pMplex ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -270,7 +270,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [pMplex, SPDY]
streamMuxer: [ pMplex, SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -298,7 +298,7 @@ describe('stream muxing', () => {
parallel([
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [SPDY]
streamMuxer: [ SPDY ]
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -308,7 +308,7 @@ describe('stream muxing', () => {
}),
(cb) => createNode('/ip4/0.0.0.0/tcp/0', {
modules: {
streamMuxer: [Mplex]
streamMuxer: [ Mplex ]
}
}, (err, node) => {
expect(err).to.not.exist()

View File

@@ -25,11 +25,14 @@ describe('transports', () => {
let peerBMultiaddr
let nodeA
before(async () => {
const peerInfo = await getPeerRelay()
peerB = new PeerInfo(peerInfo.id)
peerBMultiaddr = `/ip4/127.0.0.1/tcp/9200/ws/p2p/${peerInfo.id.toB58String()}`
peerB.multiaddrs.add(peerBMultiaddr)
before((done) => {
getPeerRelay((err, peerInfo) => {
expect(err).to.not.exist()
peerB = new PeerInfo(peerInfo.id)
peerBMultiaddr = `/ip4/127.0.0.1/tcp/9200/ws/p2p/${peerInfo.id.toB58String()}`
peerB.multiaddrs.add(peerBMultiaddr)
done()
})
})
after((done) => nodeA.stop(done))
@@ -52,7 +55,7 @@ describe('transports', () => {
const b = new Node({
peerInfo: peerInfo,
modules: {
streamMuxer: [Mplex]
streamMuxer: [ Mplex ]
}
})
expect(b._modules.streamMuxer).to.eql([require('pull-mplex')])

View File

@@ -17,9 +17,7 @@ const createNode = require('./utils/create-node.js')
const tryEcho = require('./utils/try-echo')
const echo = require('./utils/echo')
const {
WRTC_RENDEZVOUS_MULTIADDR
} = require('./utils/constants')
const { WRTC_RENDEZVOUS_MULTIADDR } = require('./utils/constants')
describe('transports', () => {
describe('TCP only', () => {
@@ -578,24 +576,25 @@ describe('transports', () => {
let nodeTCP
let nodeWS
let nodeWebSocketStar
let ss
const PORT = 24642
before(async () => {
ss = await rendezvous.start({
port: PORT
})
})
let ss
before((done) => {
parallel([
(cb) => {
rendezvous.start({ port: 24642 }, (err, server) => {
expect(err).to.not.exist()
ss = server
cb()
})
},
(cb) => {
const wstar = new WSStar()
createNode([
'/ip4/0.0.0.0/tcp/0',
'/ip4/127.0.0.1/tcp/25011/ws',
`/ip4/127.0.0.1/tcp/${PORT}/ws/p2p-websocket-star`
'/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star'
], {
modules: {
transport: [
@@ -604,6 +603,13 @@ describe('transports', () => {
wstar
],
peerDiscovery: [wstar.discovery]
},
config: {
peerDiscovery: {
[wstar.discovery.tag]: {
enabled: true
}
}
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -634,11 +640,18 @@ describe('transports', () => {
const wstar = new WSStar({})
createNode([
`/ip4/127.0.0.1/tcp/${PORT}/ws/p2p-websocket-star`
'/ip4/127.0.0.1/tcp/24642/ws/p2p-websocket-star'
], {
modules: {
transport: [wstar],
peerDiscovery: [wstar.discovery]
},
config: {
peerDiscovery: {
[wstar.discovery.tag]: {
enabled: true
}
}
}
}, (err, node) => {
expect(err).to.not.exist()
@@ -657,9 +670,7 @@ describe('transports', () => {
(cb) => nodeTCP.stop(cb),
(cb) => nodeWS.stop(cb),
(cb) => nodeWebSocketStar.stop(cb),
async () => {
await ss.stop()
}
(cb) => ss.stop(cb)
], done)
})

View File

@@ -8,7 +8,6 @@ const SPDY = require('libp2p-spdy')
const MPLEX = require('libp2p-mplex')
const PULLMPLEX = require('pull-mplex')
const KadDHT = require('libp2p-kad-dht')
const GossipSub = require('libp2p-gossipsub')
const SECIO = require('libp2p-secio')
const defaultsDeep = require('@nodeutils/defaults-deep')
const libp2p = require('../..')
@@ -58,8 +57,7 @@ class Node extends libp2p {
wsStar.discovery,
Bootstrap
],
dht: KadDHT,
pubsub: GossipSub
dht: KadDHT
},
config: {
peerDiscovery: {
@@ -90,8 +88,8 @@ class Node extends libp2p {
},
enabled: false
},
pubsub: {
enabled: false
EXPERIMENTAL: {
pubsub: false
}
}
}

View File

@@ -6,7 +6,6 @@ const WS = require('libp2p-websockets')
const Bootstrap = require('libp2p-bootstrap')
const SPDY = require('libp2p-spdy')
const KadDHT = require('libp2p-kad-dht')
const GossipSub = require('libp2p-gossipsub')
const MPLEX = require('libp2p-mplex')
const PULLMPLEX = require('pull-mplex')
const SECIO = require('libp2p-secio')
@@ -53,8 +52,7 @@ class Node extends libp2p {
MulticastDNS,
Bootstrap
],
dht: KadDHT,
pubsub: GossipSub
dht: KadDHT
},
config: {
peerDiscovery: {
@@ -83,8 +81,8 @@ class Node extends libp2p {
},
enabled: true
},
pubsub: {
enabled: false
EXPERIMENTAL: {
pubsub: false
}
}
}

View File

@@ -5,7 +5,6 @@ const PeerInfo = require('peer-info')
const nextTick = require('async/nextTick')
const peerJSON = require('../fixtures/test-peer')
const multiaddr = require('multiaddr')
const promisify = require('promisify-es6')
let peerRelay = null
@@ -21,7 +20,7 @@ let peerRelay = null
* @param {function(error, PeerInfo)} callback
* @returns {void}
*/
module.exports.getPeerRelay = promisify((callback) => {
module.exports.getPeerRelay = (callback) => {
if (peerRelay) return nextTick(callback, null, peerRelay)
PeerId.createFromJSON(peerJSON, (err, peerId) => {
@@ -35,7 +34,7 @@ module.exports.getPeerRelay = promisify((callback) => {
callback(null, peerRelay)
})
})
}
module.exports.WS_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/14444/ws')
module.exports.WRTC_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/15555/ws')
module.exports.WS_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/14444/wss')
module.exports.WRTC_RENDEZVOUS_MULTIADDR = multiaddr('/ip4/127.0.0.1/tcp/15555/wss')