@safeapi/safeapi
Version:
SafeAPI: Secure, deterministic, and tamper-resistant API policy engine for Node and browser.
68 lines (67 loc) • 1.97 kB
JavaScript
/*
@ts-ignore
Node built-ins (fs, path, process) used here are available at runtime,
but we intentionally avoid pulling @types/node into the workspace to
prevent pnpm registry resolution conflicts before publishing.
*/
/**
* @internal
* SafeAPI CLI Simulator Entrypoint
*/
// @ts-ignore
import fs from "fs";
// @ts-ignore
import path from "path";
import { simulateRequest } from "./SafeApiCLISimulator";
import { renderReplay } from "./visualReplay";
function loadJson(filePath) {
// @ts-ignore
return JSON.parse(fs.readFileSync(path.resolve(filePath), "utf8"));
}
function main() {
// @ts-ignore
const args = process.argv.slice(2);
let policyPath = "";
let inputPath = "";
let auditPath = "";
let trace = false;
let visual = false;
for (let i = 0; i < args.length; i++) {
if (args[i] === "--policy")
policyPath = args[++i];
if (args[i] === "--input")
inputPath = args[++i];
if (args[i] === "--trace")
trace = true;
if (args[i] === "--replay")
auditPath = args[++i];
if (args[i] === "--visual")
visual = true;
}
if (auditPath) {
const audit = loadJson(auditPath);
if (visual) {
console.log(renderReplay(audit));
}
else {
console.log(JSON.stringify(audit, null, 2));
}
// @ts-ignore
process.exit(0);
}
if (!policyPath || !inputPath) {
console.error("Usage: pnpm safeapi:simulate --policy <policy.json> --input <request.json> [--trace] [--visual] [--replay <audit.json>]");
// @ts-ignore
process.exit(1);
}
const policy = loadJson(policyPath);
const input = loadJson(inputPath);
const sim = simulateRequest(policy, input, { trace });
if (visual) {
console.log(renderReplay(sim.auditPayload));
}
else {
console.log(JSON.stringify(sim, null, 2));
}
}
main();