Files
seal-use/src/cli/printer.ts
T

80 lines
3.0 KiB
TypeScript

import { writeFileSync } from "node:fs";
import { booleanOption, stringOption } from "./parser.js";
import { PayloadTooLargeError } from "./errors.js";
import type { OutputOptions } from "./types.js";
const RULE_LIST_FULL_COUNT_LIMIT = 100;
const RULE_LIST_FULL_BYTES_LIMIT = 120_000;
export const RULE_SUMMARY_FIELDS = ["id", "code", "status", "strictness", "scope", "createdAt", "updatedAt"];
const DEFAULT_OUTPUT_BYTES_LIMIT = 200_000;
export function formatConsoleOutput(values: unknown[]): string {
return values.map((value) => (typeof value === "string" ? value : String(value))).join(" ");
}
export function joinOutput(lines: string[]): string {
if (!lines.length) return "";
return `${lines.join("\n")}\n`;
}
export function redactSensitiveText(value: string): string {
return value
.replace(/([?&](?:accessToken|token|returnToken)=)[^&\s]+/gi, "$1<redacted>")
.replace(/(Bearer\s+)[A-Za-z0-9._~+/=-]+/gi, "$1<redacted>");
}
export function printJson(value: unknown, options: OutputOptions = {}) {
protectLargeOutput(value, options);
const json = `${JSON.stringify(value, null, 2)}\n`;
if (options.outputFile) {
writeFileSync(options.outputFile, json);
console.log(JSON.stringify({ outputFile: options.outputFile, bytes: Buffer.byteLength(json, "utf-8") }, null, 2));
return;
}
console.log(json.trimEnd());
}
function protectLargeOutput(result: unknown, options: OutputOptions = {}) {
if (options.allowFull || options.outputFile) return;
const bodyBytes = Buffer.byteLength(JSON.stringify(result), "utf-8");
if (bodyBytes > DEFAULT_OUTPUT_BYTES_LIMIT) {
throw new PayloadTooLargeError(
`Output is large (${bodyBytes} bytes). Use --summary, --fields, --count, or --output-file; pass --full only when you really need the full response.`
);
}
}
export function protectLargeRuleListOutput(result: unknown, allowFull: boolean) {
if (allowFull) return;
const data = result && typeof result === "object" ? (result as {
count?: unknown;
rules?: unknown;
}) : {};
const count = typeof data.count === "number" ? data.count : undefined;
const rulesCount = Array.isArray(data.rules) ? data.rules.length : undefined;
const bodyBytes = Buffer.byteLength(JSON.stringify(result), "utf-8");
const effectiveCount = count ?? rulesCount ?? 0;
if (effectiveCount > RULE_LIST_FULL_COUNT_LIMIT || bodyBytes > RULE_LIST_FULL_BYTES_LIMIT) {
throw new PayloadTooLargeError(
`Rule list is large (${effectiveCount} rules, ${bodyBytes} bytes). Use "seal-use rules list --summary" or "seal-use rules count"; pass --full only when you really need full descriptions.`,
"seal-use rules list --summary"
);
}
}
export function outputOptions(options: Record<string, string | boolean>): OutputOptions {
return {
allowFull: booleanOption(options.full) === true,
outputFile: stringOption(options.outputFile) ?? stringOption(options["output-file"])
};
}
import { renderHelp } from "./help.js";
export function printHelp(target: string[] = []) {
console.log(renderHelp(target));
}