Skip to content

fix(types): make toNodeHandler() assignable to http1 and http2 servers - #368

Open
tada5hi wants to merge 1 commit into
h3js:mainfrom
tada5hi:fix/to-node-handler-type
Open

tada5hi wants to merge 1 commit into
h3js:mainfrom
tada5hi:fix/to-node-handler-type

Conversation

@tada5hi

@tada5hi tada5hi commented Nov 12, 2026 •

Copy link
Copy Markdown

resolves #367

toNodeHandler() returned NodeHttpHandler & AdapterMeta, a union of the http1 and http2 handler types, which is not assignable to the request listener of node:http, node:https or node:http2. The function it returns takes NodeServerRequest / NodeServerResponse and handles both protocols, so the return type is now NodeHttp1Handler & NodeHttp2Handler & AdapterMeta.

This is a type-only change:

  • The converted handler needs no cast, it already satisfies both signatures.
  • The toFetchHandler() round trip returns the original __nodeHandler, which is typed as the union, so that branch has one cast. The intersection is optimistic there if the original handler was written for a single protocol.
  • The as NodeHttp1Handler casts on toNodeHandler() results in the tests are removed.
  • test/types.test-d.ts asserts the result extends both request listener types.

Verification

  • A standalone tsc --strict check passing the result to http.createServer, https.createServer, http2.createServer and http2.createSecureServer passes with this change and reports 6 errors without it.
  • Not run locally: pnpm lint, pnpm typecheck and the vitest suite. Relying on CI for those.

Summary by CodeRabbit

  • New Features
    • toNodeHandler is now typed for use with both HTTP/1 and HTTP/2 servers, including node:http, node:https, and node:http2.
    • The handler can be passed directly to Node server creation without additional type casts.

@tada5hi
tada5hi requested a review from pi0 as a code owner November 12, 2026 12:12
@vercel

vercel Bot commented Nov 12, 2026

Copy link
Copy Markdown

@tada5hi is attempting to deploy a commit to the unjs Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

toNodeHandler now declares a return type compatible with both HTTP/1 and HTTP/2 handlers. Tests check that the result can be passed directly to Node server APIs and that its type is assignable to both protocol listener types.

Changes: Node handler protocol typing

Layer / File(s) Summary
Combined protocol return type
src/adapters/_node/adapter.ts
toNodeHandler returns the intersection of the HTTP/1 and HTTP/2 handler types, including when it returns an existing __nodeHandler.
Listener compatibility checks
test/types.test-d.ts, test/node-adapters.test.ts, test/node-error-paths.test.ts, test/node-promises.test.ts
Tests remove HTTP/1 casts from toNodeHandler results and assert compatibility with HTTP/1 and HTTP/2 listener types.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • h3js/srvx#310: Changes the handler stored by toFetchHandler, which is returned by toNodeHandler on a round trip.
Merge Risk: Minimal · up to 50b7d

This change only adjusts TypeScript types so toNodeHandler() results can be passed to node:http, node:https and node:http2 servers without a cast. Runtime behavior is unchanged. The known caveat that round-tripped single-protocol handlers are typed optimistically is a stated tradeoff, so no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the return type of toNodeHandler() is widened so the handler is assignable to both HTTP/1 and HTTP/2 request listeners. The raw summary and objectives confirm this is …
Linked Issues check Passed Issue #367 requires that the return type of toNodeHandler() be assignable to the request listeners of node:http, node:https and node:http2 servers without a cast. At the reviewed head, `src/ad…
Out of Scope Changes check Passed All changed hunks relate to #367. The return-type change and its JSDoc note are in src/adapters/_node/adapter.ts. The NodeHttp1Handler/NodeHttp2Handler imports support the new return type. The r…
Full details: Title check

Explanation

The title names the main change: the return type of toNodeHandler() is widened so the handler is assignable to both HTTP/1 and HTTP/2 request listeners. The raw summary and objectives confirm this is a type-only change to toNodeHandler(), with matching test updates that remove casts and add type assertions. The conventional 'fix(types):' prefix matches the type-only nature of the change.

Full details: Linked Issues check

Explanation

Issue #367 requires that the return type of toNodeHandler() be assignable to the request listeners of node:http, node:https and node:http2 servers without a cast. At the reviewed head, src/adapters/_node/adapter.ts changes the return type from NodeHttpHandler & AdapterMeta to NodeHttp1Handler & NodeHttp2Handler & AdapterMeta. The toFetchHandler() round-trip branch returns handler.__nodeHandler with a cast to the same intersection, which matches the caveat in #367. The casts on toNodeHandler() results in the tests are removed (per the PR summary). test/types.test-d.ts adds type assertions that ReturnType<typeof toNodeHandler> extends RequestListener (node:http) and an Http2ServerRequest/Http2ServerResponse listener signature (node:http2). I read the diff only. I did not run tsc, pnpm typecheck or Vitest. The author also did not run them locally. The type-level evidence supports the requirement.

Full details: Out of Scope Changes check

Explanation

All changed hunks relate to #367. The return-type change and its JSDoc note are in src/adapters/_node/adapter.ts. The NodeHttp1Handler/NodeHttp2Handler imports support the new return type. The round-trip cast carries the caveat named in #367. The test files remove casts that the new return type makes unnecessary, and test/types.test-d.ts adds assertions for the new type. No unrelated behavior change appears in the diff. The runtime conversion path is unchanged.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the types with care,
HTTP/1 and HTTP/2 meet there.
No cast is needed in the test,
The handler fits each listener best.
Then hops away to nibble a leaf.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 542bccc1-7dcf-44cf-89f0-4d856649111c
Commits

Reviewing files that changed from the base of the PR and between a84f62b and 50b7d41.

Files selected for processing: 5

  • src/adapters/_node/adapter.ts
  • test/node-adapters.test.ts
  • test/node-error-paths.test.ts
  • test/node-promises.test.ts
  • test/types.test-d.ts

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
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.

toNodeHandler() return type is not assignable to node:http / node:http2 servers

1 participant