From 2993c7725b48baed5010d8531500bc33b17b2881 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 19 Aug 2023 15:52:00 +0200 Subject: [PATCH 1/2] [Firefox] Exclude more `workerPort` related code in MOZCENTRAL builds Given that this code is (and has always been) unused in the Firefox PDF Viewer, we don't need to include it in that build-target. --- src/display/api.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/display/api.js b/src/display/api.js index 08b34aa46..e05a747f3 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2077,7 +2077,10 @@ class PDFWorker { this._webWorker = null; this._messageHandler = null; - if (port) { + if ( + (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) && + port + ) { if (PDFWorker.#workerPorts.has(port)) { throw new Error("Cannot use more than one PDFWorker per port."); } @@ -2113,6 +2116,9 @@ class PDFWorker { } _initializeFromPort(port) { + if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { + throw new Error("Not implemented: _initializeFromPort"); + } this._port = port; this._messageHandler = new MessageHandler("main", "worker", port); this._messageHandler.on("ready", function () { From 988ce2820ba2085ed8d21413abc100970dafefe1 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 19 Aug 2023 16:18:38 +0200 Subject: [PATCH 2/2] Initialize the `PDFWorker.#workerPorts` WeakMap lazily By default this WeakMap isn't needed, and it's simple enough to initialize it lazily instead. --- src/display/api.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index e05a747f3..b21e02aff 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -2061,7 +2061,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { * @param {PDFWorkerParameters} params - The worker initialization parameters. */ class PDFWorker { - static #workerPorts = new WeakMap(); + static #workerPorts; constructor({ name = null, @@ -2081,10 +2081,10 @@ class PDFWorker { (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) && port ) { - if (PDFWorker.#workerPorts.has(port)) { + if (PDFWorker.#workerPorts?.has(port)) { throw new Error("Cannot use more than one PDFWorker per port."); } - PDFWorker.#workerPorts.set(port, this); + (PDFWorker.#workerPorts ||= new WeakMap()).set(port, this); this._initializeFromPort(port); return; } @@ -2290,7 +2290,7 @@ class PDFWorker { this._webWorker.terminate(); this._webWorker = null; } - PDFWorker.#workerPorts.delete(this._port); + PDFWorker.#workerPorts?.delete(this._port); this._port = null; if (this._messageHandler) { this._messageHandler.destroy(); @@ -2308,7 +2308,7 @@ class PDFWorker { if (!params?.port) { throw new Error("PDFWorker.fromPort - invalid method signature."); } - const cachedPort = this.#workerPorts.get(params.port); + const cachedPort = this.#workerPorts?.get(params.port); if (cachedPort) { if (cachedPort._pendingDestroy) { throw new Error(