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: Replace SafeBuffer with Buffer, support BASE64 in the browser #74

Merged
merged 4 commits into from
Oct 10, 2022

Conversation

prantlf
Copy link
Contributor

@prantlf prantlf commented Jan 13, 2022

This is a part of changes to get this module usable in the web browser
and more lightweight at the same time.

  • Remove the safe-buffer dependency. Use the Buffer constructor and
    Buffer.from depending on what is available.
  • Use atob/btoa with a workaround for UTF-8 in the browser.

Fixes #70 without dropping the compatibility with Node.js 0.10.

Copy link
Collaborator

@phated phated left a comment

Choose a reason for hiding this comment

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

This is really awesome and I think we should make the switch! I added some comments inline.

index.js Outdated
Comment on lines 22 to 24
function decodeBase64(base64) {
return new Buffer(base64, 'base64').toString();
} :
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
function decodeBase64(base64) {
return new Buffer(base64, 'base64').toString();
} :
function decodeBase64(base64) {
if (typeof value === 'number') {
throw new TypeError('The value to decode must not be of type number.)
}
return new Buffer(base64, 'base64').toString();
} :

safer-buffer checks to make sure that the value isn't a number (I believe that is because new Buffer allocates bytes of length value if the value is a number).

index.js Outdated
Comment on lines 18 to 21
var decodeBase64 = typeof Buffer !== 'undefined' ? Buffer.from ?
function decodeBase64(base64) {
return Buffer.from(base64, 'base64').toString();
} :
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make these if statements instead of ternary?

index.js Outdated
Comment on lines 64 to 65
Converter.prototype.toBase64 = typeof Buffer !== 'undefined' ? Buffer.from ?
function () {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can these be if statements too?

Copy link
Owner

Choose a reason for hiding this comment

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

Agreed + this function should be declared elsewhere and named and then just referenced here, otherwise it is hard to debug since it'll be anonymous.

Additionally probably should do Buffer.from.bind(Buffer just in case in the future that function will refer to Buffer as this.

prantlf and others added 3 commits October 10, 2022 12:19
This is a part of changes to get this module usable in the web browser
and more lightweight at the same time.

* Remove the safe-buffer dependency. Use the Bufer constructor and
  Buffer.from depending on what is available.
* Use atob/btoa with a workaround for UTF-8 in the browser.
@phated phated force-pushed the remove-safe-buffer branch from c2919ce to cb8f059 Compare October 10, 2022 19:26
@phated
Copy link
Collaborator

phated commented Oct 10, 2022

@thlorenz I rebased on top of the GitHub Actions and updated the code based on what we discussed above. Can you take another look?

Copy link
Collaborator

@phated phated left a comment

Choose a reason for hiding this comment

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

I made the changes so this approval is pretty meaningless 😛

@phated phated requested a review from thlorenz October 10, 2022 19:41
Copy link
Owner

@thlorenz thlorenz left a comment

Choose a reason for hiding this comment

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

LGTM aside from the nits.
Feel free to merge once you fix those.
Thanks!

index.js Show resolved Hide resolved
index.js Show resolved Hide resolved
@thlorenz
Copy link
Owner

Some of the CI is failing on this one now .. not sure if it's a fluke, but at any rate we could probably take anything <=12.0 out of CI (we're basically just wasting electricity with this :) )

@phated
Copy link
Collaborator

phated commented Oct 10, 2022

Some of the CI is failing on this one now .. not sure if it's a fluke, but at any rate we could probably take anything <=12.0 out of CI (we're basically just wasting electricity with this :) )

Windows + node <4 is really flakey. I think we should remove them when we bump the 2.0 major (which I'll begin prepping after we release this). Sound good?

@phated phated merged commit 3197441 into thlorenz:master Oct 10, 2022
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.

Safe to remove safe-buffer
3 participants