I have a stream that is being read from an audio source and I'm trying to store it into a Buffer
. From the documentation that I've read, you are able to pipe
the stream into one using fs.createWriteStream(~buffer~)
instead of a file path.
I'm doing this currently as:
const outputBuffer = Buffer.alloc(150000)
const stream = fs.createWriteStream(outputBuffer)
but when I run it, it throws an error saying that the Path: must be a string without null bytes
for the file system call.
If I'm misunderstanding the docs or missing something obvious please let me know!
The first parameter to fs.createWriteStream()
is the filename to read. That is why you receive that particular error.
There is no way to read from a stream directly into an existing Buffer
. There was a node EP to support this, but it more or less died off because there are some potential gotchas with it.
For now you will need to either copy the bytes manually or if you don't want node to allocate extra Buffers, you will need to manually call fs.open()
, fs.read()
(this is the method that allows you to pass in your Buffer
instance, along with an offset), and fs.close()
.