Skip to content

Commit

Permalink
lib, url: fix nodejs#51609 by adding a win argument
Browse files Browse the repository at this point in the history
  • Loading branch information
avivkeller authored Apr 12, 2024
1 parent 2cd3073 commit 5f33e62
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1460,14 +1460,14 @@ function getPathFromURLPosix(url) {
return decodeURIComponent(pathname);
}

function fileURLToPath(path) {
function fileURLToPath(path, win = false) {
if (typeof path === 'string')
path = new URL(path);
else if (!isURL(path))
throw new ERR_INVALID_ARG_TYPE('path', ['string', 'URL'], path);
if (path.protocol !== 'file:')
throw new ERR_INVALID_URL_SCHEME('file');
return isWindows ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
return (isWindows || win) ? getPathFromURLWin32(path) : getPathFromURLPosix(path);
}

// The following characters are percent-encoded when converting from file path
Expand Down Expand Up @@ -1504,7 +1504,7 @@ function encodePathChars(filepath) {
return filepath;
}

function pathToFileURL(filepath) {
function pathToFileURL(filepath, win = false) {
if (isWindows && StringPrototypeStartsWith(filepath, '\\\\')) {
const outURL = new URL('file://');
// UNC path format: \\server\share\resource
Expand Down Expand Up @@ -1534,7 +1534,7 @@ function pathToFileURL(filepath) {
const filePathLast = StringPrototypeCharCodeAt(filepath,
filepath.length - 1);
if ((filePathLast === CHAR_FORWARD_SLASH ||
(isWindows && filePathLast === CHAR_BACKWARD_SLASH)) &&
((win || isWindows) && filePathLast === CHAR_BACKWARD_SLASH)) &&
resolved[resolved.length - 1] !== path.sep)
resolved += '/';

Expand Down

0 comments on commit 5f33e62

Please sign in to comment.