Skip to content

Commit

Permalink
fox: remove error-inject and fix error handling (#1409)
Browse files Browse the repository at this point in the history
  • Loading branch information
tinovyatkin committed May 16, 2020
1 parent f7c732f commit faeaff5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
12 changes: 6 additions & 6 deletions lib/response.js
Expand Up @@ -6,7 +6,6 @@
*/

const contentDisposition = require('content-disposition');
const ensureErrorHandler = require('error-inject');
const getType = require('cache-content-type');
const onFinish = require('on-finished');
const escape = require('escape-html');
Expand Down Expand Up @@ -167,12 +166,13 @@ module.exports = {
}

// stream
if ('function' === typeof val.pipe) {
if (val instanceof Stream) {
onFinish(this.res, destroy.bind(null, val));
ensureErrorHandler(val, err => this.ctx.onerror(err));

// overwriting
if (null != original && original !== val) this.remove('Content-Length');
if (original != val) {
val.once('error', err => this.ctx.onerror(err));
// overwriting
if (null != original) this.remove('Content-Length');
}

if (setType) this.type = 'bin';
return;
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -32,7 +32,6 @@
"depd": "^1.1.2",
"destroy": "^1.0.4",
"encodeurl": "^1.0.2",
"error-inject": "^1.0.0",
"escape-html": "^1.0.3",
"fresh": "~0.5.2",
"http-assert": "^1.3.0",
Expand Down
11 changes: 11 additions & 0 deletions test/response/body.js
Expand Up @@ -4,6 +4,7 @@
const response = require('../helpers/context').response;
const assert = require('assert');
const fs = require('fs');
const Stream = require('stream');

describe('res.body=', () => {
describe('when Content-Type is set', () => {
Expand Down Expand Up @@ -108,6 +109,16 @@ describe('res.body=', () => {
res.body = fs.createReadStream('LICENSE');
assert.equal('application/octet-stream', res.header['content-type']);
});

it('should add error handler to the stream, but only once', () => {
const res = response();
const body = new Stream.PassThrough();
assert.strictEqual(body.listenerCount('error'), 0);
res.body = body;
assert.strictEqual(body.listenerCount('error'), 1);
res.body = body;
assert.strictEqual(body.listenerCount('error'), 1);
});
});

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

0 comments on commit faeaff5

Please sign in to comment.