Skip to content

isBuffer: Check object equality not class name #418

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export function haveBuffer(): boolean {

/** Callable in any environment to check if value is a Buffer */
export function isBuffer(value: unknown): value is Buffer {
return typeof value === 'object' && value?.constructor?.name === 'Buffer';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return isUint8Array(value) && typeof (value as any)?.toJSON === 'function';
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to check for the properties on the buffer that the driver actually needs to exist in order to successfully do its job instead of relying on an arbitrary condition to infer that the object is of the correct type; furthermore, we should add some unit tests around types that are expected to pass the check and the ones that are expected to fail

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to check for the properties on the buffer that the driver actually needs to exist in order to successfully do its job instead of relying on an arbitrary condition to infer that the object is of the correct type;

Soooo... if clean code is the goal here, the way to do that is to just never check whether something is a Buffer, because there's never a good reason to do that (e.g. all Node.js built-in APIs which accept a Buffer also accept all other kinds of Uint8Arrays). Luckily, that's actually pretty easy to do (bonus bugfix included 🙂): #432

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that was pretty much my thought, will take a look at the new code soon, thanks!


// To ensure that 0.4 of node works correctly
Expand Down