Skip to content

Conversation

@LiviaMedeiros
Copy link
Member

These methods are already present on Uint8Array, so without defining them on Buffer they return instances of Uint8Array. This introduces wrapper to return instances of Buffer instead.

Fixes: #61146

@LiviaMedeiros LiviaMedeiros added buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run. dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. dont-land-on-v24.x PRs that should not land on the v24.x-staging branch and should not be released in v24.x. labels Dec 23, 2025
@codecov
Copy link

codecov bot commented Dec 23, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.55%. Comparing base (55600e6) to head (9c4e4c2).
⚠️ Report is 26 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61157      +/-   ##
==========================================
+ Coverage   88.52%   88.55%   +0.02%     
==========================================
  Files         703      704       +1     
  Lines      208589   208810     +221     
  Branches    40226    40281      +55     
==========================================
+ Hits       184650   184908     +258     
+ Misses      15954    15926      -28     
+ Partials     7985     7976       -9     
Files with missing lines Coverage Δ
lib/buffer.js 100.00% <100.00%> (ø)

... and 40 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@avivkeller
Copy link
Member

Remember to update the docs

Copy link
Member

@avivkeller avivkeller left a 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

@LiviaMedeiros
Copy link
Member Author

Agreed, added to the docs.
Initially i intentionally omitted it because these are basically inherited from Uint8Array (we don't document Buffer.prototype.at() whatsoever), but it's worth adding to indicate that these are similar-but-different from Buffer.from(string, encoding).

Copy link
Member

@avivkeller avivkeller left a comment

Choose a reason for hiding this comment

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

Docs + impl lgtm

@LiviaMedeiros LiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Dec 26, 2025
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Dec 26, 2025
@nodejs-github-bot
Copy link
Collaborator

@avivkeller avivkeller added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 28, 2025
lib/buffer.js Outdated
* @returns {Buffer}
*/
Buffer.fromHex = function fromHex(str) {
// TODO(LiviaMedeiros): replace with primordial once `--js-base-64` is not optional
Copy link
Contributor

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]

@LiviaMedeiros LiviaMedeiros removed the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 30, 2025
@aduh95
Copy link
Contributor

aduh95 commented Dec 30, 2025

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

@LiviaMedeiros
Copy link
Member Author

@aduh95 IIUC buffer.js is a part of early bootstrap, which is why getOptionValue('--js-base-64') is not available yet, and Uint8Array.{fromHex,fromBase64} are still undefined even when the feature is enabled. Neither

'fromHex' in Uint8Array
Uint8Array.fromHex !== undefined
typeof Uint8Array.fromHex !== 'undefined'
const { Uint8Array: { fromHex } } = primordials
const { globalThis: { Uint8Array: { fromHex } } } = primordials
const { Uint8Array: { fromHex } } = globalThis

gives anything meaningful.
If I'm missing something and it is possible to check synchronously, please let me know, I'd be happy to replace it with if.

@targos
Copy link
Member

targos commented Dec 31, 2025

Why do we care about --js-base-64? We only explicitly support a small subset of V8 flags and I don't see a compelling reason in this case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

buffer Issues and PRs related to the buffer subsystem. dont-land-on-v20.x PRs that should not land on the v20.x-staging branch and should not be released in v20.x. dont-land-on-v22.x PRs that should not land on the v22.x-staging branch and should not be released in v22.x. dont-land-on-v24.x PRs that should not land on the v24.x-staging branch and should not be released in v24.x. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Buffer.fromHex and Buffer.fromBase64 should return Buffer instances

5 participants