import type { ParserOptions, StreamParserOptions } from '@json2csv/plainjs'; import JSON2CSVWHATWGTransformStream, { type AwaitableReadableStream, } from './TransformStream.js'; export default class JSON2CSVNodeAsyncParser< TRaw extends object, T extends object, > { private opts: ParserOptions; private asyncOpts: StreamParserOptions; private writableStrategy?: QueuingStrategy; private readableStrategy?: QueuingStrategy; constructor( opts: ParserOptions = {}, asyncOpts: StreamParserOptions = {}, writableStrategy?: QueuingStrategy, readableStrategy?: QueuingStrategy, ) { this.opts = opts; this.asyncOpts = asyncOpts; this.writableStrategy = writableStrategy; this.readableStrategy = readableStrategy; } /** * Main function that converts json to csv. * * @param {Stream|Array|Object} data Array of JSON objects to be converted to CSV * @returns {Stream} A stream producing the CSV formated data as a string */ parse( data: | string | string | ArrayBufferView | Iterable | AsyncIterable | TRaw | ReadableStream, ): AwaitableReadableStream { if (typeof data === 'string' || ArrayBuffer.isView(data)) { data = new ReadableStream({ start(controller) { controller.enqueue(data as TRaw); controller.close(); }, }); } else if (Array.isArray(data)) { this.asyncOpts.objectMode = true; data = new ReadableStream({ start(controller) { (data as Array) .filter((item) => item !== null) .forEach((item) => controller.enqueue(item)); controller.close(); }, }); } else if (typeof data === 'object' && !(data instanceof ReadableStream)) { this.asyncOpts.objectMode = true; data = new ReadableStream({ start(controller) { controller.enqueue(data as TRaw); controller.close(); }, }); } if (!(data instanceof ReadableStream)) { throw new Error( 'Data should be a JSON object, JSON array, typed array, string or stream', ); } const transform = new JSON2CSVWHATWGTransformStream( this.opts, this.asyncOpts, this.writableStrategy, this.readableStrategy, ); return data.pipeThrough(transform) as AwaitableReadableStream; } }