{"_id":"h3-compression","_rev":"5-c9269d0d9c1e537a96e40f104d4b87bc","name":"h3-compression","dist-tags":{"latest":"1.1.0"},"versions":{"0.3.1":{"name":"h3-compression","version":"0.3.1","keywords":["h3","h3-compression","gzip","brotli","deflate","compression"],"author":{"name":"Gregor Becker","email":"gregor@codedredd.de"},"license":"MIT","_id":"h3-compression@0.3.1","maintainers":[{"name":"codedredd","email":"gregor@codedredd.de"}],"homepage":"https://github.com/CodeDredd/h3-compression#readme","bugs":{"url":"https://github.com/CodeDredd/h3-compression/issues"},"dist":{"shasum":"27ac820f1b2fa6d3f8c6ed6f8dbb144527a3d77e","tarball":"https://registry.npmjs.org/h3-compression/-/h3-compression-0.3.1.tgz","fileCount":8,"integrity":"sha512-udEFyes36yIvjDu4/VR7vixxqLFJYn/kkb/1DUwnPOzO+wSPRf/8WM/YjlZRt0GPvjKvXQGxpjgtOUPywZeJ2w==","signatures":[{"sig":"MEUCIBNXsbfDByWjLo2e7Ufiw0ADsN8vYZ3OgmrKzaDMrC6bAiEAtVWjIykEEi44AyPIo+4CRowTSE2gj84Neqbd/gFW4dM=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15766},"main":"./dist/index.mjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"}},"funding":"https://github.com/sponsors/codedredd","gitHead":"700ddf9eac2d2a42e41505977e67a4cbf6971d5e","scripts":{"dev":"unbuild --stub","lint":"eslint .","test":"vitest","build":"unbuild","start":"esno src/index.ts","prepare":"simple-git-hooks","release":"bumpp && npm publish","test:types":"vitest typecheck","prepublishOnly":"nr build"},"_npmUser":{"name":"codedredd","email":"gregor@codedredd.de"},"repository":{"url":"git+https://github.com/CodeDredd/h3-compression.git","type":"git"},"_npmVersion":"9.5.1","description":"Adds compression to h3 request (brotli, gzip, deflate)","directories":{},"lint-staged":{"*":"eslint --fix"},"sideEffects":false,"_nodeVersion":"18.16.0","typesVersions":{"*":{"*":["./dist/*","./dist/index.d.ts"]}},"_hasShrinkwrap":false,"packageManager":"pnpm@8.7.0","devDependencies":{"h3":"^1.8.1","esno":"^0.17.0","pnpm":"^8.7.0","vite":"^4.4.9","bumpp":"^9.2.0","eslint":"^8.48.0","rimraf":"^5.0.1","vitest":"^0.34.3","unbuild":"^2.0.0","@antfu/ni":"^0.21.6","supertest":"^6.3.3","typescript":"^5.2.2","@types/node":"^20.5.7","lint-staged":"^14.0.1","@antfu/utils":"^0.7.6","@types/supertest":"^2.0.12","simple-git-hooks":"^2.9.0","node-fetch-native":"^1.4.0","@vitest/coverage-v8":"^0.34.3","@antfu/eslint-config":"^0.41.0"},"peerDependencies":{"h3":"^1.6.0"},"simple-git-hooks":{"pre-commit":"pnpm lint-staged"},"_npmOperationalInternal":{"tmp":"tmp/h3-compression_0.3.1_1693393855632_0.6350337407260374","host":"s3://npm-registry-packages"}},"0.3.2":{"name":"h3-compression","version":"0.3.2","keywords":["h3","h3-compression","gzip","brotli","deflate","compression"],"author":{"name":"Gregor Becker","email":"gregor@codedredd.de"},"license":"MIT","_id":"h3-compression@0.3.2","maintainers":[{"name":"codedredd","email":"gregor@codedredd.de"}],"homepage":"https://github.com/CodeDredd/h3-compression#readme","bugs":{"url":"https://github.com/CodeDredd/h3-compression/issues"},"dist":{"shasum":"2d5e803a0a51fabf32deb34fa82e108577f6922b","tarball":"https://registry.npmjs.org/h3-compression/-/h3-compression-0.3.2.tgz","fileCount":8,"integrity":"sha512-B+yCKyDRnO0BXSfjAP4tCXJgJwmnKp3GyH5Yh66mY9KuOCrrGQSPk/gBFG2TgH7OyB/6mvqNZ1X0XNVuy0qRsw==","signatures":[{"sig":"MEUCIQCGTRTeJDvUahp0Nkr7zgtAwTIaHRck/drAGfGKZv8YYgIgePfnlsxr8sPyxArE/0LMhpd0M4F59QkKzmHR6hfbGaQ=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":21150},"main":"./dist/index.mjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"}},"funding":"https://github.com/sponsors/codedredd","gitHead":"b1a87319784db234f5ecdbab8124e40312467c70","scripts":{"dev":"unbuild --stub","lint":"eslint .","test":"vitest","build":"unbuild","start":"esno src/index.ts","prepare":"simple-git-hooks","release":"bumpp && npm publish","test:types":"vitest typecheck","prepublishOnly":"nr build"},"_npmUser":{"name":"codedredd","email":"gregor@codedredd.de"},"repository":{"url":"git+https://github.com/CodeDredd/h3-compression.git","type":"git"},"_npmVersion":"9.5.1","description":"Adds compression to h3 request (brotli, gzip, deflate)","directories":{},"lint-staged":{"*":"eslint --fix"},"sideEffects":false,"_nodeVersion":"18.16.0","typesVersions":{"*":{"*":["./dist/*","./dist/index.d.ts"]}},"_hasShrinkwrap":false,"packageManager":"pnpm@8.7.0","devDependencies":{"h3":"^1.8.1","esno":"^0.17.0","pnpm":"^8.7.0","vite":"^4.4.9","bumpp":"^9.2.0","eslint":"^8.48.0","rimraf":"^5.0.1","vitest":"^0.34.3","unbuild":"^2.0.0","@antfu/ni":"^0.21.6","supertest":"^6.3.3","typescript":"^5.2.2","@types/node":"^20.5.7","lint-staged":"^14.0.1","@antfu/utils":"^0.7.6","@types/supertest":"^2.0.12","simple-git-hooks":"^2.9.0","node-fetch-native":"^1.4.0","@vitest/coverage-v8":"^0.34.3","@antfu/eslint-config":"^0.41.0"},"peerDependencies":{"h3":"^1.6.0"},"simple-git-hooks":{"pre-commit":"pnpm lint-staged"},"_npmOperationalInternal":{"tmp":"tmp/h3-compression_0.3.2_1693607261904_0.7178934594780038","host":"s3://npm-registry-packages"}},"1.0.0":{"name":"h3-compression","version":"1.0.0","keywords":["h3","h3-compression","gzip","brotli","deflate","compression"],"author":{"name":"Gregor Becker","email":"gregor@codedredd.de"},"license":"MIT","_id":"h3-compression@1.0.0","maintainers":[{"name":"codedredd","email":"gregor@codedredd.de"}],"homepage":"https://github.com/CodeDredd/h3-compression#readme","bugs":{"url":"https://github.com/CodeDredd/h3-compression/issues"},"dist":{"shasum":"f02d4a10ac74c91c5d340769d010aeefcfcf48d7","tarball":"https://registry.npmjs.org/h3-compression/-/h3-compression-1.0.0.tgz","fileCount":8,"integrity":"sha512-vRNhPwet3mtJfqHjya9Ot3jE/3dBeY/IVt3h53pLk5iCtu5xfuGRaWZwteO28e7jQFRtbI7O9VkPWIHvsLefQQ==","signatures":[{"sig":"MEUCIQCcm1bLxrXKq4JAkcm2j7uEYwa+y+iWviozsZSt6jqiBAIgeqalT+NbvVr5EPqaIPDXA4SFF9aXn+pv6hq4oyY51l8=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":40749},"main":"./dist/index.mjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"}},"funding":"https://github.com/sponsors/codedredd","gitHead":"03afc65136787e0b8a9c9e892728cdd0e4eb2ea2","scripts":{"dev":"unbuild --stub","lint":"eslint .","test":"vitest","build":"unbuild","start":"esno src/index.ts","prepare":"simple-git-hooks","release":"bumpp && npm publish","test:types":"vitest typecheck","prepublishOnly":"nr build"},"_npmUser":{"name":"codedredd","email":"gregor@codedredd.de"},"repository":{"url":"git+https://github.com/CodeDredd/h3-compression.git","type":"git"},"_npmVersion":"9.6.7","description":"Adds compression to h3 request (brotli, gzip, deflate)","directories":{},"lint-staged":{"*":"eslint --fix"},"sideEffects":false,"_nodeVersion":"18.17.0","typesVersions":{"*":{"*":["./dist/*","./dist/index.d.ts"]}},"_hasShrinkwrap":false,"packageManager":"pnpm@8.7.0","devDependencies":{"h3":"2.0.1-rc.22","esno":"^0.17.0","pnpm":"^8.7.0","vite":"^4.4.9","bumpp":"^9.2.0","eslint":"^8.48.0","rimraf":"^5.0.1","vitest":"^0.34.3","unbuild":"^2.0.0","@antfu/ni":"^0.21.6","supertest":"^6.3.3","typescript":"^5.2.2","@types/node":"^20.5.7","lint-staged":"^14.0.1","@antfu/utils":"^0.7.6","@types/supertest":"^2.0.12","simple-git-hooks":"^2.9.0","node-fetch-native":"^1.4.0","@vitest/coverage-v8":"^0.34.3","@antfu/eslint-config":"^0.41.0"},"peerDependencies":{"h3":"^1.6.0 || ^2.0.0"},"simple-git-hooks":{"pre-commit":"pnpm lint-staged"},"_npmOperationalInternal":{"tmp":"tmp/h3-compression_1.0.0_1781000212905_0.2725878700075415","host":"s3://npm-registry-packages-npm-production"}},"1.0.1":{"name":"h3-compression","version":"1.0.1","keywords":["h3","h3-compression","gzip","brotli","deflate","compression"],"author":{"name":"Gregor Becker","email":"gregor@codedredd.de"},"license":"MIT","_id":"h3-compression@1.0.1","maintainers":[{"name":"codedredd","email":"gregor@codedredd.de"}],"homepage":"https://github.com/CodeDredd/h3-compression#readme","bugs":{"url":"https://github.com/CodeDredd/h3-compression/issues"},"dist":{"shasum":"a9b880dc0f8941e0987d44b42d898543f02d5cbe","tarball":"https://registry.npmjs.org/h3-compression/-/h3-compression-1.0.1.tgz","fileCount":8,"integrity":"sha512-JNQSvdy4RDNzI+GVQNoRRga2qtP0fp7ykOmJ2hT5ydkV/mCdpLqyhy3rAKh69TsBrXjahqHkhweegD2S2GOaVA==","signatures":[{"sig":"MEUCICVbQZkeBAQn2Tfo9n2JPCCio4fZfmMq0lscalVXZuHvAiEAzgteHRqeeoimr3Te5Vlkv/O+cPpub0UCcYJS+QQ4Isg=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":40892},"main":"./dist/index.mjs","type":"module","types":"./dist/index.d.ts","module":"./dist/index.mjs","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"}},"funding":"https://github.com/sponsors/codedredd","gitHead":"018a02b6b025a5c6de8ba6a632cbeacb7d6e9ed2","scripts":{"dev":"unbuild --stub","lint":"eslint .","test":"vitest","build":"unbuild","start":"esno src/index.ts","prepare":"simple-git-hooks","release":"bumpp && npm publish","test:types":"vitest typecheck","prepublishOnly":"nr build"},"_npmUser":{"name":"codedredd","email":"gregor@codedredd.de"},"repository":{"url":"git+https://github.com/CodeDredd/h3-compression.git","type":"git"},"_npmVersion":"9.6.7","description":"Adds compression to h3 request (brotli, gzip, deflate)","directories":{},"lint-staged":{"*":"eslint --fix"},"sideEffects":false,"_nodeVersion":"18.17.0","typesVersions":{"*":{"*":["./dist/*","./dist/index.d.ts"]}},"_hasShrinkwrap":false,"packageManager":"pnpm@8.7.0","devDependencies":{"h3":"2.0.1-rc.22","esno":"^0.17.0","pnpm":"^8.7.0","vite":"^4.4.9","bumpp":"^9.2.0","eslint":"^8.48.0","rimraf":"^5.0.1","rollup":"3.28.1","vitest":"^0.34.3","unbuild":"^2.0.0","@antfu/ni":"^0.21.6","supertest":"^6.3.3","typescript":"^5.2.2","@types/node":"^20.5.7","lint-staged":"^14.0.1","@antfu/utils":"^0.7.6","@types/supertest":"^2.0.12","simple-git-hooks":"^2.9.0","node-fetch-native":"^1.4.0","@vitest/coverage-v8":"^0.34.3","@antfu/eslint-config":"^0.41.0"},"peerDependencies":{"h3":"^1.6.0 || ^2.0.0"},"simple-git-hooks":{"pre-commit":"pnpm lint-staged"},"_npmOperationalInternal":{"tmp":"tmp/h3-compression_1.0.1_1781070584981_0.5893825117218945","host":"s3://npm-registry-packages-npm-production"}},"1.1.0":{"name":"h3-compression","type":"module","version":"1.1.0","packageManager":"pnpm@8.7.0","description":"Adds compression to h3 request (brotli, gzip, deflate, zstd)","author":{"name":"Gregor Becker","email":"gregor@codedredd.de"},"license":"MIT","funding":"https://github.com/sponsors/codedredd","homepage":"https://github.com/CodeDredd/h3-compression#readme","repository":{"type":"git","url":"git+https://github.com/CodeDredd/h3-compression.git"},"bugs":{"url":"https://github.com/CodeDredd/h3-compression/issues"},"keywords":["h3","h3-compression","gzip","brotli","deflate","zstd","compression"],"sideEffects":false,"exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.cjs"},"./nuxt":{"types":"./dist/nuxt.d.ts","import":"./dist/nuxt.mjs","require":"./dist/nuxt.cjs"},"./nuxt-runtime":{"types":"./dist/runtime/nitro-plugin.d.ts","import":"./dist/runtime/nitro-plugin.mjs"},"./package.json":"./package.json"},"main":"./dist/index.mjs","module":"./dist/index.mjs","types":"./dist/index.d.ts","typesVersions":{"*":{"*":["./dist/*","./dist/index.d.ts"]}},"engines":{"node":">=20.11.1"},"scripts":{"build":"unbuild","dev":"unbuild --stub","lint":"eslint .","prepublishOnly":"nr build","release":"bumpp && npm publish","start":"esno src/index.ts","test":"vitest","test:types":"vitest typecheck","prepare":"simple-git-hooks"},"peerDependencies":{"@nuxt/kit":"^3.0.0 || ^4.0.0","h3":"^1.6.0 || ^2.0.0"},"peerDependenciesMeta":{"@nuxt/kit":{"optional":true}},"devDependencies":{"@antfu/eslint-config":"^0.41.0","@antfu/ni":"^0.21.6","@antfu/utils":"^0.7.6","@nuxt/kit":"^3.13.0","@nuxt/schema":"3.21.11","@types/node":"^22.20.1","@types/supertest":"^2.0.12","@vitest/coverage-v8":"^0.34.3","bumpp":"^9.2.0","eslint":"^8.48.0","esno":"^0.17.0","h3":"2.0.1-rc.22","lint-staged":"^14.0.1","node-fetch-native":"^1.4.0","pnpm":"^8.7.0","rimraf":"^5.0.1","rollup":"3.28.1","simple-git-hooks":"^2.9.0","supertest":"^6.3.3","typescript":"^5.2.2","unbuild":"^2.0.0","vite":"^4.4.9","vitest":"^0.34.3"},"simple-git-hooks":{"pre-commit":"pnpm lint-staged"},"lint-staged":{"*":"eslint --fix"},"_id":"h3-compression@1.1.0","gitHead":"dab4d75410aef18caf6f9e7ffd2a0d9d2eddb4ca","_nodeVersion":"22.14.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-30+lmvUAAdfKXoEO3vWcFBMseCwn3gGI5uf95d1qussvwuvVBuIQBhphKd74UNBZ1IFcQL/qpUIQV9JgUwV0Hw==","shasum":"a66f1f46ce6f199c01d6bacd3b0505ccc11d7575","tarball":"https://registry.npmjs.org/h3-compression/-/h3-compression-1.1.0.tgz","fileCount":17,"unpackedSize":90598,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCvE1rll//mOTFrRncz0j0BxCmtdErUwHXlwaHLI0i7DQIhAIMKMBkKvcOWMu7lsBIGeVz1tFI1b8Ck1WOlqLg83xfM"}]},"_npmUser":{"name":"codedredd","email":"gregor@codedredd.de"},"directories":{},"maintainers":[{"name":"codedredd","email":"gregor@codedredd.de"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/h3-compression_1.1.0_1786009938673_0.6959801425583789"},"_hasShrinkwrap":false}},"time":{"created":"2023-08-30T11:10:55.630Z","modified":"2026-08-06T09:52:18.990Z","0.3.1":"2023-08-30T11:10:55.763Z","0.3.2":"2023-09-01T22:27:42.081Z","1.0.0":"2026-06-09T10:16:53.059Z","1.0.1":"2026-06-10T05:49:45.144Z","1.1.0":"2026-08-06T09:52:18.836Z"},"bugs":{"url":"https://github.com/CodeDredd/h3-compression/issues"},"author":{"name":"Gregor Becker","email":"gregor@codedredd.de"},"license":"MIT","homepage":"https://github.com/CodeDredd/h3-compression#readme","keywords":["h3","h3-compression","gzip","brotli","deflate","zstd","compression"],"repository":{"type":"git","url":"git+https://github.com/CodeDredd/h3-compression.git"},"description":"Adds compression to h3 request (brotli, gzip, deflate, zstd)","maintainers":[{"name":"codedredd","email":"gregor@codedredd.de"}],"readme":"# H3-compression\n\n[![npm version][npm-version-src]][npm-version-href]\n[![npm downloads][npm-downloads-src]][npm-downloads-href]\n[![bundle][bundle-src]][bundle-href]\n[![JSDocs][jsdocs-src]][jsdocs-href]\n[![License][license-src]][license-href]\n\n> Handles compression for H3\n\n## Features\n\n✔️ &nbsp;**Zlib Compression:** You can use zlib compression (brotli, gzip, deflate and opt-in zstd)\n\n✔️ &nbsp;**Stream Compression:** You can use stream compressions (gzip, deflate and opt-in brotli / zstd)\n\n✔️ &nbsp;**Compression Detection:** It uses the best compression which is accepted\n\n✔️ &nbsp;**h3 v1 & v2:** Works with both [h3](https://h3.dev) v1 and v2\n\n✔️ &nbsp;**Nuxt module:** Drop `h3-compression/nuxt` into `nuxt.config` and configure it there\n\n\n\n## Install\n\n```bash\n# Using npm\nnpm install h3-compression\n\n# Using yarn\nyarn add h3-compression\n\n# Using pnpm\npnpm add h3-compression\n```\n\n## Usage (h3 v1)\n\n```ts\nimport { createServer } from 'node:http'\nimport { createApp, eventHandler, toNodeListener } from 'h3'\nimport { useCompressionStream } from 'h3-compression'\n\nconst app = createApp({ onBeforeResponse: useCompressionStream }) // or { onBeforeResponse: useCompression }\napp.use(\n  '/',\n  eventHandler(() => 'Hello world!'),\n)\n\ncreateServer(toNodeListener(app)).listen(process.env.PORT || 3000)\n```\n\nExample using <a href=\"https://github.com/unjs/listhen\">listhen</a> for an elegant listener:\n\n```ts\nimport { createApp, eventHandler, toNodeListener } from 'h3'\nimport { listen } from 'listhen'\nimport { useCompressionStream } from 'h3-compression'\n\nconst app = createApp({ onBeforeResponse: useCompressionStream }) // or { onBeforeResponse: useCompression }\napp.use(\n  '/',\n  eventHandler(() => 'Hello world!'),\n)\n\nlisten(toNodeListener(app))\n```\n\n## Usage (h3 v2)\n\nIn [h3 v2](https://h3.dev) the response is an immutable web `Response` and the `onBeforeResponse`\nhook was removed. Use the `compression` / `compressionStream` middleware instead — they read the\nresponse returned by the next handler and replace it with a compressed one.\n\n```ts\nimport { createServer } from 'node:http'\nimport { H3, toNodeHandler } from 'h3'\nimport { compression } from 'h3-compression'\n\nconst app = new H3()\n\napp.use(compression()) // or app.use(compressionStream())\napp.get('/', () => 'Hello world!')\n\ncreateServer(toNodeHandler(app)).listen(process.env.PORT || 3000)\n```\n\nYou can also force a specific method (e.g. `compression('gzip')`) instead of detecting it from the\n`Accept-Encoding` header.\n\n## Brotli and stream compression\n\nThe native [`CompressionStream`](https://developer.mozilla.org/en-US/docs/Web/API/CompressionStream)\nimplements the WHATWG `CompressionFormat` enum, which only defines `gzip`, `deflate` and\n`deflate-raw` — there is no brotli format. This package therefore streams brotli through\n`node:zlib` instead, so it is available wherever `node:zlib` is (Node, and runtimes with node\ncompatibility), but not on pure edge runtimes.\n\nBecause brotli is noticeably more CPU-expensive per request than gzip, it is **never picked\nautomatically**. Turn it on with the `brotli` flag, or force it as the method:\n\n```ts\napp.use(compressionStream()) // gzip / deflate — unchanged default\napp.use(compressionStream({ brotli: true })) // brotli, then gzip, then deflate\napp.use(compressionStream('br')) // always brotli\n```\n\nThe same flag works for the composable:\n\n```ts\nawait useCompressionStream(event, response, { brotli: true })\n// or explicitly\nawait useBrotliCompressionStream(event, response)\n```\n\n> [!NOTE]\n> The brotli stream is flushed per chunk (`BROTLI_OPERATION_FLUSH`) so that streamed responses\n> stay streamed. With zlib's defaults brotli buffers the whole body until the source closes.\n\n## Zstd\n\nZstd is supported on both paths, and is opt-in for a different reason than brotli: `node:zlib`\nonly gained zstd in **Node 22.15.0** (and 23.8.0). Enabling it by default would make the\nnegotiated `Content-Encoding` depend on the Node version the app happens to run on, which is a\npoor thing to discover in production. The package itself only requires Node >= 20.11.1, the same\nfloor as h3.\n\n```ts\napp.use(compression({ zstd: true })) // zstd, then brotli, then gzip, then deflate\napp.use(compressionStream({ zstd: true, brotli: true })) // same order, streamed\n\nawait useCompression(event, response, { zstd: true })\n```\n\nBehaviour on a runtime without zstd:\n\n- with the `zstd: true` **flag**, zstd is skipped during negotiation and the next accepted\n  encoding is used — no error, no special-casing needed in your code\n- when **forced** (`compression('zstd')`, `useZstdCompression`), a `TypeError` naming the\n  required Node version is thrown, because silently sending something else would be worse\n\nBranch on it yourself with the exported predicate:\n\n```ts\nimport { isZstdSupported } from 'h3-compression'\n\napp.use(compression({ zstd: isZstdSupported() }))\n```\n\n## Nuxt 3 & 4\n\nAdd the module and you're done — it wires the right Nitro hooks, skips Nuxt's internal\nroutes and filters by content type for you:\n\n```ts\n// nuxt.config.ts\nexport default defineNuxtConfig({\n  modules: ['h3-compression/nuxt'],\n})\n```\n\nEverything is configurable under the `compression` key:\n\n```ts\nexport default defineNuxtConfig({\n  modules: ['h3-compression/nuxt'],\n  compression: {\n    enabled: true,\n    encoding: 'zlib', // or 'stream'\n    brotli: false, // stream path only — zlib always prefers brotli\n    zstd: false, // needs Node >= 22.15\n    method: undefined, // force one method instead of negotiating\n    contentTypes: ['text/', 'application/json', 'application/javascript', 'application/xml', 'image/svg+xml'],\n    exclude: ['/_nuxt', '/__nuxt'],\n    routeRules: true, // also compress cached (swr/isr) routes and /server/api\n    threshold: 0, // skip bodies smaller than this many bytes\n  },\n})\n```\n\n| Option | Default | What it does |\n| --- | --- | --- |\n| `enabled` | `true` | Turn compression off without removing the module |\n| `encoding` | `'zlib'` | `'zlib'` buffers the body; `'stream'` pipes it through a compression transform |\n| `brotli` | `false` | Consider brotli when negotiating. Only meaningful for `'stream'` — the zlib path already prefers brotli |\n| `zstd` | `false` | Consider zstd when negotiating. Ignored on Node < 22.15, see [Zstd](#zstd) |\n| `method` | – | Force one method instead of negotiating from `Accept-Encoding` |\n| `contentTypes` | text, JSON, JS, XML, SVG | Prefix match against `Content-Type`. Set to `[]` to compress everything |\n| `exclude` | `['/_nuxt', '/__nuxt']` | Path prefixes to skip. Compressing these breaks Nuxt's error page |\n| `routeRules` | `true` | Also attach to `beforeResponse`, which is what cached (`swr`/`isr`) routes and `/server/api` handlers go through |\n| `threshold` | `0` | Skip bodies below this size — under roughly a kilobyte compression makes payloads *larger*. Ignored for `'stream'`, where the size is not known up front |\n\n> [!NOTE]\n> `contentTypes` and `exclude` **replace** the defaults rather than extending them.\n> Spread the defaults in if you want to add to them.\n\n### Doing it manually\n\nThe module is a convenience wrapper — the hooks are still yours to wire if you want\ndifferent behaviour per route:\n\n`server/plugins/compression.ts`\n````ts\nimport { useCompression } from 'h3-compression'\n\nexport default defineNitroPlugin((nitro) => {\n  // Freshly rendered SSR pages.\n  nitro.hooks.hook('render:response', async (response, { event }) => {\n    // Skip internal nuxt routes (e.g. error page)\n    if (['/_nuxt', '/__nuxt'].some(prefix => getRequestURL(event).pathname.startsWith(prefix)))\n      return\n\n    if (!response.headers?.['content-type']?.startsWith('text/html'))\n      return\n\n    await useCompression(event, response)\n  })\n\n  // The `render:response` hook only runs for freshly rendered SSR pages.\n  // Responses served from the Nitro route cache (`routeRules` with `swr` / `isr`)\n  // and `/server/api` handlers go through `beforeResponse` instead.\n  nitro.hooks.hook('beforeResponse', async (event, response) => {\n    if (['/_nuxt', '/__nuxt'].some(prefix => event.path.startsWith(prefix)))\n      return\n\n    await useCompression(event, response)\n  })\n})\n````\n\n`useCompression` compresses string, `Buffer`/`Uint8Array` and JSON (object) bodies and\nskips everything else (e.g. streams), so binary assets are left untouched. If you only\nwant to compress specific content types, guard on `response.headers?.['content-type']`\nbefore calling it.\n\n## Utilities\n\nH3-compression has a concept of composable utilities that accept `event` (from `eventHandler((event) => {})`) as their first argument and `response` as their second.\n\n#### Zlib Compression\n\n- `useGZipCompression(event, response)`\n- `useDeflateCompression(event, response)`\n- `useBrotliCompression(event, response)`\n- `useZstdCompression(event, response)` &nbsp;– requires Node >= 22.15\n- `useCompression(event, response, options?)` &nbsp;– pass `{ zstd: true }` to include zstd\n\n#### Stream Compression\n\n- `useGZipCompressionStream(event, response)`\n- `useDeflateCompressionStream(event, response)`\n- `useBrotliCompressionStream(event, response)`\n- `useZstdCompressionStream(event, response)` &nbsp;– requires Node >= 22.15\n- `useCompressionStream(event, response, options?)` &nbsp;– pass `{ brotli: true }` / `{ zstd: true }`\n\n#### Middleware (h3 v2)\n\n- `compression(method | options?)` &nbsp;– middleware using zlib (brotli, gzip, deflate, opt-in zstd)\n- `compressionStream(method | options?)` &nbsp;– stream middleware (gzip, deflate, opt-in brotli / zstd)\n- `compressResponse(event, value, method?, options?)` &nbsp;– low-level helper returning a compressed `Response`\n- `compressResponseStream(event, value, method?, options?)` &nbsp;– low-level stream helper returning a compressed `Response`\n- `isZstdSupported()` &nbsp;– whether the runtime can compress with zstd\n\n#### Nuxt\n\n- `h3-compression/nuxt` &nbsp;– the Nuxt module, configured under the `compression` key\n\n## Sponsors\n\n<p align=\"center\">\n  <a href=\"https://pinia-orm.codedredd.de/sponsorkit/sponsors.png\">\n    <img src='https://pinia-orm.codedredd.de/sponsorkit/sponsors.svg'/>\n  </a>\n</p>\n\n## Releated Projects\n\n- [H3](https://github.com/unjs/h3)\n\n## License\n\n[MIT](./LICENSE) License © 2023-PRESENT [Gregor Becker](https://github.com/CodeDredd)\n\n\n<!-- Badges -->\n\n[npm-version-src]: https://img.shields.io/npm/v/h3-compression?style=flat&colorA=080f12&colorB=1fa669\n[npm-version-href]: https://npmjs.com/package/h3-compression\n[npm-downloads-src]: https://img.shields.io/npm/dm/h3-compression?style=flat&colorA=080f12&colorB=1fa669\n[npm-downloads-href]: https://npmjs.com/package/h3-compression\n[bundle-src]: https://img.shields.io/bundlephobia/minzip/h3-compression?style=flat&colorA=080f12&colorB=1fa669&label=minzip\n[bundle-href]: https://bundlephobia.com/result?p=h3-compression\n[license-src]: https://img.shields.io/github/license/CodeDredd/h3-compression.svg?style=flat&colorA=080f12&colorB=1fa669\n[license-href]: https://github.com/CodeDredd/h3-compression/blob/main/LICENSE\n[jsdocs-src]: https://img.shields.io/badge/jsdocs-reference-080f12?style=flat&colorA=080f12&colorB=1fa669\n[jsdocs-href]: https://www.jsdocs.io/package/h3-compression\n","readmeFilename":"README.md"}