Skip to content

fix: prevent TypeError when ws enabled but server is undefined#1163

Merged
chimurai merged 1 commit intochimurai:masterfrom
bjnewman:fix-websocket-server-undefined
Apr 3, 2026
Merged

fix: prevent TypeError when ws enabled but server is undefined#1163
chimurai merged 1 commit intochimurai:masterfrom
bjnewman:fix-websocket-server-undefined

Conversation

@bjnewman
Copy link
Copy Markdown
Contributor

Closes #143

Hey! While working on #1162 (dropping Node 14/16/18), we ran into this issue and figured we'd fix it too.

The bug

When ws: true is enabled but the server object is undefined, trying to call catchUpgradeRequest(server) crashes with:

TypeError: Cannot read property 'on' of null at catchUpgradeRequest

Happens with spdy/HTTP2, Bun, Deno, and some SSL setups.

The fix

Simple guard check before calling catchUpgradeRequest:

if (this.proxyOptions.ws === true && server) {
  this.catchUpgradeRequest(server);
}

Tests

Added 6 edge case tests:

  • Server is undefined
  • Server is null
  • Multiple requests without server
  • ws:false with missing server
  • Matching path scenarios
  • Websockets still work when server exists

All 193 tests passing.

Thanks for maintaining this project!

@chimurai chimurai added this to the v4 milestone Apr 3, 2026
@chimurai chimurai force-pushed the fix-websocket-server-undefined branch from f2c7089 to 80af45c Compare April 3, 2026 22:36
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 3, 2026

npm i https://pkg.pr.new/http-proxy-middleware@1163

commit: 5ba8987

@chimurai chimurai merged commit b67d34d into chimurai:master Apr 3, 2026
10 checks passed
@chimurai
Copy link
Copy Markdown
Owner

chimurai commented Apr 3, 2026

Thanks!

@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 96.335%. remained the same
when pulling 80af45c on bjnewman:fix-websocket-server-undefined
into a6ebb03 on chimurai:master.

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.

TypeError: Cannot read property 'on' of null at catchUpgradeRequest

3 participants