Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ jobs:
strategy:
matrix:
node: [18, 20]
h3: [1, 2]
os: [ubuntu-latest, macos-latest]
fail-fast: false

name: test (node-${{ matrix.node }}, h3-v${{ matrix.h3 }}, ${{ matrix.os }})

steps:
- uses: actions/checkout@v3
with:
Expand All @@ -51,6 +54,8 @@ jobs:
node-version: ${{ matrix.node }}
cache: pnpm
- run: pnpm install
- name: Install h3 v${{ matrix.h3 }}
run: pnpm add -D h3@${{ matrix.h3 == 1 && '^1.8.0' || '2.0.1-rc.22' }} --ignore-scripts
- run: pnpm build
# - run: pnpm test:types
- run: pnpm vitest --coverage && rm -rf coverage/tmp
Expand Down
37 changes: 36 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

✔️  **Compression Detection:** It uses the best compression which is accepted

✔️  **h3 v1 & v2:** Works with both [h3](https://h3.dev) v1 and v2



## Install
Expand All @@ -31,7 +33,7 @@ yarn add h3-compression
pnpm add h3-compression
```

## Usage
## Usage (h3 v1)

```ts
import { createServer } from 'node:http'
Expand Down Expand Up @@ -63,6 +65,32 @@ app.use(
listen(toNodeListener(app))
```

## Usage (h3 v2)

In [h3 v2](https://h3.dev) the response is an immutable web `Response` and the `onBeforeResponse`
hook was removed. Use the `compression` / `compressionStream` middleware instead — they read the
response returned by the next handler and replace it with a compressed one.

```ts
import { createServer } from 'node:http'
import { H3, toNodeHandler } from 'h3'
import { compression } from 'h3-compression'

const app = new H3()

app.use(compression()) // or app.use(compressionStream())
app.get('/', () => 'Hello world!')

createServer(toNodeHandler(app)).listen(process.env.PORT || 3000)
```

You can also force a specific method (e.g. `compression('gzip')`) instead of detecting it from the
`Accept-Encoding` header.

> [!NOTE]
> `compressionStream` uses the native [`CompressionStream`](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream),
> which only supports `gzip` and `deflate` (no brotli).

## Nuxt 3 & 4

If you want to use it in Nuxt you can define a nitro plugin.
Expand Down Expand Up @@ -104,6 +132,13 @@ H3-compression has a concept of composable utilities that accept `event` (from `
- `useDeflateCompressionStream(event, response)`
- `useCompressionStream(event, response)`

#### Middleware (h3 v2)

- `compression(method?)`  – middleware using zlib (brotli, gzip, deflate)
- `compressionStream(method?)`  – middleware using the native `CompressionStream` (gzip, deflate)
- `compressResponse(event, value, method?)`  – low-level helper returning a compressed `Response`
- `compressResponseStream(event, value, method?)`  – low-level stream helper returning a compressed `Response`

## Sponsors

<p align="center">
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"prepare": "simple-git-hooks"
},
"peerDependencies": {
"h3": "^1.6.0"
"h3": "^1.6.0 || ^2.0.0"
},
"devDependencies": {
"@antfu/eslint-config": "^0.41.0",
Expand All @@ -72,7 +72,7 @@
"bumpp": "^9.2.0",
"eslint": "^8.48.0",
"esno": "^0.17.0",
"h3": "^1.8.1",
"h3": "2.0.1-rc.22",
"lint-staged": "^14.0.1",
"node-fetch-native": "^1.4.0",
"pnpm": "^8.7.0",
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"postinstall": "nuxt prepare"
},
"devDependencies": {
"@nuxt/devtools": "latest",
"@nuxt/devtools": "^0.8.2",
"nuxt": "^3.7.0"
}
}
75 changes: 39 additions & 36 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading