Skip to content

Commit

Permalink
fix: support truncated gzip (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting committed May 15, 2023
1 parent f5f7c18 commit 51fa0fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 8 additions & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,14 @@ async function httpNetworkFetch (
for (const coding of codings) {
// https://www.rfc-editor.org/rfc/rfc9112.html#section-7.2
if (coding === 'x-gzip' || coding === 'gzip') {
decoders.push(zlib.createGunzip())
decoders.push(zlib.createGunzip({
// Be less strict when decoding compressed responses, since sometimes
// servers send slightly invalid responses that are still accepted
// by common browsers.
// Always using Z_SYNC_FLUSH is what cURL does.
flush: zlib.constants.Z_SYNC_FLUSH,
finishFlush: zlib.constants.Z_SYNC_FLUSH
}))
} else if (coding === 'deflate') {
decoders.push(zlib.createInflate())
} else if (coding === 'br') {
Expand Down
14 changes: 6 additions & 8 deletions test/node-fetch/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -640,15 +640,13 @@ describe('node-fetch', () => {
})
})

xit('should decompress slightly invalid gzip response', () => {
it('should decompress slightly invalid gzip response', async () => {
const url = `${base}gzip-truncated`
return fetch(url).then(res => {
expect(res.headers.get('content-type')).to.equal('text/plain')
return res.text().then(result => {
expect(result).to.be.a('string')
expect(result).to.equal('hello world')
})
})
const res = await fetch(url)
expect(res.headers.get('content-type')).to.equal('text/plain')
const result = await res.text()
expect(result).to.be.a('string')
expect(result).to.equal('hello world')
})

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

0 comments on commit 51fa0fe

Please sign in to comment.