From 6c3336f04f7d751883ef56fc974339bdae9853ff Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 21 Oct 2024 11:58:06 +0200 Subject: [PATCH] Re-factor the `DefaultFileReaderFactory` unit-test helper We can re-use the existing helpers from `src/display/` rather than re-implementing the functionality here. --- src/display/node_utils.js | 1 + test/unit/test_utils.js | 21 ++++++--------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/src/display/node_utils.js b/src/display/node_utils.js index 0dcffdb2c878a..c0b1e6e2ce529 100644 --- a/src/display/node_utils.js +++ b/src/display/node_utils.js @@ -150,6 +150,7 @@ class NodeStandardFontDataFactory extends BaseStandardFontDataFactory { } export { + fetchData, NodeCanvasFactory, NodeCMapReaderFactory, NodeFilterFactory, diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index 507fc15649f48..473b292e83935 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -16,6 +16,8 @@ import { assert, isNodeJS } from "../../src/shared/util.js"; import { NullStream, StringStream } from "../../src/core/stream.js"; import { Page, PDFDocument } from "../../src/core/document.js"; +import { fetchData as fetchDataDOM } from "../../src/display/display_utils.js"; +import { fetchData as fetchDataNode } from "../../src/display/node_utils.js"; import { Ref } from "../../src/core/primitives.js"; let fs, http; @@ -33,27 +35,16 @@ const STANDARD_FONT_DATA_URL = isNodeJS ? "./external/standard_fonts/" : "../../external/standard_fonts/"; -class DOMFileReaderFactory { +class DefaultFileReaderFactory { static async fetch(params) { - const response = await fetch(params.path); - if (!response.ok) { - throw new Error(response.statusText); + if (isNodeJS) { + return fetchDataNode(params.path); } - return new Uint8Array(await response.arrayBuffer()); - } -} - -class NodeFileReaderFactory { - static async fetch(params) { - const data = await fs.promises.readFile(params.path); + const data = await fetchDataDOM(params.path, /* type = */ "arraybuffer"); return new Uint8Array(data); } } -const DefaultFileReaderFactory = isNodeJS - ? NodeFileReaderFactory - : DOMFileReaderFactory; - function buildGetDocumentParams(filename, options) { const params = Object.create(null); params.url = isNodeJS