diff options
| author | Michael Williamson <mike@zwobble.org> | 2025-09-10 15:40:13 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2025-09-10 15:40:39 +0100 |
| commit | 162324eb530d781aa59acce597997646ee59d1eb (patch) | |
| tree | 671a2252c6d4f135831f9cd444b81adbe8f14795 | |
| parent | 3705a9daefcac5dda88a165a92e5479e90a794a7 (diff) | |
Remove Files.relativeToFile() in favour of options argument
| -rw-r--r-- | lib/docx/docx-reader.js | 4 | ||||
| -rw-r--r-- | lib/docx/files.js | 11 | ||||
| -rw-r--r-- | test/docx/files.tests.js | 26 |
3 files changed, 19 insertions, 22 deletions
diff --git a/lib/docx/docx-reader.js b/lib/docx/docx-reader.js index 0d48d0a..342ec46 100644 --- a/lib/docx/docx-reader.js +++ b/lib/docx/docx-reader.js @@ -21,7 +21,9 @@ var Files = require("./files").Files; function read(docxFile, input) { input = input || {}; - var files = input.path ? Files.relativeToFile(input.path) : new Files(null); + var files = new Files({ + relativeToFile: input.path + }); return promises.props({ contentTypes: readContentTypesFromZipFile(docxFile), diff --git a/lib/docx/files.js b/lib/docx/files.js index 502db08..4b09b69 100644 --- a/lib/docx/files.js +++ b/lib/docx/files.js @@ -12,7 +12,9 @@ exports.Files = Files; exports.uriToPath = uriToPath; -function Files(base) { +function Files(options) { + var base = options.relativeToFile ? dirname(options.relativeToFile) : null; + function read(uri, encoding) { return resolveUri(uri).then(function(path) { return readFile(path, encoding).caught(function(error) { @@ -39,13 +41,6 @@ function Files(base) { } -function relativeToFile(filePath) { - return new Files(dirname(filePath)); -} - -Files.relativeToFile = relativeToFile; - - var readFile = promises.promisify(fs.readFile.bind(fs)); diff --git a/test/docx/files.tests.js b/test/docx/files.tests.js index 75999aa..9a39acb 100644 --- a/test/docx/files.tests.js +++ b/test/docx/files.tests.js @@ -15,33 +15,33 @@ var readFile = promises.promisify(fs.readFile.bind(fs)); test("Files", { "can open files with file URI": function() { var filePath = path.resolve(testing.testPath("tiny-picture.png")); - var files = new Files(null); + var files = new Files({}); return files.read("file:///" + filePath.replace(/^\//, ""), "base64").then(function(contents) { return readFile(filePath, "base64").then(function(expectedContents) { assert.deepEqual(contents, expectedContents); }); }); }, - + "can open files with relative URI": function() { var filePath = path.resolve(testing.testPath("tiny-picture.png")); - var files = new Files(testing.testPath(".")); + var files = new Files({relativeToFile: testing.testPath("./document.docx")}); return files.read("tiny-picture.png", "base64").then(function(contents) { return readFile(filePath, "base64").then(function(expectedContents) { assert.deepEqual(contents, expectedContents); }); }); }, - + "given base is not set when opening relative uri then error is raised": function() { - var files = new Files(null); + var files = new Files({}); return assertError(files.read("not-a-real-file.png", "base64"), function(err) { assert.equal(err.message, "could not find external image 'not-a-real-file.png', path of input document is unknown"); }); }, - + "error if relative uri cannot be opened": function() { - var files = new Files("/tmp"); + var files = new Files({relativeToFile: "/tmp/document.docx"}); return assertError(files.read("not-a-real-file.png", "base64"), function(err) { assertRegex(err.message, /could not open external image: 'not-a-real-file.png' \(document directory: '\/tmp'\)\nENOENT.*\/tmp\/not-a-real-file.png.*/); }); @@ -64,30 +64,30 @@ test("uriToPath", { assert.equal(uriToPath("file:///a/b/c", "linux"), "/a/b/c"); assert.equal(uriToPath("file:///a/b/c", "win32"), "/a/b/c"); }, - + "URI is unquoted": function() { assert.equal(uriToPath("file:///a%20b"), "/a b"); }, - + "when host is set to localhost then path can be found": function() { assert.equal(uriToPath("file://localhost/a/b/c"), "/a/b/c"); }, - + "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/); }, - + "leading slash is not dropped on Windows file URIs when platform is not Windows": function() { assert.equal(uriToPath("file:///c:/a", "linux"), "/c:/a"); }, - + "leading slash is dropped on Windows file URIs when platform is Windows": function() { assert.equal(uriToPath("file:///c:/a", "win32"), "c:/a"); assert.equal(uriToPath("file:///C:/a", "win32"), "C:/a"); }, - + "relative URI is unquoted": function() { assert.equal(uriToPath("a%20b/c"), "a b/c"); } |
