Merge pull request #13882 from Snuffleupagus/PDFWorker-rm-closure
[api-minor] Remove the closure from the `PDFWorker` class, in the `src/display/api.js` file
This commit is contained in:
commit
036b81496e
@ -1938,129 +1938,59 @@ class LoopbackPort {
|
|||||||
* the constants from {@link VerbosityLevel} should be used.
|
* the constants from {@link VerbosityLevel} should be used.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/** @type {any} */
|
const PDFWorkerUtil = {
|
||||||
const PDFWorker = (function PDFWorkerClosure() {
|
isWorkerDisabled: false,
|
||||||
const pdfWorkerPorts = new WeakMap();
|
fallbackWorkerSrc: null,
|
||||||
let isWorkerDisabled = false;
|
fakeWorkerId: 0,
|
||||||
let fallbackWorkerSrc;
|
};
|
||||||
let nextFakeWorkerId = 0;
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
|
||||||
let fakeWorkerCapability;
|
|
||||||
|
|
||||||
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
|
|
||||||
// eslint-disable-next-line no-undef
|
// eslint-disable-next-line no-undef
|
||||||
if (isNodeJS && typeof __non_webpack_require__ === "function") {
|
if (isNodeJS && typeof __non_webpack_require__ === "function") {
|
||||||
// Workers aren't supported in Node.js, force-disabling them there.
|
// Workers aren't supported in Node.js, force-disabling them there.
|
||||||
isWorkerDisabled = true;
|
PDFWorkerUtil.isWorkerDisabled = true;
|
||||||
|
|
||||||
fallbackWorkerSrc = PDFJSDev.test("LIB")
|
PDFWorkerUtil.fallbackWorkerSrc = PDFJSDev.test("LIB")
|
||||||
? "../pdf.worker.js"
|
? "../pdf.worker.js"
|
||||||
: "./pdf.worker.js";
|
: "./pdf.worker.js";
|
||||||
} else if (typeof document === "object") {
|
} else if (typeof document === "object") {
|
||||||
const pdfjsFilePath = document?.currentScript?.src;
|
const pdfjsFilePath = document?.currentScript?.src;
|
||||||
if (pdfjsFilePath) {
|
if (pdfjsFilePath) {
|
||||||
fallbackWorkerSrc = pdfjsFilePath.replace(
|
PDFWorkerUtil.fallbackWorkerSrc = pdfjsFilePath.replace(
|
||||||
/(\.(?:min\.)?js)(\?.*)?$/i,
|
/(\.(?:min\.)?js)(\?.*)?$/i,
|
||||||
".worker$1$2"
|
".worker$1$2"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function getWorkerSrc() {
|
PDFWorkerUtil.createCDNWrapper = function (url) {
|
||||||
if (GlobalWorkerOptions.workerSrc) {
|
|
||||||
return GlobalWorkerOptions.workerSrc;
|
|
||||||
}
|
|
||||||
if (typeof fallbackWorkerSrc !== "undefined") {
|
|
||||||
if (!isNodeJS) {
|
|
||||||
deprecated('No "GlobalWorkerOptions.workerSrc" specified.');
|
|
||||||
}
|
|
||||||
return fallbackWorkerSrc;
|
|
||||||
}
|
|
||||||
throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');
|
|
||||||
}
|
|
||||||
|
|
||||||
function getMainThreadWorkerMessageHandler() {
|
|
||||||
try {
|
|
||||||
return globalThis.pdfjsWorker?.WorkerMessageHandler || null;
|
|
||||||
} catch (ex) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loads worker code into main-thread.
|
|
||||||
function setupFakeWorkerGlobal() {
|
|
||||||
if (fakeWorkerCapability) {
|
|
||||||
return fakeWorkerCapability.promise;
|
|
||||||
}
|
|
||||||
fakeWorkerCapability = createPromiseCapability();
|
|
||||||
|
|
||||||
const loader = async function () {
|
|
||||||
const mainWorkerMessageHandler = getMainThreadWorkerMessageHandler();
|
|
||||||
|
|
||||||
if (mainWorkerMessageHandler) {
|
|
||||||
// The worker was already loaded using e.g. a `<script>` tag.
|
|
||||||
return mainWorkerMessageHandler;
|
|
||||||
}
|
|
||||||
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
|
|
||||||
const worker = await import("pdfjs/core/worker.js");
|
|
||||||
return worker.WorkerMessageHandler;
|
|
||||||
}
|
|
||||||
if (
|
|
||||||
PDFJSDev.test("GENERIC") &&
|
|
||||||
isNodeJS &&
|
|
||||||
// eslint-disable-next-line no-undef
|
|
||||||
typeof __non_webpack_require__ === "function"
|
|
||||||
) {
|
|
||||||
// Since bundlers, such as Webpack, cannot be told to leave `require`
|
|
||||||
// statements alone we are thus forced to jump through hoops in order
|
|
||||||
// to prevent `Critical dependency: ...` warnings in third-party
|
|
||||||
// deployments of the built `pdf.js`/`pdf.worker.js` files; see
|
|
||||||
// https://github.com/webpack/webpack/issues/8826
|
|
||||||
//
|
|
||||||
// The following hack is based on the assumption that code running in
|
|
||||||
// Node.js won't ever be affected by e.g. Content Security Policies that
|
|
||||||
// prevent the use of `eval`. If that ever occurs, we should revert this
|
|
||||||
// to a normal `__non_webpack_require__` statement and simply document
|
|
||||||
// the Webpack warnings instead (telling users to ignore them).
|
|
||||||
//
|
|
||||||
// eslint-disable-next-line no-eval
|
|
||||||
const worker = eval("require")(getWorkerSrc());
|
|
||||||
return worker.WorkerMessageHandler;
|
|
||||||
}
|
|
||||||
await loadScript(getWorkerSrc());
|
|
||||||
return window.pdfjsWorker.WorkerMessageHandler;
|
|
||||||
};
|
|
||||||
loader().then(fakeWorkerCapability.resolve, fakeWorkerCapability.reject);
|
|
||||||
|
|
||||||
return fakeWorkerCapability.promise;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createCDNWrapper(url) {
|
|
||||||
// We will rely on blob URL's property to specify origin.
|
// We will rely on blob URL's property to specify origin.
|
||||||
// We want this function to fail in case if createObjectURL or Blob do not
|
// We want this function to fail in case if createObjectURL or Blob do not
|
||||||
// exist or fail for some reason -- our Worker creation will fail anyway.
|
// exist or fail for some reason -- our Worker creation will fail anyway.
|
||||||
const wrapper = "importScripts('" + url + "');";
|
const wrapper = `importScripts("${url}");`;
|
||||||
return URL.createObjectURL(new Blob([wrapper]));
|
return URL.createObjectURL(new Blob([wrapper]));
|
||||||
}
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PDF.js web worker abstraction that controls the instantiation of PDF
|
* PDF.js web worker abstraction that controls the instantiation of PDF
|
||||||
* documents. Message handlers are used to pass information from the main
|
* documents. Message handlers are used to pass information from the main
|
||||||
* thread to the worker thread and vice versa. If the creation of a web
|
* thread to the worker thread and vice versa. If the creation of a web
|
||||||
* worker is not possible, a "fake" worker will be used instead.
|
* worker is not possible, a "fake" worker will be used instead.
|
||||||
|
*
|
||||||
|
* @param {PDFWorkerParameters} params - The worker initialization parameters.
|
||||||
*/
|
*/
|
||||||
// eslint-disable-next-line no-shadow
|
class PDFWorker {
|
||||||
class PDFWorker {
|
static get _workerPorts() {
|
||||||
/**
|
return shadow(this, "_workerPorts", new WeakMap());
|
||||||
* @param {PDFWorkerParameters} params - Worker initialization parameters.
|
}
|
||||||
*/
|
|
||||||
constructor({
|
constructor({
|
||||||
name = null,
|
name = null,
|
||||||
port = null,
|
port = null,
|
||||||
verbosity = getVerbosityLevel(),
|
verbosity = getVerbosityLevel(),
|
||||||
} = {}) {
|
} = {}) {
|
||||||
if (port && pdfWorkerPorts.has(port)) {
|
if (port && PDFWorker._workerPorts.has(port)) {
|
||||||
throw new Error("Cannot use more than one PDFWorker per port");
|
throw new Error("Cannot use more than one PDFWorker per port.");
|
||||||
}
|
}
|
||||||
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
@ -2074,21 +2004,33 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
this._messageHandler = null;
|
this._messageHandler = null;
|
||||||
|
|
||||||
if (port) {
|
if (port) {
|
||||||
pdfWorkerPorts.set(port, this);
|
PDFWorker._workerPorts.set(port, this);
|
||||||
this._initializeFromPort(port);
|
this._initializeFromPort(port);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._initialize();
|
this._initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Promise for worker initialization completion.
|
||||||
|
* @type {Promise<void>}
|
||||||
|
*/
|
||||||
get promise() {
|
get promise() {
|
||||||
return this._readyCapability.promise;
|
return this._readyCapability.promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current `workerPort`, when it exists.
|
||||||
|
* @type {Worker}
|
||||||
|
*/
|
||||||
get port() {
|
get port() {
|
||||||
return this._port;
|
return this._port;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current MessageHandler-instance.
|
||||||
|
* @type {MessageHandler}
|
||||||
|
*/
|
||||||
get messageHandler() {
|
get messageHandler() {
|
||||||
return this._messageHandler;
|
return this._messageHandler;
|
||||||
}
|
}
|
||||||
@ -2097,8 +2039,8 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
this._port = port;
|
this._port = port;
|
||||||
this._messageHandler = new MessageHandler("main", "worker", port);
|
this._messageHandler = new MessageHandler("main", "worker", port);
|
||||||
this._messageHandler.on("ready", function () {
|
this._messageHandler.on("ready", function () {
|
||||||
// Ignoring 'ready' event -- MessageHandler shall be already initialized
|
// Ignoring "ready" event -- MessageHandler should already be initialized
|
||||||
// and ready to accept the messages.
|
// and ready to accept messages.
|
||||||
});
|
});
|
||||||
this._readyCapability.resolve();
|
this._readyCapability.resolve();
|
||||||
}
|
}
|
||||||
@ -2111,10 +2053,10 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
// Uint8Array as it arrives on the worker. (Chrome added this with v.15.)
|
// Uint8Array as it arrives on the worker. (Chrome added this with v.15.)
|
||||||
if (
|
if (
|
||||||
typeof Worker !== "undefined" &&
|
typeof Worker !== "undefined" &&
|
||||||
!isWorkerDisabled &&
|
!PDFWorkerUtil.isWorkerDisabled &&
|
||||||
!getMainThreadWorkerMessageHandler()
|
!PDFWorker._mainThreadWorkerMessageHandler
|
||||||
) {
|
) {
|
||||||
let workerSrc = getWorkerSrc();
|
let workerSrc = PDFWorker.workerSrc;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Wraps workerSrc path into blob URL, if the former does not belong
|
// Wraps workerSrc path into blob URL, if the former does not belong
|
||||||
@ -2124,7 +2066,7 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
PDFJSDev.test("GENERIC") &&
|
PDFJSDev.test("GENERIC") &&
|
||||||
!isSameOrigin(window.location.href, workerSrc)
|
!isSameOrigin(window.location.href, workerSrc)
|
||||||
) {
|
) {
|
||||||
workerSrc = createCDNWrapper(
|
workerSrc = PDFWorkerUtil.createCDNWrapper(
|
||||||
new URL(workerSrc, window.location).href
|
new URL(workerSrc, window.location).href
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -2196,9 +2138,7 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const sendTest = () => {
|
const sendTest = () => {
|
||||||
const testObj = new Uint8Array([
|
const testObj = new Uint8Array([this.postMessageTransfers ? 255 : 0]);
|
||||||
this.postMessageTransfers ? 255 : 0,
|
|
||||||
]);
|
|
||||||
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the
|
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the
|
||||||
// typed array. Also, checking if we can use transfers.
|
// typed array. Also, checking if we can use transfers.
|
||||||
try {
|
try {
|
||||||
@ -2210,10 +2150,9 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// It might take time for worker to initialize (especially when AMD
|
// It might take time for the worker to initialize. We will try to send
|
||||||
// loader is used). We will try to send test immediately, and then
|
// the "test" message immediately, and once the "ready" message arrives.
|
||||||
// when 'ready' message will arrive. The worker shall process only
|
// The worker shall process only the first received "test" message.
|
||||||
// first received 'test'.
|
|
||||||
sendTest();
|
sendTest();
|
||||||
return;
|
return;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -2226,12 +2165,12 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_setupFakeWorker() {
|
_setupFakeWorker() {
|
||||||
if (!isWorkerDisabled) {
|
if (!PDFWorkerUtil.isWorkerDisabled) {
|
||||||
warn("Setting up fake worker.");
|
warn("Setting up fake worker.");
|
||||||
isWorkerDisabled = true;
|
PDFWorkerUtil.isWorkerDisabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
setupFakeWorkerGlobal()
|
PDFWorker._setupFakeWorkerGlobal
|
||||||
.then(WorkerMessageHandler => {
|
.then(WorkerMessageHandler => {
|
||||||
if (this.destroyed) {
|
if (this.destroyed) {
|
||||||
this._readyCapability.reject(new Error("Worker was destroyed"));
|
this._readyCapability.reject(new Error("Worker was destroyed"));
|
||||||
@ -2241,7 +2180,7 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
this._port = port;
|
this._port = port;
|
||||||
|
|
||||||
// All fake workers use the same port, making id unique.
|
// All fake workers use the same port, making id unique.
|
||||||
const id = "fake" + nextFakeWorkerId++;
|
const id = `fake${PDFWorkerUtil.fakeWorkerId++}`;
|
||||||
|
|
||||||
// If the main thread is our worker, setup the handling for the
|
// If the main thread is our worker, setup the handling for the
|
||||||
// messages -- the main thread sends to it self.
|
// messages -- the main thread sends to it self.
|
||||||
@ -2273,7 +2212,7 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
this._webWorker.terminate();
|
this._webWorker.terminate();
|
||||||
this._webWorker = null;
|
this._webWorker = null;
|
||||||
}
|
}
|
||||||
pdfWorkerPorts.delete(this._port);
|
PDFWorker._workerPorts.delete(this._port);
|
||||||
this._port = null;
|
this._port = null;
|
||||||
if (this._messageHandler) {
|
if (this._messageHandler) {
|
||||||
this._messageHandler.destroy();
|
this._messageHandler.destroy();
|
||||||
@ -2282,25 +2221,93 @@ const PDFWorker = (function PDFWorkerClosure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {PDFWorkerParameters} params - The worker initialization
|
* @param {PDFWorkerParameters} params - The worker initialization parameters.
|
||||||
* parameters.
|
|
||||||
*/
|
*/
|
||||||
static fromPort(params) {
|
static fromPort(params) {
|
||||||
if (!params || !params.port) {
|
if (!params?.port) {
|
||||||
throw new Error("PDFWorker.fromPort - invalid method signature.");
|
throw new Error("PDFWorker.fromPort - invalid method signature.");
|
||||||
}
|
}
|
||||||
if (pdfWorkerPorts.has(params.port)) {
|
if (this._workerPorts.has(params.port)) {
|
||||||
return pdfWorkerPorts.get(params.port);
|
return this._workerPorts.get(params.port);
|
||||||
}
|
}
|
||||||
return new PDFWorker(params);
|
return new PDFWorker(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
static getWorkerSrc() {
|
/**
|
||||||
return getWorkerSrc();
|
* The current `workerSrc`, when it exists.
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
|
static get workerSrc() {
|
||||||
|
if (GlobalWorkerOptions.workerSrc) {
|
||||||
|
return GlobalWorkerOptions.workerSrc;
|
||||||
|
}
|
||||||
|
if (PDFWorkerUtil.fallbackWorkerSrc !== null) {
|
||||||
|
if (!isNodeJS) {
|
||||||
|
deprecated('No "GlobalWorkerOptions.workerSrc" specified.');
|
||||||
|
}
|
||||||
|
return PDFWorkerUtil.fallbackWorkerSrc;
|
||||||
|
}
|
||||||
|
throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');
|
||||||
|
}
|
||||||
|
|
||||||
|
static get _mainThreadWorkerMessageHandler() {
|
||||||
|
try {
|
||||||
|
return globalThis.pdfjsWorker?.WorkerMessageHandler || null;
|
||||||
|
} catch (ex) {
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return PDFWorker;
|
|
||||||
})();
|
// Loads worker code into the main-thread.
|
||||||
|
static get _setupFakeWorkerGlobal() {
|
||||||
|
const loader = async () => {
|
||||||
|
const mainWorkerMessageHandler = this._mainThreadWorkerMessageHandler;
|
||||||
|
|
||||||
|
if (mainWorkerMessageHandler) {
|
||||||
|
// The worker was already loaded using e.g. a `<script>` tag.
|
||||||
|
return mainWorkerMessageHandler;
|
||||||
|
}
|
||||||
|
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
|
||||||
|
const worker = await import("pdfjs/core/worker.js");
|
||||||
|
return worker.WorkerMessageHandler;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
PDFJSDev.test("GENERIC") &&
|
||||||
|
isNodeJS &&
|
||||||
|
// eslint-disable-next-line no-undef
|
||||||
|
typeof __non_webpack_require__ === "function"
|
||||||
|
) {
|
||||||
|
// Since bundlers, such as Webpack, cannot be told to leave `require`
|
||||||
|
// statements alone we are thus forced to jump through hoops in order
|
||||||
|
// to prevent `Critical dependency: ...` warnings in third-party
|
||||||
|
// deployments of the built `pdf.js`/`pdf.worker.js` files; see
|
||||||
|
// https://github.com/webpack/webpack/issues/8826
|
||||||
|
//
|
||||||
|
// The following hack is based on the assumption that code running in
|
||||||
|
// Node.js won't ever be affected by e.g. Content Security Policies that
|
||||||
|
// prevent the use of `eval`. If that ever occurs, we should revert this
|
||||||
|
// to a normal `__non_webpack_require__` statement and simply document
|
||||||
|
// the Webpack warnings instead (telling users to ignore them).
|
||||||
|
//
|
||||||
|
// eslint-disable-next-line no-eval
|
||||||
|
const worker = eval("require")(this.workerSrc);
|
||||||
|
return worker.WorkerMessageHandler;
|
||||||
|
}
|
||||||
|
await loadScript(this.workerSrc);
|
||||||
|
return window.pdfjsWorker.WorkerMessageHandler;
|
||||||
|
};
|
||||||
|
|
||||||
|
return shadow(this, "_setupFakeWorkerGlobal", loader());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
|
||||||
|
PDFWorker.getWorkerSrc = function () {
|
||||||
|
deprecated(
|
||||||
|
"`PDFWorker.getWorkerSrc()`, please use `PDFWorker.workerSrc` instead."
|
||||||
|
);
|
||||||
|
return this.workerSrc;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* For internal use only.
|
* For internal use only.
|
||||||
|
@ -501,7 +501,7 @@ describe("api", function () {
|
|||||||
pending("Worker is not supported in Node.js.");
|
pending("Worker is not supported in Node.js.");
|
||||||
}
|
}
|
||||||
|
|
||||||
const workerSrc = PDFWorker.getWorkerSrc();
|
const workerSrc = PDFWorker.workerSrc;
|
||||||
expect(typeof workerSrc).toEqual("string");
|
expect(typeof workerSrc).toEqual("string");
|
||||||
expect(workerSrc).toEqual(GlobalWorkerOptions.workerSrc);
|
expect(workerSrc).toEqual(GlobalWorkerOptions.workerSrc);
|
||||||
});
|
});
|
||||||
|
@ -2139,7 +2139,7 @@ async function loadFakeWorker() {
|
|||||||
window.pdfjsWorker = await import("pdfjs/core/worker.js");
|
window.pdfjsWorker = await import("pdfjs/core/worker.js");
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
return loadScript(PDFWorker.getWorkerSrc());
|
return loadScript(PDFWorker.workerSrc);
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadAndEnablePDFBug(enabledTabs) {
|
function loadAndEnablePDFBug(enabledTabs) {
|
||||||
|
Loading…
Reference in New Issue
Block a user