{"_id":"@aeolun/compress","_rev":"4-25683ba66940e4d4518d1d29db6699ce","name":"@aeolun/compress","dist-tags":{"latest":"8.0.6"},"versions":{"8.0.3":{"name":"@aeolun/compress","version":"8.0.3","keywords":["fastify","compression","deflate","gzip","brotli"],"author":{"url":"http://delved.org","name":"Tomas Della Vedova - @delvedor"},"license":"MIT","_id":"@aeolun/compress@8.0.3","maintainers":[{"name":"aeolun","email":"bart@serial-experiments.com"}],"contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Manuel Spigolon","email":"behemoth89@gmail.com"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"homepage":"https://github.com/fastify/fastify-compress#readme","bugs":{"url":"https://github.com/fastify/fastify-compress/issues"},"tsd":{"directory":"test/types"},"dist":{"shasum":"e8ea5ad5f2c31c1f6350021df39543ccb880b72e","tarball":"https://registry.npmjs.org/@aeolun/compress/-/compress-8.0.3.tgz","fileCount":19,"integrity":"sha512-q2gXoGfzRQUESuXhE92Y8SZdlquOtThD/PM2GYmqC4Fl8uLhPErkLe8ymVjwsnh06f2C14kda7HOEGFdl0HP4A==","signatures":[{"sig":"MEYCIQC2dijDDkEXOLx9e70p+zbkNCvPFK8q9z01AwLCUq8fEAIhAPXzHR+9G/MDlw2t4hilS25X86dki2AZunN5xS93j2jj","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":183291},"main":"index.js","type":"commonjs","_from":"file:aeolun-compress-8.0.3.tgz","types":"types/index.d.ts","funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"scripts":{"lint":"eslint","test":"npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","test:unit":"node --test","test:coverage":"c8 node --test && c8 report --reporter=html","test:typescript":"tsd","test:unit:verbose":"npm run test:unit -- -Rspec"},"_npmUser":{"name":"aeolun","actor":{"name":"aeolun","type":"user","email":"bart@serial-experiments.com"},"email":"bart@serial-experiments.com"},"_resolved":"/private/var/folders/cq/0v7l3m1d42d8358j09j7x4500000gq/T/abf5e7e5622f1b530eb9c209bbd5c937/aeolun-compress-8.0.3.tgz","_integrity":"sha512-q2gXoGfzRQUESuXhE92Y8SZdlquOtThD/PM2GYmqC4Fl8uLhPErkLe8ymVjwsnh06f2C14kda7HOEGFdl0HP4A==","repository":{"url":"git+https://github.com/fastify/fastify-compress.git","type":"git"},"_npmVersion":"10.9.2","description":"Fastify compression utils","directories":{},"_nodeVersion":"22.16.0","dependencies":{"pump":"^3.0.0","mime-db":"^1.52.0","pumpify":"^2.0.1","minipass":"^7.0.4","peek-stream":"^1.1.3","fastify-plugin":"^5.0.0","readable-stream":"^4.5.2","@fastify/accept-negotiator":"^2.0.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","tsd":"^0.32.0","eslint":"^9.17.0","adm-zip":"^0.5.12","fastify":"^5.0.0","jsonstream":"^1.0.3","typescript":"~5.8.3","@types/node":"^22.0.0","neostandard":"^0.12.0","@fastify/pre-commit":"^2.1.0"},"_npmOperationalInternal":{"tmp":"tmp/compress_8.0.3_1750227979366_0.06399942141030035","host":"s3://npm-registry-packages-npm-production"}},"8.0.4":{"name":"@aeolun/compress","version":"8.0.4","keywords":["fastify","compression","deflate","gzip","brotli"],"author":{"url":"http://delved.org","name":"Tomas Della Vedova - @delvedor"},"license":"MIT","_id":"@aeolun/compress@8.0.4","maintainers":[{"name":"aeolun","email":"bart@serial-experiments.com"}],"contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Manuel Spigolon","email":"behemoth89@gmail.com"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"homepage":"https://github.com/fastify/fastify-compress#readme","bugs":{"url":"https://github.com/fastify/fastify-compress/issues"},"tsd":{"directory":"test/types"},"dist":{"shasum":"68d97210a6ce855eeb135ef5c30fa51e1f281ddc","tarball":"https://registry.npmjs.org/@aeolun/compress/-/compress-8.0.4.tgz","fileCount":19,"integrity":"sha512-tFnISEPQmQgjMHnHfw1CCegIq6+GTjV8phrWNSfIyLJTWQrmQs7Of9SlPTT6GM5K+Yt2q0OgwJFZUMKjU5EW/g==","signatures":[{"sig":"MEUCIQCU93bgLzEGPAPge1CpQwo2x1cWC5bMFLZORtkI1s/e0wIgKH5gktZN1L61jHkjUVrlB3gALXu7PDQkrsWzA1BI+8Y=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":183359},"main":"index.js","type":"commonjs","_from":"file:aeolun-compress-8.0.4.tgz","types":"types/index.d.ts","funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"scripts":{"lint":"eslint","test":"npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","test:unit":"node --test","test:coverage":"c8 node --test && c8 report --reporter=html","test:typescript":"tsd","test:unit:verbose":"npm run test:unit -- -Rspec"},"_npmUser":{"name":"aeolun","actor":{"name":"aeolun","type":"user","email":"bart@serial-experiments.com"},"email":"bart@serial-experiments.com"},"_resolved":"/private/var/folders/cq/0v7l3m1d42d8358j09j7x4500000gq/T/c1cdf563b33defaf9cf7107392db1c2a/aeolun-compress-8.0.4.tgz","_integrity":"sha512-tFnISEPQmQgjMHnHfw1CCegIq6+GTjV8phrWNSfIyLJTWQrmQs7Of9SlPTT6GM5K+Yt2q0OgwJFZUMKjU5EW/g==","repository":{"url":"git+https://github.com/fastify/fastify-compress.git","type":"git"},"_npmVersion":"10.9.2","description":"Fastify compression utils","directories":{},"_nodeVersion":"22.16.0","dependencies":{"pump":"^3.0.0","mime-db":"^1.52.0","pumpify":"^2.0.1","minipass":"^7.0.4","peek-stream":"^1.1.3","fastify-plugin":"^5.0.0","readable-stream":"^4.5.2","@fastify/accept-negotiator":"^2.0.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","tsd":"^0.32.0","eslint":"^9.17.0","adm-zip":"^0.5.12","fastify":"^5.0.0","jsonstream":"^1.0.3","typescript":"~5.8.3","@types/node":"^22.0.0","neostandard":"^0.12.0","@fastify/pre-commit":"^2.1.0"},"_npmOperationalInternal":{"tmp":"tmp/compress_8.0.4_1750228572587_0.06999355631725868","host":"s3://npm-registry-packages-npm-production"}},"8.0.5":{"name":"@aeolun/compress","version":"8.0.5","keywords":["fastify","compression","deflate","gzip","brotli"],"author":{"url":"http://delved.org","name":"Tomas Della Vedova - @delvedor"},"license":"MIT","_id":"@aeolun/compress@8.0.5","maintainers":[{"name":"aeolun","email":"bart@serial-experiments.com"}],"contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Manuel Spigolon","email":"behemoth89@gmail.com"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"homepage":"https://github.com/fastify/fastify-compress#readme","bugs":{"url":"https://github.com/fastify/fastify-compress/issues"},"tsd":{"directory":"test/types"},"dist":{"shasum":"a3c3ab96998b64b08678fc3e82f29f34a3346ac2","tarball":"https://registry.npmjs.org/@aeolun/compress/-/compress-8.0.5.tgz","fileCount":19,"integrity":"sha512-mEni+NnZSDFMWoRlfD4ltZvYAiah+dXIom4JSQiqrvxl03B4J2Sjxjk9u5+eSE8RkSQtD1/Be6fFLj6ATqtLEg==","signatures":[{"sig":"MEQCIDoYoxI8IqmY1Mu9Bg7cc3duHsaBodzkPC+9HYdQpiyEAiBKYsWWxFymVdfTq4QHlyxqz4Vy3+cQWFfIm69jcecTzA==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":191597},"main":"index.js","type":"commonjs","_from":"file:aeolun-compress-8.0.5.tgz","types":"types/index.d.ts","funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"scripts":{"lint":"eslint","test":"npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","test:unit":"node --test","test:coverage":"c8 node --test && c8 report --reporter=html","test:typescript":"tsd","test:unit:verbose":"npm run test:unit -- -Rspec"},"_npmUser":{"name":"aeolun","actor":{"name":"aeolun","type":"user","email":"bart@serial-experiments.com"},"email":"bart@serial-experiments.com"},"_resolved":"/private/var/folders/cq/0v7l3m1d42d8358j09j7x4500000gq/T/a35555cca39148dbc4ea16512205dc97/aeolun-compress-8.0.5.tgz","_integrity":"sha512-mEni+NnZSDFMWoRlfD4ltZvYAiah+dXIom4JSQiqrvxl03B4J2Sjxjk9u5+eSE8RkSQtD1/Be6fFLj6ATqtLEg==","repository":{"url":"git+https://github.com/fastify/fastify-compress.git","type":"git"},"_npmVersion":"10.9.2","description":"Fastify compression utils","directories":{},"_nodeVersion":"22.16.0","dependencies":{"pump":"^3.0.0","mime-db":"^1.52.0","pumpify":"^2.0.1","minipass":"^7.0.4","peek-stream":"^1.1.3","fastify-plugin":"^5.0.0","readable-stream":"^4.5.2","@fastify/accept-negotiator":"^2.0.0"},"publishConfig":{"access":"public"},"_hasShrinkwrap":false,"devDependencies":{"c8":"^10.1.2","tsd":"^0.32.0","eslint":"^9.17.0","adm-zip":"^0.5.12","fastify":"^5.0.0","jsonstream":"^1.0.3","typescript":"~5.8.3","@types/node":"^22.0.0","neostandard":"^0.12.0","@fastify/pre-commit":"^2.1.0"},"_npmOperationalInternal":{"tmp":"tmp/compress_8.0.5_1750298764508_0.013499002495454215","host":"s3://npm-registry-packages-npm-production"}},"8.0.6":{"name":"@aeolun/compress","version":"8.0.6","description":"Fastify compression utils","main":"index.js","type":"commonjs","types":"types/index.d.ts","dependencies":{"@fastify/accept-negotiator":"^2.0.0","fastify-plugin":"^5.0.0","mime-db":"^1.52.0","minipass":"^7.0.4","peek-stream":"^1.1.3","pump":"^3.0.0","pumpify":"^2.0.1","readable-stream":"^4.5.2"},"devDependencies":{"@fastify/pre-commit":"^2.1.0","@types/node":"^22.0.0","adm-zip":"^0.5.12","c8":"^10.1.2","eslint":"^9.17.0","fastify":"^5.0.0","jsonstream":"^1.0.3","neostandard":"^0.12.0","tsd":"^0.32.0","typescript":"~5.8.3"},"keywords":["fastify","compression","deflate","gzip","brotli"],"author":{"name":"Tomas Della Vedova - @delvedor","url":"http://delved.org"},"contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Manuel Spigolon","email":"behemoth89@gmail.com"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"name":"Frazer Smith","email":"frazer.dev@icloud.com","url":"https://github.com/fdawgs"}],"license":"MIT","bugs":{"url":"https://github.com/fastify/fastify-compress/issues"},"homepage":"https://github.com/fastify/fastify-compress#readme","funding":[{"type":"github","url":"https://github.com/sponsors/fastify"},{"type":"opencollective","url":"https://opencollective.com/fastify"}],"repository":{"type":"git","url":"git+https://github.com/fastify/fastify-compress.git"},"tsd":{"directory":"test/types"},"publishConfig":{"access":"public"},"scripts":{"lint":"eslint","lint:fix":"eslint --fix","test":"npm run test:unit && npm run test:typescript","test:typescript":"tsd","test:unit":"node --test","test:coverage":"c8 node --test && c8 report --reporter=html","test:unit:verbose":"npm run test:unit -- -Rspec"},"_id":"@aeolun/compress@8.0.6","_integrity":"sha512-9PhNuczMOm16LDPrvFvS7qMnjjoTV0rBbH8/idzjO2YtQ29TKbpW2e3wnfGeRqxe96jciXhr8meGhFKilwpHzA==","_resolved":"/private/var/folders/cq/0v7l3m1d42d8358j09j7x4500000gq/T/ed0b481dd7f5a6877c7292b81bc69aeb/aeolun-compress-8.0.6.tgz","_from":"file:aeolun-compress-8.0.6.tgz","_nodeVersion":"22.16.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-9PhNuczMOm16LDPrvFvS7qMnjjoTV0rBbH8/idzjO2YtQ29TKbpW2e3wnfGeRqxe96jciXhr8meGhFKilwpHzA==","shasum":"174bef2a28d51df107db82333bfcaaefbc541f0a","tarball":"https://registry.npmjs.org/@aeolun/compress/-/compress-8.0.6.tgz","fileCount":19,"unpackedSize":192067,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQD+6ugyTT6UgUmlr3oRGNG0FwNWP1eNzAMAhXatgJD/yAIhAIjQukajq1wzwSmRJkmvbgBeUIw/ZLnPjAz7lM3mZ7MY"}]},"_npmUser":{"name":"aeolun","email":"bart@serial-experiments.com","actor":{"name":"aeolun","email":"bart@serial-experiments.com","type":"user"}},"directories":{},"maintainers":[{"name":"aeolun","email":"bart@serial-experiments.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/compress_8.0.6_1750299069451_0.13971319981401664"},"_hasShrinkwrap":false}},"time":{"created":"2025-06-18T06:26:19.286Z","modified":"2025-06-19T02:11:09.846Z","8.0.3":"2025-06-18T06:26:19.594Z","8.0.4":"2025-06-18T06:36:12.744Z","8.0.5":"2025-06-19T02:06:04.696Z","8.0.6":"2025-06-19T02:11:09.642Z"},"bugs":{"url":"https://github.com/fastify/fastify-compress/issues"},"author":{"name":"Tomas Della Vedova - @delvedor","url":"http://delved.org"},"license":"MIT","homepage":"https://github.com/fastify/fastify-compress#readme","keywords":["fastify","compression","deflate","gzip","brotli"],"repository":{"type":"git","url":"git+https://github.com/fastify/fastify-compress.git"},"description":"Fastify compression utils","contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Manuel Spigolon","email":"behemoth89@gmail.com"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"name":"Frazer Smith","email":"frazer.dev@icloud.com","url":"https://github.com/fdawgs"}],"maintainers":[{"name":"aeolun","email":"bart@serial-experiments.com"}],"readme":"# @fastify/compress\n\n[![CI](https://github.com/fastify/fastify-compress/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-compress/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/compress.svg?style=flat)](https://www.npmjs.com/package/@fastify/compress)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nAdds compression utils to [the Fastify `reply` object](https://fastify.dev/docs/latest/Reference/Reply/#reply) and a hook to decompress requests payloads.\nSupports `gzip`, `deflate`, and `brotli`.\n\n> ℹ️ Note: In large-scale scenarios, use a proxy like Nginx to handle response compression.\n\n> ⚠ Warning: Since `@fastify/compress` version 4.x, payloads compressed with the `zip` algorithm are not automatically uncompressed. This plugin focuses on response compression, and `zip` is not in the [IANA Table of Content Encodings](https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding).\n\n## Install\n```\nnpm i @fastify/compress\n```\n\n### Compatibility\n| Plugin version | Fastify version |\n| ---------------|-----------------|\n| `>=8.x`        | `^5.x`          |\n| `>=6.x <8.x`   | `^4.x`          |\n| `>=3.x <6.x`   | `^3.x`          |\n| `^2.x`         | `^2.x`          |\n| `>=0.x <2.x`   | `^1.x`          |\n\n\nPlease note that if a Fastify version is out of support, then so are the corresponding versions of this plugin\nin the table above.\nSee [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.\n\n\n## Usage - Compress replies\n\nThis plugin adds two functionalities to Fastify: a compress utility and a global compression hook.\n\nCurrently, the following encoding tokens are supported, using the first acceptable token in this order:\n\n1. `br`\n2. `gzip`\n3. `deflate`\n4. `*` (no preference — `@fastify/compress` will use `gzip`)\n5. `identity` (no compression)\n\nIf an unsupported encoding is received or the `'accept-encoding'` header is missing, the payload will not be compressed.\nTo return an error for unsupported encoding, use the `onUnsupportedEncoding` option.\n\nThe plugin compresses payloads based on `content-type`. If absent, it assumes `application/json`.\n\n### Supported payload types\n\nThe plugin supports compressing the following payload types:\n- Strings and Buffers\n- Node.js streams\n- Response objects (from the Fetch API)\n- ReadableStream objects (from the Web Streams API)\n\n### Global hook\nThe global compression hook is enabled by default. To disable it, pass `{ global: false }`:\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { global: false }\n)\n```\n\nIf only compression or decompression is required, set the `globalCompression` or `globalDecompression` config flags to `false` respectively (both are `true` by default).\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  // only decompress compressed incoming requests\n  { globalCompression: false }\n)\n```\n\nFastify encapsulation can be used to set global compression but run it only in a subset of routes by wrapping them inside a plugin.\n\n> ℹ️ Note: If using `@fastify/compress` plugin together with `@fastify/static` plugin, `@fastify/compress` must be registered (with *global hook*) **before** registering `@fastify/static`.\n\n### Per Route options\nDifferent compression options can be specified per route using the `compress` options in the route's configuration.\nSetting `compress: false` on any route will disable compression on the route even if global compression is enabled.\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { global: false }\n)\n\n// only compress if the payload is above a certain size and use brotli\nfastify.get('/custom-route', {\n  compress: {\n    inflateIfDeflated: true,\n    threshold: 128,\n    zlib: {\n      createBrotliCompress: () => createYourCustomBrotliCompress(),\n      createGzip: () => createYourCustomGzip(),\n      createDeflate: () => createYourCustomDeflate()\n    }\n  }, (req, reply) => {\n    // ...\n  })\n```\n\n### `reply.compress`\nThis plugin adds a `compress` method to `reply` that compresses a stream or string based on the `accept-encoding` header. If a JS object is passed, it will be stringified to JSON.\n\n> ℹ️ Note: When compressing a Response object, the compress middleware only extracts and compresses the body stream. It will handle compression-related headers (like `Content-Encoding` and `Vary`) but does not copy other headers or status from the Response object - these remain the responsibility of your application or Fastify's built-in handling.\n\nThe `compress` method uses per-route parameters if configured, otherwise it uses global parameters.\n\n```js\nimport fs from 'node:fs'\nimport fastify from 'fastify'\n\nconst app = fastify()\nawait app.register(import('@fastify/compress'), { global: false })\n\n// Compress a file stream\napp.get('/file', (req, reply) => {\n  reply\n    .type('text/plain')\n    .compress(fs.createReadStream('./package.json'))\n})\n\n// Compress a Response object from fetch\napp.get('/fetch', async (req, reply) => {\n  const response = await fetch('https://api.example.com/data')\n  reply\n    .type('application/json')\n    .compress(response)\n})\n\n// Compress a ReadableStream\napp.get('/stream', (req, reply) => {\n  const response = new Response('Hello World')\n  reply\n    .type('text/plain')\n    .compress(response.body)\n})\n\nawait app.listen({ port: 3000 })\n```\n\n## Compress Options\n\n### threshold\nThe minimum byte size for response compression. Defaults to `1024`.\n\n> ℹ️ Note: The threshold setting only applies to string and Buffer payloads. Streams (including Node.js streams, Response objects, and ReadableStream objects) are always compressed regardless of the threshold, as their size cannot be determined in advance.\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { threshold: 2048 }\n)\n```\n### customTypes\n[mime-db](https://github.com/jshttp/mime-db) determines if a `content-type` should be compressed. Additional content types can be compressed via regex or a function.\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { customTypes: /x-protobuf$/ }\n)\n```\n\nor\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { customTypes: contentType => contentType.endsWith('x-protobuf') }\n)\n```\n\n### onUnsupportedEncoding\nSet `onUnsupportedEncoding(encoding, request, reply)` to send a custom error response for unsupported encoding. The function can modify the reply and return a `string | Buffer | Stream | Error` payload.\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  {\n    onUnsupportedEncoding: (encoding, request, reply) => {\n      reply.code(406)\n      return 'We do not support the ' + encoding + ' encoding.'\n    }\n  }\n)\n```\n\n### Disable compression by header\nResponse compression can be disabled by an `x-no-compression` header in the request.\n\n### Inflate pre-compressed bodies for clients that do not support compression\nOptional feature to inflate pre-compressed data if the client does not include one of the supported compression types in its `accept-encoding` header.\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { inflateIfDeflated: true }\n)\n\nfastify.get('/file', (req, reply) =>\n  // will inflate the file  on the way out for clients\n  // that indicate they do not support compression\n  reply.send(fs.createReadStream('./file.gz')))\n```\n\n### Customize encoding priority\nBy default, `@fastify/compress` prioritizes compression as described [here](#usage). Change this by passing an array of compression tokens to the `encodings` option:\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  // Only support gzip and deflate, and prefer deflate to gzip\n  { encodings: ['deflate', 'gzip'] }\n)\n```\n\n### brotliOptions and zlibOptions\nCompression can be tuned with `brotliOptions` and `zlibOptions`, which are passed directly to native node `zlib` methods. See [class definitions](https://nodejs.org/api/zlib.html#zlib_class_options).\n\n```js\n  server.register(fastifyCompress, {\n    brotliOptions: {\n      params: {\n        [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT, // useful for APIs that primarily return text\n        [zlib.constants.BROTLI_PARAM_QUALITY]: 4, // default is 4, max is 11, min is 0\n      },\n    },\n    zlibOptions: {\n      level: 6, // default is typically 6, max is 9, min is 0\n    }\n  });\n```\n\n### Manage `Content-Length` header removal with removeContentLengthHeader\nBy default, `@fastify/compress` removes the reply `Content-Length` header. Change this by setting `removeContentLengthHeader` to `false` globally or per route.\n\n```js\n  // Global plugin scope\n  await server.register(fastifyCompress, { global: true, removeContentLengthHeader: false });\n\n  // Route-specific scope\n  fastify.get('/file', {\n    compress: { removeContentLengthHeader: false }\n  }, (req, reply) =>\n    reply.compress(fs.createReadStream('./file.gz'))\n  )\n```\n\n## Usage - Decompress request payloads\nThis plugin adds a `preParsing` hook to decompress the request payload based on the `content-encoding` request header.\n\nCurrently, the following encoding tokens are supported:\n\n1. `br`\n2. `gzip`\n3. `deflate`\n\nIf an unsupported encoding or invalid payload is received, the plugin throws an error.\n\nIf the request header is missing, the plugin yields to the next hook.\n\n### Global hook\n\nThe global request decompression hook is enabled by default. To disable it, pass `{ global: false }`:\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { global: false }\n)\n```\n\nFastify encapsulation can be used to set global decompression but run it only in a subset of routes by wrapping them inside a plugin.\n\n### Per Route options\n\nSpecify different decompression options per route using the `decompress` options in the route's configuration.\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  { global: false }\n)\n\n// Always decompress using gzip\nfastify.get('/custom-route', {\n  decompress: {\n    forceRequestEncoding: 'gzip',\n    zlib: {\n      createBrotliDecompress: () => createYourCustomBrotliDecompress(),\n      createGunzip: () => createYourCustomGunzip(),\n      createInflate: () => createYourCustomInflate()\n    }\n  }\n}, (req, reply) => {\n    // ...\n  })\n```\n\n### requestEncodings\n\nBy default, `@fastify/compress` accepts all encodings specified [here](#usage). Change this by passing an array of compression tokens to the `requestEncodings` option:\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  // Only support gzip\n  { requestEncodings: ['gzip'] }\n)\n```\n\n### forceRequestEncoding\n\nBy default, `@fastify/compress` chooses the decompression algorithm based on the `content-encoding` header.\n\nOne algorithm can be forced, and the header ignored, by providing the `forceRequestEncoding` option.\n\nIf the request payload is not compressed, `@fastify/compress` will try to decompress, resulting in an error.\n\n### onUnsupportedRequestEncoding\n\nThe response error can be customized for unsupported request payload encoding by setting `onUnsupportedEncoding(request, encoding)` to a function that returns an error.\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  {\n     onUnsupportedRequestEncoding: (request, encoding) => {\n      return {\n        statusCode: 415,\n        code: 'UNSUPPORTED',\n        error: 'Unsupported Media Type',\n        message: 'We do not support the ' + encoding + ' encoding.'\n      }\n    }\n  }\n)\n```\n\n### onInvalidRequestPayload\n\nThe response error can be customized for undetectable request payloads by setting `onInvalidRequestPayload(request, encoding)` to a function that returns an error.\n\n```js\nawait fastify.register(\n  import('@fastify/compress'),\n  {\n    onInvalidRequestPayload: (request, encoding, error) => {\n      return {\n        statusCode: 400,\n        code: 'BAD_REQUEST',\n        error: 'Bad Request',\n        message: 'This is not a valid ' + encoding + ' encoded payload: ' + error.message\n      }\n    }\n  }\n)\n```\n\n## Gotchas\n\n### Handling Unsupported Payload Types\n\nWhen `@fastify/compress` receives a payload type that it doesn't natively support for compression (excluding the types listed in [Supported payload types](#supported-payload-types)), the behavior depends on the compression method:\n\n- **Using `reply.compress()`**: The plugin will attempt to serialize the payload using Fastify's `serialize` function and then compress the result. This provides a best-effort approach to handle custom objects.\n\n- **Using global compression hook**: To prevent breaking applications, the plugin will pass through unsupported payload types without compression. This fail-safe approach ensures that servers continue to function even when encountering unexpected payload types.\n\n## Acknowledgments\n\nPast sponsors:\n\n- [LetzDoIt](http://www.letzdoitapp.com/)\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","readmeFilename":"README.md"}