import { StreamingMiddlewareOptions, ServerStreamContext, SerializedStreamState, StreamConfig, StreamChunk } from '../types'; import { StreamPriority } from '../streaming-engine'; /** * Default middleware options. */ declare const DEFAULT_MIDDLEWARE_OPTIONS: Required; /** * Streaming-specific HTTP headers. */ declare const STREAMING_HEADERS: { readonly CONTENT_TYPE: "Content-Type"; readonly TRANSFER_ENCODING: "Transfer-Encoding"; readonly X_ACCEL_BUFFERING: "X-Accel-Buffering"; readonly CACHE_CONTROL: "Cache-Control"; readonly X_STREAM_BOUNDARY: "X-Stream-Boundary"; readonly X_STREAM_NONCE: "X-Stream-Nonce"; }; /** * Generates a cryptographically secure nonce for CSP. * * @returns Base64-encoded nonce string * * @example * ```typescript * const nonce = generateNonce(); * // Returns: "abc123def456..." * ``` */ export declare function generateNonce(): string; /** * Creates a server stream context for managing streaming output. * * @description * The server stream context provides a consistent interface for writing * streaming content regardless of the underlying HTTP server framework. * * @param writer - The write function for the response * @param flush - Optional flush function * @param options - Middleware options * @returns Server stream context * * @example * ```typescript * const context = createServerStreamContext( * (chunk) => res.write(chunk), * () => res.flush?.(), * { nonce: 'abc123' } * ); * * context.write('
Content
'); * context.flush(); * context.end(); * ``` */ export declare function createServerStreamContext(writer: (chunk: string | Uint8Array) => void, flush?: () => void, options?: Partial): ServerStreamContext; /** * Creates streaming response headers. * * @param options - Middleware options * @returns Headers object for streaming response */ export declare function createStreamingHeaders(options?: Partial): Record; /** * Creates early hints (103) headers for resource preloading. * * @param resources - Resources to preload * @returns Early hints headers * * @example * ```typescript * const hints = createEarlyHints([ * { href: '/styles.css', as: 'style' }, * { href: '/app.js', as: 'script' }, * ]); * ``` */ export interface PreloadResource { href: string; as: 'script' | 'style' | 'font' | 'image' | 'fetch'; crossOrigin?: 'anonymous' | 'use-credentials'; type?: string; } export declare function createEarlyHints(resources: PreloadResource[]): string[]; /** * Serializes stream state for client-side hydration. * * @description * Creates a serialized representation of stream state that can be * embedded in the HTML for client-side rehydration. * * @param boundaryId - Boundary identifier * @param content - Rendered content * @param metadata - Additional metadata * @returns Serialized stream state * * @example * ```typescript * const serialized = serializeStreamState('hero', '
Hero Content
'); * // Embed in HTML for hydration * ``` */ export declare function serializeStreamState(boundaryId: string, content: string, metadata?: Record): SerializedStreamState; /** * Creates a script tag for hydrating stream state on the client. * * @param states - Array of serialized stream states * @param nonce - CSP nonce * @returns HTML script tag string */ export declare function createHydrationScript(states: SerializedStreamState[], nonce?: string): string; /** * Deserializes stream state from hydration data. * * @param data - Serialized state data * @returns Parsed stream state or null if invalid */ export declare function deserializeStreamState(data: unknown): SerializedStreamState | null; /** * Creates HTML markers for stream boundaries. * * @description * These markers are used to identify stream boundary locations in the * HTML output, enabling client-side hydration and activation. */ export declare const StreamMarkers: { /** * Creates a start marker for a stream boundary. */ start: (boundaryId: string, config?: Partial) => string; /** * Creates an end marker for a stream boundary. */ end: (boundaryId: string) => string; /** * Creates a placeholder marker. */ placeholder: (boundaryId: string) => string; /** * Creates an error marker. */ error: (boundaryId: string, message: string) => string; }; /** * Creates a stream chunk for server-side streaming. * * @param data - Chunk data * @param boundaryId - Parent boundary ID * @param sequence - Chunk sequence number * @param isFinal - Whether this is the final chunk * @returns Stream chunk */ export declare function createServerChunk(data: string, boundaryId: string, sequence: number, isFinal?: boolean): StreamChunk; /** * Wraps content in stream boundary markers with activation script. * * @param boundaryId - Boundary identifier * @param content - Content to wrap * @param nonce - CSP nonce * @param config - Stream configuration * @returns Wrapped content string */ export declare function wrapBoundaryContent(boundaryId: string, content: string, nonce?: string, config?: Partial): string; /** * Creates a priority-sorted stream pipeline. * * @description * Organizes stream boundaries by priority for optimal streaming order. * * @param boundaries - Map of boundary configurations * @returns Sorted array of boundary entries */ export declare function createStreamingPipeline(boundaries: Map): Array<[string, StreamConfig]>; /** * Creates a flush schedule based on priorities. * * @description * Generates a schedule for when to flush content to the client * based on boundary priorities and defer times. * * @param boundaries - Boundary configurations * @returns Flush schedule with timing */ export interface FlushScheduleEntry { boundaryId: string; priority: StreamPriority; flushAfterMs: number; } export declare function createFlushSchedule(boundaries: Map): FlushScheduleEntry[]; /** * Request-like interface for middleware. */ export interface StreamingRequest { url: string; method: string; headers: Record; } /** * Response-like interface for middleware. */ export interface StreamingResponse { setHeader: (name: string, value: string) => void; write: (chunk: string | Uint8Array) => boolean; end: (chunk?: string | Uint8Array) => void; flush?: () => void; writableEnded?: boolean; } /** * Next function type for middleware. */ export type NextFunction = (error?: Error) => void; /** * Streaming middleware function type. */ export type StreamingMiddleware = (req: StreamingRequest, res: StreamingResponse, next: NextFunction) => void | Promise; /** * Creates streaming middleware for Express-style servers. * * @description * Factory function that creates middleware for setting up streaming * response handling with proper headers and context. * * @param options - Middleware options * @returns Middleware function * * @example * ```typescript * const streamingMiddleware = createStreamingMiddleware({ * compress: true, * enableEarlyHints: true, * }); * * app.use(streamingMiddleware); * ``` */ export declare function createStreamingMiddleware(options?: Partial): StreamingMiddleware; /** * Options for React streaming render. */ export interface RenderToStreamOptions { /** Bootstrap scripts to include */ bootstrapScripts?: string[]; /** Bootstrap modules to include */ bootstrapModules?: string[]; /** CSP nonce for inline scripts */ nonce?: string; /** Called when shell is ready */ onShellReady?: () => void; /** Called when shell errors */ onShellError?: (error: Error) => void; /** Called when all content is ready */ onAllReady?: () => void; /** Called on any error */ onError?: (error: Error) => void; } /** * Creates options for React 18's renderToPipeableStream. * * @description * Generates configuration compatible with React 18's streaming SSR API. * * @param options - Render options * @returns Options for renderToPipeableStream * * @example * ```typescript * import { renderToPipeableStream } from 'react-dom/server'; * * const streamOptions = createReactStreamOptions({ * bootstrapScripts: ['/app.js'], * nonce: generateNonce(), * onShellReady() { * res.statusCode = 200; * stream.pipe(res); * }, * }); * * const { pipe } = renderToPipeableStream(, streamOptions); * ``` */ export declare function createReactStreamOptions(options?: RenderToStreamOptions): RenderToStreamOptions; /** * Creates the shell HTML for streaming SSR. * * @description * Generates the initial HTML shell that will be sent before * streaming content begins. * * @param options - Shell configuration * @returns HTML shell string */ export interface ShellOptions { /** Document title */ title?: string; /** Language attribute */ lang?: string; /** Meta tags */ meta?: Array>; /** Link tags (stylesheets, preloads) */ links?: Array>; /** Inline styles */ styles?: string; /** CSP nonce */ nonce?: string; /** Initial state to embed */ initialState?: Record; /** Root element ID */ rootId?: string; } export declare function createShellHtml(options?: ShellOptions): { head: string; bodyStart: string; bodyEnd: string; }; export { DEFAULT_MIDDLEWARE_OPTIONS, STREAMING_HEADERS };