From 97e7f639061ea9d034b4cb7e85e02275c882f2e2 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Wed, 7 Sep 2016 19:39:25 +0100 Subject: [PATCH 1/8] feat: short-circuit normalization if UNC --- lib/normalize.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/normalize.js b/lib/normalize.js index 04726d0..7606020 100644 --- a/lib/normalize.js +++ b/lib/normalize.js @@ -1,3 +1,4 @@ +var doubleSlackUNCRegExp = /^\\\\/; var doubleSlashWinRegExp = /\\+/g; var doubleSlashNixRegExp = /\/+/g; var currentDirectoryWinMiddleRegExp = /\\(\.\\)+/; @@ -34,5 +35,9 @@ module.exports = function normalize(path) { path = path.replace(parentDirectoryNixEndRegExp2, ""); path = path.replace(parentDirectoryNixEndRegExp3, "/"); + if (doubleSlackUNCRegExp.test(path)) { + return path; + } + return path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/"); }; \ No newline at end of file From 5f318767b001dd0733e24dae8e39032eec605b2d Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Wed, 7 Sep 2016 19:39:50 +0100 Subject: [PATCH 2/8] feat: make pathToArray handle UNC --- lib/MemoryFileSystem.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/MemoryFileSystem.js b/lib/MemoryFileSystem.js index abfe803..94d06ab 100644 --- a/lib/MemoryFileSystem.js +++ b/lib/MemoryFileSystem.js @@ -38,8 +38,16 @@ function isFile(item) { function pathToArray(path) { path = normalize(path); + + var UNC = /^\\\\/.test(path); var nix = /^\//.test(path); - if(!nix) { + + if(UNC) { + path = path.slice(2); + path = path.replace(/[\\\/]+/g, "\\"); + path = path.split(/[\\\/]/); + path[0] = '\\\\' + path[0]; + } else if(!nix) { if(!/^[A-Za-z]:/.test(path)) { throw new MemoryFileSystemError(errors.code.EINVAL, path); } From 6fb1ecec62e5f634cbcfb17413430882f7fb6278 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Wed, 7 Sep 2016 19:40:04 +0100 Subject: [PATCH 3/8] test: add a test case for pathToArray UNC --- test/MemoryFileSystem.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/MemoryFileSystem.js b/test/MemoryFileSystem.js index d11d889..5fc4611 100644 --- a/test/MemoryFileSystem.js +++ b/test/MemoryFileSystem.js @@ -341,6 +341,7 @@ describe("pathToArray", function() { fs.pathToArray("/a/b/c").should.be.eql(["a", "b", "c"]); fs.pathToArray("C:/a/b").should.be.eql(["C:", "a", "b"]); fs.pathToArray("C:\\a\\b").should.be.eql(["C:", "a", "b"]); + fs.pathToArray("\\\\a\\b\\c").should.be.eql(["\\\\a", "b", "c"]); }); it("should fail on invalid paths", function() { (function() { From dea22a3fa59bce5b27d6dcdc88a58e8a7961721c Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Wed, 7 Sep 2016 19:58:41 +0100 Subject: [PATCH 4/8] refactor: use lowercase name --- lib/MemoryFileSystem.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/MemoryFileSystem.js b/lib/MemoryFileSystem.js index 94d06ab..79c9933 100644 --- a/lib/MemoryFileSystem.js +++ b/lib/MemoryFileSystem.js @@ -39,10 +39,10 @@ function isFile(item) { function pathToArray(path) { path = normalize(path); - var UNC = /^\\\\/.test(path); + var unc = /^\\\\/.test(path); var nix = /^\//.test(path); - if(UNC) { + if(unc) { path = path.slice(2); path = path.replace(/[\\\/]+/g, "\\"); path = path.split(/[\\\/]/); From a1204c0292b984c9aa16f9ab562f36ef904ad035 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Wed, 7 Sep 2016 21:15:06 +0100 Subject: [PATCH 5/8] fix: remove double slashes from UNC paths --- lib/normalize.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/normalize.js b/lib/normalize.js index 7606020..fd28c84 100644 --- a/lib/normalize.js +++ b/lib/normalize.js @@ -35,9 +35,13 @@ module.exports = function normalize(path) { path = path.replace(parentDirectoryNixEndRegExp2, ""); path = path.replace(parentDirectoryNixEndRegExp3, "/"); - if (doubleSlackUNCRegExp.test(path)) { - return path; + var unc = doubleSlackUNCRegExp.test(path); + + path = path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/") + + if (unc) { + path = '\\' + path; } - return path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/"); -}; \ No newline at end of file + return path; +}; From 407053104022c5cf90b640efe04bdd21073d8349 Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Wed, 7 Sep 2016 21:15:22 +0100 Subject: [PATCH 6/8] test: add test cases for UNC paths --- test/MemoryFileSystem.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/MemoryFileSystem.js b/test/MemoryFileSystem.js index 5fc4611..df2cec1 100644 --- a/test/MemoryFileSystem.js +++ b/test/MemoryFileSystem.js @@ -333,6 +333,8 @@ describe("normalize", function() { fs.normalize("C:\\a\\b\\\c\\..\\..").should.be.eql("C:\\a"); fs.normalize("C:\\a\\b\\d\\..\\c\\..\\..").should.be.eql("C:\\a"); fs.normalize("C:\\a\\b\\d\\\\.\\\\.\\c\\.\\..").should.be.eql("C:\\a\\b\\d"); + fs.normalize("\\\\a\\\\b").should.be.eql("\\\\a\\b"); + fs.normalize("\\\\a\\\\b\\..\\c").should.be.eql("\\\\a\\c"); }); }); describe("pathToArray", function() { From a66aa97092e87cfa838126bc66b94a39ffeeba0b Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Sat, 17 Sep 2016 20:28:32 +0100 Subject: [PATCH 7/8] refactor: use regex to avoid UNC check --- lib/normalize.js | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/normalize.js b/lib/normalize.js index fd28c84..fffe743 100644 --- a/lib/normalize.js +++ b/lib/normalize.js @@ -1,5 +1,4 @@ -var doubleSlackUNCRegExp = /^\\\\/; -var doubleSlashWinRegExp = /\\+/g; +var doubleSlashWinRegExp = /(?!^)\\+/g;; var doubleSlashNixRegExp = /\/+/g; var currentDirectoryWinMiddleRegExp = /\\(\.\\)+/; var currentDirectoryWinEndRegExp = /\\\.$/; @@ -35,13 +34,7 @@ module.exports = function normalize(path) { path = path.replace(parentDirectoryNixEndRegExp2, ""); path = path.replace(parentDirectoryNixEndRegExp3, "/"); - var unc = doubleSlackUNCRegExp.test(path); - - path = path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/") - - if (unc) { - path = '\\' + path; - } + path = path.replace(doubleSlashWinRegExp, "\\").replace(doubleSlashNixRegExp, "/"); return path; }; From 3ccfa79059566caaf8923ade6a467fd1742e70bc Mon Sep 17 00:00:00 2001 From: Gajus Kuizinas Date: Sat, 17 Sep 2016 20:37:42 +0100 Subject: [PATCH 8/8] feat: add UNC paths to the join absoluteWinRegexp regex --- lib/join.js | 2 +- test/MemoryFileSystem.js | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/join.js b/lib/join.js index 149ce8a..050814a 100644 --- a/lib/join.js +++ b/lib/join.js @@ -1,6 +1,6 @@ var normalize = require("./normalize"); -var absoluteWinRegExp = /^[A-Z]:([\\\/]|$)/i; +var absoluteWinRegExp = /^(?:[A-Z]:([\\\/]|$))|(\\\\)/i; var absoluteNixRegExp = /^\//i; module.exports = function join(path, request) { diff --git a/test/MemoryFileSystem.js b/test/MemoryFileSystem.js index df2cec1..52eddd2 100644 --- a/test/MemoryFileSystem.js +++ b/test/MemoryFileSystem.js @@ -366,6 +366,7 @@ describe("join", function() { fs.join("C:\\", "a\\b").should.be.eql("C:\\a\\b"); fs.join("C:/a/b", "./../c/d").should.be.eql("C:\\a\\c\\d"); fs.join("C:\\a\\b", "./../c/d").should.be.eql("C:\\a\\c\\d"); + fs.join("\\\\a\\b\\..\\c\\d\\..").should.be.eql("\\\\a\\c"); }); it("should join paths (weird cases)", function() { var fs = new MemoryFileSystem();