m_callens
m_callens

Reputation: 6360

Write stream into buffer object

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!

Upvotes: 5

Views: 16409

Answers (1)

mscdex
mscdex

Reputation: 106746

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().

Upvotes: 1

Related Questions