Skip to content

Commit

Permalink
Re-factor the DefaultFileReaderFactory unit-test helper
Browse files Browse the repository at this point in the history
We can re-use the existing helpers from `src/display/` rather than re-implementing the functionality here.
  • Loading branch information
Snuffleupagus committed Oct 21, 2024
1 parent df69606 commit 6c3336f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/display/node_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
}

export {
fetchData,
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeFilterFactory,
Expand Down
21 changes: 6 additions & 15 deletions test/unit/test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 6c3336f

Please sign in to comment.