From f0af52032b69f75705804629748e9c6c299fecaf Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 12 Jan 2024 11:09:23 +0100 Subject: [PATCH] [api-minor] Validate the `workerPort`/`workerSrc` options Ensure that users cannot provide incorrect values when trying to set the global worker-options. This patch was prompted by occasionally seeing users manually loading the `pdf.worker.mjs`-file and then assigning it to the `workerSrc`-option, something that obviously doesn't make sense and will cause fake-workers to be used (with poor performance as a result). --- src/display/worker_options.js | 59 ++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/src/display/worker_options.js b/src/display/worker_options.js index 1e82a4f7e6191..e4bbb81a6ea45 100644 --- a/src/display/worker_options.js +++ b/src/display/worker_options.js @@ -13,21 +13,52 @@ * limitations under the License. */ -/** - * @typedef {Object} GlobalWorkerOptionsType - * @property {Worker | null} workerPort - Defines global port for worker - * process. Overrides the `workerSrc` option. - * @property {string} workerSrc - A string containing the path and filename - * of the worker file. - * - * NOTE: The `workerSrc` option should always be set, in order to prevent any - * issues when using the PDF.js library. - */ +class GlobalWorkerOptions { + static #port = null; + + static #src = ""; + + /** + * @type {Worker | null} + */ + static get workerPort() { + return this.#port; + } + + /** + * @param {Worker | null} workerPort - Defines global port for worker process. + * Overrides the `workerSrc` option. + */ + static set workerPort(val) { + if ( + !(typeof Worker !== "undefined" && val instanceof Worker) && + val !== null + ) { + throw new Error("Invalid `workerPort` type."); + } + this.#port = val; + } -/** @type {GlobalWorkerOptionsType} */ -const GlobalWorkerOptions = Object.create(null); + /** + * @type {string} + */ + static get workerSrc() { + return this.#src; + } -GlobalWorkerOptions.workerPort = null; -GlobalWorkerOptions.workerSrc = ""; + /** + * @param {string} workerSrc - A string containing the path and filename of + * the worker file. + * + * NOTE: The `workerSrc` option should always be set, in order to prevent + * any issues when using the PDF.js library. + */ + static set workerSrc(val) { + if (typeof val !== "string") { + throw new Error("Invalid `workerSrc` type."); + } + this.#src = val; + } +} export { GlobalWorkerOptions };