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'
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')
@ -15,68 +14,46 @@ const {
let wrtcRendezvous
let wsRendezvous
let node
let peerInfo
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)
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
}
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) => {
setTimeout(() =>
parallel([
(cb) => wrtcRendezvous.stop().then(cb).catch(cb),
...[node, wsRendezvous].map((s) => (cb) => s.stop(cb)),
], done),
2000
)
const after = () => {
return Promise.all([
wrtcRendezvous.stop(),
wsRendezvous.stop(),
node.stop()
])
}
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>
# [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
**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.

View File

@ -19,6 +19,7 @@
"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",

View File

@ -8,6 +8,7 @@ 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')
// 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,
Bootstrap
],
dht: DHT
dht: DHT,
pubsub: Gossipsub
},
config: {
peerDiscovery: {
@ -75,8 +77,8 @@ class Node extends libp2p {
dht: {
enabled: false
},
EXPERIMENTAL: {
pubsub: false
pubsub: {
enabled: false
}
},
connectionManager: {

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,7 +19,8 @@ class MyBundle extends libp2p {
transport: [ TCP ],
streamMuxer: [ Mplex ],
connEncryption: [ SECIO ],
peerDiscovery: [ MulticastDNS ]
peerDiscovery: [ MulticastDNS ],
pubsub: Gossipsub
},
config: {
peerDiscovery: {
@ -27,9 +28,6 @@ class MyBundle extends libp2p {
interval: 2000,
enabled: true
}
},
EXPERIMENTAL: {
pubsub: true
}
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,6 +6,7 @@ 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')

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

View File

@ -5,6 +5,7 @@ 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
@ -20,7 +21,7 @@ let peerRelay = null
* @param {function(error, PeerInfo)} callback
* @returns {void}
*/
module.exports.getPeerRelay = (callback) => {
module.exports.getPeerRelay = promisify((callback) => {
if (peerRelay) return nextTick(callback, null, peerRelay)
PeerId.createFromJSON(peerJSON, (err, peerId) => {
@ -34,7 +35,7 @@ module.exports.getPeerRelay = (callback) => {
callback(null, peerRelay)
})
}
})
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')
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')