Skip to content
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

win, url: make fileURLToPath return backslashes #25324

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,7 @@ function getPathFromURLPosix(url) {
return decodeURIComponent(pathname);
}

const forwardslashRegEx = /\//g;
function fileURLToPath(path) {
if (typeof path === 'string')
path = new URL(path);
Expand All @@ -1336,7 +1337,9 @@ function fileURLToPath(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 ?
getPathFromURLWin32(path).replace(forwardslashRegEx, '\\') :
getPathFromURLPosix(path);
}

// The following characters are percent-encoded when converting from file path
Expand Down
15 changes: 13 additions & 2 deletions test/parallel/test-fs-whatwg-url.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const assert = require('assert');
const path = require('path');
const fs = require('fs');
const os = require('os');
const URL = require('url').URL;
const { fileURLToPath, URL } = require('url');

function pathToFileURL(p) {
if (!path.isAbsolute(p))
Expand All @@ -30,6 +30,17 @@ fs.readFile(url, common.mustCall((err, data) => {
// Check that using a non file:// URL reports an error
const httpUrl = new URL('http://example.org');

// Check that the fileURLToPath uses platform separator
['file:///c:/tmp', 'file://tmp/tmp'].forEach((i) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem seems to be the file://tmp/tmp entry here being treated as invalid. Perhaps just make it file:///tmp/tmp?

assert(fileURLToPath(i).includes(path.sep));
});

// Check fileURLToPath examples from documentation
if (common.isWindows) {
assert.strictEqual(fileURLToPath('file:///C:/path/'), 'C:\\path\\');
assert.strictEqual(fileURLToPath('file://nas/foo.txt'), '\\\\nas\\foo.txt');
}

common.expectsError(
() => {
fs.readFile(httpUrl, common.mustNotCall());
Expand Down Expand Up @@ -63,7 +74,7 @@ if (common.isWindows) {
code: 'ERR_INVALID_ARG_VALUE',
type: TypeError,
message: 'The argument \'path\' must be a string or Uint8Array without ' +
'null bytes. Received \'c:/tmp/\\u0000test\''
'null bytes. Received \'c:\\\\tmp\\\\\\u0000test\''
}
);
} else {
Expand Down