mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-07-16 00:51:57 +00:00
Compare commits
1 Commits
v0.29.1
...
fix/event-
Author | SHA1 | Date | |
---|---|---|---|
|
4c1b376c65 |
@@ -7,7 +7,7 @@ stages:
|
||||
|
||||
node_js:
|
||||
- 'lts/*'
|
||||
- '14'
|
||||
- 'stable'
|
||||
|
||||
os:
|
||||
- linux
|
||||
|
12
CHANGELOG.md
12
CHANGELOG.md
@@ -1,15 +1,3 @@
|
||||
<a name="0.29.1"></a>
|
||||
## [0.29.1](https://github.com/libp2p/js-libp2p/compare/v0.29.0...v0.29.1) (2020-10-22)
|
||||
|
||||
|
||||
### Bug Fixes
|
||||
|
||||
* catch error in upgrader close call ([e04224a](https://github.com/libp2p/js-libp2p/commit/e04224a))
|
||||
* ensure streams are closed on connection close ([4c6be91](https://github.com/libp2p/js-libp2p/commit/4c6be91))
|
||||
* flakey identify test firefox ([#774](https://github.com/libp2p/js-libp2p/issues/774)) ([60d437f](https://github.com/libp2p/js-libp2p/commit/60d437f))
|
||||
|
||||
|
||||
|
||||
<a name="0.29.0"></a>
|
||||
# [0.29.0](https://github.com/libp2p/js-libp2p/compare/v0.28.10...v0.29.0) (2020-08-27)
|
||||
|
||||
|
@@ -24,7 +24,7 @@
|
||||
<a href="https://github.com/feross/standard"><img src="https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square"></a>
|
||||
<a href="https://github.com/RichardLitt/standard-readme"><img src="https://img.shields.io/badge/standard--readme-OK-green.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/npm-%3E%3D6.0.0-orange.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D12.0.0-orange.svg?style=flat-square" /></a>
|
||||
<a href=""><img src="https://img.shields.io/badge/Node.js-%3E%3D10.0.0-orange.svg?style=flat-square" /></a>
|
||||
<br>
|
||||
</p>
|
||||
|
||||
|
@@ -48,8 +48,6 @@
|
||||
* [`pubsub.unsubscribe`](#pubsubunsubscribe)
|
||||
* [`pubsub.on`](#pubsubon)
|
||||
* [`pubsub.removeListener`](#pubsubremovelistener)
|
||||
* [`pubsub.topicValidators.set`](#pubsubtopicvalidatorsset)
|
||||
* [`pubsub.topicValidators.delete`](#pubsubtopicvalidatorsdelete)
|
||||
* [`connectionManager.get`](#connectionmanagerget)
|
||||
* [`connectionManager.setPeerValue`](#connectionmanagersetpeervalue)
|
||||
* [`connectionManager.size`](#connectionmanagersize)
|
||||
|
@@ -8,7 +8,7 @@ const { NOISE } = require('libp2p-noise')
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
// To signal the addresses we want to be available, we use
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
|
@@ -12,7 +12,7 @@ const concat = require('it-concat')
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
// To signal the addresses we want to be available, we use
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
|
@@ -16,7 +16,7 @@ const createNode = async (transports, addresses = []) => {
|
||||
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: addresses
|
||||
listen: addresses.map((a) => a)
|
||||
},
|
||||
modules: {
|
||||
transport: transports,
|
||||
|
@@ -13,10 +13,10 @@ When using libp2p, you need properly configure it, that is, pick your set of mod
|
||||
You will need 4 dependencies total, so go ahead and install all of them with:
|
||||
|
||||
```bash
|
||||
> npm install libp2p libp2p-tcp libp2p-noise
|
||||
> npm install libp2p libp2p-tcp libp2p-noise peer-info
|
||||
```
|
||||
|
||||
Then, in your favorite text editor create a file with the `.js` extension. I've called mine `1.js`.
|
||||
Then, on your favorite text editor create a file with the `.js` extension. I've called mine `1.js`.
|
||||
|
||||
First thing is to create our own libp2p node! Insert:
|
||||
|
||||
@@ -30,7 +30,7 @@ const { NOISE } = require('libp2p-noise')
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
// To signal the addresses we want to be available, we use
|
||||
// To signall the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
@@ -77,41 +77,20 @@ That `QmW2cKTakTYqbQkUzBTEGXgWYFj1YEPeUndE1YWs6CBzDQ` is the PeerId that was cre
|
||||
|
||||
Now that we have our `createNode` function, let's create two nodes and make them dial to each other! You can find the complete solution at [2.js](./2.js).
|
||||
|
||||
For this step, we will need some more dependencies.
|
||||
For this step, we will need one more dependency.
|
||||
|
||||
```bash
|
||||
> npm install it-pipe it-concat libp2p-mplex
|
||||
> npm install it-pipe it-buffer
|
||||
```
|
||||
|
||||
And we also need to import the modules on our .js file:
|
||||
And we also need to import the module on our .js file:
|
||||
|
||||
```js
|
||||
const pipe = require('it-pipe')
|
||||
const concat = require('it-concat')
|
||||
const MPLEX = require('libp2p-mplex')
|
||||
const { toBuffer } = require('it-buffer')
|
||||
```
|
||||
|
||||
We are going to reuse the `createNode` function from step 1, but this time add a stream multiplexer from `libp2p-mplex`.
|
||||
```js
|
||||
const createNode = async () => {
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
// To signal the addresses we want to be available, we use
|
||||
// the multiaddr format, a self describable address
|
||||
listen: ['/ip4/0.0.0.0/tcp/0']
|
||||
},
|
||||
modules: {
|
||||
transport: [TCP],
|
||||
connEncryption: [NOISE],
|
||||
streamMuxer: [MPLEX] // <--- Add this line
|
||||
}
|
||||
})
|
||||
|
||||
await node.start()
|
||||
return node
|
||||
}
|
||||
```
|
||||
We will also make things simpler by creating another function to print the multiaddresses to avoid duplicating code.
|
||||
We are going to reuse the `createNode` function from step 1, but this time to make things simpler, we will create another function to print the addrs to avoid duplicating code.
|
||||
|
||||
```JavaScript
|
||||
function printAddrs (node, number) {
|
||||
@@ -120,7 +99,7 @@ function printAddrs (node, number) {
|
||||
}
|
||||
```
|
||||
|
||||
Then add,
|
||||
Then,
|
||||
|
||||
```js
|
||||
;(async () => {
|
||||
@@ -132,15 +111,18 @@ Then add,
|
||||
printAddrs(node1, '1')
|
||||
printAddrs(node2, '2')
|
||||
|
||||
node2.handle('/print', async ({ stream }) => {
|
||||
const result = await pipe(
|
||||
node2.handle('/print', ({ stream }) => {
|
||||
pipe(
|
||||
stream,
|
||||
concat
|
||||
async function (source) {
|
||||
for await (const msg of source) {
|
||||
console.log(msg.toString())
|
||||
}
|
||||
}
|
||||
)
|
||||
console.log(result.toString())
|
||||
})
|
||||
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
|
||||
const { stream } = await node1.dialProtocol(node2.peerId, '/print')
|
||||
|
||||
await pipe(
|
||||
@@ -149,9 +131,8 @@ Then add,
|
||||
)
|
||||
})();
|
||||
```
|
||||
For more information refer to the [docs](https://github.com/libp2p/js-libp2p/blob/master/doc/API.md).
|
||||
|
||||
The result should look like:
|
||||
The result should be look like:
|
||||
|
||||
```bash
|
||||
> node 2.js
|
||||
@@ -166,29 +147,29 @@ Hello p2p world!
|
||||
|
||||
## 3. Using multiple transports
|
||||
|
||||
Next, we want nodes to have multiple transports available to increase their chances of having a common transport in the network to communicate over. A simple scenario is a node running in the browser only having access to HTTP, WebSockets and WebRTC since the browser doesn't let you open any other kind of transport. For this node to dial to some other node, that other node needs to share a common transport.
|
||||
Next, we want to be available in multiple transports to increase our chances of having common transports in the network. A simple scenario, a node running in the browser only has access to HTTP, WebSockets and WebRTC since the browser doesn't let you open any other kind of transport, for this node to dial to some other node, that other node needs to share a common transport.
|
||||
|
||||
What we are going to do in this step is to create 3 nodes: one with TCP, another with TCP+WebSockets and another one with just WebSockets. The full solution can be found on [3.js](./3.js).
|
||||
What we are going to do in this step is to create 3 nodes, one with TCP, another with TCP+WebSockets and another one with just WebSockets. The full solution can be found on [3.js](./3.js).
|
||||
|
||||
In this example, we will need to also install `libp2p-websockets`:
|
||||
In this example, we will need to also install `libp2p-websockets`, go ahead and install:
|
||||
|
||||
```bash
|
||||
> npm install libp2p-websockets
|
||||
```
|
||||
|
||||
We want to create 3 nodes: one with TCP, one with TCP+WebSockets and one with just WebSockets. We need to update our `createNode` function to accept WebSocket connections as well. Moreover, let's upgrade our function to enable us to pick the addresses over which a node will start a listener:
|
||||
We want to create 3 nodes, one with TCP, one with TCP+WebSockets and one with just WebSockets. We need to update our `createNode` function to contemplate WebSockets as well. Moreover, let's upgrade our function to enable us to pick the addrs in which a node will start a listener:
|
||||
|
||||
```JavaScript
|
||||
// ...
|
||||
|
||||
const createNode = async (transports, addresses = []) => {
|
||||
if (!Array.isArray(addresses)) {
|
||||
addresses = [addresses]
|
||||
const createNode = async (transports, multiaddrs = []) => {
|
||||
if (!Array.isArray(multiaddrs)) {
|
||||
multiaddrs = [multiaddrs]
|
||||
}
|
||||
|
||||
const node = await Libp2p.create({
|
||||
addresses: {
|
||||
listen: addresses
|
||||
listen: multiaddrs.map((a) => multiaddr(a))
|
||||
},
|
||||
modules: {
|
||||
transport: transports,
|
||||
@@ -250,7 +231,7 @@ try {
|
||||
}
|
||||
```
|
||||
|
||||
`print` is a function that prints each piece of data from a stream onto a new line but factored into its own function to save lines:
|
||||
`print` is a function created using the code from 2.js, but factored into its own function to save lines, here it is:
|
||||
|
||||
```JavaScript
|
||||
function print ({ stream }) {
|
||||
@@ -265,7 +246,7 @@ function print ({ stream }) {
|
||||
}
|
||||
```
|
||||
|
||||
If everything was set correctly, you now should see something similar to the following after running the script:
|
||||
If everything was set correctly, you now should see the following after you run the script:
|
||||
|
||||
```Bash
|
||||
> node 3.js
|
||||
@@ -284,13 +265,13 @@ node 3 failed to dial to node 1 with:
|
||||
Error: No transport available for address /ip4/127.0.0.1/tcp/51482
|
||||
```
|
||||
|
||||
As expected, we created 3 nodes: node 1 with TCP, node 2 with TCP+WebSockets and node 3 with just WebSockets. node 1 -> node 2 and node 2 -> node 3 managed to dial correctly because they shared a common transport; however, node 3 -> node 1 failed because they didn't share any.
|
||||
As expected, we created 3 nodes, node 1 with TCP, node 2 with TCP+WebSockets and node 3 with just WebSockets. node 1 -> node 2 and node 2 -> node 3 managed to dial correctly because they shared a common transport, however, node 3 -> node 1 failed because they didn't share any.
|
||||
|
||||
## 4. How to create a new libp2p transport
|
||||
|
||||
Today there are already several transports available and plenty to come. You can find these at [interface-transport implementations](https://github.com/libp2p/js-interfaces/tree/master/src/transport#modules-that-implement-the-interface) list.
|
||||
Today there are already several transports available and plenty to come, you can find these at [interface-transport implementations](https://github.com/libp2p/js-interfaces/tree/master/src/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 as far as libp2p is concerned, if it follows the interface defined in the [spec](https://github.com/libp2p/js-interfaces/tree/master/src/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/js-interfaces/tree/master/src/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.
|
||||
|
||||
|
33
package.json
33
package.json
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "libp2p",
|
||||
"version": "0.29.1",
|
||||
"version": "0.29.0",
|
||||
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
||||
"leadMaintainer": "Jacob Heun <jacobheun@gmail.com>",
|
||||
"main": "src/index.js",
|
||||
@@ -73,6 +73,7 @@
|
||||
"p-fifo": "^1.0.0",
|
||||
"p-settle": "^4.0.1",
|
||||
"peer-id": "^0.14.2",
|
||||
"proper-event-emitter": "^1.0.0",
|
||||
"protons": "^2.0.0",
|
||||
"retimer": "^2.0.0",
|
||||
"sanitize-filename": "^1.6.3",
|
||||
@@ -105,7 +106,7 @@
|
||||
"libp2p-gossipsub": "^0.6.0",
|
||||
"libp2p-kad-dht": "^0.20.0",
|
||||
"libp2p-mdns": "^0.15.0",
|
||||
"libp2p-mplex": "^0.10.1",
|
||||
"libp2p-mplex": "^0.10.0",
|
||||
"libp2p-noise": "^2.0.0",
|
||||
"libp2p-secio": "^0.13.1",
|
||||
"libp2p-tcp": "^0.15.1",
|
||||
@@ -132,41 +133,39 @@
|
||||
"Friedel Ziegelmayer <dignifiedquire@gmail.com>",
|
||||
"Maciej Krüger <mkg20001@gmail.com>",
|
||||
"Hugo Dias <mail@hugodias.me>",
|
||||
"dirkmc <dirkmdev@gmail.com>",
|
||||
"Volker Mische <volker.mische@gmail.com>",
|
||||
"dirkmc <dirkmdev@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>",
|
||||
"Elven <mon.samuel@qq.com>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
|
||||
"Andrew Nesbitt <andrewnez@gmail.com>",
|
||||
"Elven <mon.samuel@qq.com>",
|
||||
"Giovanni T. Parra <fiatjaf@gmail.com>",
|
||||
"Ryan Bell <ryan@piing.net>",
|
||||
"Thomas Eizinger <thomas@eizinger.io>",
|
||||
"ᴠɪᴄᴛᴏʀ ʙᴊᴇʟᴋʜᴏʟᴍ <victorbjelkholm@gmail.com>",
|
||||
"Didrik Nordström <didrik@betamos.se>",
|
||||
"Henrique Dias <hacdias@gmail.com>",
|
||||
"Fei Liu <liu.feiwood@gmail.com>",
|
||||
"Irakli Gozalishvili <rfobic@gmail.com>",
|
||||
"Ethan Lam <elmemphis2000@gmail.com>",
|
||||
"Francis Gulotta <wizard@roborooter.com>",
|
||||
"Florian-Merle <florian.david.merle@gmail.com>",
|
||||
"Joel Gustafson <joelg@mit.edu>",
|
||||
"Julien Bouquillon <contact@revolunet.com>",
|
||||
"Kevin Kwok <antimatter15@gmail.com>",
|
||||
"Felipe Martins <felipebrasil93@gmail.com>",
|
||||
"Nuno Nogueira <nunofmn@gmail.com>",
|
||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||
"Fei Liu <liu.feiwood@gmail.com>",
|
||||
"RasmusErik Voel Jensen <github@solsort.com>",
|
||||
"Diogo Silva <fsdiogo@gmail.com>",
|
||||
"robertkiel <robert.kiel@validitylabs.org>",
|
||||
"Dmitriy Ryajov <dryajov@gmail.com>",
|
||||
"Soeren <nikorpoulsen@gmail.com>",
|
||||
"Sönke Hahn <soenkehahn@gmail.com>",
|
||||
"Tiago Alves <alvesjtiago@gmail.com>",
|
||||
"Daijiro Wachi <daijiro.wachi@gmail.com>",
|
||||
"Diogo Silva <fsdiogo@gmail.com>",
|
||||
"Yusef Napora <yusef@napora.org>",
|
||||
"Zane Starr <zcstarr@gmail.com>",
|
||||
"Cindy Wu <ciindy.wu@gmail.com>",
|
||||
"Daijiro Wachi <daijiro.wachi@gmail.com>",
|
||||
"Chris Bratlien <chrisbratlien@gmail.com>",
|
||||
"ebinks <elizabethjbinks@gmail.com>",
|
||||
"Bernd Strehl <bernd.strehl@gmail.com>",
|
||||
"isan_rivkin <isanrivkin@gmail.com>",
|
||||
"Florian-Merle <florian.david.merle@gmail.com>",
|
||||
"Francis Gulotta <wizard@roborooter.com>",
|
||||
"Felipe Martins <felipebrasil93@gmail.com>"
|
||||
"Henrique Dias <hacdias@gmail.com>",
|
||||
"robertkiel <robert.kiel@validitylabs.org>",
|
||||
"Irakli Gozalishvili <rfobic@gmail.com>"
|
||||
]
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const EventEmitter = require('events')
|
||||
const EventEmitter = require('proper-event-emitter')
|
||||
const multiaddr = require('multiaddr')
|
||||
|
||||
const debug = require('debug')
|
||||
|
@@ -9,7 +9,7 @@ const mergeOptions = require('merge-options')
|
||||
const LatencyMonitor = require('./latency-monitor')
|
||||
const retimer = require('retimer')
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
const EventEmitter = require('proper-event-emitter')
|
||||
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
@@ -199,6 +199,7 @@ class ConnectionManager extends EventEmitter {
|
||||
const storedConn = this.connections.get(peerIdStr)
|
||||
|
||||
this.emit('peer:connect', connection)
|
||||
|
||||
if (storedConn) {
|
||||
storedConn.push(connection)
|
||||
} else {
|
||||
|
@@ -1,6 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
const EventEmitter = require('proper-event-emitter')
|
||||
const debug = require('debug')
|
||||
const globalThis = require('ipfs-utils/src/globalthis')
|
||||
const log = debug('libp2p')
|
||||
|
@@ -5,7 +5,7 @@ const debug = require('debug')
|
||||
const log = debug('libp2p:peer-store')
|
||||
log.error = debug('libp2p:peer-store:error')
|
||||
|
||||
const { EventEmitter } = require('events')
|
||||
const EventEmitter = require('proper-event-emitter')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const AddressBook = require('./address-book')
|
||||
|
@@ -5,7 +5,6 @@ const log = debug('libp2p:upgrader')
|
||||
log.error = debug('libp2p:upgrader:error')
|
||||
const Multistream = require('multistream-select')
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const ConnectionStatus = require('libp2p-interfaces/src/connection/status')
|
||||
const PeerId = require('peer-id')
|
||||
const pipe = require('it-pipe')
|
||||
const errCode = require('err-code')
|
||||
@@ -269,18 +268,8 @@ class Upgrader {
|
||||
maConn.timeline = new Proxy(_timeline, {
|
||||
set: (...args) => {
|
||||
if (connection && args[1] === 'close' && args[2] && !_timeline.close) {
|
||||
// Wait for close to finish before notifying of the closure
|
||||
(async () => {
|
||||
try {
|
||||
if (connection.stat.status === ConnectionStatus.OPEN) {
|
||||
await connection.close()
|
||||
}
|
||||
} catch (err) {
|
||||
log.error(err)
|
||||
} finally {
|
||||
this.onConnectionEnd(connection)
|
||||
}
|
||||
})()
|
||||
connection.stat.status = 'closed'
|
||||
this.onConnectionEnd(connection)
|
||||
}
|
||||
|
||||
return Reflect.set(...args)
|
||||
@@ -306,7 +295,7 @@ class Upgrader {
|
||||
},
|
||||
newStream: newStream || errConnectionNotMultiplexed,
|
||||
getStreams: () => muxer ? muxer.streams : errConnectionNotMultiplexed,
|
||||
close: (err) => maConn.close(err)
|
||||
close: err => maConn.close(err)
|
||||
})
|
||||
|
||||
this.onConnection(connection)
|
||||
|
98
test/core/event-handling-error.spec.js
Normal file
98
test/core/event-handling-error.spec.js
Normal file
@@ -0,0 +1,98 @@
|
||||
'use strict'
|
||||
/* eslint-env mocha */
|
||||
|
||||
const chai = require('chai')
|
||||
chai.use(require('dirty-chai'))
|
||||
const { expect } = chai
|
||||
|
||||
const errCode = require('err-code')
|
||||
const { isNode, isWebWorker } = require('ipfs-utils/src/env')
|
||||
|
||||
const multiaddr = require('multiaddr')
|
||||
const PeerId = require('peer-id')
|
||||
|
||||
const peerUtils = require('../utils/creators/peer')
|
||||
const mockConnection = require('../utils/mockConnection')
|
||||
const baseOptions = require('../utils/base-options.browser')
|
||||
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
|
||||
|
||||
const relayAddr = MULTIADDRS_WEBSOCKETS[0]
|
||||
const code = 'HANDLER_ERROR'
|
||||
const errorMsg = 'handle error'
|
||||
|
||||
describe('event handlers error', () => {
|
||||
// TODO: Need a way of catching the error in the process and absorb it
|
||||
if (isWebWorker) {
|
||||
return
|
||||
}
|
||||
|
||||
let libp2p
|
||||
let peerId
|
||||
|
||||
before(async () => {
|
||||
peerId = await PeerId.create()
|
||||
})
|
||||
|
||||
beforeEach(async () => {
|
||||
[libp2p] = await peerUtils.createPeer({
|
||||
config: {
|
||||
modules: baseOptions.modules,
|
||||
addresses: {
|
||||
listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
|
||||
}
|
||||
},
|
||||
started: true
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(async () => {
|
||||
libp2p && await libp2p.stop()
|
||||
})
|
||||
|
||||
it('should throw on "peer:connect" event handler error', async () => {
|
||||
const p = catchGlobalError()
|
||||
|
||||
libp2p.connectionManager.on('peer:connect', () => {
|
||||
throw errCode(new Error(errorMsg), code)
|
||||
})
|
||||
|
||||
const connection = await mockConnection()
|
||||
libp2p.connectionManager.onConnect(connection)
|
||||
|
||||
await p
|
||||
})
|
||||
|
||||
it('should throw on "peer:discovery" event handler error', async () => {
|
||||
const p = catchGlobalError()
|
||||
|
||||
libp2p.on('peer:discovery', () => {
|
||||
throw errCode(new Error(errorMsg), code)
|
||||
})
|
||||
|
||||
const ma = multiaddr('/ip4/127.0.0.1/tcp/0')
|
||||
libp2p.peerStore.addressBook.add(peerId, [ma])
|
||||
|
||||
await p
|
||||
})
|
||||
})
|
||||
|
||||
const catchGlobalError = () => {
|
||||
return new Promise((resolve) => {
|
||||
if (isNode) {
|
||||
const originalException = process.listeners('uncaughtException').pop()
|
||||
|
||||
originalException && process.removeListener('uncaughtException', originalException)
|
||||
process.once('uncaughtException', (err) => {
|
||||
expect(err).to.exist()
|
||||
expect(err.code).to.eql(code)
|
||||
|
||||
originalException && process.listeners('uncaughtException').push(originalException)
|
||||
resolve()
|
||||
})
|
||||
} else {
|
||||
window.onerror = () => {
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
@@ -11,9 +11,7 @@ const PeerId = require('peer-id')
|
||||
const delay = require('delay')
|
||||
const pDefer = require('p-defer')
|
||||
const pSettle = require('p-settle')
|
||||
const pWaitFor = require('p-wait-for')
|
||||
const pipe = require('it-pipe')
|
||||
const pushable = require('it-pushable')
|
||||
const AggregateError = require('aggregate-error')
|
||||
const { Connection } = require('libp2p-interfaces/src/connection')
|
||||
const { AbortError } = require('libp2p-interfaces/src/transport/errors')
|
||||
@@ -301,50 +299,6 @@ describe('Dialing (direct, TCP)', () => {
|
||||
expect(libp2p.dialer.connectToPeer.callCount).to.equal(1)
|
||||
})
|
||||
|
||||
it('should close all streams when the connection closes', async () => {
|
||||
libp2p = new Libp2p({
|
||||
peerId,
|
||||
modules: {
|
||||
transport: [Transport],
|
||||
streamMuxer: [Muxer],
|
||||
connEncryption: [Crypto]
|
||||
}
|
||||
})
|
||||
|
||||
// register some stream handlers to simulate several protocols
|
||||
libp2p.handle('/stream-count/1', ({ stream }) => pipe(stream, stream))
|
||||
libp2p.handle('/stream-count/2', ({ stream }) => pipe(stream, stream))
|
||||
remoteLibp2p.handle('/stream-count/3', ({ stream }) => pipe(stream, stream))
|
||||
remoteLibp2p.handle('/stream-count/4', ({ stream }) => pipe(stream, stream))
|
||||
|
||||
libp2p.peerStore.addressBook.set(remotePeerId, remoteLibp2p.multiaddrs)
|
||||
const connection = await libp2p.dial(remotePeerId)
|
||||
|
||||
// Create local to remote streams
|
||||
const { stream } = await connection.newStream('/echo/1.0.0')
|
||||
await connection.newStream('/stream-count/3')
|
||||
await libp2p.dialProtocol(remoteLibp2p.peerId, '/stream-count/4')
|
||||
|
||||
// Partially write to the echo stream
|
||||
const source = pushable()
|
||||
stream.sink(source)
|
||||
source.push('hello')
|
||||
|
||||
// Create remote to local streams
|
||||
await remoteLibp2p.dialProtocol(libp2p.peerId, '/stream-count/1')
|
||||
await remoteLibp2p.dialProtocol(libp2p.peerId, '/stream-count/2')
|
||||
|
||||
// Verify stream count
|
||||
const remoteConn = remoteLibp2p.connectionManager.get(libp2p.peerId)
|
||||
expect(connection.streams).to.have.length(5)
|
||||
expect(remoteConn.streams).to.have.length(5)
|
||||
|
||||
// Close the connection and verify all streams have been closed
|
||||
await connection.close()
|
||||
await pWaitFor(() => connection.streams.length === 0)
|
||||
await pWaitFor(() => remoteConn.streams.length === 0)
|
||||
})
|
||||
|
||||
it('should be able to use hangup to close connections', async () => {
|
||||
libp2p = new Libp2p({
|
||||
peerId,
|
||||
|
Reference in New Issue
Block a user