Skip to content

Commit

Permalink
fix(core-p2p): disable perMessageDeflate on WS (#4441)
Browse files Browse the repository at this point in the history
* chore(core-p2p): perMessageDeflate = false on client

* test(core-p2p): perMessageDeflate = false on client

* chore(core-p2p): perMessageDeflate = false on listener
  • Loading branch information
sebastijankuzner authored Jul 8, 2021
1 parent 13d0709 commit 7a6d198
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
18 changes: 12 additions & 6 deletions __tests__/unit/core-p2p/hapi-nes/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,22 @@ const createServerWithPlugin = async (pluginOptions = {}, serverOptions = {}, wi
};

describe("Client", () => {
it("defaults options.ws.maxPayload to 102400 (node)", () => {
it("defaults options.ws.maxPayload to 102400 (node) && perMessageDeflate to false", () => {
const client = new Client("http://localhost");
// @ts-ignore
expect(client._settings.ws).toEqual({ maxPayload: 102400 });
expect(client._settings.ws).toEqual({ maxPayload: 102400, perMessageDeflate: false });
});

it("allows setting options.ws.maxPayload (node)", () => {
const client = new Client("http://localhost", { ws: { maxPayload: 100 } });
// @ts-ignore
expect(client._settings.ws).toEqual({ maxPayload: 100 });
expect(client._settings.ws).toEqual({ maxPayload: 100, perMessageDeflate: false });
});

it("prevents setting options.ws.perMessageDeflate (node)", () => {
const client = new Client("http://localhost", { ws: { perMessageDeflate: true } });
// @ts-ignore
expect(client._settings.ws).toEqual({ maxPayload: 102400, perMessageDeflate: false });
});

describe("onError", () => {
Expand Down Expand Up @@ -632,7 +638,7 @@ describe("Client", () => {
describe("request()", () => {
it("defaults to POST", async () => {
const server = await createServerWithPlugin({ headers: "*" });

server.route({
method: "POST",
path: "/",
Expand Down Expand Up @@ -778,7 +784,7 @@ describe("Client", () => {
describe("_onMessage", () => {
it("ignores invalid incoming message", async () => {
const server = await createServerWithPlugin({}, {}, true);

server.route({
method: "POST",
path: "/",
Expand Down Expand Up @@ -1010,7 +1016,7 @@ describe("Client", () => {
describe("ping / pong", () => {
it.each([["ping"], ["pong"]])("terminates when receiving a ws.%s", async (method) => {
const server = await createServerWithPlugin({}, {}, true);

server.route({
method: "POST",
path: "/",
Expand Down
7 changes: 4 additions & 3 deletions packages/core-p2p/src/hapi-nes/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,13 @@ export class Client {

options.ws = options.ws || {};

if (options.ws.maxPayload === undefined) {
options.ws.maxPayload = DEFAULT_MAX_PAYLOAD_CLIENT;
options.ws = {
maxPayload: DEFAULT_MAX_PAYLOAD_CLIENT,
...options.ws,
perMessageDeflate: false
}

// Configuration

this._url = url;
this._settings = options;
this._heartbeatTimeout = false; // Server heartbeat configuration
Expand Down
2 changes: 1 addition & 1 deletion packages/core-p2p/src/hapi-nes/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Listener {

// WebSocket listener

const options: any = { server: this._server.listener, maxPayload: settings.maxPayload };
const options: any = { server: this._server.listener, maxPayload: settings.maxPayload, perMessageDeflate: false };
if (settings.origin) {
options.verifyClient = (info) => settings.origin.indexOf(info.origin) >= 0;
}
Expand Down

0 comments on commit 7a6d198

Please sign in to comment.