Skip to content Skip to sidebar Skip to footer

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_tra

Solution 1:

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 = newTransform({
    transform(chunk, encoding, callback) {
        //Callback fires in a random amount of time 1-500 mssetTimeout(() =>callback(null, chunk), _.random(1, 500));
    },
    //Using objectMode to pass-trough Numbers, not strings/buffersobjectMode: 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

Post a Comment for "Does Node.js Stream Transform Maintain The Order Of The Chunks?"