From cd4aeda8036f80cfa3b9f1db4096d856b2fd05fb Mon Sep 17 00:00:00 2001 From: Tom Willoughby Date: Thu, 25 Jun 2020 17:56:45 +0100 Subject: [PATCH] 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 --- lib/path/index.js | 11 +++++++++-- lib/path/tests/index.js | 16 ++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/path/index.js b/lib/path/index.js index 7c7fb723e934..8c7ee12fb5b0 100644 --- a/lib/path/index.js +++ b/lib/path/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("/"); }; /** diff --git a/lib/path/tests/index.js b/lib/path/tests/index.js index 9c23bc9697e3..6b6155667553 100644 --- a/lib/path/tests/index.js +++ b/lib/path/tests/index.js @@ -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" + } } ];