Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Windows unc paths to be resolved and normalized #1351

Merged
merged 4 commits into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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