|
@@ -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');
|
|
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);
|
|
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 {
|
|
} else {
|
|
debug('Compression not needed');
|
|
debug('Compression not needed');
|
|
deferred.resolve(entry);
|
|
deferred.resolve(entry);
|
|
@@ -74,31 +71,26 @@ var GzipCompressor = function() {
|
|
|
|
|
|
var uncompressedSize = entry.weightCheck.optimized;
|
|
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 {
|
|
} else {
|
|
debug('Compressing optimized file not needed');
|
|
debug('Compressing optimized file not needed');
|
|
deferred.resolve(entry);
|
|
deferred.resolve(entry);
|