Skip to content

feat: add h3 v2 support#16

Merged
CodeDredd merged 3 commits into
mainfrom
feat/h3-v2-support
Jun 9, 2026
Merged

feat: add h3 v2 support#16
CodeDredd merged 3 commits into
mainfrom
feat/h3-v2-support

Conversation

@CodeDredd

Copy link
Copy Markdown
Owner

Closes #14

Problem

h3 v2 fundamentally changes the response pipeline and breaks this package:

  • The send utility is removed, so import { send } from 'h3' fails to load the module under v2.
  • The onBeforeResponse app hook is gone (v2 only has onRequest/onResponse/onError), and onResponse's return value is ignored (toResponse does .then(() => response)), so the body can no longer be replaced through a hook.
  • The response is now an immutable web Response.

Solution — dual support for h3 v1 and v2

  • src/helper.ts: access send (v1) / toResponse (v2) dynamically via a namespace import so the module loads under both versions. compress falls back to mutating response.body when send is unavailable. Adds web-Response helpers compressResponse / compressResponseStream and a stream-aware getStreamCompression.
  • src/middleware.ts (new): compression() and compressionStream() middleware for h3 v2 — they call next(), normalize the result with toResponse, and return a compressed Response. Optional forced method (e.g. compression('gzip')).
  • src/index.ts: export the new middleware, helpers and types.
  • peerDependencies: widen h3 to ^1.6.0 || ^2.0.0 (devDep pinned to the real v2 2.0.1-rc.22; npm h3@2.0.0 is a deprecated stub).
  • Tests: ported to h3 v2 — mockEvent for the mutable / Nitro path, a real new H3() app + supertest for the middleware. gzip/deflate/br covered. 15 tests pass, ~97% coverage, lint + build green.
  • README: new "Usage (h3 v2)" section and middleware utilities.

Notes

  • The only uncovered branch is the v1 send path (not reachable when tested under v2); it is byte-identical to the previously shipped code.
  • CI currently runs against v2 only. Happy to add an h3: [1, 2] matrix dimension if preferred.

🤖 Generated with Claude Code

CodeDredd and others added 2 commits June 9, 2026 11:35
h3 v2 makes the response an immutable web `Response`, removes the `send`
utility and the `onBeforeResponse` app hook. This adds dual support for
h3 v1 and v2:

- access `send` (v1) / `toResponse` (v2) dynamically so the module loads
  under both versions; `compress` mutates the body when `send` is gone
- add `compression()` / `compressionStream()` middleware for h3 v2 plus the
  low-level `compressResponse` / `compressResponseStream` helpers
- widen the `h3` peer range to `^1.6.0 || ^2.0.0`
- port tests to h3 v2 (mockEvent for the mutable/nitro path, a real H3 app
  for the middleware) and document v2 usage

Closes #14

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Add an `h3: [1, 2]` matrix dimension and switch the installed h3 version per
job. Tests are split by version: v1-only (app `onBeforeResponse` hook) and
v2-only (mockEvent + middleware) suites are gated via `describe.runIf` and a
`test/_version` feature-detect, with version-specific h3 symbols accessed
lazily so every file loads under both versions.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@CodeDredd CodeDredd force-pushed the feat/h3-v2-support branch from 6e6b63e to 808f03f Compare June 9, 2026 09:35
The playground used `@nuxt/devtools: latest`. Re-resolving the lockfile for
the h3 v2 devDep bumped it to a version pulling `@nuxt/kit@4.4.8` / `vite@8`,
which import `styleText` from `node:util` (Node 20+). The playground's
`nuxt prepare` postinstall then crashed the install on Node 18.

Pin it to `^0.8.2` (the version main already resolved) so the playground tree
stays Node-18 compatible. This also shrinks the lockfile diff to just the h3
v1 -> v2 swap.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@CodeDredd CodeDredd merged commit 7b492a8 into main Jun 9, 2026
9 checks passed
@CodeDredd CodeDredd deleted the feat/h3-v2-support branch June 9, 2026 09:46
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.

H3 v2 version not surport

1 participant