Skip to content

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

Open
bjnewman wants to merge 1 commit intochimurai:masterfrom
bjnewman:fix-websocket-server-undefined
Open

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

Conversation

@bjnewman
Copy link

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!

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

1 participant