Merge pull request #19436 from Snuffleupagus/api-FetchBinaryData

Combine the main-thread message handlers for CMap-, StandardFontData-, and Wasm-files
This commit is contained in:
Tim van der Meij 2025-02-07 21:13:58 +01:00 committed by GitHub
commit b43efdd545
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 20 additions and 43 deletions

View File

@ -386,13 +386,16 @@ class PartialEvaluator {
if (this.options.cMapUrl !== null) { if (this.options.cMapUrl !== null) {
// Only compressed CMaps are (currently) supported here. // Only compressed CMaps are (currently) supported here.
const cMapData = await fetchBinaryData( data = {
`${this.options.cMapUrl}${name}.bcmap` cMapData: await fetchBinaryData(`${this.options.cMapUrl}${name}.bcmap`),
); isCompressed: true,
data = { cMapData, isCompressed: true }; };
} else { } else {
// Get the data on the main-thread instead. // Get the data on the main-thread instead.
data = await this.handler.sendWithPromise("FetchBuiltInCMap", { name }); data = await this.handler.sendWithPromise("FetchBinaryData", {
type: "cMapReaderFactory",
name,
});
} }
// Cache the CMap data, to avoid fetching it repeatedly. // Cache the CMap data, to avoid fetching it repeatedly.
this.builtInCMapCache.set(name, data); this.builtInCMapCache.set(name, data);
@ -427,7 +430,8 @@ class PartialEvaluator {
); );
} else { } else {
// Get the data on the main-thread instead. // Get the data on the main-thread instead.
data = await this.handler.sendWithPromise("FetchStandardFontData", { data = await this.handler.sendWithPromise("FetchBinaryData", {
type: "standardFontDataFactory",
filename, filename,
}); });
} }

View File

@ -49,9 +49,10 @@ class JpxImage {
if (this.#wasmUrl !== null) { if (this.#wasmUrl !== null) {
this.#buffer = await fetchBinaryData(`${this.#wasmUrl}${filename}`); this.#buffer = await fetchBinaryData(`${this.#wasmUrl}${filename}`);
} else { } else {
this.#buffer = await this.#handler.sendWithPromise("FetchWasm", { this.#buffer = await this.#handler.sendWithPromise(
filename, "FetchBinaryData",
}); { type: "wasmFactory", filename }
);
} }
} }
const results = await WebAssembly.instantiate(this.#buffer, imports); const results = await WebAssembly.instantiate(this.#buffer, imports);

View File

@ -2881,49 +2881,21 @@ class WorkerTransport {
}); });
}); });
messageHandler.on("FetchBuiltInCMap", async data => { messageHandler.on("FetchBinaryData", async data => {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: FetchBuiltInCMap"); throw new Error("Not implemented: FetchBinaryData");
} }
if (this.destroyed) { if (this.destroyed) {
throw new Error("Worker was destroyed."); throw new Error("Worker was destroyed.");
} }
if (!this.cMapReaderFactory) { const factory = this[data.type];
throw new Error(
"CMapReaderFactory not initialized, see the `useWorkerFetch` parameter."
);
}
return this.cMapReaderFactory.fetch(data);
});
messageHandler.on("FetchStandardFontData", async data => { if (!factory) {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: FetchStandardFontData");
}
if (this.destroyed) {
throw new Error("Worker was destroyed.");
}
if (!this.standardFontDataFactory) {
throw new Error( throw new Error(
"StandardFontDataFactory not initialized, see the `useWorkerFetch` parameter." `${data.type} not initialized, see the \`useWorkerFetch\` parameter.`
); );
} }
return this.standardFontDataFactory.fetch(data); return factory.fetch(data);
});
messageHandler.on("FetchWasm", async data => {
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
throw new Error("Not implemented: FetchWasm");
}
if (this.destroyed) {
throw new Error("Worker was destroyed.");
}
if (!this.wasmFactory) {
throw new Error(
"WasmFactory not initialized, see the `useWorkerFetch` parameter."
);
}
return this.wasmFactory.fetch(data);
}); });
} }