bun icon indicating copy to clipboard operation
bun copied to clipboard

`Buffer.isBuffer` throws an error when it is assigned to a variable

Open yossizahn opened this issue 3 years ago • 6 comments
trafficstars

Version

0.1.4

Platform

Darwin Yossis-Air 21.5.0 Darwin Kernel Version 21.5.0: Tue Apr 26 21:08:29 PDT 2022; root:xnu-8020.121.3~4/RELEASE_ARM64_T8101 arm64

What steps will reproduce the bug?

const isBuffer = Buffer.isBuffer;
console.log(isBuffer({}))

How often does it reproduce? Is there a required condition?

No response

What is the expected behavior?

Expected to print true

What do you see instead?

3 | const isBuffer = Buffer.isArray;
4 | console.log(isBuffer({}))
               ^
 TypeError: isBuffer is not a function. (In 'isBuffer({})', 'isBuffer' is undefined)

Additional information

No response

yossizahn avatar Jul 17 '22 21:07 yossizahn

Be sure to import it before use it.

Try add this to your code.

import { Buffer } from 'node:buffer'

greatbody avatar Jul 18 '22 07:07 greatbody

Ok,thanks @greatbody, I tried your suggestion, and this leads me to more questions :) Take this code:

import { Buffer as nodeBuffer } from 'node:buffer';

console.log(new Buffer()) // succeeds even with no argument
// console.log(new nodeBuffer()) // this throws an error

console.log(nodeBuffer === Buffer)

const nodeIsBuffer = nodeBuffer.isBuffer;
const bufferIsBuffer = Buffer.isBuffer;

console.log(nodeIsBuffer(nodeBuffer.from('')));
console.log(bufferIsBuffer(Buffer.from('')));

So the global Buffer is not the same (and is incompatible with) the node:buffer import?

Either way there seems to be a global Buffer object wich displays the bug I reported.

yossizahn avatar Jul 18 '22 08:07 yossizahn

The original example prints false in node.

The polyfill from https://github.com/oven-sh/bun/blob/main/examples/discord-interactions/polyfill.js works around the issue

reviewher avatar Jul 18 '22 17:07 reviewher

There's something really weird here. The first print is working properly, while the second is failing. Why is this so, isn't it supposed to be the same thing?

//test.js
const isBuffer = Buffer.isBuffer;
console.log("Buffer.isBuffer({}): ", Buffer.isBuffer({}));
console.log("isBuffer({}): ", isBuffer({}));
user@PC:/...bun-folder$ bun test.js 
Buffer.isBuffer({}):  false
1 | console.log("Buffer.isBuffer({}): ", Buffer.isBuffer({}));
2 | 
3 | console.log("isBuffer({}): ", isBuffer({}));
                                 ^
 TypeError: Cannot convert undefined or null to object
      at ...bun-folder/test.js:3:30

MusiCode1 avatar Jul 18 '22 20:07 MusiCode1

Indeed, it works:

//test.js
const isBuffer = Buffer.isBuffer.bind(Buffer);
console.log("Buffer.isBuffer({}): ", Buffer.isBuffer({}));
console.log("isBuffer({}): ", isBuffer({}));

MusiCode1 avatar Jul 18 '22 20:07 MusiCode1

I don't know if it helps, but upgrade bun.

xhyrom avatar Aug 07 '22 05:08 xhyrom

It seems to fix the original issue, I'll close it for now.

yossizahn avatar Aug 16 '22 12:08 yossizahn