fastify-compress
Advanced tools
Comparing version 0.1.0 to 0.2.0
20
index.js
@@ -48,9 +48,3 @@ 'use strict' | ||
if (encoding === undefined) { | ||
closeStream(payload) | ||
this.code(400).send(new Error('Missing `accept encoding` header')) | ||
return | ||
} | ||
if (encoding === 'identity') { | ||
if (encoding === undefined || encoding === 'identity') { | ||
return this.send(payload) | ||
@@ -100,9 +94,2 @@ } | ||
if (encoding === undefined) { | ||
closeStream(payload) | ||
reply.code(400) | ||
next(new Error('Missing `accept encoding` header')) | ||
return | ||
} | ||
if (encoding === null) { | ||
@@ -115,3 +102,3 @@ closeStream(payload) | ||
if (encoding === 'identity') { | ||
if (encoding === undefined || encoding === 'identity') { | ||
return next() | ||
@@ -161,2 +148,5 @@ } | ||
} | ||
if (acceptEncodings[i].indexOf('*') > -1) { | ||
return 'gzip' | ||
} | ||
} | ||
@@ -163,0 +153,0 @@ return null |
{ | ||
"name": "fastify-compress", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Fastify compression utils", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -20,5 +20,8 @@ # fastify-compress | ||
- `'br'` | ||
- `'*'` | ||
If an unsupported encoding is received, it will automatically return a `406` error, if the `'accept-encoding'` header is missing, it will return a `400` error. | ||
If the `'accept-encoding'` header specifies no preferred encoding with an asterisk `*` the payload will be compressed with `gzip`. | ||
If an unsupported encoding is received, it will automatically return a `406` error, if the `'accept-encoding'` header is missing, it will not compress the payload. | ||
It automatically defines if a payload should be compressed or not based on its `Content-Type`, if no content type is present, it will assume is `application/json`. | ||
@@ -74,2 +77,8 @@ | ||
### Disable compression by header | ||
You can selectively disable the response compression by using the `x-no-compression` header in the request. | ||
## Note | ||
Please have in mind that in large scale scenarios, you should use a proxy like Nginx to handle response-compression. | ||
## Acknowledgements | ||
@@ -76,0 +85,0 @@ This project is kindly sponsored by: |
47
test.js
@@ -59,2 +59,25 @@ 'use strict' | ||
test('should send a gzipped data for * header', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { global: false }) | ||
fastify.get('/', (req, reply) => { | ||
reply.type('text/plain').compress(createReadStream('./package.json')) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': '*' | ||
} | ||
}, res => { | ||
t.strictEqual(res.headers['content-encoding'], 'gzip') | ||
const file = readFileSync('./package.json', 'utf8') | ||
const payload = zlib.gunzipSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), file) | ||
}) | ||
}) | ||
test('should send a brotli data', t => { | ||
@@ -132,3 +155,3 @@ t.plan(2) | ||
test('Missing header', t => { | ||
test('should not compress on missing header', t => { | ||
t.plan(2) | ||
@@ -146,9 +169,4 @@ const fastify = Fastify() | ||
}, res => { | ||
const payload = JSON.parse(res.payload) | ||
t.strictEqual(res.statusCode, 400) | ||
t.deepEqual({ | ||
error: 'Bad Request', | ||
message: 'Missing `accept encoding` header', | ||
statusCode: 400 | ||
}, payload) | ||
t.strictEqual(res.statusCode, 200) | ||
t.notOk(res.headers['content-encoding']) | ||
}) | ||
@@ -170,10 +188,13 @@ }) | ||
url: '/', | ||
method: 'GET' | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'compress' | ||
} | ||
}, res => { | ||
const payload = JSON.parse(res.payload) | ||
t.strictEqual(res.statusCode, 400) | ||
t.strictEqual(res.statusCode, 406) | ||
t.deepEqual({ | ||
error: 'Bad Request', | ||
message: 'Missing `accept encoding` header', | ||
statusCode: 400 | ||
error: 'Not Acceptable', | ||
message: 'Unsupported encoding', | ||
statusCode: 406 | ||
}, payload) | ||
@@ -180,0 +201,0 @@ }) |
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
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
20478
550
89
0