2
2
3
3
/*!
4
4
* compression
5
+ * Copyright(c) 2019 CodeIter (https://github.com/CodeIter)
5
6
* Copyright(c) 2017 Arturas Molcanovas
6
7
* Copyright(c) 2010 Sencha Inc.
7
8
* Copyright(c) 2011 TJ Holowaychuk
@@ -28,19 +29,18 @@ const zlib = require('zlib');
28
29
* Optional dependencies handling. If some binary dependencies cannot build in
29
30
* this environment, or are incompatible with this version of Node, the rest of
30
31
* the module should work!
31
- * Known dependency issues:
32
+ * Known dependency issues:
32
33
* - node-zopfli-es is not compatible with Node <8.11.
33
34
* - iltorb is not required for Node >= 11.8, whose zlib has brotli built in.
34
35
*/
35
36
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 ( ) ;
44
44
45
45
/**
46
46
* Module exports.
@@ -76,6 +76,7 @@ function compression(options) {
76
76
const opts = options || { } ;
77
77
78
78
// options
79
+ const zopfli = zopfliCompat ( 'useZopfliForGzip' in opts ? opts . useZopfliForGzip : true ) ;
79
80
const filter = opts . filter || shouldCompress ;
80
81
let threshold = bytes . parse ( opts . threshold ) ;
81
82
@@ -93,7 +94,7 @@ function compression(options) {
93
94
} ) ;
94
95
95
96
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 ;
97
98
98
99
const shouldCache = opts . cache || stubTrue ;
99
100
@@ -359,7 +360,7 @@ function shouldTransform(req, res) {
359
360
! cacheControlNoTransformRegExp . test ( cacheControl ) ;
360
361
}
361
362
362
- function createCache ( size ) {
363
+ function createCache ( size , zopfli ) {
363
364
const index = { } ;
364
365
const lru = new lruCache ( {
365
366
max : size ,
@@ -411,7 +412,7 @@ function createCache(size) {
411
412
const result = new BufferWritable ( ) ;
412
413
413
414
new BufferReadable ( buffer )
414
- . pipe ( getBestQualityReencoder ( coding ) )
415
+ . pipe ( getBestQualityReencoder ( coding , zopfli ) )
415
416
. pipe ( result )
416
417
. on ( 'finish' , function ( ) {
417
418
const itemInCache = lru . peek ( key ) ;
@@ -489,7 +490,7 @@ BufferDuplex.prototype._write = function (chunk, encoding, callback) {
489
490
490
491
// get a decode --> encode transform stream that will re-encode the content at
491
492
// the best quality available for that coding method.
492
- function getBestQualityReencoder ( coding ) {
493
+ function getBestQualityReencoder ( coding , zopfli ) {
493
494
switch ( coding ) {
494
495
case 'gzip' :
495
496
return multipipe ( zlib . createGunzip ( ) , zopfli . createGzip ( ) ) ;
0 commit comments