From 84d476a185c1ab216c3fb4b0615625dc2d8effdb Mon Sep 17 00:00:00 2001 From: Mark Russell <39885340+mark1russell7@users.noreply.github.com> Date: Sat, 14 Nov 2026 13:08:08 -0500 Subject: [PATCH] fix(eio-client/types): fix the type of the perMessageDeflate option and of send() The perMessageDeflate option can be set to false to disable the permessage-deflate extension (as stated in its documentation), and its default value is { threshold: 1024 }. The send() and write() methods accept a callback as their second argument at runtime, but their signature did not allow it. Co-Authored-By: Claude Opus 5.5 --- packages/engine.io-client/lib/socket.ts | 24 ++++++++++++++----- .../lib/transports/websocket.node.ts | 5 +++- packages/engine.io-client/test/connection.js | 10 ++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/engine.io-client/lib/socket.ts b/packages/engine.io-client/lib/socket.ts index 2bd3603c21..caed5bc088 100644 --- a/packages/engine.io-client/lib/socket.ts +++ b/packages/engine.io-client/lib/socket.ts @@ -246,9 +246,9 @@ export interface SocketOptions { /** * parameters of the WebSocket permessage-deflate extension (see ws module api docs). Set to false to disable. - * @default false + * @default { threshold: 1024 } */ - perMessageDeflate?: { threshold: number }; + perMessageDeflate?: boolean | { threshold?: number }; /** * The path to get our client file from, in the case of the server @@ -934,7 +934,13 @@ export class Socket extends Emitter< * @param {Function} fn - callback function. * @return {Socket} for chaining. */ - public write(msg: RawData, options?: WriteOptions, fn?: () => void) { + public write(msg: RawData, fn?: () => void): this; + public write(msg: RawData, options?: WriteOptions, fn?: () => void): this; + public write( + msg: RawData, + options?: WriteOptions | (() => void), + fn?: () => void, + ) { this._sendPacket("message", msg, options, fn); return this; } @@ -947,7 +953,13 @@ export class Socket extends Emitter< * @param {Function} fn - callback function. * @return {Socket} for chaining. */ - public send(msg: RawData, options?: WriteOptions, fn?: () => void) { + public send(msg: RawData, fn?: () => void): this; + public send(msg: RawData, options?: WriteOptions, fn?: () => void): this; + public send( + msg: RawData, + options?: WriteOptions | (() => void), + fn?: () => void, + ) { this._sendPacket("message", msg, options, fn); return this; } @@ -964,7 +976,7 @@ export class Socket extends Emitter< private _sendPacket( type: PacketType, data?: RawData, - options?: WriteOptions, + options?: WriteOptions | (() => void), fn?: () => void, ) { if ("function" === typeof data) { @@ -974,7 +986,7 @@ export class Socket extends Emitter< if ("function" === typeof options) { fn = options; - options = null; + options = {}; } if ("closing" === this.readyState || "closed" === this.readyState) { diff --git a/packages/engine.io-client/lib/transports/websocket.node.ts b/packages/engine.io-client/lib/transports/websocket.node.ts index c587cceb73..02d4950e15 100644 --- a/packages/engine.io-client/lib/transports/websocket.node.ts +++ b/packages/engine.io-client/lib/transports/websocket.node.ts @@ -40,7 +40,10 @@ export class WS extends BaseWS { const len = // @ts-ignore "string" === typeof data ? Buffer.byteLength(data) : data.length; - if (len < this.opts.perMessageDeflate.threshold) { + const { threshold } = this.opts.perMessageDeflate as { + threshold?: number; + }; + if (len < threshold) { opts.compress = false; } } diff --git a/packages/engine.io-client/test/connection.js b/packages/engine.io-client/test/connection.js index aaa0c1f1a0..e6c8dc09ee 100644 --- a/packages/engine.io-client/test/connection.js +++ b/packages/engine.io-client/test/connection.js @@ -60,6 +60,16 @@ describe("connection", function () { }); }); + it("should accept a callback as the second argument of send()", (done) => { + const socket = new Socket(); + socket.on("open", () => { + socket.send("hi", () => { + socket.close(); + done(); + }); + }); + }); + it("should not send packets if socket closes", (done) => { const socket = new Socket(); socket.on("open", () => {