url: return backslashes from fileURLToPath on win
PR-URL: https://github.com/nodejs/node/pull/25349 Fixes: https://github.com/nodejs/node/issues/25265 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
This commit is contained in:
parent
eb664c3b6d
commit
7237eaa335
@ -1275,6 +1275,8 @@ function urlToOptions(url) {
|
|||||||
return options;
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const forwardSlashRegEx = /\//g;
|
||||||
|
|
||||||
function getPathFromURLWin32(url) {
|
function getPathFromURLWin32(url) {
|
||||||
var hostname = url.hostname;
|
var hostname = url.hostname;
|
||||||
var pathname = url.pathname;
|
var pathname = url.pathname;
|
||||||
@ -1289,6 +1291,7 @@ function getPathFromURLWin32(url) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pathname = pathname.replace(forwardSlashRegEx, '\\');
|
||||||
pathname = decodeURIComponent(pathname);
|
pathname = decodeURIComponent(pathname);
|
||||||
if (hostname !== '') {
|
if (hostname !== '') {
|
||||||
// If hostname is set, then we have a UNC path
|
// If hostname is set, then we have a UNC path
|
||||||
@ -1297,7 +1300,7 @@ function getPathFromURLWin32(url) {
|
|||||||
// about percent encoding because the URL parser will have
|
// about percent encoding because the URL parser will have
|
||||||
// already taken care of that for us. Note that this only
|
// already taken care of that for us. Note that this only
|
||||||
// causes IDNs with an appropriate `xn--` prefix to be decoded.
|
// causes IDNs with an appropriate `xn--` prefix to be decoded.
|
||||||
return `//${domainToUnicode(hostname)}${pathname}`;
|
return `\\\\${domainToUnicode(hostname)}${pathname}`;
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, it's a local path that requires a drive letter
|
// Otherwise, it's a local path that requires a drive letter
|
||||||
var letter = pathname.codePointAt(1) | 0x20;
|
var letter = pathname.codePointAt(1) | 0x20;
|
||||||
|
@ -63,7 +63,7 @@ if (common.isWindows) {
|
|||||||
code: 'ERR_INVALID_ARG_VALUE',
|
code: 'ERR_INVALID_ARG_VALUE',
|
||||||
type: TypeError,
|
type: TypeError,
|
||||||
message: 'The argument \'path\' must be a string or Uint8Array without ' +
|
message: 'The argument \'path\' must be a string or Uint8Array without ' +
|
||||||
'null bytes. Received \'c:/tmp/\\u0000test\''
|
'null bytes. Received \'c:\\\\tmp\\\\\\u0000test\''
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
152
test/parallel/test-url-fileurltopath.js
Normal file
152
test/parallel/test-url-fileurltopath.js
Normal file
@ -0,0 +1,152 @@
|
|||||||
|
'use strict';
|
||||||
|
const { isWindows } = require('../common');
|
||||||
|
const assert = require('assert');
|
||||||
|
const url = require('url');
|
||||||
|
|
||||||
|
function testInvalidArgs(...args) {
|
||||||
|
for (const arg of args) {
|
||||||
|
assert.throws(() => url.fileURLToPath(arg), {
|
||||||
|
code: 'ERR_INVALID_ARG_TYPE'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Input must be string or URL
|
||||||
|
testInvalidArgs(null, undefined, 1, {}, true);
|
||||||
|
|
||||||
|
// Input must be a file URL
|
||||||
|
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
|
||||||
|
code: 'ERR_INVALID_URL_SCHEME'
|
||||||
|
});
|
||||||
|
|
||||||
|
{
|
||||||
|
const withHost = new URL('file://host/a');
|
||||||
|
|
||||||
|
if (isWindows) {
|
||||||
|
assert.strictEqual(url.fileURLToPath(withHost), '\\\\host\\a');
|
||||||
|
} else {
|
||||||
|
assert.throws(() => url.fileURLToPath(withHost), {
|
||||||
|
code: 'ERR_INVALID_FILE_URL_HOST'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
if (isWindows) {
|
||||||
|
assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), {
|
||||||
|
code: 'ERR_INVALID_FILE_URL_PATH'
|
||||||
|
});
|
||||||
|
assert.throws(() => url.fileURLToPath('file:///C:/a%5C/'), {
|
||||||
|
code: 'ERR_INVALID_FILE_URL_PATH'
|
||||||
|
});
|
||||||
|
assert.throws(() => url.fileURLToPath('file:///?:/'), {
|
||||||
|
code: 'ERR_INVALID_FILE_URL_PATH'
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
assert.throws(() => url.fileURLToPath('file:///a%2F/'), {
|
||||||
|
code: 'ERR_INVALID_FILE_URL_PATH'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let testCases;
|
||||||
|
if (isWindows) {
|
||||||
|
testCases = [
|
||||||
|
// lowercase ascii alpha
|
||||||
|
{ path: 'C:\\foo', fileURL: 'file:///C:/foo' },
|
||||||
|
// uppercase ascii alpha
|
||||||
|
{ path: 'C:\\FOO', fileURL: 'file:///C:/FOO' },
|
||||||
|
// dir
|
||||||
|
{ path: 'C:\\dir\\foo', fileURL: 'file:///C:/dir/foo' },
|
||||||
|
// trailing separator
|
||||||
|
{ path: 'C:\\dir\\', fileURL: 'file:///C:/dir/' },
|
||||||
|
// dot
|
||||||
|
{ path: 'C:\\foo.mjs', fileURL: 'file:///C:/foo.mjs' },
|
||||||
|
// space
|
||||||
|
{ path: 'C:\\foo bar', fileURL: 'file:///C:/foo%20bar' },
|
||||||
|
// question mark
|
||||||
|
{ path: 'C:\\foo?bar', fileURL: 'file:///C:/foo%3Fbar' },
|
||||||
|
// number sign
|
||||||
|
{ path: 'C:\\foo#bar', fileURL: 'file:///C:/foo%23bar' },
|
||||||
|
// ampersand
|
||||||
|
{ path: 'C:\\foo&bar', fileURL: 'file:///C:/foo&bar' },
|
||||||
|
// equals
|
||||||
|
{ path: 'C:\\foo=bar', fileURL: 'file:///C:/foo=bar' },
|
||||||
|
// colon
|
||||||
|
{ path: 'C:\\foo:bar', fileURL: 'file:///C:/foo:bar' },
|
||||||
|
// semicolon
|
||||||
|
{ path: 'C:\\foo;bar', fileURL: 'file:///C:/foo;bar' },
|
||||||
|
// percent
|
||||||
|
{ path: 'C:\\foo%bar', fileURL: 'file:///C:/foo%25bar' },
|
||||||
|
// backslash
|
||||||
|
{ path: 'C:\\foo\\bar', fileURL: 'file:///C:/foo/bar' },
|
||||||
|
// backspace
|
||||||
|
{ path: 'C:\\foo\bbar', fileURL: 'file:///C:/foo%08bar' },
|
||||||
|
// tab
|
||||||
|
{ path: 'C:\\foo\tbar', fileURL: 'file:///C:/foo%09bar' },
|
||||||
|
// newline
|
||||||
|
{ path: 'C:\\foo\nbar', fileURL: 'file:///C:/foo%0Abar' },
|
||||||
|
// carriage return
|
||||||
|
{ path: 'C:\\foo\rbar', fileURL: 'file:///C:/foo%0Dbar' },
|
||||||
|
// latin1
|
||||||
|
{ path: 'C:\\fóóbàr', fileURL: 'file:///C:/f%C3%B3%C3%B3b%C3%A0r' },
|
||||||
|
// euro sign (BMP code point)
|
||||||
|
{ path: 'C:\\€', fileURL: 'file:///C:/%E2%82%AC' },
|
||||||
|
// rocket emoji (non-BMP code point)
|
||||||
|
{ path: 'C:\\🚀', fileURL: 'file:///C:/%F0%9F%9A%80' }
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
testCases = [
|
||||||
|
// lowercase ascii alpha
|
||||||
|
{ path: '/foo', fileURL: 'file:///foo' },
|
||||||
|
// uppercase ascii alpha
|
||||||
|
{ path: '/FOO', fileURL: 'file:///FOO' },
|
||||||
|
// dir
|
||||||
|
{ path: '/dir/foo', fileURL: 'file:///dir/foo' },
|
||||||
|
// trailing separator
|
||||||
|
{ path: '/dir/', fileURL: 'file:///dir/' },
|
||||||
|
// dot
|
||||||
|
{ path: '/foo.mjs', fileURL: 'file:///foo.mjs' },
|
||||||
|
// space
|
||||||
|
{ path: '/foo bar', fileURL: 'file:///foo%20bar' },
|
||||||
|
// question mark
|
||||||
|
{ path: '/foo?bar', fileURL: 'file:///foo%3Fbar' },
|
||||||
|
// number sign
|
||||||
|
{ path: '/foo#bar', fileURL: 'file:///foo%23bar' },
|
||||||
|
// ampersand
|
||||||
|
{ path: '/foo&bar', fileURL: 'file:///foo&bar' },
|
||||||
|
// equals
|
||||||
|
{ path: '/foo=bar', fileURL: 'file:///foo=bar' },
|
||||||
|
// colon
|
||||||
|
{ path: '/foo:bar', fileURL: 'file:///foo:bar' },
|
||||||
|
// semicolon
|
||||||
|
{ path: '/foo;bar', fileURL: 'file:///foo;bar' },
|
||||||
|
// percent
|
||||||
|
{ path: '/foo%bar', fileURL: 'file:///foo%25bar' },
|
||||||
|
// backslash
|
||||||
|
{ path: '/foo\\bar', fileURL: 'file:///foo%5Cbar' },
|
||||||
|
// backspace
|
||||||
|
{ path: '/foo\bbar', fileURL: 'file:///foo%08bar' },
|
||||||
|
// tab
|
||||||
|
{ path: '/foo\tbar', fileURL: 'file:///foo%09bar' },
|
||||||
|
// newline
|
||||||
|
{ path: '/foo\nbar', fileURL: 'file:///foo%0Abar' },
|
||||||
|
// carriage return
|
||||||
|
{ path: '/foo\rbar', fileURL: 'file:///foo%0Dbar' },
|
||||||
|
// latin1
|
||||||
|
{ path: '/fóóbàr', fileURL: 'file:///f%C3%B3%C3%B3b%C3%A0r' },
|
||||||
|
// euro sign (BMP code point)
|
||||||
|
{ path: '/€', fileURL: 'file:///%E2%82%AC' },
|
||||||
|
// rocket emoji (non-BMP code point)
|
||||||
|
{ path: '/🚀', fileURL: 'file:///%F0%9F%9A%80' },
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const { path, fileURL } of testCases) {
|
||||||
|
const fromString = url.fileURLToPath(fileURL);
|
||||||
|
assert.strictEqual(fromString, path);
|
||||||
|
const fromURL = url.fileURLToPath(new URL(fileURL));
|
||||||
|
assert.strictEqual(fromURL, path);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user