Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Validate the workerPort/workerSrc options #17504

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 45 additions & 14 deletions src/display/worker_options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };