[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.
This commit is contained in:
Jonas Jenwald 2023-09-30 13:25:47 +02:00
parent 8158628a90
commit 0a970ee443

View File

@ -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 `<script>` tag.