From 66a48cf672e3e356441af30c85395cd334e87307 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20Wa=CC=88rting?= Date: Mon, 15 May 2023 22:38:00 +0200 Subject: [PATCH] fix: support truncated gzip --- lib/fetch/index.js | 9 ++++++++- test/node-fetch/main.js | 14 ++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/lib/fetch/index.js b/lib/fetch/index.js index 51998732427..90683a8febb 100644 --- a/lib/fetch/index.js +++ b/lib/fetch/index.js @@ -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') { diff --git a/test/node-fetch/main.js b/test/node-fetch/main.js index 8dc20f2ce64..358a969b574 100644 --- a/test/node-fetch/main.js +++ b/test/node-fetch/main.js @@ -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', () => {