-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
buffer: add Buffer.fromHex() and Buffer.fromBase64()
#61157
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
base: main
Are you sure you want to change the base?
buffer: add Buffer.fromHex() and Buffer.fromBase64()
#61157
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #61157 +/- ##
========================================
Coverage 88.52% 88.52%
========================================
Files 703 704 +1
Lines 208589 208798 +209
Branches 40226 40275 +49
========================================
+ Hits 184650 184841 +191
- Misses 15954 15955 +1
- Partials 7985 8002 +17
🚀 New features to boost your workflow:
|
|
Remember to update the docs |
avivkeller
left a comment
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.
Other than this is missing a doc entry, LGTM
|
Agreed, added to the docs. |
avivkeller
left a comment
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.
Docs + impl lgtm
lib/buffer.js
Outdated
| * @returns {Buffer} | ||
| */ | ||
| Buffer.fromHex = function fromHex(str) { | ||
| // TODO(LiviaMedeiros): replace with primordial once `--js-base-64` is not optional |
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.
Should we define those only if --no-js-base-64 as it is the case today?
$ node --no-js-base-64 -p 'Buffer.fromHex'
undefined
$ node -p 'Buffer.fromHex'
[Function: fromHex]|
I don't think lazy loading makes sense here, I don't see what's the upside. Why not put it under an if statement instead? $ ./node -p '"fromHex" in Buffer'
true
$ ./node --no-js-base-64 -p '"fromHex" in Buffer'
false |
|
@aduh95 IIUC 'fromHex' in Uint8Array
Uint8Array.fromHex !== undefined
typeof Uint8Array.fromHex !== 'undefined'
const { Uint8Array: { fromHex } } = primordials
const { globalThis: { Uint8Array: { fromHex } } } = primordials
const { Uint8Array: { fromHex } } = globalThisgives anything meaningful. |
|
Why do we care about |
This comment was marked as resolved.
This comment was marked as resolved.
I would love to do this if both variables didn't end up as |
Renegade334
left a comment
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 still don't really see what usage case this is intended for, given that Buffer already has long-established patterns for interacting with base64.
However, even if this is deemed worthwhile, I can't see that it's worthwhile enough to be bending over backwards to hack around the issues of shadowing a flagged feature. I feel like the best approach is to leave this alone, if at least until the feature is unflagged.
Users can easily replicate this functionality themselves by changing the prototype of their returned Uint8Array, if their usage case is truly not amenable to Buffer.from().
|
The reasoning for this comes from #61146. It's not really a new feature to be used with
Agreed that this function is awkward in terms of actual usage (users who need |
I can't reproduce, they are defined for me (unless I pass |
These methods are already present on
Uint8Array, so without defining them onBufferthey return instances ofUint8Array. This introduces wrapper to return instances ofBufferinstead.Fixes: #61146