Skip to content

Commit 1072f51

Browse files
CodeIterAlorel
authored andcommitted
feat: added useZopfliForGzip option
Closes Alorel#39
1 parent 43e0fa0 commit 1072f51

File tree

7 files changed

+136
-126
lines changed

7 files changed

+136
-126
lines changed

.travis.yml

-20
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,3 @@ cache:
1010
before_cache: rm -rf node_modules/.cache
1111
install: npm install --build-from-source
1212
after_success: cat ./coverage/lcov.info | coveralls
13-
14-
#stages:
15-
#- test
16-
##- name: Deploy
17-
## if: tag IS present
18-
19-
#jobs:
20-
# include:
21-
# - stage: Deploy
22-
# node_js: stable
23-
# script: node ./prepublish.js
24-
# after_success: false
25-
## deploy:
26-
## - provider: npm
27-
## skip_cleanup: true
28-
## on:
29-
## tags: true
30-
31-
## api_key:
32-
## secure: iSr5sIvDsSP3c+55Wa91G3a3WXkb8Eb9iGc6wontJDZQx7PZ+xYHFFYEAQkK6LYw08i0eik/2xTdt76djb1jySOi/OlvAQIGsf/GPcTG0qRcRvW/rR6CM7yX36xQ9pWUn/PEuDHSmzBnKVHHrhbYR66olUrOBu7GcgTJoLDj0ppFSbtUMqqFCM8bNQs8d3gMgwF96+7jpzjHbFDVrB+jMN8qNZF29RO+Xqv9oaPSQyY2o7IJUwsbVGUs6g6EpnNlj4p7MfzBmhoNLikY73UtW2teLyobDTf8Qv0+PwuRq1WwB8UhlgSlqQ+TWaFcywqmhiZIm30fu69M1qfkuO6N51jIuvq19qCZekcl02vS5kUs0VnbOozp+5BuQNdEzkbmrQrtDkGXaI86ZVr7MUGfFZqPQlEcIoOQyRT6cA8BtZ4SrhCS73xLbFoHcLhbXR7i3gpNQK1Ap5YMnlwQS+z4e5NI6h/VQ+8GRXuVCTclAuJdpXkJWGuoOMfLK0keCkO1iqETW4Iv7t58tjJXiwfU8VBgxd/8bI5lyVoy9VpBoptaDjfWJpfaSskXjxTv+RVVmwhjr44wngvvNid93q0hB7+AFhr4kR52fQ8vXtZtfM3fdTUy036IUo/omQzXKV7EyL/QFkI62a4EDJAFWpuZOO+wtlCaijbcadTrtIV5wJk=

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ project.
4646
- [API](#api)
4747
- [`shrinkRay([options])`](#shrinkrayoptions)
4848
- [Options](#options)
49+
- [useZopfliForGzip](#usezopfliforgzip)
4950
- [filter](#filter)
5051
- [cache](#cache)
5152
- [cacheSize](#cachesize)
@@ -129,6 +130,11 @@ we have also moved all of the gzip/deflate/zlib-specific parameters
129130
into a sub-object called `zlib`. If you use `zlib` parameters at the root level
130131
of options in `shrink-ray`, you will get a deprecation warning.
131132

133+
#### useZopfliForGzip
134+
135+
Whether to use [node-zopfli-es](https://www.npmjs.com/package/node-zopfli-es) (`true`) or zlib (`false`) for gzip compression.
136+
Defaults to `true`.
137+
132138
#### filter
133139

134140
A function to decide if the response should be considered for compression.

brotli-compat.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
module.exports = getBrotliModule;
2-
3-
function getBrotliModule() {
1+
module.exports = function brotliCompat() {
42
const zlib = require('zlib');
53

64
if (typeof zlib.createBrotliCompress === 'function') {
@@ -12,8 +10,8 @@ function getBrotliModule() {
1210
quality: zlib.constants.BROTLI_PARAM_QUALITY,
1311
lgwin: zlib.constants.BROTLI_PARAM_LGWIN,
1412
lgblock: zlib.constants.BROTLI_PARAM_LGBLOCK,
15-
disable_literal_context_modeling:
16-
zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING,
13+
disable_literal_context_modeling:
14+
zlib.constants.BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING,
1715
large_window: zlib.constants.BROTLI_PARAM_LARGE_WINDOW
1816
};
1917
const iltorbOptionsToNodeZlibBrotliOpts = iltorbOpts => {
@@ -23,11 +21,11 @@ function getBrotliModule() {
2321
if (ILTORB_OPTION_NAMES_TO_BROTLI_PARAM_NAMES.hasOwnProperty(key)) {
2422
params[ILTORB_OPTION_NAMES_TO_BROTLI_PARAM_NAMES[key]] = iltorbOpts[
2523
key
26-
];
24+
];
2725
}
2826
});
2927
return { params };
30-
}
28+
};
3129

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

4442
}
45-
43+
4644
// If we get here, then our NodeJS does not support brotli natively.
4745
try {
4846
return require('iltorb');
@@ -55,8 +53,8 @@ function getBrotliModule() {
5553
}
5654
);
5755
}
58-
56+
5957
// Return a signal value instead of throwing an exception, so the code in the
6058
// index file doesn't have to try/catch again.
6159
return false;
62-
}
60+
};

index.js

+14-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

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

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

4545
/**
4646
* Module exports.
@@ -76,6 +76,7 @@ function compression(options) {
7676
const opts = options || {};
7777

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

@@ -93,7 +94,7 @@ function compression(options) {
9394
});
9495

9596
if (!opts.hasOwnProperty('cacheSize')) opts.cacheSize = '128mB';
96-
const cache = opts.cacheSize ? createCache(bytes(opts.cacheSize.toString())) : null;
97+
const cache = opts.cacheSize ? createCache(bytes(opts.cacheSize.toString()), zopfli) : null;
9798

9899
const shouldCache = opts.cache || stubTrue;
99100

@@ -359,7 +360,7 @@ function shouldTransform(req, res) {
359360
!cacheControlNoTransformRegExp.test(cacheControl);
360361
}
361362

362-
function createCache(size) {
363+
function createCache(size, zopfli) {
363364
const index = {};
364365
const lru = new lruCache({
365366
max: size,
@@ -411,7 +412,7 @@ function createCache(size) {
411412
const result = new BufferWritable();
412413

413414
new BufferReadable(buffer)
414-
.pipe(getBestQualityReencoder(coding))
415+
.pipe(getBestQualityReencoder(coding, zopfli))
415416
.pipe(result)
416417
.on('finish', function () {
417418
const itemInCache = lru.peek(key);
@@ -489,7 +490,7 @@ BufferDuplex.prototype._write = function (chunk, encoding, callback) {
489490

490491
// get a decode --> encode transform stream that will re-encode the content at
491492
// the best quality available for that coding method.
492-
function getBestQualityReencoder(coding) {
493+
function getBestQualityReencoder(coding, zopfli) {
493494
switch (coding) {
494495
case 'gzip':
495496
return multipipe(zlib.createGunzip(), zopfli.createGzip());

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"name": "shrink-ray-current",
33
"description": "Node.js compression middleware with brotli and zopfli support",
4-
"version": "4.0.0",
4+
"version": "4.1.0",
55
"contributors": [
6-
"Arturas Molcanovas <[email protected]> (https://alorel.github.io)",
6+
"Arturas Molcanovas <[email protected]> (https://github.com/Alorel)",
7+
"CodeIter (https://github.com/CodeIter)",
78
"Douglas Christopher Wilson <[email protected]>",
89
"Jonathan Ong <[email protected]> (http://jongleberry.com)",
910
"Scott Davis (https://github.com/scttdavs)",

0 commit comments

Comments
 (0)