From a882195e9ba66d2e59ebbb28fcdb2efac7bc4cc5 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 18 May 2025 10:49:02 +0200 Subject: [PATCH] Remove Node.js-specific checks when using the Fetch API Given that Node.js has full support for the Fetch API since version 21, see the "History" data at https://nodejs.org/api/globals.html#fetch, it seems unnecessary for us to manually check for various globals before using it. Since our primary development target is browsers in general, and Firefox in particular, being able to remove Node.js-specific compatibility code is always helpful. Note that we still, for now, support Node.js version 20 and if the relevant globals are not available then Errors will instead be thrown from within the `PDFFetchStream` class. --- src/display/api.js | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 61b326891..f6723bee6 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -462,32 +462,14 @@ function getDocument(src = {}) { if (!url) { throw new Error("getDocument - no `url` parameter provided."); } - let NetworkStream; - - if ( - typeof PDFJSDev !== "undefined" && - PDFJSDev.test("GENERIC") && - isNodeJS - ) { - if (isValidFetchUrl(url)) { - if ( - typeof fetch === "undefined" || - typeof Response === "undefined" || - !("body" in Response.prototype) - ) { - throw new Error( - "getDocument - the Fetch API was disabled in Node.js, see `--no-experimental-fetch`." - ); - } - NetworkStream = PDFFetchStream; - } else { - NetworkStream = PDFNodeStream; - } - } else { - NetworkStream = isValidFetchUrl(url) - ? PDFFetchStream + // eslint-disable-next-line no-nested-ternary + const NetworkStream = isValidFetchUrl(url) + ? PDFFetchStream + : typeof PDFJSDev !== "undefined" && + PDFJSDev.test("GENERIC") && + isNodeJS + ? PDFNodeStream : PDFNetworkStream; - } networkStream = new NetworkStream({ url,