import { Buffer } from "node:buffer"; import { decodeInputrcKeySequence } from "./key-sequence.js"; export class InputrcMacroError extends Error { constructor(message: string) { super(message); this.name = "InputrcMacroError"; } } export type MacroExpansionOptions = { maxBytes?: number; }; /** Decode one inputrc macro while enforcing a bounded emitted-input budget. */ export function expandInputrcMacro( source: string, options: MacroExpansionOptions = {}, ): string { const maxBytes = options.maxBytes ?? 4096; if (!Number.isInteger(maxBytes) || maxBytes < 1) { throw new RangeError("Inputrc macro maxBytes must be a positive integer"); } const expanded = decodeInputrcKeySequence(source); if (Buffer.byteLength(expanded, "utf8") > maxBytes) { throw new InputrcMacroError( `Inputrc macro exceeds ${maxBytes} emitted bytes`, ); } return expanded; }