Print more warnings about potential problems in Node.js environments

- Warn if the "regular" PDF.js build is used in Node.js environments, since that won't load any of the relevant polyfills.

 - Warn if the `require` function cannot be accessed, since currently we're just "swallowing" any errors.
This commit is contained in:
Jonas Jenwald 2024-12-07 14:01:00 +01:00
parent b870c5d2ad
commit c60a6d1ebd

View File

@ -26,11 +26,10 @@ if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
); );
} }
if ( if (isNodeJS) {
typeof PDFJSDev !== "undefined" && if (typeof PDFJSDev === "undefined" || PDFJSDev.test("SKIP_BABEL")) {
!PDFJSDev.test("SKIP_BABEL") && warn("Please use the `legacy` build in Node.js environments.");
isNodeJS } else {
) {
let canvas; let canvas;
try { try {
const require = process const require = process
@ -42,7 +41,9 @@ if (
} catch (ex) { } catch (ex) {
warn(`Cannot load "@napi-rs/canvas" package: "${ex}".`); warn(`Cannot load "@napi-rs/canvas" package: "${ex}".`);
} }
} catch {} } catch (ex) {
warn(`Cannot access the \`require\` function: "${ex}".`);
}
if (!globalThis.DOMMatrix) { if (!globalThis.DOMMatrix) {
if (canvas?.DOMMatrix) { if (canvas?.DOMMatrix) {
@ -66,6 +67,7 @@ if (
} }
} }
} }
}
async function fetchData(url) { async function fetchData(url) {
const fs = process.getBuiltinModule("fs"); const fs = process.getBuiltinModule("fs");