Skip to content

Commit 91b9b57

Browse files
committed
fix: support deflate raw responses
1 parent ba70685 commit 91b9b57

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/fetch/index.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const {
5757
} = require('./constants')
5858
const { kHeadersList, kConstruct } = require('../core/symbols')
5959
const EE = require('events')
60-
const { Readable, pipeline } = require('stream')
60+
const { Readable, pipeline, Transform } = require('stream')
6161
const { addAbortListener, isErrored, isReadable, nodeMajor, nodeMinor, bufferToLowerCasedHeaderName } = require('../core/util')
6262
const { dataURLProcessor, serializeAMimeType, parseMIMEType } = require('./dataURL')
6363
const { getGlobalDispatcher } = require('../global')
@@ -1096,7 +1096,7 @@ function fetchFinale (fetchParams, response) {
10961096
// 3. Set up transformStream with transformAlgorithm set to identityTransformAlgorithm and flushAlgorithm
10971097
// set to processResponseEndOfBody.
10981098
const transformStream = new TransformStream({
1099-
start () {},
1099+
start () { },
11001100
transform (chunk, controller) {
11011101
controller.enqueue(chunk)
11021102
},
@@ -2186,7 +2186,35 @@ async function httpNetworkFetch (
21862186
finishFlush: zlib.constants.Z_SYNC_FLUSH
21872187
}))
21882188
} 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)
21902218
} else if (coding === 'br') {
21912219
decoders.push(zlib.createBrotliDecompress())
21922220
} else {
@@ -2202,7 +2230,7 @@ async function httpNetworkFetch (
22022230
headersList,
22032231
body: decoders.length
22042232
? pipeline(this.body, ...decoders, () => { })
2205-
: this.body.on('error', () => {})
2233+
: this.body.on('error', () => { })
22062234
})
22072235

22082236
return true

test/node-fetch/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ describe('node-fetch', () => {
659659
})
660660
})
661661

662-
xit('should decompress deflate raw response from old apache server', () => {
662+
it('should decompress deflate raw response from old apache server', () => {
663663
const url = `${base}deflate-raw`
664664
return fetch(url).then(res => {
665665
expect(res.headers.get('content-type')).to.equal('text/plain')

0 commit comments

Comments
 (0)