Saturday, 27 May 2017

Why isBuffer returns false for an fs.readFileSync call without encoding

In Node (v6.3.1) I am reading a file using fs.readFileSync(path).

The result (data) is a buffer-like object that when I print to STDOUT (console.log(data)) I get:

{"type":"Buffer","data":[45,45,45,...]}

But calling

data instanceof Buffer

or

Buffer.isBuffer(data)

I always get false.

Why? How can I test if the returned data is a buffer?

Context: I simply want to avoid long dumps to STDOUT and clean up the output using JSON.stringify(data, replacer) by shortening the buffer outputs.



via Gabriel Petrovay

No comments:

Post a Comment