Skip to content
This repository was archived by the owner on Jun 3, 2020. It is now read-only.

Commit 364e827

Browse files
committed
Use stream pump()ing for more reliable close handling
1 parent 7a21702 commit 364e827

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

hyperswarm/peersocket.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const EventEmitter = require('events')
22
const crypto = require('crypto')
33
const createSwarm = require('@hyperswarm/network')
44
const lpstream = require('length-prefixed-stream')
5+
const pump = require('pump')
56
const schemas = require('./peersocket-schemas')
67
const {extractOrigin} = require('../../../lib/strings')
78

@@ -158,7 +159,13 @@ function handleConnection (swarm, socket, details) {
158159
}
159160
lobby.connections.add(conn)
160161
lobby.emit('connection', {socketInfo: {id}})
161-
encoder.pipe(socket).pipe(decoder)
162+
163+
// wire up message-framers and handle close
164+
pump(encoder, socket, decoder, err => {
165+
if (err) console.log('PeerSocket connection error', err)
166+
lobby.connections.remove(conn)
167+
conn.events.emit('close')
168+
})
162169

163170
// wire up events
164171
decoder.on('data', message => {
@@ -169,9 +176,5 @@ function handleConnection (swarm, socket, details) {
169176
console.log('Failed to decode received PeerSocket message', e)
170177
}
171178
})
172-
socket.on('close', () => {
173-
lobby.connections.remove(conn)
174-
conn.events.emit('close')
175-
})
176179
}
177180
}

0 commit comments

Comments
 (0)