Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap a buffer as a stream2 Readable stream?

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 this module is based on the stream1 interface.

like image 590
Jerome WAGNER Avatar asked Apr 16 '13 13:04

Jerome WAGNER


People also ask

What is the correct way to pipe a readable stream?

To consume a readable stream, we can use the pipe / unpipe methods, or the read / unshift / resume methods. To consume a writable stream, we can make it the destination of pipe / unpipe , or just write to it with the write method and call the end method when we're done.

What is difference between buffer and stream?

So what is the difference between Stream & Buffer? A buffer has a specified, definite length whereas a stream does not. A stream is a sequence of bytes that is read and/or written to, while a buffer is a sequence of bytes that is stored.

Is node buffer a stream?

Streams work on a concept called buffer. A buffer is a temporary memory that a stream takes to hold some data until it is consumed. In a stream, the buffer size is decided by the highWatermark property on the stream instance which is a number denoting the size of the buffer in bytes.


2 Answers

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) 
like image 80
zjonsson Avatar answered Oct 13 '22 19:10

zjonsson


As natevw suggested, it's even more idiomatic to use a stream.PassThrough, and end it with the buffer:

var buffer = new Buffer( 'foo' ); var bufferStream = new stream.PassThrough(); bufferStream.end( buffer ); bufferStream.pipe( process.stdout ); 

This is also how buffers are converted/piped in vinyl-fs.

like image 45
morris4 Avatar answered Oct 13 '22 19:10

morris4