buffer: add Buffer.fromHex() and Buffer.fromBase64()#61157
buffer: add Buffer.fromHex() and Buffer.fromBase64()#61157LiviaMedeiros wants to merge 5 commits intonodejs:mainfrom
Buffer.fromHex() and Buffer.fromBase64()#61157Conversation
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.
Other than this is missing a doc entry, LGTM
|
Agreed, added to the docs. |
lib/buffer.js
Outdated
There was a problem hiding this comment.
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.
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