From 0a970ee4434e62ed9ab5f134a9fc86ba8da81247 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 30 Sep 2023 13:25:47 +0200 Subject: [PATCH] [api-major] Remove the `fallbackWorkerSrc` functionality in browsers The user should *always* provide a correct `GlobalWorkerOptions.workerSrc` value when using the PDF.js library in browser environments. Note that the fallback: - Has been deprecated ever since PR 11418, first released in version `2.4.456` over three years ago. - Was always a best-effort solution, with no guarantees that it'd actually work correctly. - With upcoming changes, w.r.t. outputting JavaScript modules, it'd now be more diffiult to determine the correct value. --- src/display/api.js | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 43300b052..14382074f 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -45,7 +45,6 @@ import { SerializableEmpty, } from "./annotation_storage.js"; import { - deprecated, DOMCanvasFactory, DOMCMapReaderFactory, DOMFilterFactory, @@ -1984,7 +1983,6 @@ class LoopbackPort { const PDFWorkerUtil = { isWorkerDisabled: false, - fallbackWorkerSrc: null, fakeWorkerId: 0, }; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { @@ -1993,17 +1991,9 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { // Workers aren't supported in Node.js, force-disabling them there. PDFWorkerUtil.isWorkerDisabled = true; - PDFWorkerUtil.fallbackWorkerSrc = PDFJSDev.test("LIB") + GlobalWorkerOptions.workerSrc ||= PDFJSDev.test("LIB") ? "../pdf.worker.js" : "./pdf.worker.js"; - } else if (typeof document === "object") { - const pdfjsFilePath = document?.currentScript?.src; - if (pdfjsFilePath) { - PDFWorkerUtil.fallbackWorkerSrc = pdfjsFilePath.replace( - /(\.(?:min\.)?js)(\?.*)?$/i, - ".worker$1$2" - ); - } } // Check if URLs have the same origin. For non-HTTP based URLs, returns false. @@ -2119,7 +2109,7 @@ class PDFWorker { // Uint8Array as it arrives on the worker. (Chrome added this with v.15.) if ( !PDFWorkerUtil.isWorkerDisabled && - !PDFWorker._mainThreadWorkerMessageHandler + !PDFWorker.#mainThreadWorkerMessageHandler ) { let { workerSrc } = PDFWorker; @@ -2308,19 +2298,10 @@ class PDFWorker { if (GlobalWorkerOptions.workerSrc) { return GlobalWorkerOptions.workerSrc; } - if ( - (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && - PDFWorkerUtil.fallbackWorkerSrc !== null - ) { - if (!isNodeJS) { - deprecated('No "GlobalWorkerOptions.workerSrc" specified.'); - } - return PDFWorkerUtil.fallbackWorkerSrc; - } throw new Error('No "GlobalWorkerOptions.workerSrc" specified.'); } - static get _mainThreadWorkerMessageHandler() { + static get #mainThreadWorkerMessageHandler() { try { return globalThis.pdfjsWorker?.WorkerMessageHandler || null; } catch { @@ -2331,7 +2312,7 @@ class PDFWorker { // Loads worker code into the main-thread. static get _setupFakeWorkerGlobal() { const loader = async () => { - const mainWorkerMessageHandler = this._mainThreadWorkerMessageHandler; + const mainWorkerMessageHandler = this.#mainThreadWorkerMessageHandler; if (mainWorkerMessageHandler) { // The worker was already loaded using e.g. a `