fastify-compress
Advanced tools
Comparing version 0.3.0 to 0.4.0
13
index.js
@@ -31,3 +31,3 @@ 'use strict' | ||
function compress (payload) { | ||
if (!payload) { | ||
if (payload == null) { | ||
this.res.log.warn('compress: missing payload') | ||
@@ -59,3 +59,3 @@ this.send(new Error('Internal server error')) | ||
if (payload._readableState === undefined) { | ||
if (typeof payload.pipe !== 'function') { | ||
if (typeof payload !== 'string') { | ||
@@ -77,3 +77,3 @@ payload = this.serialize(payload) | ||
function onSend (req, reply, payload, next) { | ||
if (!payload) { | ||
if (payload == null) { | ||
reply.res.log.warn('compress: missing payload') | ||
@@ -105,6 +105,3 @@ return next() | ||
if (payload._readableState === undefined) { | ||
if (typeof payload !== 'string') { | ||
payload = reply.serialize(payload) | ||
} | ||
if (typeof payload.pipe !== 'function') { | ||
if (Buffer.byteLength(payload) < threshold) { | ||
@@ -160,4 +157,4 @@ return next() | ||
module.exports = fp(compressPlugin, { | ||
fastify: '>=0.39.1', | ||
fastify: '>=0.40.0', | ||
name: 'fastify-compress' | ||
}) |
{ | ||
"name": "fastify-compress", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Fastify compression utils", | ||
@@ -9,8 +9,9 @@ "main": "index.js", | ||
"mime-db": "^1.32.0", | ||
"pump": "^2.0.0", | ||
"pump": "^2.0.1", | ||
"string-to-stream": "^1.1.0" | ||
}, | ||
"devDependencies": { | ||
"fastify": "^0.39.1", | ||
"fastify": "^0.40.0", | ||
"iltorb": "^2.0.3", | ||
"jsonstream": "^1.0.3", | ||
"standard": "^10.0.3", | ||
@@ -17,0 +18,0 @@ "tap": "^11.0.1" |
53
test.js
@@ -8,2 +8,3 @@ 'use strict' | ||
const fs = require('fs') | ||
const JSONStream = require('jsonstream') | ||
const createReadStream = fs.createReadStream | ||
@@ -643,1 +644,53 @@ const readFileSync = fs.readFileSync | ||
}) | ||
test('should support stream1 (reply compress)', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { global: false }) | ||
fastify.get('/', (req, reply) => { | ||
const stream = JSONStream.stringify() | ||
reply.type('text/plain').compress(stream) | ||
stream.write({ hello: 'world' }) | ||
stream.end({ a: 42 }) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'gzip' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.headers['content-encoding'], 'gzip') | ||
const payload = zlib.gunzipSync(res.rawPayload) | ||
t.deepEqual(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }]) | ||
}) | ||
}) | ||
test('should support stream1 (global hook)', t => { | ||
t.plan(3) | ||
const fastify = Fastify() | ||
fastify.register(compressPlugin, { threshold: 0 }) | ||
fastify.get('/', (req, reply) => { | ||
const stream = JSONStream.stringify() | ||
reply.type('text/plain').send(stream) | ||
stream.write({ hello: 'world' }) | ||
stream.end({ a: 42 }) | ||
}) | ||
fastify.inject({ | ||
url: '/', | ||
method: 'GET', | ||
headers: { | ||
'accept-encoding': 'gzip' | ||
} | ||
}, (err, res) => { | ||
t.error(err) | ||
t.strictEqual(res.headers['content-encoding'], 'gzip') | ||
const payload = zlib.gunzipSync(res.rawPayload) | ||
t.deepEqual(JSON.parse(payload.toString()), [{ hello: 'world' }, { a: 42 }]) | ||
}) | ||
}) |
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
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
25741
734
0
5
Updatedpump@^2.0.1