Compare commits

...

11 Commits

Author SHA1 Message Date
d788433b43 chore: release version v0.26.0 2019-08-07 20:48:42 +02:00
d5a977b227 chore: update contributors 2019-08-07 20:48:41 +02:00
0489972b4b chore: release version v0.26.0-rc.3 2019-08-06 12:27:28 +02:00
3f31b1f422 chore: update contributors 2019-08-06 12:27:27 +02:00
a2b3446ed7 docs: async migration (#397) 2019-08-06 12:01:16 +02:00
ff7a6c86a0 fix: promisified methods (#398)
* chore: update ws rendezvous dep

test(fix): fix tests with latest ws rendezvous server

* fix: promisification of libp2p methods

test: add tests to verify promisify support until async/await endeavor is complete

* chore: fix linting
2019-08-06 10:53:23 +02:00
9a8d609a59 chore: release version v0.26.0-rc.2 2019-08-01 17:17:54 +02:00
9fef58cb7d chore: update contributors 2019-08-01 17:17:54 +02:00
684f283aec chore: update switch (#395)
BREAKING CHANGE: switch configuration has changed.
'blacklistTTL' is now 'denyTTL' and 'blackListAttempts' is now 'denyAttempts'
2019-08-01 17:15:02 +02:00
3e95e6f9e4 fix: dont override methods of created instance (#394)
* fix: dont override methods of created instance

* chore: fix lint
2019-08-01 16:08:38 +02:00
f4f3f0f03a fix: pubsub default config (#393)
* docs: update browser example pubsub

* docs: fix pubsub example config

* fix: make pubsub default to enabled

This allows for only adding the module to have it enabled. Previously you would have to supply and enable the module which is unncessary for users
2019-07-31 18:47:30 +02:00
16 changed files with 224 additions and 125 deletions

View File

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

View File

@ -1,3 +1,40 @@
<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)
### 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> <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) # [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)

View File

@ -94,6 +94,8 @@ npm install --save libp2p
## Usage ## 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) ### [Tutorials and Examples](/examples)
You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarios. You can find multiple examples on the [examples folder](/examples) that will guide you through using libp2p for several scenarios.

View File

@ -19,6 +19,7 @@
"detect-dom-ready": "^1.0.2", "detect-dom-ready": "^1.0.2",
"libp2p": "../../../", "libp2p": "../../../",
"libp2p-bootstrap": "~0.9.7", "libp2p-bootstrap": "~0.9.7",
"libp2p-gossipsub": "~0.0.4",
"libp2p-kad-dht": "^0.15.3", "libp2p-kad-dht": "^0.15.3",
"libp2p-mplex": "~0.8.5", "libp2p-mplex": "~0.8.5",
"libp2p-secio": "~0.11.1", "libp2p-secio": "~0.11.1",

View File

@ -8,6 +8,7 @@ const SPDY = require('libp2p-spdy')
const SECIO = require('libp2p-secio') const SECIO = require('libp2p-secio')
const Bootstrap = require('libp2p-bootstrap') const Bootstrap = require('libp2p-bootstrap')
const DHT = require('libp2p-kad-dht') const DHT = require('libp2p-kad-dht')
const Gossipsub = require('libp2p-gossipsub')
const libp2p = require('libp2p') const libp2p = require('libp2p')
// Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json // Find this list at: https://github.com/ipfs/js-ipfs/blob/master/src/core/runtime/config-browser.json
@ -48,7 +49,8 @@ class Node extends libp2p {
wsstar.discovery, wsstar.discovery,
Bootstrap Bootstrap
], ],
dht: DHT dht: DHT,
pubsub: Gossipsub
}, },
config: { config: {
peerDiscovery: { peerDiscovery: {
@ -75,8 +77,8 @@ class Node extends libp2p {
dht: { dht: {
enabled: false enabled: false
}, },
EXPERIMENTAL: { pubsub: {
pubsub: false enabled: false
} }
}, },
connectionManager: { connectionManager: {

View File

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

View File

@ -1,6 +1,6 @@
{ {
"name": "libp2p", "name": "libp2p",
"version": "0.26.0-rc.1", "version": "0.26.0",
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack", "description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>", "leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
"main": "src/index.js", "main": "src/index.js",
@ -49,7 +49,7 @@
"fsm-event": "^2.1.0", "fsm-event": "^2.1.0",
"libp2p-connection-manager": "^0.1.0", "libp2p-connection-manager": "^0.1.0",
"libp2p-ping": "^0.8.5", "libp2p-ping": "^0.8.5",
"libp2p-switch": "^0.42.12", "libp2p-switch": "^0.43.0",
"libp2p-websockets": "^0.12.2", "libp2p-websockets": "^0.12.2",
"mafmt": "^6.0.7", "mafmt": "^6.0.7",
"multiaddr": "^6.1.0", "multiaddr": "^6.1.0",
@ -83,7 +83,7 @@
"libp2p-tcp": "^0.13.0", "libp2p-tcp": "^0.13.0",
"libp2p-webrtc-star": "^0.16.1", "libp2p-webrtc-star": "^0.16.1",
"libp2p-websocket-star": "~0.10.2", "libp2p-websocket-star": "~0.10.2",
"libp2p-websocket-star-rendezvous": "~0.3.0", "libp2p-websocket-star-rendezvous": "~0.4.1",
"lodash.times": "^4.3.2", "lodash.times": "^4.3.2",
"merge-options": "^1.0.1", "merge-options": "^1.0.1",
"nock": "^10.0.6", "nock": "^10.0.6",

View File

@ -62,8 +62,8 @@ const configSchema = s({
}), }),
// Pubsub config // Pubsub config
pubsub: s('object?', { pubsub: s('object?', {
// DHT defaults // Pubsub defaults
enabled: false enabled: true
}) })
}, {}) }, {})

View File

@ -190,9 +190,7 @@ class Libp2p extends EventEmitter {
// promisify all instance methods // promisify all instance methods
;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => { ;['start', 'stop', 'dial', 'dialProtocol', 'dialFSM', 'hangUp', 'ping'].forEach(method => {
this[method] = promisify(this[method], { this[method] = promisify(this[method], { context: this })
context: this
})
}) })
} }
@ -302,6 +300,13 @@ 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) { hangUp (peer, callback) {
this._getPeerInfo(peer, (err, peerInfo) => { this._getPeerInfo(peer, (err, peerInfo) => {
if (err) { return callback(err) } if (err) { return callback(err) }
@ -310,6 +315,13 @@ 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) { ping (peer, callback) {
if (!this.isStarted()) { if (!this.isStarted()) {
return callback(notStarted('ping', this.state._state)) return callback(notStarted('ping', this.state._state))

View File

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

View File

@ -83,7 +83,7 @@ describe('configuration', () => {
autoDial: true autoDial: true
}, },
pubsub: { pubsub: {
enabled: false enabled: true
}, },
dht: { dht: {
kBucketSize: 20, kBucketSize: 20,
@ -145,7 +145,7 @@ describe('configuration', () => {
} }
}, },
pubsub: { pubsub: {
enabled: false enabled: true
}, },
dht: { dht: {
kBucketSize: 20, kBucketSize: 20,
@ -174,8 +174,8 @@ describe('configuration', () => {
const options = { const options = {
peerInfo, peerInfo,
switch: { switch: {
blacklistTTL: 60e3, denyTTL: 60e3,
blackListAttempts: 5, denyAttempts: 5,
maxParallelDials: 100, maxParallelDials: 100,
maxColdCalls: 50, maxColdCalls: 50,
dialTimeout: 30e3 dialTimeout: 30e3
@ -188,8 +188,8 @@ describe('configuration', () => {
expect(validateConfig(options)).to.deep.include({ expect(validateConfig(options)).to.deep.include({
switch: { switch: {
blacklistTTL: 60e3, denyTTL: 60e3,
blackListAttempts: 5, denyAttempts: 5,
maxParallelDials: 100, maxParallelDials: 100,
maxColdCalls: 50, maxColdCalls: 50,
dialTimeout: 30e3 dialTimeout: 30e3
@ -270,7 +270,7 @@ describe('configuration', () => {
}, },
config: { config: {
pubsub: { pubsub: {
enabled: false enabled: true
}, },
peerDiscovery: { peerDiscovery: {
autoDial: true autoDial: true

View File

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

87
test/promisify.node.js Normal file
View File

@ -0,0 +1,87 @@
/* 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

@ -25,14 +25,11 @@ describe('transports', () => {
let peerBMultiaddr let peerBMultiaddr
let nodeA let nodeA
before((done) => { before(async () => {
getPeerRelay((err, peerInfo) => { const peerInfo = await getPeerRelay()
expect(err).to.not.exist() peerB = new PeerInfo(peerInfo.id)
peerB = new PeerInfo(peerInfo.id) peerBMultiaddr = `/ip4/127.0.0.1/tcp/9200/ws/p2p/${peerInfo.id.toB58String()}`
peerBMultiaddr = `/ip4/127.0.0.1/tcp/9200/ws/p2p/${peerInfo.id.toB58String()}` peerB.multiaddrs.add(peerBMultiaddr)
peerB.multiaddrs.add(peerBMultiaddr)
done()
})
}) })
after((done) => nodeA.stop(done)) after((done) => nodeA.stop(done))

View File

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

View File

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