fastify-compression
Advanced tools
Comparing version 1.0.0 to 1.1.0
14
index.js
@@ -13,3 +13,12 @@ 'use strict'; | ||
const supportedEncodings = ['gzip', 'deflate']; | ||
const compressionStreams = { | ||
gzip: zlib.createGzip, | ||
deflate: zlib.createDeflate | ||
} | ||
if (opts.brotli) { | ||
compressionStreams.br = opts.brotli.compressStream; | ||
supportedEncodings.push('br'); | ||
} | ||
fastify.addHook('onSend', compression) | ||
@@ -22,3 +31,2 @@ | ||
if (shouldCompress(reply, method)) { | ||
let payloadStream; | ||
if (Buffer.byteLength(payload) < threshold) { | ||
@@ -28,6 +36,6 @@ done(); | ||
} | ||
payloadStream = stringToStream(payload); | ||
let payloadStream = stringToStream(payload); | ||
setVaryHeader(reply); | ||
reply.header('Content-Encoding', method); | ||
const compressionStream = method === 'gzip' ? zlib.createGzip() : zlib.createDeflate(); | ||
const compressionStream = compressionStreams[method](); | ||
@@ -34,0 +42,0 @@ pump(payloadStream, compressionStream, onEnd.bind(request)) |
{ | ||
"name": "fastify-compression", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "A compression plugin for fastify", | ||
"main": "index.js", | ||
"scripts": { | ||
"unit": "tap test/*", | ||
"unit": "tap test/*.test.js", | ||
"test": "npm run lint && npm run unit", | ||
@@ -37,6 +37,7 @@ "lint": "./node_modules/eslint/bin/eslint.js index.js lib/*", | ||
"eslint": "^4.19.1", | ||
"iltorb": "^2.0.3", | ||
"fastify": "^1.2.1", | ||
"request": "^2.85.0", | ||
"tap": "^11.1.4" | ||
"tap": "^12.0.0" | ||
} | ||
} |
@@ -8,3 +8,3 @@ # fastify-compression | ||
A compression plugin for [Fastify](http://fastify.io/). Supports `gzip` and `deflate`. | ||
A compression plugin for [Fastify](http://fastify.io/). Supports `gzip`, `deflate` and `brotli`. | ||
@@ -23,6 +23,8 @@ ## Usage | ||
### compression(fastify, options, next) | ||
Compresses the payload with `gzip` or `deflate` if the payload length is above the threshold and a `Accept-Encoding` header is send with the request. In case of an asterisk `*` in the `Accept-Encoding` header `gzip` will be chosen. | ||
Compresses the payload with `gzip`, `brotli` or `deflate` if the payload length is above the threshold and a `Accept-Encoding` header is send with the request. In case of an asterisk `*` in the `Accept-Encoding` header `gzip` will be chosen. | ||
### options | ||
#### threshold (optional) | ||
A `number` that specifies the threshold used to determine if compression should be applied. Defaults to `1024`. | ||
#### brotli | ||
To enable Brotli compression pass the [iltorb](https://www.npmjs.com/package/iltorb) module with the `brotli` option. | ||
@@ -29,0 +31,0 @@ ## License |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20360
8
506
31
5
1