Re-factor the DefaultFileReaderFactory unit-test helper
We can re-use the existing helpers from `src/display/` rather than re-implementing the functionality here.
This commit is contained in:
parent
df696063b9
commit
6c3336f04f
@ -150,6 +150,7 @@ class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
fetchData,
|
||||||
NodeCanvasFactory,
|
NodeCanvasFactory,
|
||||||
NodeCMapReaderFactory,
|
NodeCMapReaderFactory,
|
||||||
NodeFilterFactory,
|
NodeFilterFactory,
|
||||||
|
|||||||
@ -16,6 +16,8 @@
|
|||||||
import { assert, isNodeJS } from "../../src/shared/util.js";
|
import { assert, isNodeJS } from "../../src/shared/util.js";
|
||||||
import { NullStream, StringStream } from "../../src/core/stream.js";
|
import { NullStream, StringStream } from "../../src/core/stream.js";
|
||||||
import { Page, PDFDocument } from "../../src/core/document.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";
|
import { Ref } from "../../src/core/primitives.js";
|
||||||
|
|
||||||
let fs, http;
|
let fs, http;
|
||||||
@ -33,27 +35,16 @@ const STANDARD_FONT_DATA_URL = isNodeJS
|
|||||||
? "./external/standard_fonts/"
|
? "./external/standard_fonts/"
|
||||||
: "../../external/standard_fonts/";
|
: "../../external/standard_fonts/";
|
||||||
|
|
||||||
class DOMFileReaderFactory {
|
class DefaultFileReaderFactory {
|
||||||
static async fetch(params) {
|
static async fetch(params) {
|
||||||
const response = await fetch(params.path);
|
if (isNodeJS) {
|
||||||
if (!response.ok) {
|
return fetchDataNode(params.path);
|
||||||
throw new Error(response.statusText);
|
|
||||||
}
|
}
|
||||||
return new Uint8Array(await response.arrayBuffer());
|
const data = await fetchDataDOM(params.path, /* type = */ "arraybuffer");
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class NodeFileReaderFactory {
|
|
||||||
static async fetch(params) {
|
|
||||||
const data = await fs.promises.readFile(params.path);
|
|
||||||
return new Uint8Array(data);
|
return new Uint8Array(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const DefaultFileReaderFactory = isNodeJS
|
|
||||||
? NodeFileReaderFactory
|
|
||||||
: DOMFileReaderFactory;
|
|
||||||
|
|
||||||
function buildGetDocumentParams(filename, options) {
|
function buildGetDocumentParams(filename, options) {
|
||||||
const params = Object.create(null);
|
const params = Object.create(null);
|
||||||
params.url = isNodeJS
|
params.url = isNodeJS
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user