diff --git a/lib/index.js b/lib/index.js index 8285f33..68bf50c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,11 +33,10 @@ var yellowLabTools = function(url, options) { // If a screenshot saveFunction was provided in the options if (options && typeof options.saveScreenshotFn === 'function') { - const screenshotTmpPath = data.params.options.screenshot; debug('Now optimizing screenshot...'); // TODO: temporarily set all screenshot sizes to 600px, until we find a solution - ScreenshotHandler.findAndOptimizeScreenshot(screenshotTmpPath, 600) + ScreenshotHandler.findAndOptimizeScreenshot(data.params.options.screenshot, 600) .then(function(screenshotBuffer) { debug('Screenshot optimized, now saving...'); @@ -51,7 +50,6 @@ var yellowLabTools = function(url, options) { // Remove uneeded temp screenshot path delete data.params.options.screenshot; - return ScreenshotHandler.deleteTmpFile(screenshotTmpPath); }) .catch(function(err) { diff --git a/lib/metadata/policies.js b/lib/metadata/policies.js index 1dcf3a2..ead2c2e 100644 --- a/lib/metadata/policies.js +++ b/lib/metadata/policies.js @@ -729,7 +729,7 @@ var policies = { "totalWeight": { "tool": "redownload", "label": "Total weight", - "message": "
The weight is of course very important if you want the page to load fast. Try to stay under 1.5MB.
", + "message": "The weight is of course very important if you want the page to load fast. Try to stay under 1MB, which is already very long to download over a slow connection.
", "isOkThreshold": 1572864, "isBadThreshold": 3145728, "isAbnormalThreshold": 5242880, diff --git a/lib/screenshotHandler.js b/lib/screenshotHandler.js index c175fd1..3aab753 100644 --- a/lib/screenshotHandler.js +++ b/lib/screenshotHandler.js @@ -1,19 +1,90 @@ -var debug = require('debug')('ylt:screenshotHandler'); -var sharp = require('sharp'); +var debug = require('debug')('ylt:screenshotHandlerAgent'); +var Jimp = require('jimp'); var Q = require('q'); var fs = require('fs'); var path = require('path'); -// Disable sharp cache to reduce the "disk is full" error on Amazon Lambda -sharp.cache(false); var screenshotHandler = function() { - this.findAndOptimizeScreenshot = async function(tmpScreenshotPath, width) { - return sharp(tmpScreenshotPath) - .resize({width: 600}) - .jpeg({quality: 85}) - .toBuffer(); + this.findAndOptimizeScreenshot = function(tmpScreenshotPath, width) { + var that = this; + + debug('Starting screenshot transformation'); + + return this.openImage(tmpScreenshotPath) + + .then(function(image) { + that.deleteTmpFile(tmpScreenshotPath); + return that.resizeImage(image, width); + }) + + .then(this.toBuffer); + }; + + + this.openImage = function(imagePath) { + var deferred = Q.defer(); + + Jimp.read(imagePath, function(err, image){ + if (err) { + debug('Could not open imagePath %s', imagePath); + debug(err); + + deferred.reject(err); + } else { + debug('Image correctly open'); + deferred.resolve(image); + } + }); + + return deferred.promise; + }; + + + this.resizeImage = function(image, newWidth) { + var deferred = Q.defer(); + + var currentWidth = image.bitmap.width; + + if (currentWidth > 0) { + var ratio = newWidth / currentWidth; + + image.scale(ratio, function(err, image){ + if (err) { + debug('Could not resize image'); + debug(err); + + deferred.reject(err); + } else { + debug('Image correctly resized'); + deferred.resolve(image); + } + }); + } else { + deferred.reject('Could not resize an empty image'); + } + + return deferred.promise; + }; + + + this.toBuffer = function(image) { + var deferred = Q.defer(); + + image.quality(85).getBuffer(Jimp.MIME_JPEG, function(err, buffer){ + if (err) { + debug('Could not save image to buffer'); + debug(err); + + deferred.reject(err); + } else { + debug('Image correctly transformed to buffer'); + deferred.resolve(buffer); + } + }); + + return deferred.promise; }; diff --git a/lib/tools/redownload/contentTypeChecker.js b/lib/tools/redownload/contentTypeChecker.js index a37c7d5..ea06d88 100644 --- a/lib/tools/redownload/contentTypeChecker.js +++ b/lib/tools/redownload/contentTypeChecker.js @@ -1,12 +1,20 @@ -var debug = require('debug')('ylt:contentTypeChecker'); -var Q = require('q'); -var FileType = require('file-type'); -var isSvg = require('is-svg'); -var isJson = require('is-json'); +var debug = require('debug')('ylt:contentTypeChecker'); +var Q = require('q'); +var isJpg = require('is-jpg'); +var isPng = require('is-png'); +var isSvg = require('is-svg'); +var isGif = require('is-gif'); +var isWebp = require('is-webp'); +var isWoff = require('is-woff'); +var isWoff2 = require('is-woff2'); +var isOtf = require('is-otf'); +var isTtf = require('is-ttf'); +var isEot = require('is-eot'); +var isJson = require('is-json'); var ContentTypeChecker = function() { - async function checkContentType(entry) { + function checkContentType(entry) { var deferred = Q.defer(); // Setting isSomething values: @@ -47,12 +55,12 @@ var ContentTypeChecker = function() { var foundType; try { - foundType = await findContentType(entry.weightCheck.bodyBuffer); + foundType = findContentType(entry.weightCheck.bodyBuffer); // If it's an image or a font, then rewrite. if (foundType !== null && (foundType.type === 'image' || foundType.type === 'webfont' || foundType.type === 'json')) { if (foundType.type !== entry.type) { - debug('Content type %s is wrong for %s. It should be %s.', entry.type, entry.url, foundType.type); + debug('Content type %s is wrong for %s. It should be %s.', entry.type, entry.ulr, foundType.type); } rewriteContentType(entry, foundType); } @@ -68,21 +76,52 @@ var ContentTypeChecker = function() { return deferred.promise; } - async function findContentType(bodyBuffer) { + function findContentType(bodyBuffer) { var bodyStr = bodyBuffer.toString(); + if (isJpg(bodyBuffer)) { + return contentTypes.jpeg; + } + + if (isPng(bodyBuffer)) { + return contentTypes.png; + } + // https://github.com/sindresorhus/is-svg/issues/7 if (/