Skip to content
Permalink
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
  • Loading branch information
TomWilloughby committed Jun 25, 2020
1 parent 7fbc79f commit cd4aeda8036f80cfa3b9f1db4096d856b2fd05fb
Showing with 25 additions and 2 deletions.
  1. +9 −2 lib/path/index.js
  2. +16 −0 lib/path/tests/index.js
@@ -14,7 +14,7 @@ var isAbsolute =
* @returns {boolean} `true` if path is absolute
*/
path.isAbsolute = function isAbsolute(path) {
return /^(?:\/|\w+:)/.test(path);
return /^(?:\/|\w+:|\\\\\w+)/.test(path);
};

var normalize =
@@ -24,6 +24,13 @@ var normalize =
* @returns {string} Normalized path
*/
path.normalize = function normalize(path) {
var firstTwoCharacters = path.substring(0,2);
var uncPrefix = "";
if (firstTwoCharacters === "\\\\") {
uncPrefix = firstTwoCharacters;
path = path.substring(2);
}

path = path.replace(/\\/g, "/")
.replace(/\/{2,}/g, "/");
var parts = path.split("/"),
@@ -44,7 +51,7 @@ path.normalize = function normalize(path) {
else
++i;
}
return prefix + parts.join("/");
return uncPrefix + prefix + parts.join("/");
};

/**
@@ -10,6 +10,8 @@ tape.test("path", function(test) {
test.notOk(path.isAbsolute("some\\path\\file.js"), "should identify relative windows paths");
test.notOk(path.isAbsolute("some/path/file.js"), "should identify relative unix paths");

test.ok(path.isAbsolute("\\\\some-unc\\path\\file.js"), "should identify windows unc paths");

var paths = [
{
actual: "X:\\some\\..\\.\\path\\\\file.js",
@@ -45,6 +47,20 @@ tape.test("path", function(test) {
}, {
actual: "/.././path//file.js",
normal: "/path/file.js"
}, {
actual: "\\\\some-unc\\path\\file.js",
normal: "\\\\some-unc/path/file.js",
resolve: {
origin: "\\\\some-unc\\path\\origin.js",
expected: "\\\\some-unc/path/file.js"
}
}, {
actual: "\\\\some-unc\\path\\..\\file.js",
normal: "\\\\some-unc/file.js",
resolve: {
origin: "\\\\some-unc\\path\\..\\origin.js",
expected: "\\\\some-unc/file.js"
}
}
];

0 comments on commit cd4aeda

Please sign in to comment.