Skip to content

[WRFL-2471] Upgrade dependencies #623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"express": "^4.17.1",
"html-minifier": "^4.0.0",
"lighthouse": "^9.6.3",
"puppeteer": "^18.0.0"
"puppeteer": "24.8.2"
},
"engines": {
"node": ">=18.14.0"
Expand Down
6 changes: 3 additions & 3 deletions src/e2e/mocks/puppeteer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ const puppeteer = () =>
jest.unstable_mockModule('puppeteer', () => {
return {
default: {
createBrowserFetcher: () => ({
localRevisions: () => Promise.resolve(['123']),
revisionInfo: () => Promise.resolve({ executablePath: 'path' }),
launch: () => Promise.resolve({
wsEndpoint: () => 'ws://127.0.0.1:9222/devtools/browser/xyz',
close: () => Promise.resolve(),
}),
},
};
Expand Down
13 changes: 11 additions & 2 deletions src/lib/persist-results/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ import { dirname } from 'path';
import { mkdir, writeFile } from 'fs/promises';

const persistResults = async ({ report, path }) => {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, report);
console.log('Attempting to persist report to:', path);
console.log('Report directory:', dirname(path));

try {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, report);
console.log('Successfully wrote report to:', path);
} catch (error) {
console.error('Failed to persist report:', error);
throw error; // Re-throw to maintain existing error handling
}
};

export default persistResults;
6 changes: 2 additions & 4 deletions src/lib/run-audit-with-server/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { join } from 'path';

import { formatResults } from '../../format.js';
import { runLighthouse, getBrowserPath } from '../../run-lighthouse.js';
import { runLighthouse } from '../../run-lighthouse.js';
import persistResults from '../persist-results/index.js';
import getServer from '../get-server/index.js';

Expand All @@ -16,13 +16,11 @@ const runAuditWithServer = async ({
try {
const { server } = getServer({ serveDir: serveDir, auditUrl: url });

const browserPath = await getBrowserPath();

const { error, results } = await new Promise((resolve) => {
const instance = server.listen(async () => {
try {
const fullPath = path ? `${server.url}/${path}` : server.url;
const results = await runLighthouse(browserPath, fullPath, settings);
const results = await runLighthouse(fullPath, settings);
resolve({ error: false, results });
} catch (error) {
resolve({ error });
Expand Down
8 changes: 4 additions & 4 deletions src/lib/run-audit-with-url/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { formatResults } from '../../format.js';
import { runLighthouse, getBrowserPath } from '../../run-lighthouse.js';
import { runLighthouse } from '../../run-lighthouse.js';

const runAuditWithUrl = async ({ path = '', url, thresholds, settings }) => {
try {
const browserPath = await getBrowserPath();

const getResults = async () => {
const fullPath = path ? `${url}/${path}` : url;
const results = await runLighthouse(browserPath, fullPath, settings);
const results = await runLighthouse(fullPath, settings);

try {
return { results };
Expand All @@ -19,6 +17,7 @@ const runAuditWithUrl = async ({ path = '', url, thresholds, settings }) => {
const { error, results } = await getResults();

if (error) {
console.log('error line 22 run audit with url', error);
return { error };
} else {
const { summary, shortSummary, details, report, errors, runtimeError } =
Expand All @@ -37,6 +36,7 @@ const runAuditWithUrl = async ({ path = '', url, thresholds, settings }) => {
};
}
} catch (error) {
console.log('error line 41 run audit with url', error);
return { error };
}
};
Expand Down
70 changes: 52 additions & 18 deletions src/run-lighthouse.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,79 @@
import puppeteer from 'puppeteer';
import lighthouse from 'lighthouse';
import log from 'lighthouse-logger';
import chromeLauncher from 'chrome-launcher';
import log from 'lighthouse-logger';
import puppeteer from 'puppeteer';

export const getBrowserPath = async () => {
const browserFetcher = puppeteer.createBrowserFetcher();
const revisions = await browserFetcher.localRevisions();
if (revisions.length <= 0) {
throw new Error('Could not find local browser');
}
const info = await browserFetcher.revisionInfo(revisions[0]);
return info.executablePath;
};

export const runLighthouse = async (browserPath, url, settings) => {
export const runLighthouse = async (url, settings) => {
let chrome;
try {
const logLevel = 'error';
const logLevel = settings?.logLevel || 'error';
log.setLevel(logLevel);
chrome = await chromeLauncher.launch({
chromePath: browserPath,

// Let Puppeteer handle Chrome installation with its defaults
let executablePath;
try {
const browser = await puppeteer.launch({
headless: 'new',
args: [
'--no-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-software-rasterizer',
'--disable-setuid-sandbox',
'--no-zygote'
]
});

// Get the executable path from Puppeteer's browser instance
executablePath = browser.process().spawnArgs[0];
console.log('Using Chrome at:', executablePath);
await browser.close();
} catch (err) {
console.error('Error launching Chrome:', err.message);
throw err; // We need Chrome to continue
}

// Configure chrome-launcher
const launchOptions = {
chromeFlags: [
'--headless',
'--headless=new',
'--no-sandbox',
'--disable-gpu',
'--disable-dev-shm-usage',
'--disable-software-rasterizer',
'--disable-setuid-sandbox',
'--no-zygote'
],
logLevel,
});
handleSIGINT: true,
chromePath: executablePath
};

console.log('Chrome launch options:', launchOptions);

chrome = await chromeLauncher.launch(launchOptions);
console.log('Chrome launched on port:', chrome.port);
console.log('Starting Lighthouse audit for URL:', url);
const results = await lighthouse(
url,
{
port: chrome.port,
output: 'html',
logLevel,
onlyCategories: settings?.onlyCategories,
locale: settings?.locale || 'en-US',
formFactor: settings?.preset === 'desktop' ? 'desktop' : 'mobile',
},
settings,
);
console.log('Lighthouse audit completed');
return results;
} catch (error) {
console.error('Error during Lighthouse run:', error);
throw error;
} finally {
if (chrome) {
console.log('Cleaning up Chrome...');
await chrome.kill();
}
}
Expand Down
Loading
Loading