Skip to content

Commit 8339cb3

Browse files
authored
fix: request with brotli content encoding (#57)
* fix: request with brotli content encoding * replaced gzip with decompress middleware
1 parent 29beb25 commit 8339cb3

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

src/components/proxy-middleware/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ class ProxyMiddlewareManager {
348348
init_handlers = () => {
349349
this.proxy.onRequestHandlers = [];
350350
this.proxy.onConnectHandlers = [];
351-
this.proxy.use(Proxy.gunzip);
351+
this.proxy.use(Proxy.decompress);
352352

353353
// this.init_ssl_tunneling_handler();
354354
this.init_amiusing_handler();

src/lib/proxy/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import DefaultExport, {Proxy, PROXY_HANDLER_TYPE, gunzip, wildcard} from "./lib/proxy"
1+
import DefaultExport, {Proxy, PROXY_HANDLER_TYPE, gunzip, decompress, wildcard} from "./lib/proxy"
22

33
export default DefaultExport;
44

5-
export {Proxy, PROXY_HANDLER_TYPE, gunzip, wildcard}
5+
export {Proxy, PROXY_HANDLER_TYPE, gunzip, decompress, wildcard}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"use strict";
2+
3+
var zlib = require("zlib");
4+
5+
const decompressMiddleware = {
6+
onResponse: function (ctx, callback) {
7+
if (
8+
ctx.serverToProxyResponse.headers["content-encoding"] &&
9+
ctx.serverToProxyResponse.headers["content-encoding"].toLowerCase() ==
10+
"gzip"
11+
) {
12+
delete ctx.serverToProxyResponse.headers["content-encoding"];
13+
ctx.addResponseFilter(zlib.createGunzip());
14+
} else if (
15+
ctx.serverToProxyResponse.headers["content-encoding"] &&
16+
ctx.serverToProxyResponse.headers["content-encoding"].toLowerCase() ==
17+
"br"
18+
) {
19+
delete ctx.serverToProxyResponse.headers["content-encoding"];
20+
ctx.addResponseFilter(zlib.createBrotliDecompress());
21+
}
22+
return callback();
23+
},
24+
onRequest: function (ctx, callback) {
25+
ctx.proxyToServerRequestOptions.headers["accept-encoding"] = "gzip";
26+
return callback();
27+
},
28+
};
29+
30+
module.exports = decompressMiddleware;

src/lib/proxy/lib/middleware/gunzip.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ const gunzipMiddleware = {
2020
},
2121
};
2222

23-
module.exports = gunzipMiddleware;
23+
module.exports = gunzipMiddleware;

src/lib/proxy/lib/proxy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ module.exports = function () {
2424
};
2525

2626
module.exports.gunzip = require("./middleware/gunzip");
27+
module.exports.decompress = require("./middleware/decompress");
2728
module.exports.wildcard = require("./middleware/wildcard");
2829

2930
var Proxy: any = function () {

0 commit comments

Comments
 (0)