mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-10 14:21:33 +00:00
Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
ebdb696742 | |||
721e6ee9ce | |||
98f2903088 | |||
3dda282dfd | |||
03faf69212 | |||
0062a4b5eb | |||
f7f85dce0a | |||
59df82a675 | |||
6651401f0b | |||
cd43863db6 | |||
23e8293b75 | |||
68c170a40d | |||
bd8a35aaf9 | |||
248d86d050 | |||
8225b11082 | |||
1355af2b51 |
17
CHANGELOG.md
17
CHANGELOG.md
@ -1,3 +1,20 @@
|
|||||||
|
<a name="0.16.1"></a>
|
||||||
|
## [0.16.1](https://github.com/libp2p/js-libp2p/compare/v0.16.0...v0.16.1) (2018-02-07)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<a name="0.16.0"></a>
|
||||||
|
# [0.16.0](https://github.com/libp2p/js-libp2p/compare/v0.15.2...v0.16.0) (2018-02-07)
|
||||||
|
|
||||||
|
|
||||||
|
### Features
|
||||||
|
|
||||||
|
* add explicit error for case peer id not included in multiaddr ([#155](https://github.com/libp2p/js-libp2p/issues/155)) ([bd8a35a](https://github.com/libp2p/js-libp2p/commit/bd8a35a))
|
||||||
|
* dialProtocol and small refactor ([6651401](https://github.com/libp2p/js-libp2p/commit/6651401))
|
||||||
|
* use libp2p-switch ([23e8293](https://github.com/libp2p/js-libp2p/commit/23e8293))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<a name="0.15.2"></a>
|
<a name="0.15.2"></a>
|
||||||
## [0.15.2](https://github.com/libp2p/js-libp2p/compare/v0.15.1...v0.15.2) (2018-01-28)
|
## [0.15.2](https://github.com/libp2p/js-libp2p/compare/v0.15.1...v0.15.2) (2018-01-28)
|
||||||
|
|
||||||
|
17
README.md
17
README.md
@ -158,11 +158,20 @@ class Node extends libp2p {
|
|||||||
|
|
||||||
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.
|
`callback` is a function with the following `function (err) {}` signature, where `err` is an Error in case stopping the node fails.
|
||||||
|
|
||||||
#### `libp2p.dial(peer [, protocol, callback])`
|
#### `libp2p.dial(peer, callback)`
|
||||||
|
|
||||||
> Dials to another peer in the network.
|
> Dials to another peer in the network, establishes the connection.
|
||||||
|
|
||||||
- `peer`: can be an instance of [PeerInfo][], [PeerId][] or [multiaddr][]
|
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||||
|
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||||
|
|
||||||
|
`callback` is a function with the following `function (err, conn) {}` signature, where `err` is an Error in of failure to dial the connection and `conn` is a [Connection][] instance in case of a protocol selected, if not it is undefined.
|
||||||
|
|
||||||
|
#### `libp2p.dialProtocol(peer, protocol, callback)`
|
||||||
|
|
||||||
|
> Dials to another peer in the network and selects a protocol to talk with that peer.
|
||||||
|
|
||||||
|
- `peer`: can be an instance of [PeerInfo][], [PeerId][], [multiaddr][], or a multiaddr string
|
||||||
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
- `protocol`: String that defines the protocol (e.g '/ipfs/bitswap/1.1.0')
|
||||||
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
- `callback`: Function with signature `function (err, conn) {}` where `conn` is a [Connection](https://github.com/libp2p/interface-connection) object
|
||||||
|
|
||||||
@ -311,6 +320,8 @@ List of packages currently in existence for libp2p
|
|||||||
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://david-dm.org/libp2p/js-libp2p-utp?type=dev) |
|
| [`libp2p-utp`](//github.com/libp2p/js-libp2p-utp) | [](//github.com/libp2p/js-libp2p-utp/releases) | [](https://david-dm.org/libp2p/js-libp2p-utp) | [](https://david-dm.org/libp2p/js-libp2p-utp?type=dev) |
|
||||||
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://david-dm.org/libp2p/js-libp2p-websockets?type=dev) |
|
| [`libp2p-websockets`](//github.com/libp2p/js-libp2p-websockets) | [](//github.com/libp2p/js-libp2p-websockets/releases) | [](https://david-dm.org/libp2p/js-libp2p-websockets) | [](https://david-dm.org/libp2p/js-libp2p-websockets?type=dev) |
|
||||||
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star?type=dev) |
|
| [`libp2p-webrtc-star`](//github.com/libp2p/js-libp2p-webrtc-star) | [](//github.com/libp2p/js-libp2p-webrtc-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star) | [](https://david-dm.org/libp2p/js-libp2p-webrtc-star?type=dev) |
|
||||||
|
| [`libp2p-websocket-star`](//github.com/libp2p/js-libp2p-websocket-star) | [](//github.com/libp2p/js-libp2p-websocket-star/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star?type=dev) |
|
||||||
|
| [`libp2p-websocket-star-rendezvous`](//github.com/libp2p/js-libp2p-websocket-star-rendezvous) | [](//github.com/libp2p/js-libp2p-websocket-star-rendezvous/releases) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous) | [](https://david-dm.org/libp2p/js-libp2p-websocket-star-rendezvous?type=dev) |
|
||||||
| **Connection Upgrades** |
|
| **Connection Upgrades** |
|
||||||
| [`interface-connection`](//github.com/libp2p/interface-connection) | [](//github.com/libp2p/interface-connection/releases) | [](https://david-dm.org/libp2p/interface-connection) | [](https://david-dm.org/libp2p/interface-connection?type=dev) |
|
| [`interface-connection`](//github.com/libp2p/interface-connection) | [](//github.com/libp2p/interface-connection/releases) | [](https://david-dm.org/libp2p/interface-connection) | [](https://david-dm.org/libp2p/interface-connection?type=dev) |
|
||||||
| **Stream Muxers** |
|
| **Stream Muxers** |
|
||||||
|
@ -47,7 +47,7 @@ async.parallel([
|
|||||||
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
|
console.log(ma.toString() + '/ipfs/' + idListener.toB58String())
|
||||||
})
|
})
|
||||||
|
|
||||||
nodeDialer.dial(peerListener, '/chat/1.0.0', (err, conn) => {
|
nodeDialer.dialProtocol(peerListener, '/chat/1.0.0', (err, conn) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ async.parallel([
|
|||||||
'/ipfs/' + dialerId.toB58String()))
|
'/ipfs/' + dialerId.toB58String()))
|
||||||
|
|
||||||
console.log('Dialing to peer:', listenerMultiaddr.toString())
|
console.log('Dialing to peer:', listenerMultiaddr.toString())
|
||||||
dialerNode.dial(listenerPeerInfo, '/echo/1.0.0', (err, conn) => {
|
dialerNode.dialProtocol(listenerPeerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
|
|
||||||
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')
|
console.log('nodeA dialed to nodeB on protocol: /echo/1.0.0')
|
||||||
|
@ -52,7 +52,7 @@ parallel([
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
node1.dial(node2.peerInfo, '/a-protocol', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/a-protocol', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['This information is sent out encrypted to the other peer']), conn)
|
pull(pull.values(['This information is sent out encrypted to the other peer']), conn)
|
||||||
})
|
})
|
||||||
|
@ -75,20 +75,20 @@ parallel([
|
|||||||
})
|
})
|
||||||
*/
|
*/
|
||||||
|
|
||||||
node1.dial(node2.peerInfo, '/your-protocol', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/your-protocol', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['my own protocol, wow!']), conn)
|
pull(pull.values(['my own protocol, wow!']), conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
node1.dial(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['semver me please']), conn)
|
pull(pull.values(['semver me please']), conn)
|
||||||
})
|
})
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
node1.dial(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['do I fall into your criteria?']), conn)
|
pull(pull.values(['do I fall into your criteria?']), conn)
|
||||||
})
|
})
|
||||||
|
@ -60,17 +60,17 @@ parallel([
|
|||||||
})
|
})
|
||||||
|
|
||||||
series([
|
series([
|
||||||
(cb) => node1.dial(node2.peerInfo, '/a', (err, conn) => {
|
(cb) => node1.dialProtocol(node2.peerInfo, '/a', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['protocol (a)']), conn)
|
pull(pull.values(['protocol (a)']), conn)
|
||||||
cb()
|
cb()
|
||||||
}),
|
}),
|
||||||
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
|
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['protocol (b)']), conn)
|
pull(pull.values(['protocol (b)']), conn)
|
||||||
cb()
|
cb()
|
||||||
}),
|
}),
|
||||||
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
|
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['another conn on protocol (b)']), conn)
|
pull(pull.values(['another conn on protocol (b)']), conn)
|
||||||
cb()
|
cb()
|
||||||
|
@ -30,7 +30,7 @@ node2.handle('/your-protocol', (protocol, conn) => {
|
|||||||
After the protocol is _handled_, now we can dial to it.
|
After the protocol is _handled_, now we can dial to it.
|
||||||
|
|
||||||
```JavaScript
|
```JavaScript
|
||||||
node1.dial(node2.peerInfo, '/your-protocol', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/your-protocol', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['my own protocol, wow!']), conn)
|
pull(pull.values(['my own protocol, wow!']), conn)
|
||||||
})
|
})
|
||||||
@ -47,7 +47,7 @@ node2.handle('/another-protocol/1.0.1', (protocol, conn) => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
// ...
|
// ...
|
||||||
node1.dial(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/another-protocol/1.0.0', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['semver me please']), conn)
|
pull(pull.values(['semver me please']), conn)
|
||||||
})
|
})
|
||||||
@ -74,7 +74,7 @@ node2.handle('/custom-match-func', (protocol, conn) => {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
// ...
|
// ...
|
||||||
node1.dial(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/custom-match-func/some-query', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['do I fall into your criteria?']), conn)
|
pull(pull.values(['do I fall into your criteria?']), conn)
|
||||||
})
|
})
|
||||||
@ -129,17 +129,17 @@ node2.handle('/b', (protocol, conn) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
series([
|
series([
|
||||||
(cb) => node1.dial(node2.peerInfo, '/a', (err, conn) => {
|
(cb) => node1.dialProtocol(node2.peerInfo, '/a', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['protocol (a)']), conn)
|
pull(pull.values(['protocol (a)']), conn)
|
||||||
cb()
|
cb()
|
||||||
}),
|
}),
|
||||||
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
|
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['protocol (b)']), conn)
|
pull(pull.values(['protocol (b)']), conn)
|
||||||
cb()
|
cb()
|
||||||
}),
|
}),
|
||||||
(cb) => node1.dial(node2.peerInfo, '/b', (err, conn) => {
|
(cb) => node1.dialProtocol(node2.peerInfo, '/b', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
pull(pull.values(['another conn on protocol (b)']), conn)
|
pull(pull.values(['another conn on protocol (b)']), conn)
|
||||||
cb()
|
cb()
|
||||||
@ -158,9 +158,9 @@ another protocol (b)
|
|||||||
|
|
||||||
# 3. Bidirectional connections
|
# 3. Bidirectional connections
|
||||||
|
|
||||||
There is one last trick on _protocol and stream multiplexing_ that libp2p uses to make everyone's life easier and that is _biderectional connection_.
|
There is one last trick on _protocol and stream multiplexing_ that libp2p uses to make everyone's life easier and that is _bidirectional connection_.
|
||||||
|
|
||||||
With the aid of both mechanisms, we can reuse an incomming connection to dial streams out too, this is specially useful when you are behind tricky NAT, firewalls or if you are running in a browser, where you can have listening addrs, but you can dial out. By dialing out, you enable other peers to talk with you in Protocols that they want, simply by opening a new multiplexed stream.
|
With the aid of both mechanisms, we can reuse an incomming connection to dial streams out too, this is specially useful when you are behind tricky NAT, firewalls or if you are running in a browser, where you can't have listening addrs, but you can dial out. By dialing out, you enable other peers to talk with you in Protocols that they want, simply by opening a new multiplexed stream.
|
||||||
|
|
||||||
You can see this working on example [3.js](./3.js). The result should look like the following:
|
You can see this working on example [3.js](./3.js). The result should look like the following:
|
||||||
|
|
||||||
|
@ -54,7 +54,7 @@ parallel([
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
node1.dial(node2.peerInfo, '/print', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
|
|
||||||
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
|
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
|
||||||
|
@ -66,19 +66,19 @@ parallel([
|
|||||||
node2.handle('/print', print)
|
node2.handle('/print', print)
|
||||||
node3.handle('/print', print)
|
node3.handle('/print', print)
|
||||||
|
|
||||||
node1.dial(node2.peerInfo, '/print', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
|
|
||||||
pull(pull.values(['node 1 dialed to node 2 successfully']), conn)
|
pull(pull.values(['node 1 dialed to node 2 successfully']), conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
node2.dial(node3.peerInfo, '/print', (err, conn) => {
|
node2.dialProtocol(node3.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
|
|
||||||
pull(pull.values(['node 2 dialed to node 3 successfully']), conn)
|
pull(pull.values(['node 2 dialed to node 3 successfully']), conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
node3.dial(node1.peerInfo, '/print', (err, conn) => {
|
node3.dialProtocol(node1.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('node 3 failed to dial to node 1 with:', err.message)
|
console.log('node 3 failed to dial to node 1 with:', err.message)
|
||||||
}
|
}
|
||||||
|
@ -143,7 +143,7 @@ parallel([
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
node1.dial(node2.peerInfo, '/print', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
|
|
||||||
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
|
pull(pull.values(['Hello', ' ', 'p2p', ' ', 'world', '!']), conn)
|
||||||
@ -240,21 +240,21 @@ parallel([
|
|||||||
node3.handle('/print', print)
|
node3.handle('/print', print)
|
||||||
|
|
||||||
// node 1 (TCP) dials to node 2 (TCP+WebSockets)
|
// node 1 (TCP) dials to node 2 (TCP+WebSockets)
|
||||||
node1.dial(node2.peerInfo, '/print', (err, conn) => {
|
node1.dialProtocol(node2.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
|
|
||||||
pull(pull.values(['node 1 dialed to node 2 successfully']), conn)
|
pull(pull.values(['node 1 dialed to node 2 successfully']), conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
// node 2 (TCP+WebSockets) dials to node 2 (WebSockets)
|
// node 2 (TCP+WebSockets) dials to node 2 (WebSockets)
|
||||||
node2.dial(node3.peerInfo, '/print', (err, conn) => {
|
node2.dialProtocol(node3.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) { throw err }
|
if (err) { throw err }
|
||||||
|
|
||||||
pull(pull.values(['node 2 dialed to node 3 successfully']), conn)
|
pull(pull.values(['node 2 dialed to node 3 successfully']), conn)
|
||||||
})
|
})
|
||||||
|
|
||||||
// node 3 (WebSockets) attempts to dial to node 1 (TCP)
|
// node 3 (WebSockets) attempts to dial to node 1 (TCP)
|
||||||
node3.dial(node1.peerInfo, '/print', (err, conn) => {
|
node3.dialProtocol(node1.peerInfo, '/print', (err, conn) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.log('node 3 failed to dial to node 1 with:', err.message)
|
console.log('node 3 failed to dial to node 1 with:', err.message)
|
||||||
}
|
}
|
||||||
@ -298,8 +298,8 @@ As expected, we created 3 nodes, node 1 with TCP, node 2 with TCP+WebSockets and
|
|||||||
|
|
||||||
Today there are already 3 transports available, one in the works and plenty to come, you can find these at [interface-transport implementations](https://github.com/libp2p/interface-transport#modules-that-implement-the-interface) list.
|
Today there are already 3 transports available, one in the works and plenty to come, you can find these at [interface-transport implementations](https://github.com/libp2p/interface-transport#modules-that-implement-the-interface) list.
|
||||||
|
|
||||||
Adding more transports is done through the same way as you added TCP and WebSockets. Some transports might offer extra functionalities but for what is libp2p concern, as long as it follows the interface defined at the [spec](https://github.com/libp2p/interface-transport#api), it will be able to use it.
|
Adding more transports is done through the same way as you added TCP and WebSockets. Some transports might offer extra functionalities, but as far as libp2p is concerned, if it follows the interface defined at the [spec](https://github.com/libp2p/interface-transport#api) it will be able to use it.
|
||||||
|
|
||||||
If you decide to implement a transport yourself, please consider adding to the list so that others can use it as well.
|
If you decide to implement a transport yourself, please consider adding to the list so that others can use it as well.
|
||||||
|
|
||||||
Hope this tutorial was useful. We are always looking to improve it, contributions are welcome!
|
Hope this tutorial was useful. We are always looking to improve it, so contributions are welcome!
|
||||||
|
13
package.json
13
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "libp2p",
|
"name": "libp2p",
|
||||||
"version": "0.15.2",
|
"version": "0.16.1",
|
||||||
"description": "JavaScript base class for libp2p bundles",
|
"description": "JavaScript base class for libp2p bundles",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -26,7 +26,7 @@
|
|||||||
"node": ">=6.0.0",
|
"node": ">=6.0.0",
|
||||||
"npm": ">=3.0.0"
|
"npm": ">=3.0.0"
|
||||||
},
|
},
|
||||||
"pre-commit": [
|
"pre-push": [
|
||||||
"lint",
|
"lint",
|
||||||
"test"
|
"test"
|
||||||
],
|
],
|
||||||
@ -39,7 +39,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "^2.6.0",
|
"async": "^2.6.0",
|
||||||
"libp2p-ping": "~0.6.0",
|
"libp2p-ping": "~0.6.0",
|
||||||
"libp2p-swarm": "~0.35.1",
|
"libp2p-switch": "~0.36.0",
|
||||||
"mafmt": "^3.0.2",
|
"mafmt": "^3.0.2",
|
||||||
"multiaddr": "^3.0.2",
|
"multiaddr": "^3.0.2",
|
||||||
"peer-book": "~0.5.4",
|
"peer-book": "~0.5.4",
|
||||||
@ -53,13 +53,13 @@
|
|||||||
"dirty-chai": "^2.0.1",
|
"dirty-chai": "^2.0.1",
|
||||||
"electron-webrtc": "~0.3.0",
|
"electron-webrtc": "~0.3.0",
|
||||||
"libp2p-circuit": "~0.1.4",
|
"libp2p-circuit": "~0.1.4",
|
||||||
"libp2p-kad-dht": "~0.6.0",
|
"libp2p-kad-dht": "~0.6.3",
|
||||||
"libp2p-mdns": "~0.9.1",
|
"libp2p-mdns": "~0.9.2",
|
||||||
"libp2p-multiplex": "~0.5.1",
|
"libp2p-multiplex": "~0.5.1",
|
||||||
"libp2p-railing": "~0.7.1",
|
"libp2p-railing": "~0.7.1",
|
||||||
"libp2p-secio": "~0.9.1",
|
"libp2p-secio": "~0.9.1",
|
||||||
"libp2p-spdy": "~0.11.0",
|
"libp2p-spdy": "~0.11.0",
|
||||||
"libp2p-tcp": "~0.11.2",
|
"libp2p-tcp": "~0.11.5",
|
||||||
"libp2p-webrtc-star": "~0.13.3",
|
"libp2p-webrtc-star": "~0.13.3",
|
||||||
"libp2p-websockets": "~0.10.4",
|
"libp2p-websockets": "~0.10.4",
|
||||||
"libp2p-websocket-star": "~0.7.2",
|
"libp2p-websocket-star": "~0.7.2",
|
||||||
@ -75,6 +75,7 @@
|
|||||||
},
|
},
|
||||||
"contributors": [
|
"contributors": [
|
||||||
"Chris Bratlien <chrisbratlien@gmail.com>",
|
"Chris Bratlien <chrisbratlien@gmail.com>",
|
||||||
|
"Chris Dostert <chrisdostert@users.noreply.github.com>",
|
||||||
"Daijiro Wachi <daijiro.wachi@gmail.com>",
|
"Daijiro Wachi <daijiro.wachi@gmail.com>",
|
||||||
"David Dias <daviddias.p@gmail.com>",
|
"David Dias <daviddias.p@gmail.com>",
|
||||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||||
|
20
src/content-routing.js
Normal file
20
src/content-routing.js
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
return {
|
||||||
|
findProviders: (key, timeout, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.findProviders(key, timeout, callback)
|
||||||
|
},
|
||||||
|
provide: (key, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.provide(key, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
27
src/dht.js
Normal file
27
src/dht.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
return {
|
||||||
|
put: (key, value, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.put(key, value, callback)
|
||||||
|
},
|
||||||
|
get: (key, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.get(key, callback)
|
||||||
|
},
|
||||||
|
getMany (key, nVals, callback) {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.getMany(key, nVals, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
src/get-peer-info.js
Normal file
48
src/get-peer-info.js
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const PeerId = require('peer-id')
|
||||||
|
const PeerInfo = require('peer-info')
|
||||||
|
const multiaddr = require('multiaddr')
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
/*
|
||||||
|
* Helper method to check the data type of peer and convert it to PeerInfo
|
||||||
|
*/
|
||||||
|
return function (peer, callback) {
|
||||||
|
let p
|
||||||
|
// PeerInfo
|
||||||
|
if (PeerInfo.isPeerInfo(peer)) {
|
||||||
|
p = peer
|
||||||
|
// Multiaddr instance or Multiaddr String
|
||||||
|
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
|
||||||
|
if (typeof peer === 'string') {
|
||||||
|
peer = multiaddr(peer)
|
||||||
|
}
|
||||||
|
|
||||||
|
const peerIdB58Str = peer.getPeerId()
|
||||||
|
if (!peerIdB58Str) {
|
||||||
|
throw new Error(`peer multiaddr instance or string must include peerId`)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
p = node.peerBook.get(peerIdB58Str)
|
||||||
|
} catch (err) {
|
||||||
|
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
||||||
|
}
|
||||||
|
p.multiaddrs.add(peer)
|
||||||
|
|
||||||
|
// PeerId
|
||||||
|
} else if (PeerId.isPeerId(peer)) {
|
||||||
|
const peerIdB58Str = peer.toB58String()
|
||||||
|
try {
|
||||||
|
p = node.peerBook.get(peerIdB58Str)
|
||||||
|
} catch (err) {
|
||||||
|
return node.peerRouting.findPeer(peer, callback)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return setImmediate(() => callback(new Error('peer type not recognized')))
|
||||||
|
}
|
||||||
|
|
||||||
|
setImmediate(() => callback(null, p))
|
||||||
|
}
|
||||||
|
}
|
176
src/index.js
176
src/index.js
@ -7,12 +7,14 @@ const setImmediate = require('async/setImmediate')
|
|||||||
const each = require('async/each')
|
const each = require('async/each')
|
||||||
const series = require('async/series')
|
const series = require('async/series')
|
||||||
|
|
||||||
const Ping = require('libp2p-ping')
|
|
||||||
const Swarm = require('libp2p-swarm')
|
|
||||||
const PeerId = require('peer-id')
|
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
const PeerBook = require('peer-book')
|
const PeerBook = require('peer-book')
|
||||||
const multiaddr = require('multiaddr')
|
const Switch = require('libp2p-switch')
|
||||||
|
const Ping = require('libp2p-ping')
|
||||||
|
|
||||||
|
const peerRouting = require('./peer-routing')
|
||||||
|
const contentRouting = require('./content-routing')
|
||||||
|
const dht = require('./dht')
|
||||||
|
const getPeerInfo = require('./get-peer-info')
|
||||||
|
|
||||||
exports = module.exports
|
exports = module.exports
|
||||||
|
|
||||||
@ -31,28 +33,28 @@ class Node extends EventEmitter {
|
|||||||
|
|
||||||
this._isStarted = false
|
this._isStarted = false
|
||||||
|
|
||||||
this.swarm = new Swarm(this.peerInfo, this.peerBook)
|
this.switch = new Switch(this.peerInfo, this.peerBook)
|
||||||
|
|
||||||
// Attach stream multiplexers
|
// Attach stream multiplexers
|
||||||
if (this.modules.connection && this.modules.connection.muxer) {
|
if (this.modules.connection && this.modules.connection.muxer) {
|
||||||
let muxers = this.modules.connection.muxer
|
let muxers = this.modules.connection.muxer
|
||||||
muxers = Array.isArray(muxers) ? muxers : [muxers]
|
muxers = Array.isArray(muxers) ? muxers : [muxers]
|
||||||
muxers.forEach((muxer) => this.swarm.connection.addStreamMuxer(muxer))
|
muxers.forEach((muxer) => this.switch.connection.addStreamMuxer(muxer))
|
||||||
|
|
||||||
// If muxer exists, we can use Identify
|
// If muxer exists, we can use Identify
|
||||||
this.swarm.connection.reuse()
|
this.switch.connection.reuse()
|
||||||
|
|
||||||
// If muxer exists, we can use Relay for listening/dialing
|
// If muxer exists, we can use Relay for listening/dialing
|
||||||
this.swarm.connection.enableCircuitRelay(_options.relay)
|
this.switch.connection.enableCircuitRelay(_options.relay)
|
||||||
|
|
||||||
// Received incommind dial and muxer upgrade happened,
|
// Received incommind dial and muxer upgrade happened,
|
||||||
// reuse this muxed connection
|
// reuse this muxed connection
|
||||||
this.swarm.on('peer-mux-established', (peerInfo) => {
|
this.switch.on('peer-mux-established', (peerInfo) => {
|
||||||
this.emit('peer:connect', peerInfo)
|
this.emit('peer:connect', peerInfo)
|
||||||
this.peerBook.put(peerInfo)
|
this.peerBook.put(peerInfo)
|
||||||
})
|
})
|
||||||
|
|
||||||
this.swarm.on('peer-mux-closed', (peerInfo) => {
|
this.switch.on('peer-mux-closed', (peerInfo) => {
|
||||||
this.emit('peer:disconnect', peerInfo)
|
this.emit('peer:disconnect', peerInfo)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -62,7 +64,7 @@ class Node extends EventEmitter {
|
|||||||
let cryptos = this.modules.connection.crypto
|
let cryptos = this.modules.connection.crypto
|
||||||
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
|
cryptos = Array.isArray(cryptos) ? cryptos : [cryptos]
|
||||||
cryptos.forEach((crypto) => {
|
cryptos.forEach((crypto) => {
|
||||||
this.swarm.connection.crypto(crypto.tag, crypto.encrypt)
|
this.switch.connection.crypto(crypto.tag, crypto.encrypt)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,67 +78,22 @@ class Node extends EventEmitter {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mount default protocols
|
|
||||||
Ping.mount(this.swarm)
|
|
||||||
|
|
||||||
// dht provided components (peerRouting, contentRouting, dht)
|
// dht provided components (peerRouting, contentRouting, dht)
|
||||||
if (_modules.DHT) {
|
if (_modules.DHT) {
|
||||||
this._dht = new this.modules.DHT(this.swarm, {
|
this._dht = new this.modules.DHT(this.switch, {
|
||||||
kBucketSize: 20,
|
kBucketSize: 20,
|
||||||
datastore: _options.DHT && _options.DHT.datastore
|
datastore: _options.DHT && _options.DHT.datastore
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
this.peerRouting = {
|
this.peerRouting = peerRouting(this)
|
||||||
findPeer: (id, callback) => {
|
this.contentRouting = contentRouting(this)
|
||||||
if (!this._dht) {
|
this.dht = dht(this)
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.findPeer(id, callback)
|
this._getPeerInfo = getPeerInfo(this)
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.contentRouting = {
|
// Mount default protocols
|
||||||
findProviders: (key, timeout, callback) => {
|
Ping.mount(this.switch)
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.findProviders(key, timeout, callback)
|
|
||||||
},
|
|
||||||
provide: (key, callback) => {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.provide(key, callback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.dht = {
|
|
||||||
put: (key, value, callback) => {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.put(key, value, callback)
|
|
||||||
},
|
|
||||||
get: (key, callback) => {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.get(key, callback)
|
|
||||||
},
|
|
||||||
getMany (key, nVals, callback) {
|
|
||||||
if (!this._dht) {
|
|
||||||
return callback(new Error('DHT is not available'))
|
|
||||||
}
|
|
||||||
|
|
||||||
this._dht.getMany(key, nVals, callback)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -167,7 +124,7 @@ class Node extends EventEmitter {
|
|||||||
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
const multiaddrs = this.peerInfo.multiaddrs.toArray()
|
||||||
transports.forEach((transport) => {
|
transports.forEach((transport) => {
|
||||||
if (transport.filter(multiaddrs).length > 0) {
|
if (transport.filter(multiaddrs).length > 0) {
|
||||||
this.swarm.transport.add(
|
this.switch.transport.add(
|
||||||
transport.tag || transport.constructor.name, transport)
|
transport.tag || transport.constructor.name, transport)
|
||||||
} else if (transport.constructor &&
|
} else if (transport.constructor &&
|
||||||
transport.constructor.name === 'WebSockets') {
|
transport.constructor.name === 'WebSockets') {
|
||||||
@ -178,11 +135,11 @@ class Node extends EventEmitter {
|
|||||||
})
|
})
|
||||||
|
|
||||||
series([
|
series([
|
||||||
(cb) => this.swarm.listen(cb),
|
(cb) => this.switch.start(cb),
|
||||||
(cb) => {
|
(cb) => {
|
||||||
if (ws) {
|
if (ws) {
|
||||||
// always add dialing on websockets
|
// always add dialing on websockets
|
||||||
this.swarm.transport.add(ws.tag || ws.constructor.name, ws)
|
this.switch.transport.add(ws.tag || ws.constructor.name, ws)
|
||||||
}
|
}
|
||||||
|
|
||||||
// all transports need to be setup before discover starts
|
// all transports need to be setup before discover starts
|
||||||
@ -237,7 +194,7 @@ class Node extends EventEmitter {
|
|||||||
}
|
}
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => this.swarm.close(cb),
|
(cb) => this.switch.stop(cb),
|
||||||
(cb) => {
|
(cb) => {
|
||||||
this.emit('stop')
|
this.emit('stop')
|
||||||
cb()
|
cb()
|
||||||
@ -252,18 +209,22 @@ class Node extends EventEmitter {
|
|||||||
return this._isStarted
|
return this._isStarted
|
||||||
}
|
}
|
||||||
|
|
||||||
ping (peer, callback) {
|
dial (peer, callback) {
|
||||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
|
||||||
if (err) {
|
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
callback(null, new Ping(this.swarm, peerInfo))
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
|
if (err) { return callback(err) }
|
||||||
|
|
||||||
|
this.switch.dial(peerInfo, (err) => {
|
||||||
|
if (err) { return callback(err) }
|
||||||
|
|
||||||
|
this.peerBook.put(peerInfo)
|
||||||
|
callback()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
dial (peer, protocol, callback) {
|
dialProtocol (peer, protocol, callback) {
|
||||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
|
|
||||||
if (typeof protocol === 'function') {
|
if (typeof protocol === 'function') {
|
||||||
@ -272,14 +233,10 @@ class Node extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
if (err) {
|
if (err) { return callback(err) }
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.swarm.dial(peerInfo, protocol, (err, conn) => {
|
this.switch.dial(peerInfo, protocol, (err, conn) => {
|
||||||
if (err) {
|
if (err) { return callback(err) }
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
this.peerBook.put(peerInfo)
|
this.peerBook.put(peerInfo)
|
||||||
callback(null, conn)
|
callback(null, conn)
|
||||||
})
|
})
|
||||||
@ -290,56 +247,27 @@ class Node extends EventEmitter {
|
|||||||
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
|
|
||||||
this._getPeerInfo(peer, (err, peerInfo) => {
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
if (err) {
|
if (err) { return callback(err) }
|
||||||
return callback(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.swarm.hangUp(peerInfo, callback)
|
this.switch.hangUp(peerInfo, callback)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
ping (peer, callback) {
|
||||||
|
assert(this.isStarted(), NOT_STARTED_ERROR_MESSAGE)
|
||||||
|
this._getPeerInfo(peer, (err, peerInfo) => {
|
||||||
|
if (err) { return callback(err) }
|
||||||
|
|
||||||
|
callback(null, new Ping(this.switch, peerInfo))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
handle (protocol, handlerFunc, matchFunc) {
|
handle (protocol, handlerFunc, matchFunc) {
|
||||||
this.swarm.handle(protocol, handlerFunc, matchFunc)
|
this.switch.handle(protocol, handlerFunc, matchFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
unhandle (protocol) {
|
unhandle (protocol) {
|
||||||
this.swarm.unhandle(protocol)
|
this.switch.unhandle(protocol)
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Helper method to check the data type of peer and convert it to PeerInfo
|
|
||||||
*/
|
|
||||||
_getPeerInfo (peer, callback) {
|
|
||||||
let p
|
|
||||||
// PeerInfo
|
|
||||||
if (PeerInfo.isPeerInfo(peer)) {
|
|
||||||
p = peer
|
|
||||||
// Multiaddr instance or Multiaddr String
|
|
||||||
} else if (multiaddr.isMultiaddr(peer) || typeof peer === 'string') {
|
|
||||||
if (typeof peer === 'string') {
|
|
||||||
peer = multiaddr(peer)
|
|
||||||
}
|
|
||||||
const peerIdB58Str = peer.getPeerId()
|
|
||||||
try {
|
|
||||||
p = this.peerBook.get(peerIdB58Str)
|
|
||||||
} catch (err) {
|
|
||||||
p = new PeerInfo(PeerId.createFromB58String(peerIdB58Str))
|
|
||||||
}
|
|
||||||
p.multiaddrs.add(peer)
|
|
||||||
|
|
||||||
// PeerId
|
|
||||||
} else if (PeerId.isPeerId(peer)) {
|
|
||||||
const peerIdB58Str = peer.toB58String()
|
|
||||||
try {
|
|
||||||
p = this.peerBook.get(peerIdB58Str)
|
|
||||||
} catch (err) {
|
|
||||||
return this.peerRouting.findPeer(peer, callback)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return setImmediate(() => callback(new Error('peer type not recognized')))
|
|
||||||
}
|
|
||||||
|
|
||||||
setImmediate(() => callback(null, p))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
src/peer-routing.js
Normal file
13
src/peer-routing.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
module.exports = (node) => {
|
||||||
|
return {
|
||||||
|
findPeer: (id, callback) => {
|
||||||
|
if (!node._dht) {
|
||||||
|
return callback(new Error('DHT is not available'))
|
||||||
|
}
|
||||||
|
|
||||||
|
node._dht.findPeer(id, callback)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,17 +1,16 @@
|
|||||||
/* eslint-env mocha */
|
/* eslint-env mocha */
|
||||||
'use strict'
|
'use strict'
|
||||||
|
|
||||||
const pull = require('pull-stream')
|
|
||||||
const waterfall = require('async/waterfall')
|
const waterfall = require('async/waterfall')
|
||||||
const series = require('async/series')
|
const series = require('async/series')
|
||||||
const parallel = require('async/parallel')
|
const parallel = require('async/parallel')
|
||||||
const utils = require('./utils/node')
|
const utils = require('./utils/node')
|
||||||
const Circuit = require('libp2p-circuit')
|
const Circuit = require('libp2p-circuit')
|
||||||
const multiaddr = require('multiaddr')
|
const multiaddr = require('multiaddr')
|
||||||
|
const tryEcho = require('./utils/try-echo')
|
||||||
|
|
||||||
const chai = require('chai')
|
const chai = require('chai')
|
||||||
chai.use(require('dirty-chai'))
|
chai.use(require('dirty-chai'))
|
||||||
|
|
||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
const sinon = require('sinon')
|
const sinon = require('sinon')
|
||||||
|
|
||||||
@ -39,7 +38,7 @@ describe('circuit relay', function () {
|
|||||||
node.start((err) => {
|
node.start((err) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
handlerSpies.push(sinon.spy(node.swarm.transports[Circuit.tag].listeners[0].hopHandler, 'handle'))
|
handlerSpies.push(sinon.spy(node.switch.transports[Circuit.tag].listeners[0].hopHandler, 'handle'))
|
||||||
cb(node)
|
cb(node)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -153,25 +152,16 @@ describe('circuit relay', function () {
|
|||||||
describe('any relay', function () {
|
describe('any relay', function () {
|
||||||
this.timeout(20 * 1000)
|
this.timeout(20 * 1000)
|
||||||
|
|
||||||
it('should dial from WS1 to TCP1 over any R', (done) => {
|
it('dial from WS1 to TCP1 over any R', (done) => {
|
||||||
nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
|
nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(conn).to.exist()
|
expect(conn).to.exist()
|
||||||
|
tryEcho(conn, done)
|
||||||
pull(
|
|
||||||
pull.values(['hello']),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, result) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(result[0].toString()).to.equal('hello')
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should not dial - no R from WS2 to TCP1', (done) => {
|
it('fail to dial - no R from WS2 to TCP1', (done) => {
|
||||||
nodeWS2.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
|
nodeWS2.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.exist()
|
expect(err).to.exist()
|
||||||
expect(conn).to.not.exist()
|
expect(conn).to.not.exist()
|
||||||
done()
|
done()
|
||||||
@ -182,43 +172,29 @@ describe('circuit relay', function () {
|
|||||||
describe('explicit relay', function () {
|
describe('explicit relay', function () {
|
||||||
this.timeout(20 * 1000)
|
this.timeout(20 * 1000)
|
||||||
|
|
||||||
it('should dial from WS1 to TCP1 over R1', (done) => {
|
it('dial from WS1 to TCP1 over R1', (done) => {
|
||||||
nodeWS1.dial(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
|
nodeWS1.dialProtocol(nodeTCP1.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(conn).to.exist()
|
expect(conn).to.exist()
|
||||||
|
|
||||||
pull(
|
tryEcho(conn, () => {
|
||||||
pull.values(['hello']),
|
const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString()
|
||||||
conn,
|
expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`)
|
||||||
pull.collect((err, result) => {
|
done()
|
||||||
expect(err).to.not.exist()
|
})
|
||||||
expect(result[0].toString()).to.equal('hello')
|
|
||||||
|
|
||||||
const addr = multiaddr(handlerSpies[0].args[2][0].dstPeer.addrs[0]).toString()
|
|
||||||
expect(addr).to.equal(`/ipfs/${nodeTCP1.peerInfo.id.toB58String()}`)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should dial from WS1 to TCP2 over R2', (done) => {
|
it('dial from WS1 to TCP2 over R2', (done) => {
|
||||||
nodeWS1.dial(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
|
nodeWS1.dialProtocol(nodeTCP2.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(conn).to.exist()
|
expect(conn).to.exist()
|
||||||
|
|
||||||
pull(
|
tryEcho(conn, () => {
|
||||||
pull.values(['hello']),
|
const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString()
|
||||||
conn,
|
expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`)
|
||||||
pull.collect((err, result) => {
|
done()
|
||||||
expect(err).to.not.exist()
|
})
|
||||||
expect(result[0].toString()).to.equal('hello')
|
|
||||||
|
|
||||||
const addr = multiaddr(handlerSpies[1].args[2][0].dstPeer.addrs[0]).toString()
|
|
||||||
expect(addr).to.equal(`/ipfs/${nodeTCP2.peerInfo.id.toB58String()}`)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -6,24 +6,15 @@ chai.use(require('dirty-chai'))
|
|||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
const parallel = require('async/parallel')
|
const parallel = require('async/parallel')
|
||||||
const series = require('async/series')
|
const series = require('async/series')
|
||||||
const pull = require('pull-stream')
|
|
||||||
const utils = require('./utils/node')
|
const utils = require('./utils/node')
|
||||||
|
const tryEcho = require('./utils/try-echo')
|
||||||
const createNode = utils.createNode
|
const createNode = utils.createNode
|
||||||
const echo = utils.echo
|
const echo = utils.echo
|
||||||
|
|
||||||
function test (nodeA, nodeB, callback) {
|
function test (nodeA, nodeB, callback) {
|
||||||
nodeA.dial(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
tryEcho(conn, callback)
|
||||||
pull(
|
|
||||||
pull.values([Buffer.from('hey')]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data).to.be.eql([Buffer.from('hey')])
|
|
||||||
callback()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +127,7 @@ describe('stream muxing', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('spdy + multiplex switched order', function (done) {
|
it('spdy + multiplex switched order', function (done) {
|
||||||
this.timeout(5000)
|
this.timeout(5 * 1000)
|
||||||
|
|
||||||
let nodeA
|
let nodeA
|
||||||
let nodeB
|
let nodeB
|
||||||
@ -170,7 +161,7 @@ describe('stream muxing', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('one without the other fails to establish a muxedConn', function (done) {
|
it('one without the other fails to establish a muxedConn', function (done) {
|
||||||
this.timeout(5000)
|
this.timeout(5 * 1000)
|
||||||
|
|
||||||
let nodeA
|
let nodeA
|
||||||
let nodeB
|
let nodeB
|
||||||
@ -200,12 +191,12 @@ describe('stream muxing', () => {
|
|||||||
(cb) => setup(cb),
|
(cb) => setup(cb),
|
||||||
(cb) => {
|
(cb) => {
|
||||||
// it will just 'warm up a conn'
|
// it will just 'warm up a conn'
|
||||||
expect(Object.keys(nodeA.swarm.muxers)).to.have.length(1)
|
expect(Object.keys(nodeA.switch.muxers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeB.swarm.muxers)).to.have.length(1)
|
expect(Object.keys(nodeB.switch.muxers)).to.have.length(1)
|
||||||
|
|
||||||
nodeA.dial(nodeB.peerInfo, (err) => {
|
nodeA.dial(nodeB.peerInfo, (err) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -12,6 +12,7 @@ const parallel = require('async/parallel')
|
|||||||
const goodbye = require('pull-goodbye')
|
const goodbye = require('pull-goodbye')
|
||||||
const serializer = require('pull-serializer')
|
const serializer = require('pull-serializer')
|
||||||
const w = require('webrtcsupport')
|
const w = require('webrtcsupport')
|
||||||
|
const tryEcho = require('./utils/try-echo')
|
||||||
|
|
||||||
const Node = require('./utils/bundle.browser')
|
const Node = require('./utils/bundle.browser')
|
||||||
const rawPeer = require('./fixtures/test-peer.json')
|
const rawPeer = require('./fixtures/test-peer.json')
|
||||||
@ -63,7 +64,7 @@ describe('transports', () => {
|
|||||||
|
|
||||||
// General connectivity tests
|
// General connectivity tests
|
||||||
|
|
||||||
it('libp2p.dial using Multiaddr nodeA to nodeB', (done) => {
|
it('.dial using Multiaddr', (done) => {
|
||||||
nodeA.dial(peerB.multiaddrs.toArray()[0], (err) => {
|
nodeA.dial(peerB.multiaddrs.toArray()[0], (err) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
@ -77,26 +78,18 @@ describe('transports', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('libp2p.dial using Multiaddr on Protocol nodeA to nodeB', (done) => {
|
it('.dialProtocol using Multiaddr', (done) => {
|
||||||
nodeA.dial(peerB.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(peerB.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
|
|
||||||
pull(
|
tryEcho(conn, done)
|
||||||
pull.values([Buffer.from('hey')]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data).to.eql([Buffer.from('hey')])
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('libp2p.hangUp using Multiaddr nodeA to nodeB', (done) => {
|
it('.hangUp using Multiaddr', (done) => {
|
||||||
nodeA.hangUp(peerB.multiaddrs.toArray()[0], (err) => {
|
nodeA.hangUp(peerB.multiaddrs.toArray()[0], (err) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
@ -105,13 +98,13 @@ describe('transports', () => {
|
|||||||
function check () {
|
function check () {
|
||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('libp2p.dial using PeerInfo nodeA to nodeB', (done) => {
|
it('.dial using PeerInfo', (done) => {
|
||||||
nodeA.dial(peerB, (err) => {
|
nodeA.dial(peerB, (err) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
@ -125,26 +118,18 @@ describe('transports', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('libp2p.dial using PeerInfo on Protocol nodeA to nodeB', (done) => {
|
it('.dialProtocol using PeerInfo', (done) => {
|
||||||
nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
|
|
||||||
pull(
|
tryEcho(conn, done)
|
||||||
pull.values([Buffer.from('hey')]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data).to.eql([Buffer.from('hey')])
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('libp2p.hangUp using PeerInfo nodeA to nodeB', (done) => {
|
it('.hangUp using PeerInfo', (done) => {
|
||||||
nodeA.hangUp(peerB, (err) => {
|
nodeA.hangUp(peerB, (err) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
setTimeout(check, 500)
|
setTimeout(check, 500)
|
||||||
@ -153,7 +138,7 @@ describe('transports', () => {
|
|||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -161,7 +146,7 @@ describe('transports', () => {
|
|||||||
|
|
||||||
describe('stress', () => {
|
describe('stress', () => {
|
||||||
it('one big write', (done) => {
|
it('one big write', (done) => {
|
||||||
nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
const rawMessage = Buffer.alloc(100000)
|
const rawMessage = Buffer.alloc(100000)
|
||||||
rawMessage.fill('a')
|
rawMessage.fill('a')
|
||||||
@ -180,7 +165,7 @@ describe('transports', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('many writes', (done) => {
|
it('many writes', (done) => {
|
||||||
nodeA.dial(peerB, '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(peerB, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
const s = serializer(goodbye({
|
const s = serializer(goodbye({
|
||||||
@ -235,39 +220,30 @@ describe('transports', () => {
|
|||||||
done()
|
done()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('listen on the two libp2p nodes', (done) => {
|
it('start two libp2p nodes', (done) => {
|
||||||
parallel([
|
parallel([
|
||||||
(cb) => node1.start(cb),
|
(cb) => node1.start(cb),
|
||||||
(cb) => node2.start(cb)
|
(cb) => node2.start(cb)
|
||||||
], done)
|
], done)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('handle a protocol on the first node', () => {
|
it('.handle echo on first node', () => {
|
||||||
node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('dial from the second node to the first node', (done) => {
|
it('.dialProtocol from the second node to the first node', (done) => {
|
||||||
node1.dial(peer2, '/echo/1.0.0', (err, conn) => {
|
node1.dialProtocol(peer2, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
setTimeout(check, 500)
|
setTimeout(check, 500)
|
||||||
|
|
||||||
function check () {
|
function check () {
|
||||||
const text = 'hello'
|
|
||||||
const peers1 = node1.peerBook.getAll()
|
const peers1 = node1.peerBook.getAll()
|
||||||
expect(Object.keys(peers1)).to.have.length(1)
|
expect(Object.keys(peers1)).to.have.length(1)
|
||||||
|
|
||||||
const peers2 = node2.peerBook.getAll()
|
const peers2 = node2.peerBook.getAll()
|
||||||
expect(Object.keys(peers2)).to.have.length(1)
|
expect(Object.keys(peers2)).to.have.length(1)
|
||||||
|
|
||||||
pull(
|
tryEcho(conn, done)
|
||||||
pull.values([Buffer.from(text)]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data[0].toString()).to.equal(text)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -280,7 +256,7 @@ describe('transports', () => {
|
|||||||
function check () {
|
function check () {
|
||||||
const peers = node1.peerBook.getAll()
|
const peers = node1.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -291,8 +267,8 @@ describe('transports', () => {
|
|||||||
|
|
||||||
function check () {
|
function check () {
|
||||||
if (++counter === 3) {
|
if (++counter === 3) {
|
||||||
expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1)
|
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
|
||||||
expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1)
|
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -355,28 +331,19 @@ describe('transports', () => {
|
|||||||
node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
node2.handle('/echo/1.0.0', (protocol, conn) => pull(conn, conn))
|
||||||
})
|
})
|
||||||
|
|
||||||
it('dial from the second node to the first node', (done) => {
|
it('.dialProtocol from the second node to the first node', (done) => {
|
||||||
node1.dial(peer2, '/echo/1.0.0', (err, conn) => {
|
node1.dialProtocol(peer2, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
setTimeout(check, 500)
|
setTimeout(check, 500)
|
||||||
|
|
||||||
function check () {
|
function check () {
|
||||||
const text = 'hello'
|
|
||||||
const peers1 = node1.peerBook.getAll()
|
const peers1 = node1.peerBook.getAll()
|
||||||
expect(Object.keys(peers1)).to.have.length(1)
|
expect(Object.keys(peers1)).to.have.length(1)
|
||||||
|
|
||||||
const peers2 = node2.peerBook.getAll()
|
const peers2 = node2.peerBook.getAll()
|
||||||
expect(Object.keys(peers2)).to.have.length(1)
|
expect(Object.keys(peers2)).to.have.length(1)
|
||||||
|
|
||||||
pull(
|
tryEcho(conn, done)
|
||||||
pull.values([Buffer.from(text)]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data[0].toString()).to.equal(text)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -389,7 +356,7 @@ describe('transports', () => {
|
|||||||
function check () {
|
function check () {
|
||||||
const peers = node1.peerBook.getAll()
|
const peers = node1.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(node1.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(node1.switch.muxedConns)).to.have.length(0)
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -400,8 +367,8 @@ describe('transports', () => {
|
|||||||
|
|
||||||
function check () {
|
function check () {
|
||||||
if (++counter === 3) {
|
if (++counter === 3) {
|
||||||
expect(Object.keys(node1.swarm.muxedConns).length).to.equal(1)
|
expect(Object.keys(node1.switch.muxedConns).length).to.equal(1)
|
||||||
expect(Object.keys(node2.swarm.muxedConns).length).to.equal(1)
|
expect(Object.keys(node2.switch.muxedConns).length).to.equal(1)
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,13 @@ chai.use(require('dirty-chai'))
|
|||||||
const expect = chai.expect
|
const expect = chai.expect
|
||||||
const parallel = require('async/parallel')
|
const parallel = require('async/parallel')
|
||||||
const series = require('async/series')
|
const series = require('async/series')
|
||||||
const pull = require('pull-stream')
|
|
||||||
const utils = require('./utils/node.js')
|
const utils = require('./utils/node.js')
|
||||||
const signalling = require('libp2p-webrtc-star/src/sig-server')
|
const signalling = require('libp2p-webrtc-star/src/sig-server')
|
||||||
const rendezvous = require('libp2p-websocket-star-rendezvous')
|
const rendezvous = require('libp2p-websocket-star-rendezvous')
|
||||||
const WSStar = require('libp2p-websocket-star')
|
const WSStar = require('libp2p-websocket-star')
|
||||||
const WRTCStar = require('libp2p-webrtc-star')
|
const WRTCStar = require('libp2p-webrtc-star')
|
||||||
const wrtc = require('wrtc')
|
const wrtc = require('wrtc')
|
||||||
|
const tryEcho = require('./utils/try-echo')
|
||||||
|
|
||||||
const createNode = utils.createNode
|
const createNode = utils.createNode
|
||||||
const echo = utils.echo
|
const echo = utils.echo
|
||||||
@ -73,18 +73,10 @@ describe('transports', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it('nodeA.dial nodeB using PeerInfo', (done) => {
|
it('nodeA.dial nodeB using PeerInfo', (done) => {
|
||||||
nodeA.dial(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(nodeB.peerInfo, '/echo/1.0.0', (err, conn) => {
|
||||||
expect(err).to.not.exist()
|
expect(err).to.not.exist()
|
||||||
|
|
||||||
pull(
|
tryEcho(conn, done)
|
||||||
pull.values([Buffer.from('hey')]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data).to.be.eql([Buffer.from('hey')])
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -98,14 +90,14 @@ describe('transports', () => {
|
|||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeB.peerBook.getAll()
|
const peers = nodeB.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
|
|
||||||
expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeB.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], done)
|
], done)
|
||||||
@ -113,8 +105,8 @@ describe('transports', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('nodeA.dial nodeB using multiaddr', (done) => {
|
it('nodeA.dialProtocol nodeB using multiaddr', (done) => {
|
||||||
nodeA.dial(nodeB.peerInfo.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(nodeB.peerInfo.multiaddrs.toArray()[0], '/echo/1.0.0', (err, conn) => {
|
||||||
// Some time for Identify to finish
|
// Some time for Identify to finish
|
||||||
setTimeout(check, 500)
|
setTimeout(check, 500)
|
||||||
|
|
||||||
@ -125,27 +117,17 @@ describe('transports', () => {
|
|||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
|
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeB.peerBook.getAll()
|
const peers = nodeB.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
|
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], () => {
|
], () => tryEcho(conn, done))
|
||||||
pull(
|
|
||||||
pull.values([Buffer.from('hey')]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data).to.be.eql([Buffer.from('hey')])
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -161,14 +143,14 @@ describe('transports', () => {
|
|||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
|
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeB.peerBook.getAll()
|
const peers = nodeB.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
|
|
||||||
expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeB.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], done)
|
], done)
|
||||||
@ -176,8 +158,8 @@ describe('transports', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('nodeA.dial nodeB using PeerId', (done) => {
|
it('nodeA.dialProtocol nodeB using PeerId', (done) => {
|
||||||
nodeA.dial(nodeB.peerInfo.id, '/echo/1.0.0', (err, conn) => {
|
nodeA.dialProtocol(nodeB.peerInfo.id, '/echo/1.0.0', (err, conn) => {
|
||||||
// Some time for Identify to finish
|
// Some time for Identify to finish
|
||||||
setTimeout(check, 500)
|
setTimeout(check, 500)
|
||||||
|
|
||||||
@ -187,26 +169,16 @@ describe('transports', () => {
|
|||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeB.peerBook.getAll()
|
const peers = nodeB.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], () => {
|
], () => tryEcho(conn, done))
|
||||||
pull(
|
|
||||||
pull.values([Buffer.from('hey')]),
|
|
||||||
conn,
|
|
||||||
pull.collect((err, data) => {
|
|
||||||
expect(err).to.not.exist()
|
|
||||||
expect(data).to.eql([Buffer.from('hey')])
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -221,13 +193,13 @@ describe('transports', () => {
|
|||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeB.peerBook.getAll()
|
const peers = nodeB.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeB.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeB.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], done)
|
], done)
|
||||||
@ -291,13 +263,13 @@ describe('transports', () => {
|
|||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeTCP.peerBook.getAll()
|
const peers = nodeTCP.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeTCP.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeTCPnWS.peerBook.getAll()
|
const peers = nodeTCPnWS.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], done)
|
], done)
|
||||||
@ -315,14 +287,14 @@ describe('transports', () => {
|
|||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeTCP.peerBook.getAll()
|
const peers = nodeTCP.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeTCP.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeTCP.switch.muxedConns)).to.have.length(0)
|
||||||
|
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeTCPnWS.peerBook.getAll()
|
const peers = nodeTCPnWS.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], done)
|
], done)
|
||||||
@ -342,13 +314,13 @@ describe('transports', () => {
|
|||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeTCPnWS.peerBook.getAll()
|
const peers = nodeTCPnWS.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(2)
|
expect(Object.keys(peers)).to.have.length(2)
|
||||||
expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeWS.peerBook.getAll()
|
const peers = nodeWS.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(1)
|
expect(Object.keys(nodeWS.switch.muxedConns)).to.have.length(1)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], done)
|
], done)
|
||||||
@ -366,14 +338,14 @@ describe('transports', () => {
|
|||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeTCPnWS.peerBook.getAll()
|
const peers = nodeTCPnWS.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(2)
|
expect(Object.keys(peers)).to.have.length(2)
|
||||||
expect(Object.keys(nodeTCPnWS.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeTCPnWS.switch.muxedConns)).to.have.length(0)
|
||||||
|
|
||||||
cb()
|
cb()
|
||||||
},
|
},
|
||||||
(cb) => {
|
(cb) => {
|
||||||
const peers = nodeWS.peerBook.getAll()
|
const peers = nodeWS.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeWS.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeWS.switch.muxedConns)).to.have.length(0)
|
||||||
cb()
|
cb()
|
||||||
}
|
}
|
||||||
], done)
|
], done)
|
||||||
@ -481,7 +453,7 @@ describe('transports', () => {
|
|||||||
let i = 1;
|
let i = 1;
|
||||||
[nodeAll, otherNode].forEach((node) => {
|
[nodeAll, otherNode].forEach((node) => {
|
||||||
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
|
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
|
||||||
expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed)
|
expect(Object.keys(node.switch.muxedConns)).to.have.length(muxed)
|
||||||
})
|
})
|
||||||
callback()
|
callback()
|
||||||
}
|
}
|
||||||
@ -624,7 +596,7 @@ describe('transports', () => {
|
|||||||
let i = 1;
|
let i = 1;
|
||||||
[nodeAll, otherNode].forEach((node) => {
|
[nodeAll, otherNode].forEach((node) => {
|
||||||
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
|
expect(Object.keys(node.peerBook.getAll())).to.have.length(i-- ? peers : 1)
|
||||||
expect(Object.keys(node.swarm.muxedConns)).to.have.length(muxed)
|
expect(Object.keys(node.switch.muxedConns)).to.have.length(muxed)
|
||||||
})
|
})
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ describe('Turbolence tests', () => {
|
|||||||
function check () {
|
function check () {
|
||||||
const peers = nodeA.peerBook.getAll()
|
const peers = nodeA.peerBook.getAll()
|
||||||
expect(Object.keys(peers)).to.have.length(1)
|
expect(Object.keys(peers)).to.have.length(1)
|
||||||
expect(Object.keys(nodeA.swarm.muxedConns)).to.have.length(0)
|
expect(Object.keys(nodeA.switch.muxedConns)).to.have.length(0)
|
||||||
done()
|
done()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
21
test/utils/try-echo.js
Normal file
21
test/utils/try-echo.js
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
'use strict'
|
||||||
|
|
||||||
|
const pull = require('pull-stream')
|
||||||
|
const chai = require('chai')
|
||||||
|
const dirtyChai = require('dirty-chai')
|
||||||
|
const expect = chai.expect
|
||||||
|
chai.use(dirtyChai)
|
||||||
|
|
||||||
|
module.exports = (conn, callback) => {
|
||||||
|
const values = [Buffer.from('echo')]
|
||||||
|
|
||||||
|
pull(
|
||||||
|
pull.values(values),
|
||||||
|
conn,
|
||||||
|
pull.collect((err, _values) => {
|
||||||
|
expect(err).to.not.exist()
|
||||||
|
expect(_values).to.eql(values)
|
||||||
|
callback()
|
||||||
|
})
|
||||||
|
)
|
||||||
|
}
|
Reference in New Issue
Block a user