Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

fix: conditionally emit errors from muxer #84

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
**/*.log
test/repo-tests*
**/bundle.js
docs

# Logs
logs
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@
"npm": ">=3.0.0"
},
"devDependencies": {
"aegir": "^17.0.1",
"aegir": "^17.1.1",
"chai": "^4.2.0",
"chai-checkmark": "^1.0.1",
"dirty-chai": "^2.0.1",
"interface-stream-muxer": "~0.6.0",
"libp2p-tcp": "~0.13.0",
"libp2p-websockets": "~0.12.0",
"multiaddr": "^5.0.0",
"multiaddr": "^6.0.0",
"pull-file": "^1.1.0",
"pull-pair": "^1.1.0",
"run-parallel": "^1.1.9",
Expand All @@ -55,7 +55,8 @@
"tape": "^4.9.0"
},
"dependencies": {
"interface-connection": "~0.3.2",
"debug": "^4.1.0",
"interface-connection": "~0.3.3",
"pull-catch": "^1.0.0",
"pull-stream": "^3.6.9",
"pull-stream-to-stream": "^1.3.4",
Expand Down
19 changes: 19 additions & 0 deletions src/muxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const toPull = require('stream-to-pull-stream')
const pullCatch = require('pull-catch')
const pull = require('pull-stream')
const noop = () => {}
const debug = require('debug')
const log = debug('spdy')
log.error = debug('spdy:error')

function catchError (stream) {
return {
Expand Down Expand Up @@ -57,6 +60,22 @@ module.exports = class Muxer extends EventEmitter {
})
}

/**
* Conditionally emit errors if we have listeners. All other
* events are sent to EventEmitter.emit
*
* @param {string} eventName
* @param {...any} args
* @returns {void}
*/
emit (eventName, ...args) {
if (eventName === 'error' && !this._events.error) {
log.error('error', ...args)
} else {
super.emit(eventName, ...args)
}
}

// method added to enable pure stream muxer feeling
newStream (callback) {
if (!callback) {
Expand Down
8 changes: 8 additions & 0 deletions test/muxer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,12 @@ describe('multiplex-muxer', () => {
code: 'ERR_UNKNOWN'
}))
})

it('should not throw if there are no error listeners', () => {
muxer.removeAllListeners('error')

muxer.spdy.emit('error', Object.assign(new Error('bad things'), {
code: 'ERR_UNKNOWN'
}))
})
})