Skip to content
This repository was archived by the owner on Apr 24, 2023. It is now read-only.

Commit 1711a7d

Browse files
authored
fix: transport should not handle connection if upgradeInbound throws (#32)
1 parent 42c5340 commit 1711a7d

File tree

5 files changed

+41
-23
lines changed

5 files changed

+41
-23
lines changed

.aegir.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ function boot () {
2323
return listener.listen(ma)
2424
}
2525

26-
function shutdown () {
27-
return listener.close()
26+
async function shutdown () {
27+
await listener.close()
28+
// TODO: Temporary fix per wrtc issue
29+
// https://github.com/node-webrtc/node-webrtc/issues/636
30+
process.exit(0)
2831
}
2932

3033
module.exports = {

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,19 @@
4646
"aegir": "^20.3.1",
4747
"chai": "^4.2.0",
4848
"dirty-chai": "^2.0.1",
49+
"it-pipe": "^1.1.0",
4950
"multiaddr": "^7.1.0",
51+
"streaming-iterables": "^4.1.1",
5052
"webrtcsupport": "^2.2.0"
5153
},
5254
"dependencies": {
5355
"abortable-iterator": "^2.1.0",
5456
"class-is": "^1.1.0",
5557
"concat-stream": "^2.0.0",
58+
"debug": "^4.1.1",
5659
"detect-node": "^2.0.4",
5760
"err-code": "^2.0.0",
58-
"interface-transport": "libp2p/interface-transport#chore/skip-abort-while-reading-for-webrtc",
61+
"libp2p-interfaces": "libp2p/js-interfaces#chore/skip-abort-while-reading-for-webrtc",
5962
"libp2p-utils": "^0.1.0",
6063
"mafmt": "^7.0.0",
6164
"multibase": "~0.6.0",

src/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ const createListener = require('./listener')
2121

2222
function noop () {}
2323

24-
/**
25-
* @class WebRTCDirect
26-
*/
2724
class WebRTCDirect {
2825
/**
29-
* @constructor
26+
* @class
3027
* @param {object} options
3128
* @param {Upgrader} options.upgrader
3229
*/
@@ -36,11 +33,10 @@ class WebRTCDirect {
3633
}
3734

3835
/**
39-
* @async
4036
* @param {Multiaddr} ma
4137
* @param {object} options
4238
* @param {AbortSignal} options.signal Used to abort dial requests
43-
* @returns {Connection} An upgraded Connection
39+
* @returns {Promise<Connection>} An upgraded Connection
4440
*/
4541
async dial (ma, options = {}) {
4642
const socket = await this._connect(ma, options)
@@ -113,6 +109,7 @@ class WebRTCDirect {
113109
channel.removeListener('error', onError)
114110
channel.removeListener('timeout', onTimeout)
115111
channel.removeListener('connect', onConnect)
112+
channel.removeAllListeners('signal')
116113
options.signal && options.signal.removeEventListener('abort', onAbort)
117114

118115
err ? reject(err) : resolve(channel)

src/listener.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
const http = require('http')
44
const EventEmitter = require('events')
5-
const log = require('debug')('libp2p:webrtcdirect:listener')
5+
const debug = require('debug')
6+
const log = debug('libp2p:webrtcdirect:listener')
7+
log.error = debug('libp2p:webrtcdirect:listener:error')
68

79
const isNode = require('detect-node')
810
const wrtc = require('wrtc')
911
const SimplePeer = require('simple-peer')
10-
1112
const multibase = require('multibase')
1213

1314
const toConnection = require('./socket-to-conn')
@@ -37,26 +38,42 @@ module.exports = ({ handler, upgrader }, options = {}) => {
3738
}
3839

3940
const channel = new SimplePeer(options)
41+
4042
const maConn = toConnection(channel, listener.__connections)
4143
log('new inbound connection %s', maConn.remoteAddr)
4244

43-
const conn = await upgrader.upgradeInbound(maConn)
45+
channel.on('error', (err) => {
46+
log.error(`incoming connectioned errored with ${err}`)
47+
})
48+
channel.once('close', () => {
49+
channel.removeAllListeners('error')
50+
})
51+
channel.on('signal', (signal) => {
52+
const signalStr = JSON.stringify(signal)
53+
const signalEncoded = multibase.encode('base58btc', Buffer.from(signalStr))
54+
res.end(signalEncoded.toString())
55+
})
56+
57+
channel.signal(incSignal)
58+
59+
let conn
60+
try {
61+
conn = await upgrader.upgradeInbound(maConn)
62+
} catch (err) {
63+
log.error('inbound connection failed to upgrade', err)
64+
return maConn.close()
65+
}
4466
log('inbound connection %s upgraded', maConn.remoteAddr)
4567

4668
trackConn(listener, maConn)
4769

4870
channel.on('connect', () => {
4971
listener.emit('connection', conn)
5072
handler(conn)
51-
})
5273

53-
channel.on('signal', (signal) => {
54-
const signalStr = JSON.stringify(signal)
55-
const signalEncoded = multibase.encode('base58btc', Buffer.from(signalStr))
56-
res.end(signalEncoded.toString())
74+
channel.removeAllListeners('connect')
75+
channel.removeAllListeners('signal')
5776
})
58-
59-
channel.signal(incSignal)
6077
})
6178

6279
server.on('error', (err) => listener.emit('error', err))

test/compliance.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
/* eslint-env mocha */
22
'use strict'
33

4-
const wrtc = require('wrtc')
5-
6-
const tests = require('interface-transport')
4+
const tests = require('libp2p-interfaces/src/transport/tests')
75
const multiaddr = require('multiaddr')
86

97
const WDirect = require('../src')
108

119
describe('interface-transport compliance', () => {
1210
tests({
1311
setup ({ upgrader }) {
14-
const ws = new WDirect({ upgrader, wrtc: wrtc })
12+
const ws = new WDirect({ upgrader })
1513

1614
const addrs = [
1715
multiaddr('/ip4/127.0.0.1/tcp/22222/http/p2p-webrtc-direct'),

0 commit comments

Comments
 (0)