fastify-compression
Advanced tools
Comparing version 0.3.1 to 0.4.0
27
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; | ||
supportedEncodings.push('br'); | ||
} | ||
fastify.addHook('onSend', compression) | ||
@@ -23,15 +32,10 @@ | ||
let payloadStream; | ||
let _payload = payload; | ||
if (!reply.res.getHeader('Content-Type') || reply.res.getHeader('Content-Type') === 'application/json') { | ||
reply.res.setHeader('Content-Type', 'application/json'); | ||
_payload = reply.serialize(payload); | ||
} | ||
if (Buffer.byteLength(_payload) < threshold) { | ||
if (Buffer.byteLength(payload) < threshold) { | ||
done(); | ||
return; | ||
} | ||
payloadStream = stringToStream(_payload); | ||
payloadStream = stringToStream(payload); | ||
setVaryHeader(reply); | ||
reply.header('Content-Encoding', method); | ||
const compressionStream = method === 'gzip' ? zlib.createGzip() : zlib.createDeflate(); | ||
const compressionStream = compressionStreams[method](); | ||
@@ -76,2 +80,7 @@ pump(payloadStream, compressionStream, onEnd.bind(request)) | ||
exports = module.exports = fastifyPlugin(compressionPlugin, '>=0.35.0'); | ||
const metadata = { | ||
fastify: '>=0.43.0', | ||
name: 'fastify-compression' | ||
} | ||
exports = module.exports = fastifyPlugin(compressionPlugin, metadata); |
{ | ||
"name": "fastify-compression", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "A compression plugin for fastify", | ||
@@ -24,4 +24,4 @@ "main": "index.js", | ||
"encoding-negotiator": "^1.0.2", | ||
"fastify-plugin": "^0.1.1", | ||
"pump": "^2.0.0", | ||
"fastify-plugin": "^0.2.1", | ||
"pump": "^3.0.0", | ||
"string-to-stream": "^1.1.0" | ||
@@ -38,3 +38,3 @@ }, | ||
"eslint": "^4.10.0", | ||
"fastify": "^0.35.0", | ||
"fastify": "^0.43.0", | ||
"request": "^2.83.0", | ||
@@ -41,0 +41,0 @@ "tap": "^11.0.0" |
@@ -16,3 +16,4 @@ 'use strict'; | ||
const options = {} | ||
fastify.register(fastifyCompression, options, (err) => { | ||
fastify.register(fastifyCompression, options); | ||
fastify.ready((err) => { | ||
t.error(err); | ||
@@ -140,3 +141,3 @@ }); | ||
t.strictEqual(response.headers['content-encoding'], 'gzip'); | ||
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), '"something larger than threshold"'); | ||
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold'); | ||
}) | ||
@@ -173,3 +174,3 @@ }); | ||
t.strictEqual(response.headers['content-encoding'], 'gzip'); | ||
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), '"something larger than threshold"'); | ||
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold'); | ||
}) | ||
@@ -268,3 +269,3 @@ }); | ||
t.notOk(response.headers['content-length']); | ||
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), '"something larger than threshold"'); | ||
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold'); | ||
}) | ||
@@ -271,0 +272,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
470
18947
+ Addedfastify-plugin@0.2.2(transitive)
+ Addedpump@3.0.2(transitive)
- Removedfastify-plugin@0.1.1(transitive)
- Removedpump@2.0.1(transitive)
Updatedfastify-plugin@^0.2.1
Updatedpump@^3.0.0