From e362a1e354fd60b7a01991b78ed07f90f3cb9e3c Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 12 Jun 2025 13:43:56 -0700 Subject: [PATCH] url: add fileURLToPathBuffer API 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. --- doc/api/url.md | 20 ++++ lib/internal/data_url.js | 1 + lib/internal/url.js | 118 ++++++++++++++++++++++ lib/url.js | 2 + test/parallel/test-bootstrap-modules.js | 2 + test/parallel/test-fileurltopathbuffer.js | 72 +++++++++++++ 6 files changed, 215 insertions(+) create mode 100644 test/parallel/test-fileurltopathbuffer.js diff --git a/doc/api/url.md b/doc/api/url.md index 9e4b46db3871a0..5a1f4549c51770 100644 --- a/doc/api/url.md +++ b/doc/api/url.md @@ -1358,6 +1358,26 @@ new URL('file:///hello world').pathname; // Incorrect: /hello%20world fileURLToPath('file:///hello world'); // Correct: /hello world (POSIX) ``` +### `url.fileURLToPathBuffer(url[, options])` + + + +* `url` {URL | string} The file URL string or URL object to convert to a path. +* `options` {Object} + * `windows` {boolean|undefined} `true` if the `path` should be + return as a windows filepath, `false` for posix, and + `undefined` for the system default. + **Default:** `undefined`. +* Returns: {Buffer} The fully-resolved platform-specific Node.js file path + as a {Buffer}. + +Like `url.fileURLToPath(...)` except that instead of returning a string +representation of the path, a `Buffer` is returned. This conversion is +helpful when the input URL contains percent-encoded segments that are +not valid UTF-8 / Unicode sequences. + ### `url.format(URL[, options])`