Skip to content

Commit 51fa0fe

Browse files
authored
fix: support truncated gzip (nodejs#2126)
1 parent f5f7c18 commit 51fa0fe

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

lib/fetch/index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2007,7 +2007,14 @@ async function httpNetworkFetch (
20072007
for (const coding of codings) {
20082008
// https://www.rfc-editor.org/rfc/rfc9112.html#section-7.2
20092009
if (coding === 'x-gzip' || coding === 'gzip') {
2010-
decoders.push(zlib.createGunzip())
2010+
decoders.push(zlib.createGunzip({
2011+
// Be less strict when decoding compressed responses, since sometimes
2012+
// servers send slightly invalid responses that are still accepted
2013+
// by common browsers.
2014+
// Always using Z_SYNC_FLUSH is what cURL does.
2015+
flush: zlib.constants.Z_SYNC_FLUSH,
2016+
finishFlush: zlib.constants.Z_SYNC_FLUSH
2017+
}))
20112018
} else if (coding === 'deflate') {
20122019
decoders.push(zlib.createInflate())
20132020
} else if (coding === 'br') {

test/node-fetch/main.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,13 @@ describe('node-fetch', () => {
640640
})
641641
})
642642

643-
xit('should decompress slightly invalid gzip response', () => {
643+
it('should decompress slightly invalid gzip response', async () => {
644644
const url = `${base}gzip-truncated`
645-
return fetch(url).then(res => {
646-
expect(res.headers.get('content-type')).to.equal('text/plain')
647-
return res.text().then(result => {
648-
expect(result).to.be.a('string')
649-
expect(result).to.equal('hello world')
650-
})
651-
})
645+
const res = await fetch(url)
646+
expect(res.headers.get('content-type')).to.equal('text/plain')
647+
const result = await res.text()
648+
expect(result).to.be.a('string')
649+
expect(result).to.equal('hello world')
652650
})
653651

654652
it('should decompress deflate response', () => {

0 commit comments

Comments
 (0)