Skip to content

Commit 326035a

Browse files
committed
fix: event handlers error
1 parent bb59b51 commit 326035a

File tree

6 files changed

+104
-4
lines changed

6 files changed

+104
-4
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
"p-fifo": "^1.0.0",
7474
"p-settle": "^4.0.1",
7575
"peer-id": "^0.14.0",
76+
"proper-event-emitter": "^1.0.0",
7677
"protons": "^2.0.0",
7778
"retimer": "^2.0.0",
7879
"sanitize-filename": "^1.6.3",

src/circuit/listener.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const EventEmitter = require('events')
3+
const EventEmitter = require('proper-event-emitter')
44
const multiaddr = require('multiaddr')
55

66
const debug = require('debug')

src/connection-manager/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const mergeOptions = require('merge-options')
99
const LatencyMonitor = require('./latency-monitor')
1010
const retimer = require('retimer')
1111

12-
const { EventEmitter } = require('events')
12+
const EventEmitter = require('proper-event-emitter')
1313

1414
const PeerId = require('peer-id')
1515

@@ -191,6 +191,7 @@ class ConnectionManager extends EventEmitter {
191191
const storedConn = this.connections.get(peerIdStr)
192192

193193
this.emit('peer:connect', connection)
194+
194195
if (storedConn) {
195196
storedConn.push(connection)
196197
} else {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22

3-
const { EventEmitter } = require('events')
3+
const EventEmitter = require('proper-event-emitter')
44
const debug = require('debug')
55
const globalThis = require('ipfs-utils/src/globalthis')
66
const log = debug('libp2p')

src/peer-store/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const debug = require('debug')
55
const log = debug('libp2p:peer-store')
66
log.error = debug('libp2p:peer-store:error')
77

8-
const { EventEmitter } = require('events')
8+
const EventEmitter = require('proper-event-emitter')
99
const PeerId = require('peer-id')
1010

1111
const AddressBook = require('./address-book')
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
'use strict'
2+
/* eslint-env mocha */
3+
4+
const chai = require('chai')
5+
chai.use(require('dirty-chai'))
6+
const { expect } = chai
7+
8+
const errCode = require('err-code')
9+
const { isNode, isWebWorker } = require('ipfs-utils/src/env')
10+
11+
const multiaddr = require('multiaddr')
12+
const PeerId = require('peer-id')
13+
14+
const peerUtils = require('../utils/creators/peer')
15+
const mockConnection = require('../utils/mockConnection')
16+
const baseOptions = require('../utils/base-options.browser')
17+
const { MULTIADDRS_WEBSOCKETS } = require('../fixtures/browser')
18+
19+
const relayAddr = MULTIADDRS_WEBSOCKETS[0]
20+
const code = 'HANDLER_ERROR'
21+
const errorMsg = 'handle error'
22+
23+
describe('event handlers error', () => {
24+
// TODO: Need a way of catching the error in the process and absorb it
25+
if (isWebWorker) {
26+
return
27+
}
28+
29+
let libp2p
30+
let peerId
31+
32+
before(async () => {
33+
peerId = await PeerId.create()
34+
})
35+
36+
beforeEach(async () => {
37+
[libp2p] = await peerUtils.createPeer({
38+
config: {
39+
modules: baseOptions.modules,
40+
addresses: {
41+
listen: [multiaddr(`${relayAddr}/p2p-circuit`)]
42+
}
43+
},
44+
started: true
45+
})
46+
})
47+
48+
afterEach(async () => {
49+
libp2p && await libp2p.stop()
50+
})
51+
52+
it('should throw on "peer:connect" event handler error', async () => {
53+
const p = catchGlobalError()
54+
55+
libp2p.connectionManager.on('peer:connect', () => {
56+
throw errCode(new Error(errorMsg), code)
57+
})
58+
59+
const connection = await mockConnection()
60+
libp2p.connectionManager.onConnect(connection)
61+
62+
await p
63+
})
64+
65+
it('should throw on "peer:discovery" event handler error', async () => {
66+
const p = catchGlobalError()
67+
68+
libp2p.on('peer:discovery', () => {
69+
throw errCode(new Error(errorMsg), code)
70+
})
71+
72+
const ma = multiaddr('/ip4/127.0.0.1/tcp/0')
73+
libp2p.peerStore.addressBook.add(peerId, [ma])
74+
75+
await p
76+
})
77+
})
78+
79+
const catchGlobalError = () => {
80+
return new Promise((resolve) => {
81+
if (isNode) {
82+
const originalException = process.listeners('uncaughtException').pop()
83+
84+
originalException && process.removeListener('uncaughtException', originalException)
85+
process.once('uncaughtException', (err) => {
86+
expect(err).to.exist()
87+
expect(err.code).to.eql(code)
88+
89+
originalException && process.listeners('uncaughtException').push(originalException)
90+
resolve()
91+
})
92+
} else {
93+
window.onerror = () => {
94+
resolve()
95+
}
96+
}
97+
})
98+
}

0 commit comments

Comments
 (0)