@fastify/compress
Advanced tools
Comparing version 5.0.0 to 6.0.0
20
index.js
@@ -146,4 +146,4 @@ 'use strict' | ||
? supportedEncodings | ||
.filter(encoding => opts.encodings.includes(encoding)) | ||
.sort((a, b) => opts.encodings.indexOf(a) - supportedEncodings.indexOf(b)) | ||
.filter(encoding => opts.encodings.includes(encoding)) | ||
.sort((a, b) => opts.encodings.indexOf(a) - supportedEncodings.indexOf(b)) | ||
: supportedEncodings | ||
@@ -179,4 +179,4 @@ | ||
? supportedEncodings | ||
.filter(encoding => opts.requestEncodings.includes(encoding)) | ||
.sort((a, b) => opts.requestEncodings.indexOf(a) - supportedEncodings.indexOf(b)) | ||
.filter(encoding => opts.requestEncodings.includes(encoding)) | ||
.sort((a, b) => opts.requestEncodings.indexOf(a) - supportedEncodings.indexOf(b)) | ||
: supportedEncodings | ||
@@ -273,4 +273,4 @@ | ||
? reply | ||
.header('Content-Encoding', encoding) | ||
.removeHeader('content-length') | ||
.header('Content-Encoding', encoding) | ||
.removeHeader('content-length') | ||
: reply.header('Content-Encoding', encoding) | ||
@@ -399,4 +399,4 @@ | ||
? this | ||
.header('Content-Encoding', encoding) | ||
.removeHeader('content-length') | ||
.header('Content-Encoding', encoding) | ||
.removeHeader('content-length') | ||
: this.header('Content-Encoding', encoding) | ||
@@ -558,4 +558,4 @@ | ||
module.exports = fp(compressPlugin, { | ||
fastify: '3.x', | ||
name: 'fastify-compress' | ||
fastify: '4.x', | ||
name: '@fastify/compress' | ||
}) |
{ | ||
"name": "@fastify/compress", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Fastify compression utils", | ||
@@ -26,6 +26,6 @@ "main": "index.js", | ||
"adm-zip": "^0.5.9", | ||
"fastify": "^3.25.3", | ||
"fastify": "^4.0.0-rc.2", | ||
"jsonstream": "^1.0.3", | ||
"pre-commit": "^1.2.2", | ||
"standard": "^16.0.4", | ||
"standard": "^17.0.0", | ||
"tap": "^16.0.0", | ||
@@ -32,0 +32,0 @@ "tsd": "^0.20.0", |
@@ -1,5 +0,5 @@ | ||
# fastify-compress | ||
# @fastify/compress | ||
![CI](https://github.com/fastify/fastify-compress/workflows/CI/badge.svg) | ||
[![NPM version](https://img.shields.io/npm/v/fastify-compress.svg?style=flat)](https://www.npmjs.com/package/fastify-compress) | ||
[![NPM version](https://img.shields.io/npm/v/@fastify/compress.svg?style=flat)](https://www.npmjs.com/package/@fastify/compress) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-compress/badge.svg)](https://snyk.io/test/github/fastify/fastify-compress) | ||
@@ -11,7 +11,7 @@ [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
> **Important note:** since `fastify-compress` version 4.x payloads that are compressed using the `zip` algorithm are not automatically uncompressed anymore. `fastify-compress` main feature is to provide response compression mechanism to your server, however the `zip` format does not appear in the [IANA maintained Table of Content Encodings](https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding) and thus such behavior was out of the scope of this plugin. | ||
> **Important note:** since `@fastify/compress` version 4.x payloads that are compressed using the `zip` algorithm are not automatically uncompressed anymore. `@fastify/compress` main feature is to provide response compression mechanism to your server, however the `zip` format does not appear in the [IANA maintained Table of Content Encodings](https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding) and thus such behavior was out of the scope of this plugin. | ||
## Install | ||
``` | ||
npm i fastify-compress | ||
npm i @fastify/compress | ||
``` | ||
@@ -28,3 +28,3 @@ | ||
3. `deflate` | ||
4. `*` (no preference — `fastify-compress` will use `gzip`) | ||
4. `*` (no preference — `@fastify/compress` will use `gzip`) | ||
5. `identity` (no compression) | ||
@@ -39,4 +39,4 @@ | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ global: false } | ||
@@ -47,3 +47,3 @@ ) | ||
Important note! If you are using `fastify-compress` plugin together with `fastify-static` plugin, you must register the `fastify-compress` (with *global hook*) **before** registering `fastify-static`. | ||
Important note! If you are using `@fastify/compress` plugin together with `@fastify/static` plugin, you must register the `@fastify/compress` (with *global hook*) **before** registering `@fastify/static`. | ||
@@ -53,4 +53,4 @@ ### Per Route options | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ global: false } | ||
@@ -82,8 +82,9 @@ ) | ||
```javascript | ||
const fs = require('fs') | ||
const fastify = require('fastify')() | ||
import fs from 'fs' | ||
import fastify from 'fastify' | ||
fastify.register(require('fastify-compress'), { global: false }) | ||
const app = fastify() | ||
await app.register(import('@fastify/compress'), { global: false }) | ||
fastify.get('/', (req, reply) => { | ||
app.get('/', (req, reply) => { | ||
reply | ||
@@ -94,6 +95,3 @@ .type('text/plain') | ||
fastify.listen(3000, function (err) { | ||
if (err) throw err | ||
console.log(`server listening on ${fastify.server.address().port}`) | ||
}) | ||
await app.listen(3000) | ||
``` | ||
@@ -106,4 +104,4 @@ | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ threshold: 2048 } | ||
@@ -115,4 +113,4 @@ ) | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ customTypes: /x-protobuf$/ } | ||
@@ -125,4 +123,4 @@ ) | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ | ||
@@ -143,4 +141,4 @@ onUnsupportedEncoding: (encoding, request, reply) => { | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ inflateIfDeflated: true } | ||
@@ -157,7 +155,7 @@ ) | ||
By default, `fastify-compress` prioritizes compression as described [at the beginning of §Usage - Compress replies](#usage). You can change that by passing an array of compression tokens to the `encodings` option: | ||
By default, `@fastify/compress` prioritizes compression as described [at the beginning of §Usage - Compress replies](#usage). You can change that by passing an array of compression tokens to the `encodings` option: | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
// Only support gzip and deflate, and prefer deflate to gzip | ||
@@ -187,7 +185,7 @@ { encodings: ['deflate', 'gzip'] } | ||
### Manage `Content-Length` header removal with removeContentLengthHeader | ||
By default, `fastify-compress` removes the reply `Content-Length` header. You can change that by setting the `removeContentLengthHeader` to `false` either on a global scope or on a route specific scope. | ||
By default, `@fastify/compress` removes the reply `Content-Length` header. You can change that by setting the `removeContentLengthHeader` to `false` either on a global scope or on a route specific scope. | ||
```javascript | ||
// Global plugin scope | ||
server.register(fastifyCompress, { global: true, removeContentLengthHeader: false }); | ||
await server.register(fastifyCompress, { global: true, removeContentLengthHeader: false }); | ||
@@ -220,4 +218,4 @@ // Route specific scope | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ global: false } | ||
@@ -233,4 +231,4 @@ ) | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ global: false } | ||
@@ -256,7 +254,7 @@ ) | ||
By default, `fastify-compress` accepts all encodings specified [at the beginning of §Usage - Decompress request payloads](#usage). You can change that by passing an array of compression tokens to the `requestEncodings` option: | ||
By default, `@fastify/compress` accepts all encodings specified [at the beginning of §Usage - Decompress request payloads](#usage). You can change that by passing an array of compression tokens to the `requestEncodings` option: | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
// Only support gzip | ||
@@ -269,7 +267,7 @@ { requestEncodings: ['gzip'] } | ||
By default, `fastify-compress` chooses the decompressing algorithm by looking at the `content-encoding` header, if present. | ||
By default, `@fastify/compress` chooses the decompressing algorithm by looking at the `content-encoding` header, if present. | ||
You can force one algorithm and ignore the header at all by providing the `forceRequestEncoding` option. | ||
Note that if the request payload is not compressed, `fastify-compress` will try to decompress, resulting in an error. | ||
Note that if the request payload is not compressed, `@fastify/compress` will try to decompress, resulting in an error. | ||
@@ -281,4 +279,4 @@ ### onUnsupportedRequestEncoding | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ | ||
@@ -302,4 +300,4 @@ onUnsupportedRequestEncoding: (request, encoding) => { | ||
```javascript | ||
fastify.register( | ||
require('fastify-compress'), | ||
await fastify.register( | ||
import('@fastify/compress'), | ||
{ | ||
@@ -306,0 +304,0 @@ onInvalidRequestPayload: (request, encoding, error) => { |
@@ -228,3 +228,3 @@ 'use strict' | ||
t.test('it should throw an error on invalid route `compress` settings', async (t) => { | ||
t.plan(2) | ||
t.plan(1) | ||
@@ -234,18 +234,11 @@ const fastify = Fastify() | ||
fastify.get('/', { | ||
compress: 'bad config' | ||
}, (request, reply) => { | ||
reply.send('') | ||
}) | ||
await fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'gzip' | ||
} | ||
}).catch((err) => { | ||
t.type(err, Error) | ||
try { | ||
fastify.get('/', { | ||
compress: 'bad config' | ||
}, (request, reply) => { | ||
reply.send('') | ||
}) | ||
} catch (err) { | ||
t.equal(err.message, 'Unknown value for route compress configuration') | ||
}) | ||
} | ||
}) | ||
@@ -390,3 +383,3 @@ }) | ||
t.test('it should use the old routes `{ config: compress }` options over routes `compress` options', async (t) => { | ||
t.plan(2) | ||
t.plan(1) | ||
@@ -396,23 +389,16 @@ const fastify = Fastify() | ||
fastify.get('/', { | ||
compress: { | ||
zlib: { createGzip: () => zlib.createGzip() } | ||
}, | ||
config: { | ||
compress: 'bad config' | ||
} | ||
}, (request, reply) => { | ||
reply.send('') | ||
}) | ||
await fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'gzip' | ||
} | ||
}).catch((err) => { | ||
t.type(err, Error) | ||
try { | ||
fastify.get('/', { | ||
compress: { | ||
zlib: { createGzip: () => zlib.createGzip() } | ||
}, | ||
config: { | ||
compress: 'bad config' | ||
} | ||
}, (request, reply) => { | ||
reply.send('') | ||
}) | ||
} catch (err) { | ||
t.equal(err.message, 'Unknown value for route compress configuration') | ||
}) | ||
} | ||
}) | ||
@@ -419,0 +405,0 @@ }) |
@@ -192,3 +192,3 @@ 'use strict' | ||
t.test('it should throw an error on invalid route `decompress` settings', async (t) => { | ||
t.plan(2) | ||
t.plan(1) | ||
@@ -198,17 +198,9 @@ const fastify = Fastify() | ||
fastify.post('/', { decompress: 'bad config' }, (request, reply) => { | ||
reply.send(request.body.name) | ||
}) | ||
await fastify.inject({ | ||
url: '/', | ||
method: 'POST', | ||
headers: { | ||
'content-encoding': 'gzip' | ||
}, | ||
payload: '' | ||
}).catch((err) => { | ||
t.type(err, Error) | ||
try { | ||
fastify.post('/', { decompress: 'bad config' }, (request, reply) => { | ||
reply.send(request.body.name) | ||
}) | ||
} catch (err) { | ||
t.equal(err.message, 'Unknown value for route decompress configuration') | ||
}) | ||
} | ||
}) | ||
@@ -279,3 +271,3 @@ }) | ||
t.test('it should use the old routes `{ config: decompress }` options over routes `decompress` options', async (t) => { | ||
t.plan(2) | ||
t.plan(1) | ||
@@ -285,25 +277,17 @@ const fastify = Fastify() | ||
fastify.post('/', { | ||
decompress: { | ||
zlib: { createGunzip: () => zlib.createGunzip() } | ||
}, | ||
config: { | ||
decompress: 'bad config' | ||
} | ||
}, (request, reply) => { | ||
reply.send(request.body.name) | ||
}) | ||
await fastify.inject({ | ||
url: '/', | ||
method: 'POST', | ||
headers: { | ||
'content-encoding': 'gzip' | ||
}, | ||
payload: '' | ||
}).catch((err) => { | ||
t.type(err, Error) | ||
try { | ||
fastify.post('/', { | ||
decompress: { | ||
zlib: { createGunzip: () => zlib.createGunzip() } | ||
}, | ||
config: { | ||
decompress: 'bad config' | ||
} | ||
}, (request, reply) => { | ||
reply.send(request.body.name) | ||
}) | ||
} catch (err) { | ||
t.equal(err.message, 'Unknown value for route decompress configuration') | ||
}) | ||
} | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
158950
4179
309
2