1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-19 22:58:07 +02:00

Merge pull request #18933 from Snuffleupagus/base-factory-fetchData

Change the `BaseCMapReaderFactory` fetch-helper to return a `Uint8Array`
This commit is contained in:
Tim van der Meij 2024-10-22 20:03:50 +02:00 committed by GitHub
commit 1e07b87bb6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 47 deletions

View file

@ -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