Skip to content

Commit

Permalink
feat: enabled extra-header
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud committed Nov 7, 2023
1 parent 4d3e51b commit db459d6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
14 changes: 14 additions & 0 deletions tests/script.js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ node script.js --urls=https://www.ecoindex.fr/,https://www.ecoindex.fr/comment-c
node script.js --urls-file=example-urls-list
```

<!-- copier/coller ici le résultat de la command `node ./script.js --help` -->

```bash
Options:
--version Show version number [boolean]
-d, --demo Use demo URLs. [boolean] [default: false]
-f, --urls-file Input file path. 1 url per line. [string]
-u, --urls URLs to process. Comma separated. [string]
-o, --output Output folder. [string] [default: "./reports"]
-c, --extra-header Extra object config for Lighthouse. JSON string.
[string] [default: null]
--help Show help [boolean]
```

## Objectifs :

- [x] Pouvoir passer des urls en argument ;
Expand Down
1 change: 1 addition & 0 deletions tests/script.js/extra-header.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "Cookie": "monster=blue", "x-men": "wolverine" }
3 changes: 2 additions & 1 deletion tests/script.js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"description": "Test du script lighthouse-plugin-ecoindex",
"main": "script.js",
"scripts": {
"test.js": "node ./script.js"
"test": "node ./script.js",
"help": "node ./script.js --help"
},
"author": "",
"license": "ISC",
Expand Down
50 changes: 39 additions & 11 deletions tests/script.js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async function endEcoindexPageMesure() {
* @param {boolean} isWarm
* @returns {lighthouse.Config}
*/
function getConfig(isWarm = false, extraConfig = {}) {
function getConfig(isWarm = false) {
return {
name: isWarm ? 'Warm navigation' : 'Cold navigation',
config: {
Expand All @@ -92,7 +92,6 @@ function getConfig(isWarm = false, extraConfig = {}) {
'lighthouse-plugin-ecoindex/gatherers/nodes-minus-svg-gatherer',
},
],
...extraConfig,
},
}
}
Expand Down Expand Up @@ -125,19 +124,18 @@ async function captureReport() {
default: './reports',
description: 'Output folder.',
})
.option('extra-config', {
.option('extra-header', {
alias: 'c',
type: 'string',
default: null,
description: 'Extra object config for Lighthouse. JSON string.',
})
.help().argv

const filePath = argv['urls-file']
const isDemoMode = argv['demo']
const output = argv['output']
const extraConfig = argv['extra-config']
? JSON.parse(argv['extra-config'])
: null
const extraHeader = argv['extra-header']
let urls = argv['urls'] ? argv['urls'].split(',') : null

if (!filePath && !urls && !isDemoMode) {
Expand All @@ -156,23 +154,45 @@ async function captureReport() {
console.error(`Error reading file from disk: ${err}`)
process.exit(1)
} else {
console.log(`File readed.`)
console.log(`File ${filePath} readed.`)
urls = data.split('\n')
if (urls[urls.length - 1] === '') {
urls.pop()
}
}
})
}
let extraHeaderObj = null
if (extraHeader && typeof extraHeader === 'string') {
console.log(`Parsing extra-header JSON...`)
try {
extraHeaderObj = JSON.stringify(JSON.parse(extraHeader))
console.log(`Parsing extra-header JSON as a string.`)
} catch (e) {
// console.error(`Error parsing extra-header JSON: ${e}`)
console.log(`Reading file ${extraHeader}...`)
const resolvedPath = await path.resolve(extraHeader)

await fs.readFile(resolvedPath, 'utf8', async (err, data) => {
// Make this function async
if (err) {
console.error(`Error reading file from disk: ${err}`)
process.exit(1)
} else {
console.log(`File ${extraHeader} readed.`)
extraHeaderObj = JSON.stringify(JSON.parse(data))
}
})
}
}

await new Promise(r => setTimeout(r, 3 * 1000))
console.log(`Mesure(s) start 🚀`)

if (isDemoMode) {
console.log('No URL provided, using default URL')
urls = DEFAULT_URLS
}
console.log('Reports', urls)
console.log(SEPARATOR)

// Launch a headless browser.
const browser = await puppeteer.launch({
Expand All @@ -187,10 +207,18 @@ async function captureReport() {

// Create a new page.
const page = await browser.newPage()
console.log('Reports', urls)
console.log(SEPARATOR)
if (extraHeaderObj) {
console.log(`Setting extra-header...`)
console.log(`extra-header`, extraHeaderObj)
await page.setExtraHTTPHeaders(extraHeaderObj)
console.log(SEPARATOR)
}
// Get a session handle to be able to send protocol commands to the page.
const session = await page.target().createCDPSession()
// Get a flow handle to be able to send protocol commands to the page.
const flow = await startFlow(page, getConfig(false, extraConfig))
const flow = await startFlow(page, getConfig(false))

console.log(`Mesuring...`)
console.log(`Mesure 0: ${urls[0]}`)
Expand All @@ -207,7 +235,7 @@ async function captureReport() {
for (var i = 1; i < urls.length; i++) {
if (urls[i].trim() !== '') {
console.log(`Mesure ${i}: ${urls[i]}`)
await flow.navigate(urls[i], getConfig(true, extraConfig))
await flow.navigate(urls[i], getConfig(true))
await startEcoindexPageMesure(page, session)
await endEcoindexPageMesure()
}
Expand Down

0 comments on commit db459d6

Please sign in to comment.