web-csv-toolbox
    Preparing search index...

    Class CSVRecordAssemblerTransformer<Header>

    A transform stream that converts a stream of tokens into a stream of CSV records.

    CSV-specific options (header, maxFieldCount, etc.)

    Strategy for the writable side (default: { highWaterMark: 1024, size: () => 1, checkInterval: 10 })

    Strategy for the readable side (default: { highWaterMark: 256, size: () => 1, checkInterval: 10 })

    Follows the Web Streams API pattern where queuing strategies are passed as constructor arguments, similar to the standard TransformStream.

    Default Queuing Strategy:

    • Writable side: Counts each token as 1. Default highWaterMark is 1024 tokens.
    • Readable side: Counts each record as 1. Default highWaterMark is 256 records.

    Backpressure Handling: The transformer monitors controller.desiredSize and yields to the event loop when backpressure is detected (desiredSize ≤ 0). This prevents blocking the main thread during heavy processing and allows the downstream consumer to catch up.

    These defaults are starting points based on data flow characteristics, not empirical benchmarks. Optimal values depend on your runtime environment, data size, and performance requirements.

    new ReadableStream({
    start(controller) {
    controller.enqueue("name,age\r\n");
    controller.enqueue("Alice,20\r\n");
    controller.enqueue("Bob,25\r\n");
    controller.enqueue("Charlie,30\r\n");
    controller.close();
    })
    .pipeThrough(new CSVLexerTransformer())
    .pipeThrough(new CSVRecordAssemblerTransformer())
    .pipeTo(new WritableStream({ write(row) { console.log(row); }}));
    // { name: "Alice", age: "20" }
    // { name: "Bob", age: "25" }
    // { name: "Charlie", age: "30" }
    new ReadableStream({
    start(controller) {
    controller.enqueue("Alice,20\r\n");
    controller.enqueue("Bob,25\r\n");
    controller.enqueue("Charlie,30\r\n");
    controller.close();
    }
    })
    .pipeThrough(new CSVLexerTransformer())
    .pipeThrough(new CSVRecordAssemblerTransformer({ header: ["name", "age"] }))
    .pipeTo(new WritableStream({ write(row) { console.log(row); }}));
    // { name: "Alice", age: "20" }
    // { name: "Bob", age: "25" }
    // { name: "Charlie", age: "30" }
    const transformer = new CSVRecordAssemblerTransformer(
    {},
    {
    highWaterMark: 2048, // 2048 tokens
    size: () => 1, // Each token counts as 1
    checkInterval: 20 // Check backpressure every 20 records
    },
    {
    highWaterMark: 512, // 512 records
    size: () => 1, // Each record counts as 1
    checkInterval: 5 // Check backpressure every 5 records
    }
    );

    await tokenStream
    .pipeThrough(transformer)
    .pipeTo(yourRecordProcessor);

    Type Parameters

    • Header extends ReadonlyArray<string>

      The type of the header row.

    Hierarchy

    Index

    Constructors

    Properties

    The readable read-only property of the TransformStream interface returns the ReadableStream instance controlled by this TransformStream.

    MDN Reference

    The writable read-only property of the TransformStream interface returns the WritableStream instance controlled by this TransformStream.

    MDN Reference

    Methods