import { DEFAULT_DELIMITER, DEFAULT_QUOTATION } from '../../../core/constants'; import { InferCSVRecord, ParseOptions, PickCSVHeader } from '../../../core/types'; /** * Parse CSV string stream to records. * * @category Middle-level API * @param stream CSV string stream to parse * @param options Parsing options. * @returns Async iterable iterator of records. * * If you want array of records, use {@link parseStringStream.toArray} function. * * @remarks * **Stream Execution Strategies:** * * For streams, the engine configuration supports two worker strategies: * - **stream-transfer** (recommended): Zero-copy stream transfer to worker * - Supported on Chrome, Firefox, Edge * - Automatically falls back to message-streaming on Safari * - **message-streaming**: Records sent via postMessage * - Works on all browsers including Safari * - Slightly higher overhead but more compatible * * By default, streams use main thread execution. To use workers with streams: * ```ts * import { parseStringStream, EnginePresets } from 'web-csv-toolbox'; * * // Use worker with automatic stream-transfer (falls back if not supported) * for await (const record of parseStringStream(stream, { * engine: EnginePresets.memoryEfficient() * })) { * console.log(record); * } * ``` * * Note: WASM execution is not supported for streams. If you specify * `engine: { wasm: true }` with a stream, it will fall back to main thread. * * @example Parsing CSV files from strings * * ```ts * import { parseStringStream } from 'web-csv-toolbox'; * * const csv = `name,age * Alice,42 * Bob,69`; * * const stream = new ReadableStream({ * start(controller) { * controller.enqueue(csv); * controller.close(); * }, * }); * * for await (const record of parseStringStream(stream)) { * console.log(record); * } * // Prints: * // { name: 'Alice', age: '42' } * // { name: 'Bob', age: '69' } * ``` * * @example Using worker with stream transfer for large files * ```ts * import { parseStringStream } from 'web-csv-toolbox'; * * const response = await fetch('large-file.csv'); * const stream = response.body * .pipeThrough(new TextDecoderStream()); * * // Use worker with stream-transfer strategy * for await (const record of parseStringStream(stream, { * engine: { worker: true, workerStrategy: 'stream-transfer' } * })) { * console.log(record); * } * ``` */ export declare function parseStringStream, const Delimiter extends string = DEFAULT_DELIMITER, const Quotation extends string = DEFAULT_QUOTATION, const Header extends ReadonlyArray = PickCSVHeader, const Options extends ParseOptions = ParseOptions>(csv: CSVSource, options: Options): AsyncIterableIterator>; export declare function parseStringStream, const Header extends ReadonlyArray = PickCSVHeader, const Options extends ParseOptions
= ParseOptions
>(csv: CSVSource, options?: Options): AsyncIterableIterator>; export declare function parseStringStream, const Options extends ParseOptions
= ParseOptions
>(stream: ReadableStream, options?: Options): AsyncIterableIterator>; export declare namespace parseStringStream { /** * Parse CSV string stream to records. * * @returns Array of records * * @example * * ```ts * import { parseStringStream } from 'web-csv-toolbox'; * * const csv = `name,age * Alice,42 * Bob,69`; * * const stream = new ReadableStream({ * start(controller) { * controller.enqueue(csv); * controller.close(); * }, * }); * * const records = await parseStringStream.toArray(stream); * console.log(records); * // Prints: * // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ] * ``` */ function toArray
, Options extends ParseOptions
= ParseOptions
>(stream: ReadableStream, options?: Options): Promise[]>; /** * Parse CSV string stream to records. * * @returns Array of records * * @example * * ```ts * import { parseStringStream } from 'web-csv-toolbox'; * * const csv = `name,age * Alice,42 * Bob,69`; * * const stream = new ReadableStream({ * start(controller) { * controller.enqueue(csv); * controller.close(); * }, * }); * * await parseStringStream.toStream(stream) * .pipeTo( * new WritableStream({ * write(record) { * console.log(record); * }, * }), * ); * ``` */ function toStream
, Options extends ParseOptions
= ParseOptions
>(stream: ReadableStream, options?: Options): ReadableStream>; }