@@ -57,7 +57,7 @@ const {
57
57
} = require ( './constants' )
58
58
const { kHeadersList, kConstruct } = require ( '../core/symbols' )
59
59
const EE = require ( 'events' )
60
- const { Readable, pipeline } = require ( 'stream' )
60
+ const { Readable, pipeline, Transform } = require ( 'stream' )
61
61
const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor, bufferToLowerCasedHeaderName } = require ( '../core/util' )
62
62
const { dataURLProcessor, serializeAMimeType, parseMIMEType } = require ( './dataURL' )
63
63
const { getGlobalDispatcher } = require ( '../global' )
@@ -1096,7 +1096,7 @@ function fetchFinale (fetchParams, response) {
1096
1096
// 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm
1097
1097
// set to processResponseEndOfBody.
1098
1098
const transformStream = new TransformStream ( {
1099
- start ( ) { } ,
1099
+ start ( ) { } ,
1100
1100
transform ( chunk , controller ) {
1101
1101
controller . enqueue ( chunk )
1102
1102
} ,
@@ -2186,7 +2186,35 @@ async function httpNetworkFetch (
2186
2186
finishFlush : zlib . constants . Z_SYNC_FLUSH
2187
2187
} ) )
2188
2188
} else if ( coding === 'deflate' ) {
2189
- decoders . push ( zlib . createInflate ( ) )
2189
+ // Instantiate a Stream, which pipes the response to zlib.createInflate()
2190
+ // or zlib.createInflateRaw() depending on the first byte of the Buffer.
2191
+ // If the lower byte of the first byte is 0x08, then the stream is
2192
+ // interpreted as a zlib stream, otherwise it's interpreted as a
2193
+ // raw deflate stream.
2194
+ const deflateDecoder = new Transform ( {
2195
+ transform ( chunk , encoding , callback ) {
2196
+ if ( ! this . _inflateStream ) {
2197
+ this . _inflateStream = ( chunk [ 0 ] & 0x0F ) === 0x08
2198
+ ? zlib . createInflate ( )
2199
+ : zlib . createInflateRaw ( )
2200
+
2201
+ this . _inflateStream . on ( 'data' , this . push . bind ( this ) )
2202
+ this . _inflateStream . on ( 'end' , callback )
2203
+ this . _inflateStream . on ( 'error' , callback )
2204
+ }
2205
+
2206
+ this . _inflateStream . write ( chunk , encoding , callback )
2207
+ } ,
2208
+ final ( callback ) {
2209
+ if ( this . _inflateStream ) {
2210
+ this . _inflateStream . end ( )
2211
+ this . _inflateStream = null
2212
+ }
2213
+ callback ( )
2214
+ }
2215
+ } )
2216
+
2217
+ decoders . push ( deflateDecoder )
2190
2218
} else if ( coding === 'br' ) {
2191
2219
decoders . push ( zlib . createBrotliDecompress ( ) )
2192
2220
} else {
@@ -2202,7 +2230,7 @@ async function httpNetworkFetch (
2202
2230
headersList,
2203
2231
body : decoders . length
2204
2232
? pipeline ( this . body , ...decoders , ( ) => { } )
2205
- : this . body . on ( 'error' , ( ) => { } )
2233
+ : this . body . on ( 'error' , ( ) => { } )
2206
2234
} )
2207
2235
2208
2236
return true
0 commit comments