fastify-compress
Advanced tools
Comparing version 0.7.0 to 0.7.1
15
index.js
@@ -6,4 +6,4 @@ 'use strict' | ||
const pump = require('pump') | ||
const sts = require('string-to-stream') | ||
const mimedb = require('mime-db') | ||
const intoStream = require('into-stream') | ||
@@ -18,3 +18,3 @@ function compressPlugin (fastify, opts, next) { | ||
const threshold = typeof opts.threshold === 'number' ? opts.threshold : 1024 | ||
const compressibleTypes = opts.customTypes instanceof RegExp ? opts.customTypes : /^text\/|\+json$|\+text$|\+xml$/ | ||
const compressibleTypes = opts.customTypes instanceof RegExp ? opts.customTypes : /^text\/|\+json$|\+text$|\+xml$|octet-stream$/ | ||
const compressStream = { | ||
@@ -35,3 +35,3 @@ gzip: zlib.createGzip, | ||
if (payload == null) { | ||
this.res.log.warn('compress: missing payload') | ||
this.res.log.debug('compress: missing payload') | ||
this.send(new Error('Internal server error')) | ||
@@ -63,3 +63,3 @@ return | ||
if (typeof payload.pipe !== 'function') { | ||
if (typeof payload !== 'string') { | ||
if (!Buffer.isBuffer(payload) && typeof payload !== 'string') { | ||
payload = this.serialize(payload) | ||
@@ -70,3 +70,3 @@ } | ||
} | ||
payload = sts(payload) | ||
payload = intoStream(payload) | ||
} | ||
@@ -85,3 +85,3 @@ | ||
if (payload == null) { | ||
reply.res.log.warn('compress: missing payload') | ||
reply.res.log.debug('compress: missing payload') | ||
return next() | ||
@@ -116,3 +116,3 @@ } | ||
} | ||
payload = sts(payload) | ||
payload = intoStream(payload) | ||
} | ||
@@ -123,3 +123,2 @@ | ||
.removeHeader('content-length') | ||
var stream = compressStream[encoding]() | ||
@@ -126,0 +125,0 @@ pump(payload, stream, onEnd.bind(reply)) |
{ | ||
"name": "fastify-compress", | ||
"version": "0.7.0", | ||
"version": "0.7.1", | ||
"description": "Fastify compression utils", | ||
@@ -8,5 +8,5 @@ "main": "index.js", | ||
"fastify-plugin": "^1.0.0", | ||
"into-stream": "^4.0.0", | ||
"mime-db": "^1.33.0", | ||
"pump": "^3.0.0", | ||
"string-to-stream": "^1.1.0" | ||
"pump": "^3.0.0" | ||
}, | ||
@@ -13,0 +13,0 @@ "devDependencies": { |
@@ -0,0 +0,0 @@ # fastify-compress |
138
test.js
@@ -254,2 +254,140 @@ 'use strict' | ||
test('Should compress buffer (gzip)', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { global: false, threshold: 0 }) | ||
const buf = Buffer.from('hello world') | ||
fastify.get('/', (req, reply) => { | ||
reply.compress(buf) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'gzip' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
const payload = zlib.gunzipSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), buf.toString()) | ||
}) | ||
}) | ||
test('Should compress buffer (deflate)', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { global: false, threshold: 0 }) | ||
const buf = Buffer.from('hello world') | ||
fastify.get('/', (req, reply) => { | ||
reply.compress(buf) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'deflate' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
const payload = zlib.inflateSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), buf.toString()) | ||
}) | ||
}) | ||
test('Should compress buffer (brotli)', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { global: false, brotli, threshold: 0 }) | ||
const buf = Buffer.from('hello world') | ||
fastify.get('/', (req, reply) => { | ||
reply.compress(buf) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'br' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
const payload = brotli.decompressSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), buf.toString()) | ||
}) | ||
}) | ||
test('Should compress buffer (gzip) - global', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { threshold: 0 }) | ||
const buf = Buffer.from('hello world') | ||
fastify.get('/', (req, reply) => { | ||
reply.send(buf) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'gzip' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
const payload = zlib.gunzipSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), buf.toString()) | ||
}) | ||
}) | ||
test('Should compress buffer (deflate) - global', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { threshold: 0 }) | ||
const buf = Buffer.from('hello world') | ||
fastify.get('/', (req, reply) => { | ||
reply.send(buf) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'deflate' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
const payload = zlib.inflateSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), buf.toString()) | ||
}) | ||
}) | ||
test('Should compress buffer (brotli) - global', t => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { brotli, threshold: 0 }) | ||
const buf = Buffer.from('hello world') | ||
fastify.get('/', (req, reply) => { | ||
reply.send(buf) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'br' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
const payload = brotli.decompressSync(res.rawPayload) | ||
t.strictEqual(payload.toString('utf-8'), buf.toString()) | ||
}) | ||
}) | ||
test('Should compress json data (gzip)', t => { | ||
@@ -256,0 +394,0 @@ t.plan(2) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
35831
1016
1
+ Addedinto-stream@^4.0.0
+ Addedfrom2@2.3.0(transitive)
+ Addedinto-stream@4.0.0(transitive)
+ Addedp-is-promise@2.1.0(transitive)
- Removedstring-to-stream@^1.1.0
- Removedstring-to-stream@1.1.1(transitive)