Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions packages/engine.io-client/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion packages/engine.io-client/lib/transports/websocket.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/engine.io-client/test/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down