From a1f09ffedc0e73853261264a125dcf18f48e95d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20M=C3=A9tais?= Date: Tue, 7 Nov 2023 19:59:13 +0200 Subject: [PATCH 1/2] Fix screenshotHandler and re-introduce some unwillingly deleted improvements --- lib/index.js | 3 +- lib/screenshotHandler.js | 89 +++------------------- lib/tools/redownload/contentTypeChecker.js | 77 ++++++------------- lib/tools/redownload/imageDimensions.js | 4 + lib/tools/redownload/redownload.js | 4 +- package.json | 11 +-- 6 files changed, 41 insertions(+), 147 deletions(-) diff --git a/lib/index.js b/lib/index.js index 68bf50c..cca59fe 100644 --- a/lib/index.js +++ b/lib/index.js @@ -33,10 +33,11 @@ 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(data.params.options.screenshot, 600) + ScreenshotHandler.findAndOptimizeScreenshot(screenshotTmpPath, 600) .then(function(screenshotBuffer) { debug('Screenshot optimized, now saving...'); diff --git a/lib/screenshotHandler.js b/lib/screenshotHandler.js index 3aab753..c175fd1 100644 --- a/lib/screenshotHandler.js +++ b/lib/screenshotHandler.js @@ -1,90 +1,19 @@ -var debug = require('debug')('ylt:screenshotHandlerAgent'); -var Jimp = require('jimp'); +var debug = require('debug')('ylt:screenshotHandler'); +var sharp = require('sharp'); 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 = 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; + this.findAndOptimizeScreenshot = async function(tmpScreenshotPath, width) { + return sharp(tmpScreenshotPath) + .resize({width: 600}) + .jpeg({quality: 85}) + .toBuffer(); }; diff --git a/lib/tools/redownload/contentTypeChecker.js b/lib/tools/redownload/contentTypeChecker.js index ea06d88..a37c7d5 100644 --- a/lib/tools/redownload/contentTypeChecker.js +++ b/lib/tools/redownload/contentTypeChecker.js @@ -1,20 +1,12 @@ -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 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 ContentTypeChecker = function() { - function checkContentType(entry) { + async function checkContentType(entry) { var deferred = Q.defer(); // Setting isSomething values: @@ -55,12 +47,12 @@ var ContentTypeChecker = function() { var foundType; try { - foundType = findContentType(entry.weightCheck.bodyBuffer); + foundType = await 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.ulr, foundType.type); + debug('Content type %s is wrong for %s. It should be %s.', entry.type, entry.url, foundType.type); } rewriteContentType(entry, foundType); } @@ -76,54 +68,23 @@ var ContentTypeChecker = function() { return deferred.promise; } - function findContentType(bodyBuffer) { + async 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 (/ Date: Tue, 7 Nov 2023 19:59:50 +0200 Subject: [PATCH 2/2] v3.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e91a56a..eacce92 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yellowlabtools", - "version": "3.0.0", + "version": "3.0.1", "description": "A tool that audits a webpage for performance and front-end quality issues", "license": "GPL-2.0", "author": {