Skip to content

Commit

Permalink
feat: disable perMessageDeflate by default
Browse files Browse the repository at this point in the history
The WebSocket permessage-deflate extension, while useful is some cases,
adds some extra memory overhead for each WebSocket connection, and
results in huge memory usage in production deployments.

It will now be disabled by default.
  • Loading branch information
darrachequesne committed Sep 10, 2020
1 parent 54c6797 commit 078527a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ to a single process.
- `allowUpgrades` (`Boolean`): whether to allow transport upgrades
(`true`)
- `perMessageDeflate` (`Object|Boolean`): parameters of the WebSocket permessage-deflate extension
(see [ws module](https://github.com/einaros/ws) api docs). Set to `false` to disable. (`true`)
(see [ws module](https://github.com/einaros/ws) api docs). Set to `true` to enable. (defaults to `false`)
- `threshold` (`Number`): data is compressed only if the byte size is above this value (`1024`)
- `httpCompression` (`Object|Boolean`): parameters of the http compression for the polling transports
(see [zlib](http://nodejs.org/api/zlib.html#zlib_options) api docs). Set to `false` to disable. (`true`)
Expand Down
12 changes: 9 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ class Server extends EventEmitter {
maxHttpBufferSize: 1e6,
transports: Object.keys(transports),
allowUpgrades: true,
perMessageDeflate: {
threshold: 1024
},
httpCompression: {
threshold: 1024
},
Expand All @@ -56,6 +53,15 @@ class Server extends EventEmitter {
this.corsMiddleware = require("cors")(this.opts.cors);
}

if (opts.perMessageDeflate) {
this.opts.perMessageDeflate = Object.assign(
{
threshold: 1024
},
opts.perMessageDeflate
);
}

this.init();
}

Expand Down
37 changes: 20 additions & 17 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2810,25 +2810,28 @@ describe("server", function() {
});

it("should not compress when the byte size is below threshold", function(done) {
var engine = listen({ transports: ["websocket"] }, function(port) {
engine.on("connection", function(conn) {
var socket = conn.transport.socket;
var send = socket.send;
socket.send = function(data, opts, callback) {
socket.send = send;
socket.send(data, opts, callback);
var engine = listen(
{ transports: ["websocket"], perMessageDeflate: true },
function(port) {
engine.on("connection", function(conn) {
var socket = conn.transport.socket;
var send = socket.send;
socket.send = function(data, opts, callback) {
socket.send = send;
socket.send(data, opts, callback);

expect(opts.compress).to.be(false);
conn.close();
done();
};
expect(opts.compress).to.be(false);
conn.close();
done();
};

var buf = Buffer.allocUnsafe(100);
for (var i = 0; i < buf.length; i++) buf[i] = i % 0xff;
conn.send(buf, { compress: true });
});
eioc("http://localhost:%d".s(port), { transports: ["websocket"] });
});
var buf = Buffer.allocUnsafe(100);
for (var i = 0; i < buf.length; i++) buf[i] = i % 0xff;
conn.send(buf, { compress: true });
});
eioc("http://localhost:%d".s(port), { transports: ["websocket"] });
}
);
});
});

Expand Down

0 comments on commit 078527a

Please sign in to comment.