Skip to content

Commit 65e8d1e

Browse files
fix(http): add zlib headers if missing (#5497)
1 parent 9915635 commit 65e8d1e

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎lib/adapters/http.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import stream from 'stream';
1919
import AxiosHeaders from '../core/AxiosHeaders.js';
2020
import AxiosTransformStream from '../helpers/AxiosTransformStream.js';
2121
import EventEmitter from 'events';
22+
import ZlibHeaderTransformStream from '../helpers/ZlibHeaderTransformStream.js';
2223

2324
const zlibOptions = {
2425
flush: zlib.constants.Z_SYNC_FLUSH,
@@ -425,7 +426,15 @@ export default isHttpAdapterSupported && function httpAdapter(config) {
425426
case 'x-gzip':
426427
case 'compress':
427428
case 'x-compress':
429+
// add the unzipper to the body stream processing pipeline
430+
streams.push(zlib.createUnzip(zlibOptions));
431+
432+
// remove the content-encoding in order to not confuse downstream operations
433+
delete res.headers['content-encoding'];
434+
break;
428435
case 'deflate':
436+
streams.push(new ZlibHeaderTransformStream());
437+
429438
// add the unzipper to the body stream processing pipeline
430439
streams.push(zlib.createUnzip(zlibOptions));
431440

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
3+
import stream from "stream";
4+
5+
class ZlibHeaderTransformStream extends stream.Transform {
6+
__transform(chunk, encoding, callback) {
7+
this.push(chunk);
8+
callback();
9+
}
10+
11+
_transform(chunk, encoding, callback) {
12+
if (chunk.length !== 0) {
13+
this._transform = this.__transform;
14+
15+
// Add Default Compression headers if no zlib headers are present
16+
if (chunk[0] !== 120) { // Hex: 78
17+
const header = Buffer.alloc(2);
18+
header[0] = 120; // Hex: 78
19+
header[1] = 156; // Hex: 9C
20+
this.push(header, encoding);
21+
}
22+
}
23+
24+
this.__transform(chunk, encoding, callback);
25+
}
26+
}
27+
28+
export default ZlibHeaderTransformStream;

‎test/unit/adapters/http.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const pipelineAsync = util.promisify(stream.pipeline);
3535
const finishedAsync = util.promisify(stream.finished);
3636
const gzip = util.promisify(zlib.gzip);
3737
const deflate = util.promisify(zlib.deflate);
38+
const deflateRaw = util.promisify(zlib.deflateRaw);
3839
const brotliCompress = util.promisify(zlib.brotliCompress);
3940

4041
function toleranceRange(positive, negative) {
@@ -493,13 +494,16 @@ describe('supports http with nodejs', function () {
493494
describe('algorithms', ()=> {
494495
const responseBody ='str';
495496

496-
for (const [type, zipped] of Object.entries({
497+
for (const [typeName, zipped] of Object.entries({
497498
gzip: gzip(responseBody),
498499
compress: gzip(responseBody),
499500
deflate: deflate(responseBody),
501+
'deflate-raw': deflateRaw(responseBody),
500502
br: brotliCompress(responseBody)
501503
})) {
502-
describe(`${type} decompression`, async () => {
504+
const type = typeName.split('-')[0];
505+
506+
describe(`${typeName} decompression`, async () => {
503507
it(`should support decompression`, async () => {
504508
server = await startHTTPServer(async (req, res) => {
505509
res.setHeader('Content-Encoding', type);

0 commit comments

Comments
 (0)