@@ -235,7 +235,11 @@ export default async function fetch(url, options_) {
235235 } ) ;
236236 }
237237
238- let body = pump ( response_ , new PassThrough ( ) , reject ) ;
238+ let body = pump ( response_ , new PassThrough ( ) , error => {
239+ if ( error ) {
240+ reject ( error ) ;
241+ }
242+ } ) ;
239243 // see https://github.com/nodejs/node/pull/29376
240244 /* c8 ignore next 3 */
241245 if ( process . version < 'v12.10' ) {
@@ -281,7 +285,11 @@ export default async function fetch(url, options_) {
281285
282286 // For gzip
283287 if ( codings === 'gzip' || codings === 'x-gzip' ) {
284- body = pump ( body , zlib . createGunzip ( zlibOptions ) , reject ) ;
288+ body = pump ( body , zlib . createGunzip ( zlibOptions ) , error => {
289+ if ( error ) {
290+ reject ( error ) ;
291+ }
292+ } ) ;
285293 response = new Response ( body , responseOptions ) ;
286294 resolve ( response ) ;
287295 return ;
@@ -291,20 +299,48 @@ export default async function fetch(url, options_) {
291299 if ( codings === 'deflate' || codings === 'x-deflate' ) {
292300 // Handle the infamous raw deflate response from old servers
293301 // a hack for old IIS and Apache servers
294- const raw = pump ( response_ , new PassThrough ( ) , reject ) ;
302+ const raw = pump ( response_ , new PassThrough ( ) , error => {
303+ if ( error ) {
304+ reject ( error ) ;
305+ }
306+ } ) ;
295307 raw . once ( 'data' , chunk => {
296308 // See http://stackoverflow.com/questions/37519828
297- body = ( chunk [ 0 ] & 0x0F ) === 0x08 ? pump ( body , zlib . createInflate ( ) , reject ) : pump ( body , zlib . createInflateRaw ( ) , reject ) ;
309+ if ( ( chunk [ 0 ] & 0x0F ) === 0x08 ) {
310+ body = pump ( body , zlib . createInflate ( ) , error => {
311+ if ( error ) {
312+ reject ( error ) ;
313+ }
314+ } ) ;
315+ } else {
316+ body = pump ( body , zlib . createInflateRaw ( ) , error => {
317+ if ( error ) {
318+ reject ( error ) ;
319+ }
320+ } ) ;
321+ }
298322
299323 response = new Response ( body , responseOptions ) ;
300324 resolve ( response ) ;
301325 } ) ;
326+ raw . once ( 'end' , ( ) => {
327+ // Some old IIS servers return zero-length OK deflate responses, so
328+ // 'data' is never emitted. See https://github.com/node-fetch/node-fetch/pull/903
329+ if ( ! response ) {
330+ response = new Response ( body , responseOptions ) ;
331+ resolve ( response ) ;
332+ }
333+ } ) ;
302334 return ;
303335 }
304336
305337 // For br
306338 if ( codings === 'br' ) {
307- body = pump ( body , zlib . createBrotliDecompress ( ) , reject ) ;
339+ body = pump ( body , zlib . createBrotliDecompress ( ) , error => {
340+ if ( error ) {
341+ reject ( error ) ;
342+ }
343+ } ) ;
308344 response = new Response ( body , responseOptions ) ;
309345 resolve ( response ) ;
310346 return ;
0 commit comments