Skip to content
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

isBuffer: Check object equality not class name #418

Closed
wants to merge 4 commits into from

Conversation

stefanor
Copy link

@stefanor stefanor commented Dec 10, 2020

isBuffer: Check object equality not class name

If minified, the name of the buffer class may change, breaking the isBuffer() function, and thus the library.

Since 5167be2 binary fields have been broken for users minifying their source. The .buffer attribute would contain the raw BSON blob, rather than just the field's value.

NODE-2963

If minified, the name of the buffer class may change, breaking the
`isBuffer()` function, and thus the library.

Since 5167be2 binary fields have been
broken for users minifying their source. The `.buffer` attribute would
contain the raw BSON blob, rather than just the field's value.
stefanor added a commit to stefanor/vogol that referenced this pull request Jan 12, 2021
@nbbeeken
Copy link
Contributor

Hello @stefanor thank you for your submission! Unfortunately this causes another issue on nodejs, essentially:

global.Buffer != require('buffer').Buffer

So buffers constructed using the globally available constructor, which is common practice in Node, won't return true for this isBuffer helper. We will further investigate a solution that is cross platform.

@stefanor
Copy link
Author

Fair enough.

@Alexander-L-G Alexander-L-G added the tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR label Apr 8, 2021
src/parser/utils.ts Outdated Show resolved Hide resolved
@nbbeeken nbbeeken requested review from a team, nbbeeken and dariakp and removed request for a team April 21, 2021 14:57
@stefanor
Copy link
Author

Thanks!

Copy link
Contributor

@nbbeeken nbbeeken left a comment

Choose a reason for hiding this comment

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

Thanks again @stefanor for bringing this to our attention, after I get another pair of eyes from the team I'll see to merging this.

I've made a ticket that we look into adding minification testing to our CI NODE-3217

@@ -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!

Copy link
Contributor

@dariakp dariakp left a comment

Choose a reason for hiding this comment

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

See code comment regarding improving the check and adding tests

addaleax added a commit to addaleax/js-bson that referenced this pull request Apr 22, 2021
Also fixes NODE-3223 (ensureBuffer ignores byteLength/byteOffset).
This is the "proper" alternative to
mongodb#418 and matches what e.g.
Node.js APIs do.
addaleax added a commit to addaleax/js-bson that referenced this pull request Apr 22, 2021
Also fixes NODE-3223 (ensureBuffer ignores byteLength/byteOffset).
This is the "proper" alternative to
mongodb#418 and matches what e.g.
Node.js APIs do.
addaleax added a commit to addaleax/js-bson that referenced this pull request Apr 26, 2021
Also fixes NODE-3223 (ensureBuffer ignores byteLength/byteOffset).
This is the "proper" alternative to
mongodb#418 and matches what e.g.
Node.js APIs do.
@dariakp
Copy link
Contributor

dariakp commented Apr 28, 2021

Closing this PR in favor of the more general approach in #432.

@dariakp dariakp closed this Apr 28, 2021
@stefanor stefanor deleted the support-minification branch May 2, 2021 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tracked-in-jira There is a ticket in Mongo's Jira instance tracking this issue/PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants