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

@ -127,22 +127,27 @@ class BaseCMapReaderFactory {
throw new Error("CMap name must be specified."); throw new Error("CMap name must be specified.");
} }
const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : ""); const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
const compressionType = this.isCompressed
? CMapCompressionType.BINARY
: CMapCompressionType.NONE;
return this._fetchData(url, compressionType).catch(reason => { return this._fetch(url)
throw new Error( .then(cMapData => ({
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}` cMapData,
); compressionType: this.isCompressed
}); ? CMapCompressionType.BINARY
: CMapCompressionType.NONE,
}))
.catch(reason => {
throw new Error(
`Unable to load ${this.isCompressed ? "binary " : ""}CMap at: ${url}`
);
});
} }
/** /**
* @ignore * @ignore
* @returns {Promise<Uint8Array>}
*/ */
_fetchData(url, compressionType) { async _fetch(url) {
unreachable("Abstract method `_fetchData` called."); unreachable("Abstract method `_fetch` called.");
} }
} }
@ -168,16 +173,17 @@ class BaseStandardFontDataFactory {
} }
const url = `${this.baseUrl}${filename}`; const url = `${this.baseUrl}${filename}`;
return this._fetchData(url).catch(reason => { return this._fetch(url).catch(reason => {
throw new Error(`Unable to load font data at: ${url}`); throw new Error(`Unable to load font data at: ${url}`);
}); });
} }
/** /**
* @ignore * @ignore
* @returns {Promise<Uint8Array>}
*/ */
_fetchData(url) { async _fetch(url) {
unreachable("Abstract method `_fetchData` called."); unreachable("Abstract method `_fetch` called.");
} }
} }

View File

@ -564,17 +564,14 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
/** /**
* @ignore * @ignore
*/ */
_fetchData(url, compressionType) { async _fetch(url) {
return fetchData( const data = await fetchData(
url, url,
/* type = */ this.isCompressed ? "arraybuffer" : "text" /* type = */ this.isCompressed ? "arraybuffer" : "text"
).then(data => ({ );
cMapData: return data instanceof ArrayBuffer
data instanceof ArrayBuffer ? new Uint8Array(data)
? new Uint8Array(data) : stringToBytes(data);
: stringToBytes(data),
compressionType,
}));
} }
} }
@ -582,10 +579,9 @@ class DOMStandardFontDataFactory extends BaseStandardFontDataFactory {
/** /**
* @ignore * @ignore
*/ */
_fetchData(url) { async _fetch(url) {
return fetchData(url, /* type = */ "arraybuffer").then( const data = await fetchData(url, /* type = */ "arraybuffer");
data => new Uint8Array(data) return new Uint8Array(data);
);
} }
} }

View File

@ -113,10 +113,11 @@ class NodePackages {
} }
} }
const fetchData = function (url) { async function fetchData(url) {
const fs = NodePackages.get("fs"); const fs = NodePackages.get("fs");
return fs.promises.readFile(url).then(data => new Uint8Array(data)); const data = await fs.promises.readFile(url);
}; return new Uint8Array(data);
}
class NodeFilterFactory extends BaseFilterFactory {} class NodeFilterFactory extends BaseFilterFactory {}
@ -134,8 +135,8 @@ class NodeCMapReaderFactory extends BaseCMapReaderFactory {
/** /**
* @ignore * @ignore
*/ */
_fetchData(url, compressionType) { async _fetch(url) {
return fetchData(url).then(data => ({ cMapData: data, compressionType })); return fetchData(url);
} }
} }
@ -143,12 +144,13 @@ class NodeStandardFontDataFactory extends BaseStandardFontDataFactory {
/** /**
* @ignore * @ignore
*/ */
_fetchData(url) { async _fetch(url) {
return fetchData(url); return fetchData(url);
} }
} }
export { export {
fetchData,
NodeCanvasFactory, NodeCanvasFactory,
NodeCMapReaderFactory, NodeCMapReaderFactory,
NodeFilterFactory, NodeFilterFactory,

View File

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