Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/core/core_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ function arrayBuffersToBytes(arr) {
return data;
}

async function fetchBinaryData(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch file "${url}" with "${response.statusText}".`
);
}
return new Uint8Array(await response.arrayBuffer());
}

/**
* Get the value of an inheritable property.
*
Expand Down Expand Up @@ -701,6 +711,7 @@ export {
encodeToXmlString,
escapePDFName,
escapeString,
fetchBinaryData,
getInheritableProperty,
getLookupTableFactory,
getNewAnnotationsMap,
Expand Down
21 changes: 8 additions & 13 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ import {
import { CMapFactory, IdentityCMap } from "./cmap.js";
import { Cmd, Dict, EOF, isName, Name, Ref, RefSet } from "./primitives.js";
import { ErrorFont, Font } from "./fonts.js";
import {
fetchBinaryData,
isNumberArray,
lookupMatrix,
lookupNormalRect,
} from "./core_utils.js";
import {
getEncoding,
MacRomanEncoding,
Expand All @@ -51,7 +57,6 @@ import {
import { getTilingPatternIR, Pattern } from "./pattern.js";
import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js";
import { IdentityToUnicodeMap, ToUnicodeMap } from "./to_unicode_map.js";
import { isNumberArray, lookupMatrix, lookupNormalRect } from "./core_utils.js";
import { isPDFFunction, PDFFunctionFactory } from "./function.js";
import { Lexer, Parser } from "./parser.js";
import {
Expand Down Expand Up @@ -382,16 +387,6 @@ class PartialEvaluator {
return false;
}

async #fetchData(url) {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch file "${url}" with "${response.statusText}".`
);
}
return new Uint8Array(await response.arrayBuffer());
}

async fetchBuiltInCMap(name) {
const cachedData = this.builtInCMapCache.get(name);
if (cachedData) {
Expand All @@ -401,7 +396,7 @@ class PartialEvaluator {

if (this.options.cMapUrl !== null) {
// Only compressed CMaps are (currently) supported here.
const cMapData = await this.#fetchData(
const cMapData = await fetchBinaryData(
`${this.options.cMapUrl}${name}.bcmap`
);
data = { cMapData, isCompressed: true };
Expand Down Expand Up @@ -437,7 +432,7 @@ class PartialEvaluator {

try {
if (this.options.standardFontDataUrl !== null) {
data = await this.#fetchData(
data = await fetchBinaryData(
`${this.options.standardFontDataUrl}${filename}`
);
} else {
Expand Down
9 changes: 5 additions & 4 deletions src/core/jpx.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -44,22 +45,22 @@ 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,
});
}
}
const results = await WebAssembly.instantiate(this.#buffer, imports);
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;
Expand Down
Loading