diff --git a/lib/tools/redownload/brotliCompressor.js b/lib/tools/redownload/brotliCompressor.js index cfe5913..8a1c7dd 100644 --- a/lib/tools/redownload/brotliCompressor.js +++ b/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)); + } + + entry.weightCheck.afterBrotliCompression = compressedSize; + + deferred.resolve(entry); + } } - - deferred.resolve(entry); - - } catch (err) { - debug(err); - deferred.reject(err); - } + ); } else { debug('Compression not needed'); deferred.resolve(entry); @@ -74,31 +71,26 @@ var GzipCompressor = function() { var uncompressedSize = entry.weightCheck.optimized; - try { + 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); - var buffer = brotli.compress(entry.weightCheck.bodyAfterOptimization, { - mode: 1, - quality: BROTLI_COMPRESSION_LEVEL - }); + deferred.reject(err); + } else { + var compressedSize = buffer.length; - if (buffer) { - var compressedSize = buffer.length; + debug('Correctly brotlified the minified file, was %d and is now %d bytes', uncompressedSize, compressedSize); + 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); diff --git a/package.json b/package.json index c3efdf8..3a2e0e7 100644 --- a/package.json +++ b/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",