Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@bednarik_radek/har-to-expectations

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@bednarik_radek/har-to-expectations - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

21

dist/cli/cli.js
import { createCommand } from "commander";
import { readHarFile, writeJsonFile } from "../io/io.js";
import { parser } from "../parser/har-to-expectations.js";
import { readHarFile, readJsonFile, writeJsonFile } from "../io/io.js";
import { parser, merger } from "../parser/har-to-expectations.js";
import { logger } from "../logger/logger.js";

@@ -22,2 +22,19 @@ const log = logger.child({ module: "cli" });

});
cli
.command("merge")
.description("merge existing .json expectations file with generated .har file")
.argument("<harPath>", "path to .har source file")
.argument("<jsonPath>", "path to .json expectations destination file")
.argument("<regex>", "regExp which will filter wanted requests/responses to be converted into expectations")
.option("-u, --update [bool]", "whether to update existing expectations in .json file", false)
.action(async (harPath, jsonPath, regex, options) => {
const harObject = await readHarFile(harPath);
const existingJsonObject = await readJsonFile(jsonPath);
const parsedHarObject = parser(harObject, regex);
console.log(JSON.stringify(parsedHarObject));
if (parsedHarObject !== null) {
const merged = merger(existingJsonObject, parsedHarObject, options.update);
await writeJsonFile(merged, jsonPath);
}
});
//# sourceMappingURL=cli.js.map
import { Har } from "har-format";
export declare function readHarFile(filepath: string): Promise<Har>;
export declare function readJsonFile(filepath: string): Promise<any>;
export declare function writeJsonFile(data: any, filepath: string): Promise<void>;
//# sourceMappingURL=io.d.ts.map

@@ -16,2 +16,13 @@ import { readFile, writeFile } from "fs/promises";

}
export async function readJsonFile(filepath) {
const fullpath = resolve(filepath);
log.info(`reading .json file from ${fullpath}`);
try {
return JSON.parse(await readFile(fullpath, { encoding: "utf8" }));
}
catch (err) {
log.error(`func readJsonFile returned error: ${err}`);
throw err;
}
}
export async function writeJsonFile(data, filepath) {

@@ -18,0 +29,0 @@ const fullPath = resolve(filepath);

@@ -17,2 +17,3 @@ import { Har } from "har-format";

export declare function parser(harObject: Har, regex: string): MockExpectation[] | null;
export declare function merger(existingJsonObject: MockExpectation[], parsedHarObject: MockExpectation[], update: boolean): MockExpectation[];
//# sourceMappingURL=har-to-expectations.d.ts.map
import { logger } from "../logger/logger.js";
import ProgressBar from "../cli/progress-bar.js";
import lodash from "lodash";
const log = logger.child({ module: "parser-har-to-expectations" });

@@ -58,2 +59,40 @@ export function parser(harObject, regex) {

}
export function merger(existingJsonObject, parsedHarObject, update) {
try {
const bar = new ProgressBar(process.env["LOG_ENABLED"]);
bar.start(parsedHarObject.length);
log.info(`merging ${JSON.stringify(parsedHarObject)} into ${JSON.stringify(existingJsonObject)}`);
for (const expectation of parsedHarObject) {
bar.increment();
for (const existingExpectation of existingJsonObject) {
// it is the same expectation request
// and we do not want to update
if (isEqual(expectation.httpRequest, existingExpectation.httpRequest) && !update) {
break;
}
// it is the same expectation request
// and we want to update
if (isEqual(expectation.httpRequest, existingExpectation.httpRequest) && update) {
existingExpectation.httpResponse = expectation.httpResponse;
break;
}
}
// new expectation - so just add
existingJsonObject.push(expectation);
}
bar.stop();
log.info(`merge finished with result object: ${JSON.stringify(existingJsonObject)}`);
return existingJsonObject;
}
catch (error) {
log.error(`error in func merger: ${error}`);
throw error;
}
}
function isEqual(obj1, obj2) {
if (lodash.isEqual(obj1, obj2)) {
return true;
}
return false;
}
function getQueryParameters(queryParams) {

@@ -60,0 +99,0 @@ log.info("Parsing query parameters");

5

package.json
{
"name": "@bednarik_radek/har-to-expectations",
"version": "0.2.0",
"version": "0.3.0",
"description": "CLI utility to parse .har files and convert selected requests/responses to mock-server expectation objects saved in .json file",

@@ -13,2 +13,3 @@ "main": "dist/index.js",

"devBuild": "rimraf dist && tsc && cd dist && npm link && chmod +x bin/har.js && cd ..",
"bunBuild": "bun build src/index.ts --compile --target=node --outfile=bun-dist/app",
"publish": "npm publish --access public",

@@ -34,2 +35,3 @@ "test": "echo \"Error: no test specified\" && exit 1",

"@types/har-format": "^1.2.15",
"@types/lodash": "^4.17.0",
"@types/node": "^20.10.5",

@@ -52,2 +54,3 @@ "@typescript-eslint/eslint-plugin": "^6.16.0",

"commander": "^11.1.0",
"lodash": "^4.17.21",
"pino": "^8.17.2",

@@ -54,0 +57,0 @@ "pino-pretty": "^10.3.1"

@@ -33,5 +33,8 @@ # har-to-expectations

convert <harPath> <jsonPath> <regex> converts .har file to .json file with expectations.
merge [options] <harPath> <jsonPath> <regex> merges new .har file into existing .json expectations file.
help [command] display help for command
```
## conversion
### Example

@@ -74,2 +77,14 @@

## Merging
### Example
- we want to add new expectations to the **existing** .json file
- if expectation alredy exists, we want to update the `httpResponse` part - thus updating the mocked values
```bash
npx har merge ./network-log.har ./existing-expectations.json --update
```
## Logging

@@ -76,0 +91,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc