Skip to content

feat(connect): add preferH2 connector option to offer h2 first in ALPN#5327

Open
Antamansid wants to merge 1 commit into
nodejs:mainfrom
Antamansid:feat/connector-preferh2
Open

feat(connect): add preferH2 connector option to offer h2 first in ALPN#5327
Antamansid wants to merge 1 commit into
nodejs:mainfrom
Antamansid:feat/connector-preferh2

Conversation

@Antamansid
Copy link
Copy Markdown

This relates to...

N/A — no existing issue. Affects clients talking to load balancers that select the ALPN protocol by client preference.

Rationale

The built-in connector hardcodes ALPNProtocols: ['http/1.1', 'h2'] (h1 first) after spreading user options (lib/core/connect.js), so the order cannot be overridden via the connect options. Servers that select the ALPN protocol by client preference — some load balancers do (e.g. those relying on OpenSSL's SSL_select_next_proto semantics) — then negotiate http/1.1 even with allowH2: true, forcing the client onto HTTP/1.1.

This adds an opt-in preferH2 connector option that, together with allowH2, offers ALPN as ['h2', 'http/1.1'] (h2 first), so HTTP/2 is negotiated whenever the server supports it. The default behaviour is unchanged (preferH2 defaults to false). If the server does not support HTTP/2, ALPN transparently falls back to http/1.1.

Changes

  • lib/core/connect.js: accept a preferH2 build option; when set together with allowH2, offer ALPNProtocols: ['h2', 'http/1.1'] instead of the default ['http/1.1', 'h2'].
  • types/connector.d.ts: add preferH2?: boolean to BuildOptions.
  • docs/docs/api/Connector.md: document the new option.
  • test/connect-h2-alpn-order.js: assert the offered ALPN order with and without preferH2.
  • test/types/connector.test-d.ts: type-level assertion for preferH2.

Features

  • New opt-in connector option preferH2 (default false) to offer HTTP/2 first in ALPN, for servers that pick the protocol by client preference.

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A — the default ALPN order is unchanged; preferH2 is opt-in.

Status

The built-in connector hardcodes `ALPNProtocols: ['http/1.1', 'h2']` (h1 first)
after spreading user options, so the order cannot be overridden via the `connect`
options. Servers that select the ALPN protocol by *client* preference — some load
balancers do — then negotiate `http/1.1` even with `allowH2: true`, forcing the
client onto HTTP/1.1.

Add an opt-in `preferH2` option that, together with `allowH2`, offers ALPN as
`['h2', 'http/1.1']` (h2 first) so HTTP/2 is negotiated whenever the server
supports it. The default behaviour is unchanged. If the server does not support
HTTP/2, ALPN transparently falls back to `http/1.1`.

Signed-off-by: antamansid <antamansid@mail.ru>
@Antamansid Antamansid marked this pull request as ready for review May 27, 2026 08:45
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.

1 participant