Skip to content

Commit cd4aeda

Browse files
fix: allow Windows unc paths to be resolved and normalized (#1351)
* Add unc paths to isAbsolute regex * Allow unc paths to be resolved and normalized * Remove trailing whitespace
1 parent 7fbc79f commit cd4aeda

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/path/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var isAbsolute =
1414
* @returns {boolean} `true` if path is absolute
1515
*/
1616
path.isAbsolute = function isAbsolute(path) {
17-
return /^(?:\/|\w+:)/.test(path);
17+
return /^(?:\/|\w+:|\\\\\w+)/.test(path);
1818
};
1919

2020
var normalize =
@@ -24,6 +24,13 @@ var normalize =
2424
* @returns {string} Normalized path
2525
*/
2626
path.normalize = function normalize(path) {
27+
var firstTwoCharacters = path.substring(0,2);
28+
var uncPrefix = "";
29+
if (firstTwoCharacters === "\\\\") {
30+
uncPrefix = firstTwoCharacters;
31+
path = path.substring(2);
32+
}
33+
2734
path = path.replace(/\\/g, "/")
2835
.replace(/\/{2,}/g, "/");
2936
var parts = path.split("/"),
@@ -44,7 +51,7 @@ path.normalize = function normalize(path) {
4451
else
4552
++i;
4653
}
47-
return prefix + parts.join("/");
54+
return uncPrefix + prefix + parts.join("/");
4855
};
4956

5057
/**

lib/path/tests/index.js

+16
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ tape.test("path", function(test) {
1010
test.notOk(path.isAbsolute("some\\path\\file.js"), "should identify relative windows paths");
1111
test.notOk(path.isAbsolute("some/path/file.js"), "should identify relative unix paths");
1212

13+
test.ok(path.isAbsolute("\\\\some-unc\\path\\file.js"), "should identify windows unc paths");
14+
1315
var paths = [
1416
{
1517
actual: "X:\\some\\..\\.\\path\\\\file.js",
@@ -45,6 +47,20 @@ tape.test("path", function(test) {
4547
}, {
4648
actual: "/.././path//file.js",
4749
normal: "/path/file.js"
50+
}, {
51+
actual: "\\\\some-unc\\path\\file.js",
52+
normal: "\\\\some-unc/path/file.js",
53+
resolve: {
54+
origin: "\\\\some-unc\\path\\origin.js",
55+
expected: "\\\\some-unc/path/file.js"
56+
}
57+
}, {
58+
actual: "\\\\some-unc\\path\\..\\file.js",
59+
normal: "\\\\some-unc/file.js",
60+
resolve: {
61+
origin: "\\\\some-unc\\path\\..\\origin.js",
62+
expected: "\\\\some-unc/file.js"
63+
}
4864
}
4965
];
5066

0 commit comments

Comments
 (0)