Converting a Buffer into a ReadableStream in Node.js
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. How to pass a Buffer as argument of fs.createReadStream
The first argument to fs.createReadStream()
must be the file path. You can apparently pass the path in a Buffer object, but it still must be an acceptable OS path when the Buffer is converted to a string.
It appears you are trying to pass the file content to fs.createReadStream()
. That is not how that API works. If you look into the code for fs.createReadStream()
it is completely clear in the code that it is going to call fs.open()
and pass the first argument from fs.createReadStream()
as the file path for fs.open()
.
If what you're trying to do is to create a readable stream from a buffer (no file involved), then you need to do that a different way. You can see how to do that here in this answer: Converting a Buffer into a ReadableStream in Node.js.
Conceptually, you just create a readable stream object, push the data you already have into it from your Buffer, push a null
to signify the end of the stream and create a noop _read()
method and you're done. You can then use that readable stream with any other code that expects to read it.
Related Topics
Angularjs "Controller As" or "$Scope"
How to Check If an Array Is a Subset of Another Array in JavaScript
How to Generate Sourcemaps When Using Babel and Webpack
Serialize Object to Query String in JavaScript/Jquery
How to Distinguish Mouse "Click" and "Drag"
Differencebetween Decodeuricomponent and Decodeuri
What's Wrong with Var X = New Array();
Use of @ Symbol in Node Module Names
Why Does ('0' ? 'A':'B') Behave Different Than ('0' == True ? 'A':'B')
In Angular, How to Determine the Active Route
When to Use Preventdefault( ) VS Return False
JavaScript Array .Reduce with Async/Await
Mongoose, Select a Specific Field with Find
Ng-Model Does Not Update Controller Value
Objects VS Arrays in JavaScript for Key/Value Pairs
How to Pass Jsf Managed Bean Properties to a JavaScript Function
How to Remove Text (Without Removing Inner Elements) from a Parent Element Using Jquery