Skip to content

Commit faeaff5

Browse files
authored
fox: remove error-inject and fix error handling (#1409)
1 parent f7c732f commit faeaff5

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

lib/response.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77

88
const contentDisposition = require('content-disposition');
9-
const ensureErrorHandler = require('error-inject');
109
const getType = require('cache-content-type');
1110
const onFinish = require('on-finished');
1211
const escape = require('escape-html');
@@ -167,12 +166,13 @@ module.exports = {
167166
}
168167

169168
// stream
170-
if ('function' === typeof val.pipe) {
169+
if (val instanceof Stream) {
171170
onFinish(this.res, destroy.bind(null, val));
172-
ensureErrorHandler(val, err => this.ctx.onerror(err));
173-
174-
// overwriting
175-
if (null != original && original !== val) this.remove('Content-Length');
171+
if (original != val) {
172+
val.once('error', err => this.ctx.onerror(err));
173+
// overwriting
174+
if (null != original) this.remove('Content-Length');
175+
}
176176

177177
if (setType) this.type = 'bin';
178178
return;

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
"depd": "^1.1.2",
3333
"destroy": "^1.0.4",
3434
"encodeurl": "^1.0.2",
35-
"error-inject": "^1.0.0",
3635
"escape-html": "^1.0.3",
3736
"fresh": "~0.5.2",
3837
"http-assert": "^1.3.0",

test/response/body.js

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const response = require('../helpers/context').response;
55
const assert = require('assert');
66
const fs = require('fs');
7+
const Stream = require('stream');
78

89
describe('res.body=', () => {
910
describe('when Content-Type is set', () => {
@@ -108,6 +109,16 @@ describe('res.body=', () => {
108109
res.body = fs.createReadStream('LICENSE');
109110
assert.equal('application/octet-stream', res.header['content-type']);
110111
});
112+
113+
it('should add error handler to the stream, but only once', () => {
114+
const res = response();
115+
const body = new Stream.PassThrough();
116+
assert.strictEqual(body.listenerCount('error'), 0);
117+
res.body = body;
118+
assert.strictEqual(body.listenerCount('error'), 1);
119+
res.body = body;
120+
assert.strictEqual(body.listenerCount('error'), 1);
121+
});
111122
});
112123

113124
describe('when a buffer is given', () => {

0 commit comments

Comments
 (0)