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

fix cloning of buffers #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

restfulhead
Copy link

@restfulhead restfulhead commented Jul 20, 2023

Properly handles cloning of Buffer objects in Node.js.

Without this fix, Buffer objects would get corrupted after cloning as is demonstrated by the unit test.

Properly handles cloning of buffers in Node.js and browser environments.
index.js Outdated Show resolved Hide resolved
index.js Outdated Show resolved Hide resolved
test/mutability.js Outdated Show resolved Hide resolved
test/mutability.js Outdated Show resolved Hide resolved
@restfulhead
Copy link
Author

@ljharb Could you please review after my latest changes?

index.js Outdated Show resolved Hide resolved
Comment on lines +66 to +99
} else if (isBuffer(src)) {
dst = Buffer.from(src);
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
} else if (isBuffer(src)) {
dst = Buffer.from(src);
} else if (isBuffer(src) && typeof Buffer.from === 'function') {
dst = Buffer.from(src);

i'm a bit concerned that merely mentioning Buffer would cause bundlers to bring in the buffer polyfill unnecessarily, which is why the isBuffer code is authored the way it is.

Copy link
Author

Choose a reason for hiding this comment

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

I see, yeah not sure

Copy link
Author

Choose a reason for hiding this comment

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

Any updates?

Copy link
Owner

Choose a reason for hiding this comment

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

I'm still concerned here - I don't think we can use Buffer.from without incurring a bundle size impact. I think we could use the safe-buffer package, but then we need to add a dependency.

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

Successfully merging this pull request may close these issues.

2 participants