Last active
September 29, 2019 10:06
-
-
Save prantlf/aae5faf752534bc611f23f95e8f1cb39 to your computer and use it in GitHub Desktop.
Extract URL paths from a HAR file
This file contains hidden or 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
// Useful for generating a list of paths for performance testing using `wrk` | |
// and https://github.com/timotta/wrk-scripts/blob/master/multiplepaths.lua. | |
const description = `Usage: node har2paths (file.har) > paths.txt | |
Prints URL paths of all server calls made while loading the first page | |
from the HAR file, which has been created for a SPA.` | |
const harName = process.argv[2] | |
if (!harName) { | |
console.log(description) | |
return process.exit(1) | |
} | |
const { readFile } = require('fs') | |
const { promisify } = require('util') | |
const readFileAsync = promisify(readFile) | |
function log (message) { | |
process.stderr.write(message) | |
process.stderr.write('\n') | |
} | |
async function main () { | |
try { | |
log(`Loading "${harName}"...`) | |
const harString = await readFileAsync(harName, 'utf-8') | |
log('Parsing the HAR content...') | |
const { log:harContent } = JSON.parse(harString) | |
const { title: pageURL, id: pageID } = harContent.pages[0] | |
log(`Analysing the page ${pageURL}...`) | |
const assetPaths = harContent.entries | |
.filter(({ pageref }) => pageref === pageID) | |
.map(({ request }) => new URL(request.url).pathname) | |
log(`${assetPaths.length} static assets found.`) | |
const printOutput = assetPaths.join('\n') | |
console.log(printOutput) | |
} catch (error) { | |
console.error(error) | |
process.exitCode = 1 | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment