Skip to content

Instantly share code, notes, and snippets.

@prantlf
Last active September 29, 2019 10:06
Show Gist options
  • Save prantlf/aae5faf752534bc611f23f95e8f1cb39 to your computer and use it in GitHub Desktop.
Save prantlf/aae5faf752534bc611f23f95e8f1cb39 to your computer and use it in GitHub Desktop.
Extract URL paths from a HAR file
// 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