diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-09-06 17:43:29 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-09-06 17:46:16 +0100 |
| commit | f16aeec2396f78cc3cedcaafa5d58899408ffb99 (patch) | |
| tree | b22834c594871c32ab85b0a7135dc3aa35b028be | |
| parent | 854682f7fcc4c68034b1174f461558840f39a7bf (diff) | |
Allow localhost in file:/// URIs
| -rw-r--r-- | lib/docx/files.js | 15 | ||||
| -rw-r--r-- | tests/docx/files.tests.js | 8 |
2 files changed, 14 insertions, 9 deletions
diff --git a/lib/docx/files.js b/lib/docx/files.js index e25d9cc..4e3a820 100644 --- a/lib/docx/files.js +++ b/lib/docx/files.js @@ -1,4 +1,5 @@ var fs = require("fs"); +var url = require("url"); var promises = require("../promises"); @@ -20,15 +21,11 @@ function Files() { var readFile = promises.promisify(fs.readFile.bind(fs)); -function uriToPath(uri) { - var fileUriPrefix = "file:///"; - if (startsWith(uri, fileUriPrefix)) { - return decodeURIComponent(uri.slice(fileUriPrefix.length - 1)); +function uriToPath(uriString) { + var uri = url.parse(uriString); + if (uri.protocol === "file:" && (!uri.host || uri.host === "localhost")) { + return decodeURIComponent(uri.path); } else { - throw new Error("Could not convert URI to path: " + uri); + throw new Error("Could not convert URI to path: " + uriString); } } - -function startsWith(string, search) { - return string.indexOf(search) === 0; -} diff --git a/tests/docx/files.tests.js b/tests/docx/files.tests.js index a21bc93..0ec64e1 100644 --- a/tests/docx/files.tests.js +++ b/tests/docx/files.tests.js @@ -33,4 +33,12 @@ describe("uriToPath", function() { test("URI is unquoted", function() { assert.equal(uriToPath("file:///a%20b"), "/a b"); }); + + test("when host is set to localhost then path can be found", function() { + assert.equal(uriToPath("file://localhost/a/b/c"), "/a/b/c"); + }); + + test("when host is set but not localhost then path cannot be found", function() { + assert.throws(function() { uriToPath("file://example/a/b/c"); }, /Could not convert URI to path: file:\/\/example\/a\/b\/c/); + }); }); |
