Skip to content

url: add fileURLToPathBuffer API #58700

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

Closed
wants to merge 1 commit into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Jun 12, 2025

The existing fileURLToPath() does not handle the case where the input URL contains percent-encoded characters that are not valid UTF-8 sequences. This can lead to issues, for instance, when the URL is constructed using file names in non-Unicode encodings (like Shift-JIS). This commit introduces a new API, fileURLToPathBuffer(), which returns a Buffer representing the path, allowing for accurate conversion of file URLs to paths without attempting to decode the percent-encoded bytes into characters.

This is part of the fix for #58634 but there's more to do.

@jasnell jasnell added fs Issues and PRs related to the fs subsystem / file system. url Issues and PRs related to the legacy built-in url module. semver-minor PRs that contain new features and should be released in the next minor version. labels Jun 12, 2025
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/startup
  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Jun 12, 2025
@nodejs-github-bot

This comment was marked as outdated.

@jasnell jasnell force-pushed the jasnell/add-fileurltopathbuffer branch 2 times, most recently from 249ab0d to 8ccd48d Compare June 12, 2025 20:59
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

Copy link

codecov bot commented Jun 12, 2025

Codecov Report

Attention: Patch coverage is 48.76033% with 62 lines in your changes missing coverage. Please review.

Project coverage is 90.12%. Comparing base (5e1537c) to head (e362a1e).
Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/url.js 47.45% 62 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #58700      +/-   ##
==========================================
- Coverage   90.16%   90.12%   -0.04%     
==========================================
  Files         637      637              
  Lines      188098   188122      +24     
  Branches    36905    36898       -7     
==========================================
- Hits       169601   169548      -53     
- Misses      11231    11319      +88     
+ Partials     7266     7255      -11     
Files with missing lines Coverage Δ
lib/internal/data_url.js 100.00% <100.00%> (+8.23%) ⬆️
lib/url.js 100.00% <100.00%> (ø)
lib/internal/url.js 94.38% <47.45%> (-3.48%) ⬇️

... and 47 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.

@jasnell jasnell force-pushed the jasnell/add-fileurltopathbuffer branch from 803aa96 to cb63900 Compare June 12, 2025 22:54
@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Jun 12, 2025

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

what could we do with this buffer? How could a user manipulate this?

@jasnell jasnell force-pushed the jasnell/add-fileurltopathbuffer branch from cb63900 to 755e985 Compare June 14, 2025 14:23
@jasnell
Copy link
Member Author

jasnell commented Jun 14, 2025

what could we do with this buffer?

It can be passed directly to node:fs APIs. More importantly tho, it's to help fix the issue discussed in #58634 by making it possible to normalize the arguments passed into the filter callback.

@nodejs-github-bot

This comment was marked as outdated.

Copy link
Contributor

@Ethan-Arrowood Ethan-Arrowood left a comment

Choose a reason for hiding this comment

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

I wont block on my one change recommendation, but please consider it in regards to consistency with path.win32 and path.posix pattern.

@jasnell jasnell added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jun 14, 2025
The existing `fileURLToPath()` does not handle the case
where the input URL contains percent-encoded characters
that are not valid UTF-8 sequences. This can lead to
issues, for instance, when the URL is constructed
using file names in non-Unicode encodings (like
Shift-JIS). This commit introduces a new API,
`fileURLToPathBuffer()`, which returns a `Buffer`
representing the path, allowing for accurate
conversion of file URLs to paths without attempting
to decode the percent-encoded bytes into characters.
@jasnell jasnell force-pushed the jasnell/add-fileurltopathbuffer branch from 755e985 to e362a1e Compare June 14, 2025 15:00
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@LiviaMedeiros LiviaMedeiros left a comment

Choose a reason for hiding this comment

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

Given that this is more robust, should internals that rely on fileURLToPath() be eventually migrated to using this function?

@nodejs-github-bot
Copy link
Collaborator

@jasnell
Copy link
Member Author

jasnell commented Jun 14, 2025

... should internals that rely on fileURLToPath() be eventually migrated to using this function?

Probably.

@jasnell jasnell added the commit-queue Add this label to land a pull request using GitHub Actions. label Jun 14, 2025
@nodejs-github-bot nodejs-github-bot added commit-queue-failed An error occurred while landing this pull request using GitHub Actions. and removed commit-queue Add this label to land a pull request using GitHub Actions. labels Jun 14, 2025
@nodejs-github-bot
Copy link
Collaborator

Commit Queue failed
- Loading data for nodejs/node/pull/58700
✔  Done loading data for nodejs/node/pull/58700
----------------------------------- PR info ------------------------------------
Title      url: add fileURLToPathBuffer API (#58700)
Author     James M Snell <jasnell@gmail.com> (@jasnell)
Branch     jasnell:jasnell/add-fileurltopathbuffer -> nodejs:main
Labels     fs, url, semver-minor, whatwg-url, author ready, needs-ci
Commits    1
 - url: add fileURLToPathBuffer API
Committers 1
 - James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/58700
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
------------------------------ Generated metadata ------------------------------
PR-URL: https://github.com/nodejs/node/pull/58700
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
--------------------------------------------------------------------------------
   ℹ  This PR was created on Thu, 12 Jun 2025 20:49:22 GMT
   ✔  Approvals: 3
   ✔  - Matteo Collina (@mcollina) (TSC): https://github.com/nodejs/node/pull/58700#pullrequestreview-2927770869
   ✔  - Ethan Arrowood (@Ethan-Arrowood): https://github.com/nodejs/node/pull/58700#pullrequestreview-2928406839
   ✔  - LiviaMedeiros (@LiviaMedeiros): https://github.com/nodejs/node/pull/58700#pullrequestreview-2928714008
   ✘  Last GitHub CI failed
   ℹ  Last Full PR CI on 2025-06-14T19:33:13Z: https://ci.nodejs.org/job/node-test-pull-request/67444/
- Querying data for job/node-test-pull-request/67444/
   ✔  Last Jenkins CI successful
--------------------------------------------------------------------------------
   ✔  Aborted `git node land` session in /home/runner/work/node/node/.ncu
https://github.com/nodejs/node/actions/runs/15656016109

jasnell added a commit that referenced this pull request Jun 14, 2025
The existing `fileURLToPath()` does not handle the case
where the input URL contains percent-encoded characters
that are not valid UTF-8 sequences. This can lead to
issues, for instance, when the URL is constructed
using file names in non-Unicode encodings (like
Shift-JIS). This commit introduces a new API,
`fileURLToPathBuffer()`, which returns a `Buffer`
representing the path, allowing for accurate
conversion of file URLs to paths without attempting
to decode the percent-encoded bytes into characters.

PR-URL: #58700
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
@jasnell
Copy link
Member Author

jasnell commented Jun 14, 2025

Landed in 3f6ad56

@jasnell jasnell closed this Jun 14, 2025
targos pushed a commit that referenced this pull request Jun 16, 2025
The existing `fileURLToPath()` does not handle the case
where the input URL contains percent-encoded characters
that are not valid UTF-8 sequences. This can lead to
issues, for instance, when the URL is constructed
using file names in non-Unicode encodings (like
Shift-JIS). This commit introduces a new API,
`fileURLToPathBuffer()`, which returns a `Buffer`
representing the path, allowing for accurate
conversion of file URLs to paths without attempting
to decode the percent-encoded bytes into characters.

PR-URL: #58700
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ethan Arrowood <ethan@arrowood.dev>
Reviewed-By: LiviaMedeiros <livia@cirno.name>
nodejs-github-bot added a commit that referenced this pull request Jun 16, 2025
Notable changes:

doc:
  * add islandryu to collaborators (Shima Ryuhei) #58714
fs:
  * (SEMVER-MINOR) allow correct handling of burst in fs-events with AsyncIterator (Philipp Dunkel) #58490
module:
  * (SEMVER-MINOR) remove experimental warning from type stripping (Marco Ippolito) #58643
test_runner:
  * (SEMVER-MINOR) support object property mocking (Idan Goshen) #58438
url:
  * (SEMVER-MINOR) add fileURLToPathBuffer API (James M Snell) #58700

PR-URL: #58727
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-failed An error occurred while landing this pull request using GitHub Actions. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version. url Issues and PRs related to the legacy built-in url module. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants