Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support truncated gzip #2126

Merged
merged 1 commit into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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