-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Conversation
There was a problem hiding this 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
function decodeBase64(base64) { | ||
return new Buffer(base64, 'base64').toString(); | ||
} : |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
var decodeBase64 = typeof Buffer !== 'undefined' ? Buffer.from ? | ||
function decodeBase64(base64) { | ||
return Buffer.from(base64, 'base64').toString(); | ||
} : |
There was a problem hiding this comment.
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
Converter.prototype.toBase64 = typeof Buffer !== 'undefined' ? Buffer.from ? | ||
function () { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
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.
c2919ce
to
cb8f059
Compare
@thlorenz I rebased on top of the GitHub Actions and updated the code based on what we discussed above. Can you take another look? |
There was a problem hiding this 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 😛
There was a problem hiding this 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!
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 |
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? |
This is a part of changes to get this module usable in the web browser
and more lightweight at the same time.
Buffer.from depending on what is available.
Fixes #70 without dropping the compatibility with Node.js 0.10.