Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fastify-compression

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-compression - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

lib/Compression.js

17

package.json
{
"name": "fastify-compression",
"version": "1.1.0",
"version": "1.2.0",
"description": "A compression plugin for fastify",
"main": "index.js",
"main": "lib/Compression.js",
"scripts": {
"unit": "tap test/*.test.js",
"test": "npm run lint && npm run unit",
"lint": "./node_modules/eslint/bin/eslint.js index.js lib/*",
"lint": "standard index.js lib/*",
"coveralls": "npm run unit -- --cov",

@@ -17,3 +17,6 @@ "coverage-report": "npm run coveralls && tap --coverage-report=lcov"

"gzip",
"deflate"
"deflate",
"brotli",
"compress",
"plugin"
],

@@ -27,2 +30,3 @@ "author": "Denis Fäcke",

"pump": "^3.0.0",
"semver": "^5.6.0",
"string-to-stream": "^1.1.0"

@@ -38,8 +42,9 @@ },

"devDependencies": {
"eslint": "^4.19.1",
"fastify": "^1.13.3",
"iltorb": "^2.0.3",
"fastify": "^1.2.1",
"proxyquire": "^2.1.0",
"request": "^2.85.0",
"standard": "^12.0.1",
"tap": "^12.0.0"
}
}

@@ -7,2 +7,3 @@ # fastify-compression

[![NPM version](https://img.shields.io/npm/v/fastify-compression.svg?style=flat)](https://www.npmjs.com/package/fastify-compression)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

@@ -9,0 +10,0 @@ A compression plugin for [Fastify](http://fastify.io/). Supports `gzip`, `deflate` and `brotli`.

@@ -1,449 +0,448 @@

'use strict';
'use strict'
const t = require('tap');
const test = t.test;
const Fastify = require('fastify');
const request = require('request');
const fastifyCompression = require('..');
const zlib = require('zlib');
const stringToStream = require('string-to-stream');
const t = require('tap')
const test = t.test
const Fastify = require('fastify')
const request = require('request')
const fastifyCompression = require('..')
const zlib = require('zlib')
test('register should work', t => {
t.plan(1);
const fastify = Fastify();
t.plan(1)
const fastify = Fastify()
const options = {}
fastify.register(fastifyCompression, options);
fastify.ready((err) => {
t.error(err);
});
});
const options = {}
fastify.register(fastifyCompression, options)
fastify.ready((err) => {
t.error(err)
})
})
test('should not compress if smaller than threshold', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send(200);
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send(200)
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
gzip: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], undefined)
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
gzip: true
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], undefined)
})
});
});
})
})
test('should not compress if invalid content-type', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.header('Content-Type', 'image/png');
reply.send('something larger than threshold');
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.header('Content-Type', 'image/png')
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
gzip: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], undefined)
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
gzip: true
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], undefined)
})
});
});
})
})
test('should set default value for threshold', t => {
t.plan(6);
t.plan(6)
const fastify = Fastify();
const options = {}
fastify.register(fastifyCompression, options);
let aLongString = '';
for (let i = 0; i < 75; i++) {
aLongString += 'ateststring1234567890'
}
fastify.get('/', (request, reply) => {
reply.header('Content-Type', 'text/plain')
reply.send(aLongString);
const fastify = Fastify()
const options = {}
fastify.register(fastifyCompression, options)
let aLongString = ''
for (let i = 0; i < 75; i++) {
aLongString += 'ateststring1234567890'
}
fastify.get('/', (request, reply) => {
reply.header('Content-Type', 'text/plain')
reply.send(aLongString)
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.notOk(response.headers['content-length'])
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), aLongString)
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'gzip');
t.notOk(response.headers['content-length']);
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), aLongString);
})
});
});
})
})
test('should compress if larger than threshold', t => {
t.plan(6);
t.plan(6)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.notOk(response.headers['content-length'])
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.notOk(response.headers['content-length']);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'gzip');
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold');
})
});
});
})
})
test('support * accept-encoding', t => {
t.plan(6);
t.plan(6)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': '*'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.notOk(response.headers['content-length'])
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': '*'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.notOk(response.headers['content-length']);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'gzip');
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold');
})
});
});
})
})
test('should compress if larger than threshold', t => {
t.plan(6);
t.plan(6)
const fastify = Fastify();
const options = {
threshold: 4
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send({hallo: 'welt'});
const fastify = Fastify()
const options = {
threshold: 4
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send({ hallo: 'welt' })
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.notOk(response.headers['content-length'])
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), '{"hallo":"welt"}')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.notOk(response.headers['content-length']);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'gzip');
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), '{"hallo":"welt"}');
})
});
});
})
})
test('should use gzip', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip;q=1.0, deflate;q=0.5'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip;q=1.0, deflate;q=0.5'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'gzip')
})
});
});
})
})
test('should remove content length header', t => {
t.plan(6);
t.plan(6)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip;q=1.0, deflate;q=0.5'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.notOk(response.headers['content-length'])
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip;q=1.0, deflate;q=0.5'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.notOk(response.headers['content-length']);
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold');
})
});
});
})
})
test('should use deflate', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'deflate;q=1.0, gzip;q=0.5'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'deflate')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'deflate;q=1.0, gzip;q=0.5'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'deflate')
})
});
});
})
})
test('should set vary header', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['vary'], 'Accept-Encoding')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['vary'], 'Accept-Encoding')
})
});
});
})
})
test('should append vary header', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.header('Vary', 'Origin')
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.header('Vary', 'Origin')
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['vary'], 'Origin, Accept-Encoding')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['vary'], 'Origin, Accept-Encoding')
})
});
});
})
})
test('should append vary header', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.header('Vary', '*')
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.header('Vary', '*')
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['vary'], '*')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['vary'], '*')
})
});
});
})
})
test('should not compress if no incoming accept-encoding header', t => {
t.plan(4);
t.plan(4)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], undefined)
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], undefined)
})
});
});
})
})
test('should use gzip and consider plain/text', t => {
t.plan(5);
t.plan(5)
const fastify = Fastify();
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options);
fastify.get('/', (request, reply) => {
reply.header('Content-Type', 'text/plain')
reply.send("something larger than threshold");
const fastify = Fastify()
const options = {
threshold: 8
}
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.header('Content-Type', 'text/plain')
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), 'something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'gzip'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'gzip')
t.strictEqual(zlib.gunzipSync(body).toString('utf-8'), "something larger than threshold");
})
});
});
})
})

@@ -1,41 +0,41 @@

'use strict';
'use strict'
const t = require('tap');
const test = t.test;
const Fastify = require('fastify');
const request = require('request');
const fastifyCompression = require('..');
const brotli = require('iltorb');
const t = require('tap')
const test = t.test
const Fastify = require('fastify')
const request = require('request')
const fastifyCompression = require('..')
const brotli = require('iltorb')
test('should compress with brotli if larger than threshold', t => {
t.plan(6);
t.plan(6)
const fastify = Fastify();
const fastify = Fastify()
const options = {
threshold: 8,
brotli
threshold: 8,
brotli
}
fastify.register(fastifyCompression, options);
fastify.register(fastifyCompression, options)
fastify.get('/', (request, reply) => {
reply.send("something larger than threshold");
reply.send('something larger than threshold')
})
fastify.listen(0, err => {
fastify.server.unref();
t.error(err);
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'br'
},
encoding: null
}, (err, response, body) => {
t.error(err);
t.notOk(response.headers['content-length']);
t.strictEqual(response.statusCode, 200);
t.strictEqual(response.headers['content-encoding'], 'br');
t.strictEqual(brotli.decompressSync(body).toString('utf-8'), 'something larger than threshold');
})
});
});
fastify.server.unref()
t.error(err)
request({
method: 'GET',
uri: 'http://localhost:' + fastify.server.address().port,
headers: {
'accept-encoding': 'br'
},
encoding: null
}, (err, response, body) => {
t.error(err)
t.notOk(response.headers['content-length'])
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-encoding'], 'br')
t.strictEqual(brotli.decompressSync(body).toString('utf-8'), 'something larger than threshold')
})
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc