This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in a first-pass at a node.js-based scanner.
- Loading branch information
1 parent
63a195c
commit 47aca99
Showing
4 changed files
with
178 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Snyk (https://snyk.io) policy file, patches or ignores known vulnerabilities. | ||
version: v1.14.1 | ||
ignore: {} | ||
patch: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* Lighthouse scanner | ||
* This module orchestrates parallel Lighthouse scans over one headless Chrome | ||
* instance. | ||
*/ | ||
|
||
const chromeLauncher = require('chrome-launcher'); | ||
const lighthouse = require('lighthouse'); | ||
const puppeteer = require('puppeteer'); | ||
|
||
|
||
var getBrowser = async () => { | ||
return await puppeteer.launch({ | ||
// TODO: Let executable path be overrideable. | ||
// executablePath: config.executablePath, | ||
headless: true, | ||
ignoreHTTPSErrors: true, | ||
args: [ | ||
'--no-sandbox', | ||
'--disable-gpu', | ||
'--single-process' | ||
] | ||
}); | ||
}; | ||
|
||
function launchChromeAndRunLighthouse(url, opts, config = null) { | ||
return chromeLauncher.launch({chromeFlags: opts.chromeFlags}).then(chrome => { | ||
opts.port = chrome.port; | ||
return lighthouse(url, opts, config).then(results => { | ||
return chrome.kill().then(() => results.lhr) | ||
}); | ||
}); | ||
} | ||
|
||
const opts = { | ||
chromeFlags: ['--headless', '--no-sandbox'] | ||
}; | ||
|
||
/* | ||
function configure() { | ||
let port; | ||
chromeLauncher.launch({ | ||
chromeFlags: ['--headless', '--no-sandbox'] | ||
}).then(chrome => { | ||
port = chrome.port | ||
}); | ||
} | ||
launchChromeAndRunLighthouse('https://www.whitehouse.gov', opts).then(results => { | ||
console.log(results); | ||
}); | ||
*/ | ||
|
||
getBrowser().then(async browser => { | ||
const url = 'https://www.whitehouse.gov'; | ||
const {lhr} = await lighthouse(url, { | ||
port: (new URL(browser.wsEndpoint())).port, | ||
output: 'json', | ||
logLevel: 'info', | ||
}); | ||
console.log(lhr); | ||
await browser.close(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict'; | ||
|
||
const lighthouse = require('lighthouse'); | ||
|
||
|
||
const LIGHTHOUSE_AUDITS = [ | ||
'color-contrast', | ||
'font-size', | ||
'image-alt', | ||
'input-image-alt', | ||
'performance-budget', | ||
'speed-index', | ||
'tap-targets', | ||
'timing-budget', | ||
'total-byte-weight', | ||
'unminified-css', | ||
'unminified-javascript', | ||
'uses-text-compression', | ||
'viewport', | ||
] | ||
|
||
|
||
// JS entry point for Lighthouse scan. | ||
module.exports = { | ||
scan: async (domain, environment, options, browser, page) => { | ||
const url = 'https://' + domain; | ||
try { | ||
const output = await lighthouse(url, { | ||
port: (new URL(browser.wsEndpoint())).port, | ||
onlyAudits: LIGHTHOUSE_AUDITS, | ||
|
||
disableStorageReset: false, | ||
saveAssets: false, | ||
listAllAudits: false, | ||
listTraceCategories: false, | ||
printConfig: false, | ||
output: [ 'json' ], | ||
chromeFlags: '', | ||
enableErrorReporting: false, | ||
logLevel: 'silent', | ||
outputPath: 'stdout', | ||
}); | ||
return output.lhr.audits; | ||
|
||
} catch (exc) { | ||
return { | ||
error: exc.message | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters