javascriptnode.jsstreambuffer

Converting a Buffer into a ReadableStream in Node.js


I have a library that takes as input a ReadableStream, but my input is just a base64 format image. I could convert the data I have in a Buffer like so:

var img = new Buffer(img_string, 'base64');

But I have no idea how to convert it to a ReadableStream or convert the Buffer I obtained to a ReadableStream.

Is there a way to do this?


Solution

  • You can create a ReadableStream using Node Stream Buffers like so:

    // Initialize stream
    var myReadableStreamBuffer = new streamBuffers.ReadableStreamBuffer({
      frequency: 10,      // in milliseconds.
      chunkSize: 2048     // in bytes.
    }); 
    
    // With a buffer
    myReadableStreamBuffer.put(aBuffer);
    
    // Or with a string
    myReadableStreamBuffer.put("A String", "utf8");
    

    The frequency cannot be 0 so this will introduce a certain delay.