@@ -23,7 +23,6 @@ const isFinished = require('on-finished').isFinished
2323var onHeaders = require ( 'on-headers' )
2424var vary = require ( 'vary' )
2525var zlib = require ( 'zlib' )
26- var ServerResponse = require ( 'http' ) . ServerResponse
2726
2827/**
2928 * Module exports.
@@ -90,20 +89,6 @@ function compression (options) {
9089 // proxy
9190
9291 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-
10792 if ( ! res . headersSent ) {
10893 this . writeHead ( this . statusCode )
10994 }
@@ -113,8 +98,8 @@ function compression (options) {
11398 }
11499
115100 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 )
118103 }
119104
120105 res . end = function end ( chunk , encoding , callback ) {
@@ -128,12 +113,6 @@ function compression (options) {
128113 }
129114 }
130115
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-
137116 if ( ! res . headersSent ) {
138117 // estimate the length
139118 if ( ! this . getHeader ( 'Content-Length' ) ) {
@@ -144,7 +123,7 @@ function compression (options) {
144123 }
145124
146125 if ( ! stream ) {
147- return _end . call ( this , chunk , encoding , callback )
126+ return _end . apply ( this , arguments )
148127 }
149128
150129 // mark ended
@@ -247,11 +226,12 @@ function compression (options) {
247226 res . setHeader ( 'Content-Encoding' , method )
248227 res . removeHeader ( 'Content-Length' )
249228
250- // compression
229+ // emit error on response
251230 stream . on ( 'error' , function ( err ) {
252231 res . emit ( 'error' , err )
253232 } )
254-
233+
234+ // compression
255235 stream . on ( 'data' , function onStreamData ( chunk ) {
256236 if ( isFinished ( res ) ) {
257237 debug ( 'response finished' )
0 commit comments