Browse Source

Replace brotli module by zlib

Gaël Métais 4 years ago
parent
commit
6b58b72028
2 changed files with 39 additions and 48 deletions
  1. 39 47
      lib/tools/redownload/brotliCompressor.js
  2. 0 1
      package.json

+ 39 - 47
lib/tools/redownload/brotliCompressor.js

@@ -1,7 +1,7 @@
-var debug           = require('debug')('ylt:brotliCompressor');
+var debug   = require('debug')('ylt:brotliCompressor');
 
-var Q               = require('q');
-var brotli          = require('brotli');
+var Q       = require('q');
+var zlib    = require('zlib');
 
 var gzipCompressor  = require('./gzipCompressor');
 
@@ -29,34 +29,31 @@ var GzipCompressor = function() {
                 debug('File %s was compressed with %s. Trying with Brotli.', entry.url, entry.weightCheck.compressionTool);
             }
 
-            try {
-                
-                var buffer = brotli.compress(entry.weightCheck.bodyBuffer, {
-                    mode: 0,
-                    quality: 9
-                });
-
-                if (buffer) {
-                    var compressedSize = buffer.length;
-
-                    if (!entry.weightCheck.isCompressed) {
-                        debug('Brotli size is %d, was %d, this is %d% better.', compressedSize, entry.weightCheck.bodySize, Math.round((entry.weightCheck.bodySize - compressedSize) * 100 / entry.weightCheck.bodySize));
-                    } else if (entry.weightCheck.compressionTool !== 'brotli') {
-                        debug('Brotli size is %d, was %d with %s, this is %d% better.', compressedSize, entry.weightCheck.bodySize, entry.weightCheck.compressionTool, Math.round((entry.weightCheck.bodySize - compressedSize) * 100 / entry.weightCheck.bodySize));
+            zlib.brotliCompress(entry.weightCheck.bodyBuffer, {
+                    params: {
+                        [zlib.constants.BROTLI_PARAM_QUALITY]: 9
                     }
+                }, function(err, buffer) {
+                    if (err) {
+                        debug('Could not compress uncompressed file with brotli');
+                        debug(err);
 
-                    entry.weightCheck.afterBrotliCompression = compressedSize;
+                        deferred.reject(err);
+                    } else {
+                        var compressedSize = buffer.length;
 
-                } else {
-                    debug('Failed to brotlify %s', entry.url);                    
-                }
+                        if (!entry.weightCheck.isCompressed) {
+                            debug('Brotli size is %d, was %d, this is %d% better.', compressedSize, entry.weightCheck.bodySize, Math.round((entry.weightCheck.bodySize - compressedSize) * 100 / entry.weightCheck.bodySize));
+                        } else if (entry.weightCheck.compressionTool !== 'brotli') {
+                            debug('Brotli size is %d, was %d with %s, this is %d% better.', compressedSize, entry.weightCheck.bodySize, entry.weightCheck.compressionTool, Math.round((entry.weightCheck.bodySize - compressedSize) * 100 / entry.weightCheck.bodySize));
+                        }
 
-                deferred.resolve(entry);
+                        entry.weightCheck.afterBrotliCompression = compressedSize;
 
-            } catch (err) {
-                debug(err);
-                deferred.reject(err);
-            }
+                        deferred.resolve(entry);
+                    }
+                }
+            );
         } else {
             debug('Compression not needed');
             deferred.resolve(entry);
@@ -74,31 +71,26 @@ var GzipCompressor = function() {
 
             var uncompressedSize = entry.weightCheck.optimized;
 
-            try {
-
-                var buffer = brotli.compress(entry.weightCheck.bodyAfterOptimization, {
-                    mode: 1,
-                    quality: BROTLI_COMPRESSION_LEVEL
-                });
-
-                if (buffer) {
-                    var compressedSize = buffer.length;
+            zlib.brotliCompress(Buffer.from(entry.weightCheck.bodyAfterOptimization, 'utf8'), {
+                    params: {
+                        [zlib.constants.BROTLI_PARAM_QUALITY]: 9
+                    }
+                }, function(err, buffer) {
+                    if (err) {
+                        debug('Could not compress minified file with brotli');
+                        debug(err);
 
-                    debug('Correctly brotlified the minified file, was %d and is now %d bytes', uncompressedSize, compressedSize);
+                        deferred.reject(err);
+                    } else {
+                        var compressedSize = buffer.length;
 
-                    entry.weightCheck.afterOptimizationAndBrotliCompression = compressedSize;
+                        debug('Correctly brotlified the minified file, was %d and is now %d bytes', uncompressedSize, compressedSize);
+                        entry.weightCheck.afterOptimizationAndBrotliCompression = compressedSize;
 
-                } else {
-                    debug('Failed to brotlify %s', entry.url);                    
+                        deferred.resolve(entry);
+                    }
                 }
-
-                deferred.resolve(entry);
-
-            } catch (err) {
-                debug(err);
-                deferred.reject(err);
-            }
-
+            );
         } else {
             debug('Compressing optimized file not needed');
             deferred.resolve(entry);

+ 0 - 1
package.json

@@ -29,7 +29,6 @@
     "angular-sanitize": "1.7.7",
     "async": "2.6.1",
     "body-parser": "1.18.3",
-    "brotli": "1.3.2",
     "chart.js": "2.7.3",
     "clean-css": "4.2.1",
     "color-diff": "1.1.0",