How to wrap a buffer as a stream2 Readable stream?

后端 未结 3 1324
日久生厌
日久生厌 2020-11-29 02:02

How can I transform a node.js buffer into a Readable stream following using the stream2 interface ?

I already found this answer and the stream-buffers module but thi

3条回答
  •  自闭症患者
    2020-11-29 02:21

    The easiest way is probably to create a new PassThrough stream instance, and simply push your data into it. When you pipe it to other streams, the data will be pulled out of the first stream.

    var stream = require('stream');
    
    // Initiate the source
    var bufferStream = new stream.PassThrough();
    
    // Write your buffer
    bufferStream.end(Buffer.from('Test data.'));
    
    // Pipe it to something else  (i.e. stdout)
    bufferStream.pipe(process.stdout)
    

提交回复
热议问题