Skip to content
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

Fix HAR extraction for conflicting URL paths #13

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 37 additions & 4 deletions src/har-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as path from "path";

const filenamify = require("filenamify");
const humanizeUrl = require("humanize-url");
const makeDir = require("make-dir");

export const getEntryContentAsBuffer = (entry: Entry): Buffer | undefined => {
const content = entry.response.content;
const text = content.text;
Expand All @@ -22,7 +22,7 @@ export const convertEntryAsFilePathFormat = (entry: Entry, removeQueryString: bo
const requestURL = entry.request.url;
const stripSchemaURL: string = humanizeUrl(removeQueryString ? requestURL.split("?")[0] : requestURL);
const dirnames: string[] = stripSchemaURL.split("/").map((pathname) => {
return filenamify(pathname, {maxLength: 255});
return filenamify(pathname, { maxLength: 255 });
});
const fileName = dirnames[dirnames.length - 1];
if (
Expand All @@ -43,16 +43,49 @@ export interface ExtractOptions {
removeQueryString?: boolean;
}

const ensureDir = (dirPath: string): void => {
// Split the path into components
const parts = dirPath.split(path.sep);
console.log(parts);

// Iterate through each part of the path
for (let i = 1; i <= parts.length; i++) {
const subPath = parts.slice(0, i).join(path.sep);

if (fs.existsSync(subPath)) {
if (fs.lstatSync(subPath).isFile()) {
// If subPath is a file, move it to an index file within a new directory
const fileContent = fs.readFileSync(subPath); // Read the file content
fs.unlinkSync(subPath); // Remove the file
fs.mkdirSync(subPath); // Create the directory
fs.writeFileSync(path.join(subPath, "index"), fileContent); // Write the file content to index
}
} else {
// If subPath does not exist, create the directory
fs.mkdirSync(subPath);
}
}
};

export const extract = (harContent: Har, options: ExtractOptions) => {
harContent.log.entries.forEach((entry) => {
const buffer = getEntryContentAsBuffer(entry);
if (!buffer) {
return;
}
const outputPath = path.join(options.outputDir, convertEntryAsFilePathFormat(entry, options.removeQueryString));
if (!options.dryRun) {
makeDir.sync(path.dirname(outputPath));
const outputDir = path.dirname(outputPath);

if (!options.dryRun && outputDir.length > 0) {
try {
ensureDir(outputDir);
} catch (error: any) {
if (error?.code !== "EEXIST") {
throw error;
}
}
}

if (options.verbose) {
console.log(outputPath);
}
Expand Down
Loading