File tree 3 files changed +17
-7
lines changed
3 files changed +17
-7
lines changed Original file line number Diff line number Diff line change 6
6
*/
7
7
8
8
const contentDisposition = require ( 'content-disposition' ) ;
9
- const ensureErrorHandler = require ( 'error-inject' ) ;
10
9
const getType = require ( 'cache-content-type' ) ;
11
10
const onFinish = require ( 'on-finished' ) ;
12
11
const escape = require ( 'escape-html' ) ;
@@ -167,12 +166,13 @@ module.exports = {
167
166
}
168
167
169
168
// stream
170
- if ( 'function' === typeof val . pipe ) {
169
+ if ( val instanceof Stream ) {
171
170
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
+ }
176
176
177
177
if ( setType ) this . type = 'bin' ;
178
178
return ;
Original file line number Diff line number Diff line change 32
32
"depd" : " ^1.1.2" ,
33
33
"destroy" : " ^1.0.4" ,
34
34
"encodeurl" : " ^1.0.2" ,
35
- "error-inject" : " ^1.0.0" ,
36
35
"escape-html" : " ^1.0.3" ,
37
36
"fresh" : " ~0.5.2" ,
38
37
"http-assert" : " ^1.3.0" ,
Original file line number Diff line number Diff line change 4
4
const response = require ( '../helpers/context' ) . response ;
5
5
const assert = require ( 'assert' ) ;
6
6
const fs = require ( 'fs' ) ;
7
+ const Stream = require ( 'stream' ) ;
7
8
8
9
describe ( 'res.body=' , ( ) => {
9
10
describe ( 'when Content-Type is set' , ( ) => {
@@ -108,6 +109,16 @@ describe('res.body=', () => {
108
109
res . body = fs . createReadStream ( 'LICENSE' ) ;
109
110
assert . equal ( 'application/octet-stream' , res . header [ 'content-type' ] ) ;
110
111
} ) ;
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
+ } ) ;
111
122
} ) ;
112
123
113
124
describe ( 'when a buffer is given' , ( ) => {
You can’t perform that action at this time.
0 commit comments