1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-28 23:28:16 +02:00

Handle JPX wasm fetch-response errors correctly (PR 19329 follow-up)

Currently we're not checking that the response is actually OK before getting the data, which means that rather than throwing an error we can get an empty `ArrayBuffer`.

To avoid duplicating code we can move an existing helper into `src/core/core_utils.js` and re-use it when fetching the JPX wasm-file as well.
This commit is contained in:
Jonas Jenwald 2025-01-17 01:02:39 +01:00
parent 88735d0f14
commit 6038b5a992
3 changed files with 24 additions and 17 deletions

View file

@ -14,6 +14,7 @@
*/
import { BaseException, warn } from "../shared/util.js";
import { fetchBinaryData } from "./core_utils.js";
import OpenJPEG from "../../external/openjpeg/openjpeg.js";
import { Stream } from "./stream.js";
@ -44,14 +45,14 @@ class JpxImage {
}
static async #instantiateWasm(imports, successCallback) {
const filename = "openjpeg.wasm";
try {
if (!this.#buffer) {
if (this.#wasmUrl !== null) {
const response = await fetch(`${this.#wasmUrl}openjpeg.wasm`);
this.#buffer = await response.arrayBuffer();
this.#buffer = await fetchBinaryData(`${this.#wasmUrl}${filename}`);
} else {
this.#buffer = await this.#handler.sendWithPromise("FetchWasm", {
filename: "openjpeg.wasm",
filename,
});
}
}
@ -59,7 +60,7 @@ class JpxImage {
return successCallback(results.instance);
} catch (e) {
this.#instantiationFailed = true;
warn(`Cannot load openjpeg.wasm: "${e}".`);
warn(`Cannot load ${filename}: "${e}".`);
return false;
} finally {
this.#handler = null;