Skip to content

Commit

Permalink
fix: allow Windows unc paths to be resolved and normalized (protobufj…
Browse files Browse the repository at this point in the history
…s#1351)

* Add unc paths to isAbsolute regex

* Allow unc paths to be resolved and normalized

* Remove trailing whitespace
  • Loading branch information
TomWilloughby authored and taylorcode committed Oct 16, 2020
1 parent ffc9400 commit df2f94b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/path/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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("/"),
Expand All @@ -44,7 +51,7 @@ path.normalize = function normalize(path) {
else
++i;
}
return prefix + parts.join("/");
return uncPrefix + prefix + parts.join("/");
};

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/path/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
}
}
];

Expand Down

0 comments on commit df2f94b

Please sign in to comment.