Skip to content

Commit 6e716ed

Browse files
Kevin Smithaddaleax
Kevin Smith
authored andcommittedJan 14, 2019
url: return backslashes from fileURLToPath on win
PR-URL: #25349 Fixes: #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>
1 parent 07ffa3f commit 6e716ed

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed
 

‎lib/internal/url.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,8 @@ function urlToOptions(url) {
12751275
return options;
12761276
}
12771277

1278+
const forwardSlashRegEx = /\//g;
1279+
12781280
function getPathFromURLWin32(url) {
12791281
var hostname = url.hostname;
12801282
var pathname = url.pathname;
@@ -1289,6 +1291,7 @@ function getPathFromURLWin32(url) {
12891291
}
12901292
}
12911293
}
1294+
pathname = pathname.replace(forwardSlashRegEx, '\\');
12921295
pathname = decodeURIComponent(pathname);
12931296
if (hostname !== '') {
12941297
// If hostname is set, then we have a UNC path
@@ -1297,7 +1300,7 @@ function getPathFromURLWin32(url) {
12971300
// about percent encoding because the URL parser will have
12981301
// already taken care of that for us. Note that this only
12991302
// causes IDNs with an appropriate `xn--` prefix to be decoded.
1300-
return `//${domainToUnicode(hostname)}${pathname}`;
1303+
return `\\\\${domainToUnicode(hostname)}${pathname}`;
13011304
} else {
13021305
// Otherwise, it's a local path that requires a drive letter
13031306
var letter = pathname.codePointAt(1) | 0x20;

‎test/parallel/test-fs-whatwg-url.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ if (common.isWindows) {
6363
code: 'ERR_INVALID_ARG_VALUE',
6464
type: TypeError,
6565
message: 'The argument \'path\' must be a string or Uint8Array without ' +
66-
'null bytes. Received \'c:/tmp/\\u0000test\''
66+
'null bytes. Received \'c:\\\\tmp\\\\\\u0000test\''
6767
}
6868
);
6969
} else {
+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
'use strict';
2+
const { isWindows } = require('../common');
3+
const assert = require('assert');
4+
const url = require('url');
5+
6+
function testInvalidArgs(...args) {
7+
for (const arg of args) {
8+
assert.throws(() => url.fileURLToPath(arg), {
9+
code: 'ERR_INVALID_ARG_TYPE'
10+
});
11+
}
12+
}
13+
14+
// Input must be string or URL
15+
testInvalidArgs(null, undefined, 1, {}, true);
16+
17+
// Input must be a file URL
18+
assert.throws(() => url.fileURLToPath('https://a/b/c'), {
19+
code: 'ERR_INVALID_URL_SCHEME'
20+
});
21+
22+
{
23+
const withHost = new URL('file://host/a');
24+
25+
if (isWindows) {
26+
assert.strictEqual(url.fileURLToPath(withHost), '\\\\host\\a');
27+
} else {
28+
assert.throws(() => url.fileURLToPath(withHost), {
29+
code: 'ERR_INVALID_FILE_URL_HOST'
30+
});
31+
}
32+
}
33+
34+
{
35+
if (isWindows) {
36+
assert.throws(() => url.fileURLToPath('file:///C:/a%2F/'), {
37+
code: 'ERR_INVALID_FILE_URL_PATH'
38+
});
39+
assert.throws(() => url.fileURLToPath('file:///C:/a%5C/'), {
40+
code: 'ERR_INVALID_FILE_URL_PATH'
41+
});
42+
assert.throws(() => url.fileURLToPath('file:///?:/'), {
43+
code: 'ERR_INVALID_FILE_URL_PATH'
44+
});
45+
} else {
46+
assert.throws(() => url.fileURLToPath('file:///a%2F/'), {
47+
code: 'ERR_INVALID_FILE_URL_PATH'
48+
});
49+
}
50+
}
51+
52+
{
53+
let testCases;
54+
if (isWindows) {
55+
testCases = [
56+
// lowercase ascii alpha
57+
{ path: 'C:\\foo', fileURL: 'file:///C:/foo' },
58+
// uppercase ascii alpha
59+
{ path: 'C:\\FOO', fileURL: 'file:///C:/FOO' },
60+
// dir
61+
{ path: 'C:\\dir\\foo', fileURL: 'file:///C:/dir/foo' },
62+
// trailing separator
63+
{ path: 'C:\\dir\\', fileURL: 'file:///C:/dir/' },
64+
// dot
65+
{ path: 'C:\\foo.mjs', fileURL: 'file:///C:/foo.mjs' },
66+
// space
67+
{ path: 'C:\\foo bar', fileURL: 'file:///C:/foo%20bar' },
68+
// question mark
69+
{ path: 'C:\\foo?bar', fileURL: 'file:///C:/foo%3Fbar' },
70+
// number sign
71+
{ path: 'C:\\foo#bar', fileURL: 'file:///C:/foo%23bar' },
72+
// ampersand
73+
{ path: 'C:\\foo&bar', fileURL: 'file:///C:/foo&bar' },
74+
// equals
75+
{ path: 'C:\\foo=bar', fileURL: 'file:///C:/foo=bar' },
76+
// colon
77+
{ path: 'C:\\foo:bar', fileURL: 'file:///C:/foo:bar' },
78+
// semicolon
79+
{ path: 'C:\\foo;bar', fileURL: 'file:///C:/foo;bar' },
80+
// percent
81+
{ path: 'C:\\foo%bar', fileURL: 'file:///C:/foo%25bar' },
82+
// backslash
83+
{ path: 'C:\\foo\\bar', fileURL: 'file:///C:/foo/bar' },
84+
// backspace
85+
{ path: 'C:\\foo\bbar', fileURL: 'file:///C:/foo%08bar' },
86+
// tab
87+
{ path: 'C:\\foo\tbar', fileURL: 'file:///C:/foo%09bar' },
88+
// newline
89+
{ path: 'C:\\foo\nbar', fileURL: 'file:///C:/foo%0Abar' },
90+
// carriage return
91+
{ path: 'C:\\foo\rbar', fileURL: 'file:///C:/foo%0Dbar' },
92+
// latin1
93+
{ path: 'C:\\fóóbàr', fileURL: 'file:///C:/f%C3%B3%C3%B3b%C3%A0r' },
94+
// euro sign (BMP code point)
95+
{ path: 'C:\\€', fileURL: 'file:///C:/%E2%82%AC' },
96+
// rocket emoji (non-BMP code point)
97+
{ path: 'C:\\🚀', fileURL: 'file:///C:/%F0%9F%9A%80' }
98+
];
99+
} else {
100+
testCases = [
101+
// lowercase ascii alpha
102+
{ path: '/foo', fileURL: 'file:///foo' },
103+
// uppercase ascii alpha
104+
{ path: '/FOO', fileURL: 'file:///FOO' },
105+
// dir
106+
{ path: '/dir/foo', fileURL: 'file:///dir/foo' },
107+
// trailing separator
108+
{ path: '/dir/', fileURL: 'file:///dir/' },
109+
// dot
110+
{ path: '/foo.mjs', fileURL: 'file:///foo.mjs' },
111+
// space
112+
{ path: '/foo bar', fileURL: 'file:///foo%20bar' },
113+
// question mark
114+
{ path: '/foo?bar', fileURL: 'file:///foo%3Fbar' },
115+
// number sign
116+
{ path: '/foo#bar', fileURL: 'file:///foo%23bar' },
117+
// ampersand
118+
{ path: '/foo&bar', fileURL: 'file:///foo&bar' },
119+
// equals
120+
{ path: '/foo=bar', fileURL: 'file:///foo=bar' },
121+
// colon
122+
{ path: '/foo:bar', fileURL: 'file:///foo:bar' },
123+
// semicolon
124+
{ path: '/foo;bar', fileURL: 'file:///foo;bar' },
125+
// percent
126+
{ path: '/foo%bar', fileURL: 'file:///foo%25bar' },
127+
// backslash
128+
{ path: '/foo\\bar', fileURL: 'file:///foo%5Cbar' },
129+
// backspace
130+
{ path: '/foo\bbar', fileURL: 'file:///foo%08bar' },
131+
// tab
132+
{ path: '/foo\tbar', fileURL: 'file:///foo%09bar' },
133+
// newline
134+
{ path: '/foo\nbar', fileURL: 'file:///foo%0Abar' },
135+
// carriage return
136+
{ path: '/foo\rbar', fileURL: 'file:///foo%0Dbar' },
137+
// latin1
138+
{ path: '/fóóbàr', fileURL: 'file:///f%C3%B3%C3%B3b%C3%A0r' },
139+
// euro sign (BMP code point)
140+
{ path: '/€', fileURL: 'file:///%E2%82%AC' },
141+
// rocket emoji (non-BMP code point)
142+
{ path: '/🚀', fileURL: 'file:///%F0%9F%9A%80' },
143+
];
144+
}
145+
146+
for (const { path, fileURL } of testCases) {
147+
const fromString = url.fileURLToPath(fileURL);
148+
assert.strictEqual(fromString, path);
149+
const fromURL = url.fileURLToPath(new URL(fileURL));
150+
assert.strictEqual(fromURL, path);
151+
}
152+
}

0 commit comments

Comments
 (0)