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

Stricter detection of Node Buffer in browser environment #118

Open
rosssteytler opened this issue Dec 6, 2024 · 0 comments
Open

Stricter detection of Node Buffer in browser environment #118

rosssteytler opened this issue Dec 6, 2024 · 0 comments

Comments

@rosssteytler
Copy link

It seems that in a browser environment, anything defined as Buffer on the window object, even if it's not a complete buffer implementation, causes the library to crash.

Relevant code:

const Buffer = typeof globalThis === 'object' && globalThis.Buffer;
const hasNodeBuffer = typeof Buffer !== 'undefined'
const ByteArrayAllocate = hasNodeBuffer ? Buffer.allocUnsafeSlow : Uint8Array

https://github.com/kriszyp/cbor-x/blob/master/encode.js#L8

In my case, window.Buffer contains a Buffer implementation which doesn't support allocUnsafeSlow, but this could be any definition of Buffer, such as window.Buffer = {}. I don't have the option to remove the Buffer definition from the global scope.

A possible solution might be to perform stricter checks on globalThis.Buffer to ensure allocUnsafeSlow exists before using it. Alternatively, it may be safer to introduce an option to explicitly disable the Node.js Buffer usage, which could be enabled or disabled in browser environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant