/** * vapor-chamber - streaming JSON parser * * Adapted from a dependency-free incremental JSON parser (bytes -> state * machine -> values), for progressively consuming a `fetch()` response body * (LLM/AI streaming completions, SSE tokens, large exports) without * buffering the whole payload. Framework-agnostic - no Vue imports; pair * with `http.ts`'s `HttpClient` or a bare `fetch()`. * * One small handler per state group (the CDCC guideline, * docs/whitepaper.md 3.5) - notably, the keyword states * (`true`/`false`/`null`) are one target-string index walk, not a state per * letter. * * @example * const parser = createStreamParser({ onValue: (key, value, path) => console.log(key, value) }); * const response = await fetch('/api/stream'); * await parser.stream(response); * * @example Manual chunk feeding * const parser = createStreamParser({ onValue: (k, v) => items.push(v) }); * parser.write('{"items":['); * parser.write('1,2,3]}'); * parser.end(); */ export type StreamParserPath = ReadonlyArray; export type StreamParserError = { message: string; state: number; depth: number; path: StreamParserPath; }; export type StreamParserCallbacks = { /** Emitted for each complete value (object/array members and top-level scalars). */ onValue?: (key: string, value: unknown, path: StreamParserPath) => void; onObjectStart?: (path: StreamParserPath) => void; onObjectEnd?: (path: StreamParserPath) => void; onArrayStart?: (path: StreamParserPath) => void; onArrayEnd?: (path: StreamParserPath) => void; onError?: (error: StreamParserError) => void; onEnd?: () => void; }; export type StreamParserOptions = { /** Max nesting depth. Default: 256. */ maxDepth?: number; }; export type ParserSnapshot = { state: number; depth: number; path: StreamParserPath; }; export declare class StreamParser { private readonly callbacks; private state; private isKey; private hexIndex; private readonly hexBuf; private readonly parents; private readonly path; private readonly arrayIndices; private readonly strBuf; private numStr; private currentKey; private keywordTarget; private keywordIndex; private readonly maxDepth; constructor(callbacks?: StreamParserCallbacks, options?: StreamParserOptions); /** Feed a string chunk to the parser. */ write(chunk: string): void; /** * Signal end of input. Flushes a pending number that is complete, one that * ends in a digit. An unfinished one (`-`, `1.`, `1e`, `1e+`) is an error, * as JSON.parse and the next character mid-stream say, never a value * (tests/stream-parser-malformed.test.ts). */ end(): void; /** * Stream from a fetch Response (ReadableStream body). * * A body-less response (204, 304, a HEAD reply, or a hand-built * `new Response(null)`) is treated as an empty stream rather than a crash. * Asserting the body away would throw on `getReader`, as a rejection that * skips `onError`. Ending immediately is exactly what a present but * zero-chunk body does: the read loop exits at once, the final * `decoder.decode()` contributes nothing, and `end()` runs - so this is the * same path, not a special case. `end()` still reports an unclosed structure * if the stream stops mid-value. */ stream(response: Response): Promise; /** Current parser position (for pause/resume). */ getState(): ParserSnapshot; /** Reset to initial state. */ reset(): void; private codepoint; private handleValue; private handleAfterValue; private handleKey; private handleAfterKey; private handleString; private handleEscape; private handleHex; /** `true`/`false`/`null` - a single target-string index walk instead of * ten near-duplicate per-letter states. */ private startKeyword; private pendingKeywordValue; /** True from an array's `[` until its first non-whitespace character. */ private arrayOpened; private handleKeyword; private handleNumber; private openObject; private openArray; private closeObject; private closeArray; private afterValue; private emitValue; private closeNumber; private err; } /** Factory matching the rest of vapor-chamber's `createX()` naming convention. */ export declare function createStreamParser(callbacks?: StreamParserCallbacks, options?: StreamParserOptions): StreamParser; //# sourceMappingURL=stream-parser.d.ts.map