UNPKG

@obarlik/streaming-pipeline-core

Version:

🔄 Memory-efficient circular buffer streaming pipeline with universal processing - by Codechu

114 lines (113 loc) • 3.98 kB
/** * Streaming interfaces - single buffer system * Universal approach with circular buffer and bounded lookahead/lookbehind */ import { CircularStreamBuffer, TextCircularBuffer, BufferState } from './CircularStreamBuffer'; export interface StreamPosition { readonly line: number; readonly column: number; readonly offset: number; } export interface StreamChunk<TData = any> { readonly type: string; readonly content: string; readonly data?: TData; readonly position?: StreamPosition; readonly metadata?: Record<string, any>; } export interface StreamingContext { readonly buffer: CircularStreamBuffer | TextCircularBuffer; readonly position: StreamPosition; readonly bufferState: BufferState; readonly encoding: string; readonly metadata?: Record<string, any>; readonly isEOF: boolean; readonly needsRefill: boolean; readonly canAdvance: boolean; } export interface IStreamProcessor<TData = any> { readonly name: string; readonly priority: number; canProcess(context: StreamingContext): boolean; process(context: StreamingContext): { chunks: StreamChunk<TData>[]; advance: number; }; readonly preferredLookBehind?: number; readonly preferredLookAhead?: number; readonly encoding?: string; getState?(): any; setState?(state: any): void; resetState?(): void; } export interface IStreamRenderer<TData = any, TOutput = string> { readonly format: string; renderChunk(chunk: StreamChunk<TData>): TOutput; renderChunks(chunks: StreamChunk<TData>[]): TOutput; } export interface IStreamingPipeline<TOutput = string> { processStream(input: string | Uint8Array | ReadableStream, format: string, options?: StreamOptions): AsyncIterable<TOutput>; registerProcessor(processor: IStreamProcessor): void; registerRenderer(renderer: IStreamRenderer): void; configureBuffer(options: BufferOptions): void; reset(): void; } export interface StreamOptions { readonly lookBehindSize?: number; readonly lookAheadSize?: number; readonly encoding?: string; readonly autoRefill?: boolean; readonly preserveState?: boolean; readonly refillThreshold?: number; readonly chunkSize?: number; readonly metadata?: Record<string, any>; } export interface BufferOptions { readonly lookBehindSize: number; readonly lookAheadSize: number; readonly encoding: string; readonly autoCompact: boolean; } export interface StreamEvent { readonly type: 'chunk' | 'refill' | 'eof' | 'error'; readonly data?: any; readonly position?: StreamPosition; readonly timestamp: number; } export declare class StreamingUtils { /** * Check if current position matches a pattern */ static matchesPattern(context: StreamingContext, pattern: string | RegExp): boolean; /** * Get text content around current position */ static getContext(context: StreamingContext, behind?: number, ahead?: number): string; /** * Find next occurrence of pattern */ static findNext(context: StreamingContext, pattern: string, maxDistance?: number): number; /** * Check if current position is at word boundary */ static isWordBoundary(context: StreamingContext): boolean; } /** * Base processor class with common functionality */ export declare abstract class BaseStreamProcessor implements IStreamProcessor { abstract readonly name: string; abstract readonly priority: number; readonly preferredLookBehind: number; readonly preferredLookAhead: number; readonly encoding: string; abstract canProcess(context: StreamingContext): boolean; abstract process(context: StreamingContext): { chunks: StreamChunk[]; advance: number; }; protected createChunk(type: string, content: string, data?: any, position?: StreamPosition): StreamChunk; getState?(): any; setState?(state: any): void; resetState?(): void; }