fastify-compress
Advanced tools
Comparing version 0.10.0 to 0.11.0
18
index.js
@@ -16,2 +16,3 @@ 'use strict' | ||
const isDeflate = require('is-deflate') | ||
const encodingNegotiator = require('encoding-negotiator') | ||
@@ -37,3 +38,3 @@ function compressPlugin (fastify, opts, next) { | ||
const supportedEncodings = ['deflate', 'gzip', 'identity'] | ||
const supportedEncodings = ['gzip', 'deflate', 'identity'] | ||
if (opts.brotli) { | ||
@@ -167,15 +168,4 @@ compressStream.br = opts.brotli.compressStream | ||
function getEncodingHeader (supportedEncodings, request) { | ||
var header = request.headers['accept-encoding'] | ||
if (!header) return undefined | ||
var acceptEncodings = header.split(',') | ||
for (var i = 0; i < acceptEncodings.length; i++) { | ||
var acceptEncoding = acceptEncodings[i].trim() | ||
if (supportedEncodings.indexOf(acceptEncoding) > -1) { | ||
return acceptEncoding | ||
} | ||
if (acceptEncoding.indexOf('*') > -1) { | ||
return 'gzip' | ||
} | ||
} | ||
return null | ||
const header = request.headers['accept-encoding'] | ||
return encodingNegotiator.negotiate(header, supportedEncodings) | ||
} | ||
@@ -182,0 +172,0 @@ |
{ | ||
"name": "fastify-compress", | ||
"version": "0.10.0", | ||
"version": "0.11.0", | ||
"description": "Fastify compression utils", | ||
"main": "index.js", | ||
"dependencies": { | ||
"encoding-negotiator": "^2.0.0", | ||
"fastify-plugin": "^1.0.0", | ||
@@ -17,9 +18,9 @@ "into-stream": "4.0.0", | ||
"pump": "^3.0.0", | ||
"pumpify": "^1.3.3", | ||
"string-to-stream": "^1.1.0", | ||
"unzipper": "^0.9.11" | ||
"pumpify": "^2.0.0", | ||
"string-to-stream": "^2.0.0", | ||
"unzipper": "^0.10.1" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^11.13.9", | ||
"@typescript-eslint/parser": "^1.7.0", | ||
"@types/node": "^12.0.8", | ||
"@typescript-eslint/parser": "^2.0.0", | ||
"eslint-plugin-typescript": "^0.14.0", | ||
@@ -30,3 +31,3 @@ "fastify": "^2.0.0", | ||
"pre-commit": "^1.2.2", | ||
"standard": "^12.0.0", | ||
"standard": "^14.0.2", | ||
"typescript": "^3.4.5", | ||
@@ -33,0 +34,0 @@ "tap": "^12.6.6" |
@@ -45,3 +45,3 @@ # fastify-compress | ||
const fs = require('fs') | ||
const fastify = require('fastify') | ||
const fastify = require('fastify')() | ||
@@ -48,0 +48,0 @@ fastify.register(require('fastify-compress'), { global: false }) |
@@ -146,2 +146,29 @@ 'use strict' | ||
test('should support quality syntax', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { global: false }) | ||
fastify.get('/', (req, reply) => { | ||
reply.type('text/plain').compress( | ||
createReadStream('./package.json') | ||
.pipe(zlib.createDeflate()) | ||
) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'gzip;q=0.5,deflate;q=0.6,identity;q=0.3' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.headers['content-encoding'], 'deflate') | ||
const file = readFileSync('./package.json', 'utf8') | ||
const payload = zlib.inflateSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), file) | ||
}) | ||
}) | ||
test('onSend hook should not double-compress Stream if already zipped', t => { | ||
@@ -273,2 +300,29 @@ t.plan(3) | ||
test('Unsupported encoding with quality value', t => { | ||
t.plan(3) | ||
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': 'lzma;q=1.0' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
const payload = JSON.parse(res.payload) | ||
t.strictEqual(res.statusCode, 406) | ||
t.deepEqual({ | ||
error: 'Not Acceptable', | ||
message: 'Unsupported encoding', | ||
statusCode: 406 | ||
}, payload) | ||
}) | ||
}) | ||
test('should not compress on missing header', t => { | ||
@@ -275,0 +329,0 @@ t.plan(3) |
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
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
49806
1452
0
14
+ Addedencoding-negotiator@^2.0.0
+ Addedduplexify@4.1.3(transitive)
+ Addedencoding-negotiator@2.0.1(transitive)
+ Addedpumpify@2.0.1(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedstring-to-stream@2.0.0(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedunzipper@0.10.14(transitive)
- Removedpump@2.0.1(transitive)
- Removedpumpify@1.5.1(transitive)
- Removedstring-to-stream@1.1.1(transitive)
- Removedunzipper@0.9.15(transitive)
Updatedpumpify@^2.0.0
Updatedstring-to-stream@^2.0.0
Updatedunzipper@^0.10.1