bun
bun copied to clipboard
`Buffer.isBuffer` throws an error when it is assigned to a variable
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
Be sure to import it before use it.
Try add this to your code.
import { Buffer } from 'node:buffer'
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.
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
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
Indeed, it works:
//test.js
const isBuffer = Buffer.isBuffer.bind(Buffer);
console.log("Buffer.isBuffer({}): ", Buffer.isBuffer({}));
console.log("isBuffer({}): ", isBuffer({}));
I don't know if it helps, but upgrade bun.
It seems to fix the original issue, I'll close it for now.