@@ -23,7 +23,6 @@ const isFinished = require('on-finished').isFinished
23
23
var onHeaders = require ( 'on-headers' )
24
24
var vary = require ( 'vary' )
25
25
var zlib = require ( 'zlib' )
26
- var ServerResponse = require ( 'http' ) . ServerResponse
27
26
28
27
/**
29
28
* Module exports.
@@ -90,20 +89,6 @@ function compression (options) {
90
89
// proxy
91
90
92
91
res . write = function write ( chunk , encoding , callback ) {
93
- if ( res . destroyed || res . finished || ended ) {
94
- // HACK: node doesn't expose internal errors,
95
- // we need to fake response to throw underlying errors type
96
- var fakeRes = new ServerResponse ( { } )
97
- fakeRes . on ( 'error' , function ( err ) {
98
- res . emit ( 'error' , err )
99
- } )
100
- fakeRes . destroyed = res . destroyed
101
- fakeRes . finished = res . finished || ended
102
- // throw ERR_STREAM_DESTROYED or ERR_STREAM_WRITE_AFTER_END
103
- _write . call ( fakeRes , chunk , encoding , callback )
104
- return false
105
- }
106
-
107
92
if ( ! res . headersSent ) {
108
93
this . writeHead ( this . statusCode )
109
94
}
@@ -113,8 +98,8 @@ function compression (options) {
113
98
}
114
99
115
100
return stream
116
- ? stream . write ( chunk , encoding , callback )
117
- : _write . call ( this , chunk , encoding , callback )
101
+ ? stream . write . apply ( stream , arguments )
102
+ : _write . apply ( this , arguments )
118
103
}
119
104
120
105
res . end = function end ( chunk , encoding , callback ) {
@@ -128,12 +113,6 @@ function compression (options) {
128
113
}
129
114
}
130
115
131
- if ( this . destroyed || this . finished || ended ) {
132
- this . finished = ended
133
- // throw ERR_STREAM_WRITE_AFTER_END or ERR_STREAM_ALREADY_FINISHED
134
- return _end . call ( this , chunk , encoding , callback )
135
- }
136
-
137
116
if ( ! res . headersSent ) {
138
117
// estimate the length
139
118
if ( ! this . getHeader ( 'Content-Length' ) ) {
@@ -144,7 +123,7 @@ function compression (options) {
144
123
}
145
124
146
125
if ( ! stream ) {
147
- return _end . call ( this , chunk , encoding , callback )
126
+ return _end . apply ( this , arguments )
148
127
}
149
128
150
129
// mark ended
@@ -247,11 +226,12 @@ function compression (options) {
247
226
res . setHeader ( 'Content-Encoding' , method )
248
227
res . removeHeader ( 'Content-Length' )
249
228
250
- // compression
229
+ // emit error on response
251
230
stream . on ( 'error' , function ( err ) {
252
231
res . emit ( 'error' , err )
253
232
} )
254
-
233
+
234
+ // compression
255
235
stream . on ( 'data' , function onStreamData ( chunk ) {
256
236
if ( isFinished ( res ) ) {
257
237
debug ( 'response finished' )
0 commit comments