Skip to content

Commit

Permalink
feat: added useZopfliForGzip option
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
CodeIter authored and Alorel committed Sep 28, 2019
1 parent 43e0fa0 commit 1072f51
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 126 deletions.
20 changes: 0 additions & 20 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,3 @@ cache:
before_cache: rm -rf node_modules/.cache
install: npm install --build-from-source
after_success: cat ./coverage/lcov.info | coveralls

#stages:
#- test
##- name: Deploy
## if: tag IS present

#jobs:
# include:
# - stage: Deploy
# node_js: stable
# script: node ./prepublish.js
# after_success: false
## deploy:
## - provider: npm
## skip_cleanup: true
## on:
## tags: true
## email: [email protected]
## api_key:
## secure: iSr5sIvDsSP3c+55Wa91G3a3WXkb8Eb9iGc6wontJDZQx7PZ+xYHFFYEAQkK6LYw08i0eik/2xTdt76djb1jySOi/OlvAQIGsf/GPcTG0qRcRvW/rR6CM7yX36xQ9pWUn/PEuDHSmzBnKVHHrhbYR66olUrOBu7GcgTJoLDj0ppFSbtUMqqFCM8bNQs8d3gMgwF96+7jpzjHbFDVrB+jMN8qNZF29RO+Xqv9oaPSQyY2o7IJUwsbVGUs6g6EpnNlj4p7MfzBmhoNLikY73UtW2teLyobDTf8Qv0+PwuRq1WwB8UhlgSlqQ+TWaFcywqmhiZIm30fu69M1qfkuO6N51jIuvq19qCZekcl02vS5kUs0VnbOozp+5BuQNdEzkbmrQrtDkGXaI86ZVr7MUGfFZqPQlEcIoOQyRT6cA8BtZ4SrhCS73xLbFoHcLhbXR7i3gpNQK1Ap5YMnlwQS+z4e5NI6h/VQ+8GRXuVCTclAuJdpXkJWGuoOMfLK0keCkO1iqETW4Iv7t58tjJXiwfU8VBgxd/8bI5lyVoy9VpBoptaDjfWJpfaSskXjxTv+RVVmwhjr44wngvvNid93q0hB7+AFhr4kR52fQ8vXtZtfM3fdTUy036IUo/omQzXKV7EyL/QFkI62a4EDJAFWpuZOO+wtlCaijbcadTrtIV5wJk=
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ project.
- [API](#api)
- [`shrinkRay([options])`](#shrinkrayoptions)
- [Options](#options)
- [useZopfliForGzip](#usezopfliforgzip)
- [filter](#filter)
- [cache](#cache)
- [cacheSize](#cachesize)
Expand Down Expand Up @@ -129,6 +130,11 @@ we have also moved all of the gzip/deflate/zlib-specific parameters
into a sub-object called `zlib`. If you use `zlib` parameters at the root level
of options in `shrink-ray`, you will get a deprecation warning.

#### useZopfliForGzip

Whether to use [node-zopfli-es](https://www.npmjs.com/package/node-zopfli-es) (`true`) or zlib (`false`) for gzip compression.
Defaults to `true`.

#### filter

A function to decide if the response should be considered for compression.
Expand Down
18 changes: 8 additions & 10 deletions brotli-compat.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
module.exports = getBrotliModule;

function getBrotliModule() {
module.exports = function brotliCompat() {
const zlib = require('zlib');

if (typeof zlib.createBrotliCompress === 'function') {
Expand All @@ -12,8 +10,8 @@ function getBrotliModule() {
quality: zlib.constants.BROTLI_PARAM_QUALITY,
lgwin: zlib.constants.BROTLI_PARAM_LGWIN,
lgblock: zlib.constants.BROTLI_PARAM_LGBLOCK,
disable_literal_context_modeling:
zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING,
disable_literal_context_modeling:
zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING,
large_window: zlib.constants.BROTLI_PARAM_LARGE_WINDOW
};
const iltorbOptionsToNodeZlibBrotliOpts = iltorbOpts => {
Expand All @@ -23,11 +21,11 @@ function getBrotliModule() {
if (ILTORB_OPTION_NAMES_TO_BROTLI_PARAM_NAMES.hasOwnProperty(key)) {
params[ILTORB_OPTION_NAMES_TO_BROTLI_PARAM_NAMES[key]] = iltorbOpts[
key
];
];
}
});
return { params };
}
};

/**
* Replicate the 'iltorb' interface for backwards compatibility.
Expand All @@ -42,7 +40,7 @@ function getBrotliModule() {
};

}

// If we get here, then our NodeJS does not support brotli natively.
try {
return require('iltorb');
Expand All @@ -55,8 +53,8 @@ function getBrotliModule() {
}
);
}

// Return a signal value instead of throwing an exception, so the code in the
// index file doesn't have to try/catch again.
return false;
}
};
27 changes: 14 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

/*!
* compression
* Copyright(c) 2019 CodeIter (https://github.com/CodeIter)
* Copyright(c) 2017 Arturas Molcanovas
* Copyright(c) 2010 Sencha Inc.
* Copyright(c) 2011 TJ Holowaychuk
Expand All @@ -28,19 +29,18 @@ const zlib = require('zlib');
* Optional dependencies handling. If some binary dependencies cannot build in
* this environment, or are incompatible with this version of Node, the rest of
* the module should work!
* Known dependency issues:
* Known dependency issues:
* - node-zopfli-es is not compatible with Node <8.11.
* - iltorb is not required for Node >= 11.8, whose zlib has brotli built in.
*/

const brotliCompat = require('./brotli-compat');
const zopfliCompat = require('./zopfli-compat');

// These are factory functions because they dynamically require dependencies
// and may log errors.
// They need to be tested, so they shouldn't have side effects on load.
const brotli = brotliCompat();
const zopfli = zopfliCompat();
const brotliCompat = require('./brotli-compat');
const zopfliCompat = require('./zopfli-compat');

// These are factory functions because they dynamically require dependencies
// and may log errors.
// They need to be tested, so they shouldn't have side effects on load.
const brotli = brotliCompat();

/**
* Module exports.
Expand Down Expand Up @@ -76,6 +76,7 @@ function compression(options) {
const opts = options || {};

// options
const zopfli = zopfliCompat('useZopfliForGzip' in opts ? opts.useZopfliForGzip : true);
const filter = opts.filter || shouldCompress;
let threshold = bytes.parse(opts.threshold);

Expand All @@ -93,7 +94,7 @@ function compression(options) {
});

if (!opts.hasOwnProperty('cacheSize')) opts.cacheSize = '128mB';
const cache = opts.cacheSize ? createCache(bytes(opts.cacheSize.toString())) : null;
const cache = opts.cacheSize ? createCache(bytes(opts.cacheSize.toString()), zopfli) : null;

const shouldCache = opts.cache || stubTrue;

Expand Down Expand Up @@ -359,7 +360,7 @@ function shouldTransform(req, res) {
!cacheControlNoTransformRegExp.test(cacheControl);
}

function createCache(size) {
function createCache(size, zopfli) {
const index = {};
const lru = new lruCache({
max: size,
Expand Down Expand Up @@ -411,7 +412,7 @@ function createCache(size) {
const result = new BufferWritable();

new BufferReadable(buffer)
.pipe(getBestQualityReencoder(coding))
.pipe(getBestQualityReencoder(coding, zopfli))
.pipe(result)
.on('finish', function () {
const itemInCache = lru.peek(key);
Expand Down Expand Up @@ -489,7 +490,7 @@ BufferDuplex.prototype._write = function (chunk, encoding, callback) {

// get a decode --> encode transform stream that will re-encode the content at
// the best quality available for that coding method.
function getBestQualityReencoder(coding) {
function getBestQualityReencoder(coding, zopfli) {
switch (coding) {
case 'gzip':
return multipipe(zlib.createGunzip(), zopfli.createGzip());
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "shrink-ray-current",
"description": "Node.js compression middleware with brotli and zopfli support",
"version": "4.0.0",
"version": "4.1.0",
"contributors": [
"Arturas Molcanovas <[email protected]> (https://alorel.github.io)",
"Arturas Molcanovas <[email protected]> (https://github.com/Alorel)",
"CodeIter (https://github.com/CodeIter)",
"Douglas Christopher Wilson <[email protected]>",
"Jonathan Ong <[email protected]> (http://jongleberry.com)",
"Scott Davis (https://github.com/scttdavs)",
Expand Down
Loading

0 comments on commit 1072f51

Please sign in to comment.