Skip to content

fix(eio-client/types): fix the type of the perMessageDeflate option and of send() - #5631

Open
mark1russell7 wants to merge 1 commit into
socketio:mainfrom
mark1russell7:fix/eio-client-option-types
Open

mark1russell7 wants to merge 1 commit into
socketio:mainfrom
mark1russell7:fix/eio-client-option-types

Conversation

@mark1russell7

Copy link
Copy Markdown

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

  • perMessageDeflate?: { threshold: number }: false is a type error, although the JSDoc says "Set to false to disable.", and threshold is required. The JSDoc says @default false, but the default is { threshold: 1024 }.
  • send(msg, options?, fn?) and write(msg, options?, fn?): send(msg, fn) is a type error, although the runtime accepts the callback as the second argument.

New behavior

  • perMessageDeflate?: boolean | { threshold?: number }, with @default { threshold: 1024 }. websocket.node.ts reads the threshold through a type assertion (the runtime check if (this.opts.perMessageDeflate) is unchanged).
  • send() and write() get the overloads (msg, fn?) and (msg, options?, fn?). _sendPacket() accepts WriteOptions | (() => void) as its third argument, which is what it handles at runtime. In its branch for a function, options is now set to {} instead of null, so that TypeScript narrows it to WriteOptions (the strict option is off). The value is the same as before, because the next step is options = options || {}.

No runtime behavior changes. This widens the type of an option, so code that reads opts.perMessageDeflate.threshold from the options of a socket now needs a type check.

Other information (e.g. related issues)

Fixes #5629

The type tests of the repository are disabled (test:types is exit 0), so the types were checked with tsc (strict) on a small file that uses perMessageDeflate: false and send("hello", () => {}), against the declarations built from main and from this branch. The PR also adds a test in test/connection.js ("should accept a callback as the second argument of send()") for the runtime behavior that the new overload declares.

Test results, from one run on a GitHub Actions runner (Linux, Node.js 24.22.2, TypeScript 6.0.3 of the lock file), at the commit 84d476a of this branch on top of main (f97d51a). The job follows ci-engine.io-client.yml: npm ci, then it compiles engine.io-parser, engine.io-client and engine.io. "Before" is the lib/ folder of main with the tests of this branch.

import { Socket } from "engine.io-client";

// "Set to false to disable." (JSDoc of the perMessageDeflate option)
const socket = new Socket("http://localhost:3000", { perMessageDeflate: false });

// the callback as second argument (accepted at runtime)
socket.send("hello", () => console.log("flushed"));
  • tsc before: check.ts(4,54): error TS2322: Type 'boolean' is not assignable to type '{ threshold: number; }'. and check.ts(7,22): error TS2559: Type '() => void' has no properties in common with type 'WriteOptions'.
  • tsc after: no error.
  • npx mocha --exit --require test/support/hooks.js --grep callback test/connection.js: 1 passing before and after, with XHR and with USE_FETCH=1 (the runtime already accepts the callback).
  • Full suites of the branch: test:node 110 passing, 1 pending. test:node-fetch 109 passing, 2 pending. test:node-builtin-ws 107 passing, 4 pending. format:check passes.

The run: https://github.com/mark1russell7/wirelab/actions/runs/43625189318

This PR was prepared with AI assistance (Claude). The checks named above ran before and after the change in that run, with the results above.

🤖 Generated with Claude Code

…nd 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 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[engine.io-client] types: perMessageDeflate cannot be set to false, and send(msg, callback) is not allowed

1 participant