import { text, } from 'node:stream/consumers'; /** Sentinel returned by writers for hook outputs that intentionally emit no stdout. A unique symbol avoids treating an empty string as an out-of-band signal. */ const NO_STDOUT: unique symbol = Symbol('claude-code-plugin-runtime/hook-intentionally-emits-no-stdout-bytes',); /** Serialized writer result, or {@link NO_STDOUT} when the hook protocol should receive no stdout bytes. */ type WriterOutput = string | typeof NO_STDOUT; /** Parser converts the raw stdin string into the typed event the handler consumes. Each parser asserts the trusted JSON shape at its own boundary; inputs come from Claude Code's hook dispatch system or from JSON files written by sibling hooks in this package, both trusted by contract. @example ```ts const parser: Parser = function jsonParser(raw): PreToolUseInput { // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- trusted JSON contract from Claude Code hook system return JSON.parse(raw) as PreToolUseInput; }; ``` */ type Parser = (raw: string,) => TInput; /** Handler is the pure function at the heart of every hook plugin. Receives a parsed event and returns the response payload. Synchronous or async; the runtime awaits the result. */ type HookHandler = (input: TInput,) => TOutput | Promise; /** Writer serializes the handler output for stdout. Returns a string written verbatim, or {@link NO_STDOUT} for intentional silence. String outputs receive no trailing newline, matching the wire convention Claude Code expects. */ type Writer = (output: TOutput,) => WriterOutput; /** Runtime shell for a hook plugin entry script. Reads stdin to EOF, runs the parser, dispatches to the handler, and writes the writer's serialized result to stdout. Each generated per-plugin entry script consists of importing the handler, parser, and writer for that plugin and calling {@link runHookPlugin} once at the top level. @example ```ts #!/usr/bin/env node import { runHookPlugin } from '../runtime/handler-runtime.ts'; import { guardrailHandler, guardrailParser, guardrailWriter } from '../handler/guardrail.ts'; await runHookPlugin({ parser: guardrailParser, handler: guardrailHandler, writer: guardrailWriter, }); ``` */ async function runHookPlugin( { parser, handler, writer, }: { readonly parser: Parser; readonly handler: HookHandler; readonly writer: Writer; }, ): Promise { /** Full stdin payload from Claude Code, awaited to EOF before parsing. */ const raw = await text(process.stdin,); /** Parsed hook event passed to the plugin handler below. */ const event = parser(raw,); /** Handler response; serialized by the writer for stdout. */ const output = await handler(event,); /** Serialized payload from the writer; the sentinel means the hook intentionally emits no stdout bytes. */ const serialized = writer(output,); if (serialized !== NO_STDOUT) { process.stdout .write(serialized,); } } export type { HookHandler, Parser, Writer, WriterOutput, }; export { NO_STDOUT, runHookPlugin, };