Skip to content

fix RangeError for input buffers #248

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 1 commit 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
7 changes: 6 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ function base64ToBytes(base64) {
* @return {string}
*/
function bytesToBase64(bytes) {
return btoa(String.fromCharCode.apply(null, new Uint8Array(bytes)));
const uint8Array = new Uint8Array(bytes);
let binaryString = "";
for (let i = 0; i < uint8Array.length; i++) {
binaryString += String.fromCharCode(uint8Array[i]);
Copy link
Contributor

Choose a reason for hiding this comment

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

It's been a while since I looked at JS string performance but this change makes me nervous. I believe JS strings are immutable so each time we add a character here it's making a copy of the string leaving the old one to be garbage collected.

If the issue here is that the the number of bytes passed to Uint8Array is too large, then I have a feeling this is potentially going to consume a lot of memory. An alternative would be to build an array and use join() but as this implementation was supposed to be a quick "good enough" hack, at this point I think we'd be better off re-considering vendoring in a proper cross-platform base64 lib and avoiding the use of btoa altogether.

}
return btoa(binaryString);
}

/**
Expand Down
Loading