Search code examples
javascriptnode.jsasynchronousnode-streams

Does Node.js Stream Transform maintain the order of the chunks?


I see in that the Transform Streams in the Node.js Stream API uses an asynchronous function to transform the chunks when they arrive: https://nodejs.org/api/stream.html#stream_transform_transform_chunk_encoding_callback

Does the Transform stream sends the chunks at the same order as they arrive? Because with an asynchronous function, that is not explicitly the case.


Solution

  • Short answer is: yes, transform stream guaranties that chunks are sent in the same order. (Because Streams might be used for order-sensative operations (for cryptography, or zipping-unzipping files)

    Here is a snipped that you could run to make sure:

    const {Transform} = require('stream');
    const _ = require('lodash');
    const h = require('highland');
    
    const myTransform = new Transform({
        transform(chunk, encoding, callback) {
            //Callback fires in a random amount of time 1-500 ms
            setTimeout(() => callback(null, chunk), _.random(1, 500));
        },
        //Using objectMode to pass-trough Numbers, not strings/buffers
        objectMode: true
    });
    
    //I'm using 'highland' here to create a read stream
    //The read stream emits numbers from 1 to 100 
    h(_.range(1, 100))
        .pipe(myTransform)
        //Simply logging them as they go out of transform stream
        .on('data', chunk => console.log(chunk.toString()));
    
    //The output is:
    // 1
    // 2
    // 3
    // 4 ...
    //Although the callbacks fire in random order