Skip to content

Commit

Permalink
adjust compression parameters and use permessage-deflate2 with thresh…
Browse files Browse the repository at this point in the history
…old (only messages larger than 1kb (512 chars) are compressed by default)
  • Loading branch information
leonardoventurini committed Nov 10, 2024
1 parent d7e90d8 commit b5fd7ac
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"@babel/preset-react": "^7.18.6",
"@types/lodash.isempty": "^4.4.9",
"@types/node": "^18.16.18",
"@types/sockjs": "^0.3.36",
"@types/sockjs-client": "^1.5.4",
"@typescript-eslint/eslint-plugin": "^5.56.0",
"@typescript-eslint/parser": "^5.56.0",
"eslint": "^8.36.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/ddp-server/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Package.describe({
});

Npm.depends({
"permessage-deflate": "0.1.7",
"permessage-deflate2": "0.1.8-alpha.1",
sockjs: "0.3.24",
"lodash.once": "4.1.1",
"lodash.isempty": "4.4.0",
Expand Down
17 changes: 12 additions & 5 deletions packages/ddp-server/stream_server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import once from 'lodash.once';
import zlib from 'node:zlib';

// By default, we use the permessage-deflate extension with default
// configuration. If $SERVER_WEBSOCKET_COMPRESSION is set, then it must be valid
Expand All @@ -14,12 +15,18 @@ import once from 'lodash.once';
var websocketExtensions = once(function () {
var extensions = [];

var websocketCompressionConfig = process.env.SERVER_WEBSOCKET_COMPRESSION
? JSON.parse(process.env.SERVER_WEBSOCKET_COMPRESSION) : {};
var websocketCompressionConfig = process.env.SERVER_WEBSOCKET_COMPRESSION ?
JSON.parse(process.env.SERVER_WEBSOCKET_COMPRESSION) : {};

if (websocketCompressionConfig) {
extensions.push(Npm.require('permessage-deflate').configure(
websocketCompressionConfig
));
extensions.push(Npm.require('permessage-deflate2').configure({
threshold: 512,
level: zlib.constants.Z_BEST_SPEED,
memLevel: zlib.constants.Z_MIN_MEMLEVEL,
noContextTakeover: true,
maxWindowBits: zlib.constants.Z_MIN_WINDOWBITS,
...(websocketCompressionConfig || {})
}));
}

return extensions;
Expand Down
4 changes: 2 additions & 2 deletions packages/socket-stream-client/node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Meteor } from "meteor/meteor";
import { toWebsocketUrl } from "./urls.js";
import { StreamClientCommon } from "./common.js";
import { toWebsocketUrl } from "./urls.js";

// @param endpoint {String} URL to Meteor app
// "http://subdomain.meteor.com/" or "/" or
Expand Down Expand Up @@ -132,7 +132,7 @@ export class ClientStream extends StreamClientCommon {
// require the module if we actually create a server-to-server
// connection.
var FayeWebSocket = Npm.require('faye-websocket');
var deflate = Npm.require('permessage-deflate');
var deflate = Npm.require('permessage-deflate2');

var targetUrl = toWebsocketUrl(this.endpoint);
var fayeOptions = {
Expand Down
2 changes: 1 addition & 1 deletion packages/socket-stream-client/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Package.describe({

Npm.depends({
"faye-websocket": "0.11.4",
"permessage-deflate": "0.1.7",
"permessage-deflate2": "0.1.8-alpha.1",
"lodash.isequal": "4.5.0",
"lodash.once": "4.1.1"
});
Expand Down

0 comments on commit b5fd7ac

Please sign in to comment.