Skip to content

Commit

Permalink
feat: add gzip support (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
rentallect authored Dec 16, 2023
1 parent b2b655e commit df0de41
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 16 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"dist"
],
"devDependencies": {
"@babel/core": "^7.23.6",
"@open-wc/karma-esm": "^4.0.0",
"@rollup/plugin-commonjs": "^22.0.0",
"@rollup/plugin-json": "^4.1.0",
Expand All @@ -50,14 +51,15 @@
"karma-mocha": "^2.0.1",
"mocha": "^10.0.0",
"rimraf": "^3.0.2",
"rollup": "^2.75.6",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-esformatter": "^2.0.1",
"rollup-plugin-polyfill-node": "^0.9.0",
"rollup-plugin-terser": "^7.0.2",
"typescript": "^5.2.2"
},
"dependencies": {
"@openziti/libcrypto-js": "^0.18.2",
"@openziti/libcrypto-js": "^0.19.0",
"@openziti/ziti-browzer-edge-client": "^0.6.2",
"asn1js": "^2.4.0",
"assert": "^2.0.0",
Expand All @@ -79,6 +81,7 @@
"md5.js": "^1.3.5",
"mime-types": "^2.1.35",
"multistream": "^4.1.0",
"pako": "^2.1.0",
"pkijs": "^2.3.0",
"process": "^0.11.10",
"promise-controller": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ let plugins = [
json(),
nodeResolve({preferBuiltins: false, browser: true}),
nodePolyfills(),
esformatter({indent: { value: ' '}}),
// esformatter({indent: { value: ' '}}),
// terser(),
];

Expand Down
5 changes: 3 additions & 2 deletions src/context/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2124,8 +2124,6 @@ class ZitiContext extends EventEmitter {
});

req.on('response', async res => {

let body = res.pipe(new PassThrough());

const response_options = {
url: url,
Expand All @@ -2136,6 +2134,9 @@ class ZitiContext extends EventEmitter {
timeout: request.timeout,
counter: request.counter
};

let body = res.pipe(new PassThrough( response_options ));

let response = new HttpResponse(body, response_options);

for (const hdr in response_options.headers) {
Expand Down
12 changes: 11 additions & 1 deletion src/http/readable-stream/_stream_passthrough.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@

import { Transform } from './_stream_transform';
import EventEmitter from 'events';
import pako from 'pako';
import { isEqual } from 'lodash-es';

class PassThrough extends Transform {

constructor (options) {
super(options);
this.ee = new EventEmitter()
this.ee = new EventEmitter();
if (options.headers && options.headers['content-encoding'] && isEqual(options.headers['content-encoding'].toLowerCase(), 'gzip')) {
this.isGzip = true;
this.inflator = new pako.Inflate();
}
}

_transform(chunk, encoding, cb) {
if (this.isGzip) {
this.inflator.push(chunk);
chunk = this.inflator.result;
}
cb(null, chunk, this);
};

Expand Down
7 changes: 2 additions & 5 deletions src/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,8 @@ ZitiHttpRequest.prototype.getServiceConnectAppData = function() {
headers.append( 'x-openziti-browzer-core', pjson.version );
}

// --- Disable gzip for now ---
//
// // HTTP-network-or-cache fetch step 2.15
if (this.compress && !headers.has('Accept-Encoding')) {
headers.set('Accept-Encoding', 'gzip,deflate');
if (!headers.has('Accept-Encoding')) {
headers.set('Accept-Encoding', 'gzip,deflate');
}

// if (!headers.has('Connection')) {
Expand Down
Loading

0 comments on commit df0de41

Please sign in to comment.