Skip to content

http: add shouldUpgradeCallback to let servers control HTTP upgrades#59824

Merged
nodejs-github-bot merged 2 commits intonodejs:mainfrom
pimterry:http-upgrade-callback
Sep 17, 2025
Merged

http: add shouldUpgradeCallback to let servers control HTTP upgrades#59824
nodejs-github-bot merged 2 commits intonodejs:mainfrom
pimterry:http-upgrade-callback

Conversation

@pimterry
Copy link
Member

@pimterry pimterry commented Sep 9, 2025

Fixes #57054

Previously, you could either add no upgrade event handler to your HTTP server, in which case all HTTP upgrade requests were ignored, or you could add an upgrade handler and all upgrade attempts would always effectively succeed, going straight to the upgrade event and skipping normal request handling.

That can be inflexible, as there's plenty of cases where you want to support just upgrading specific requests (most commonly, specific protocols - e.g. upgrading to support websockets but not h2c).

This change adds a new shouldUpgradeCallback option to HTTP servers, which receives the request details and returns a boolean that controls whether the request should be upgraded. Existing behaviour isn't changed, by making the default option value () => server.listenerCount('upgrade') > 0.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. http Issues or PRs related to the http subsystem. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve HTTP server 'upgrade' event generation logic

4 participants