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

微笑、不失礼 提交于 2019-12-01 07:42:52

问题


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.


回答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 = 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


来源:https://stackoverflow.com/questions/42341097/does-node-js-stream-transform-maintain-the-order-of-the-chunks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!