From a5485e1ef7033e3e50b33f95135316ad42a69ed1 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 19 Dec 2019 17:39:55 +0100 Subject: [PATCH] [api-minor] Support loading the fake worker from `GlobalWorkerOptions.workerSrc` in Node.js There's no particularily good reason, as far as I can tell, to not support a custom worker path in Node.js environments (even if workers aren't supported). This patch thus make the Node.js fake worker loader code-path consistent with the fallback code-path used with *browser* fake worker loader. Finally, this patch also deprecates[1] the `fallbackWorkerSrc` functionality, except in Node.js, since the user should *always* provide correct worker options since the fallback is nothing more than a best-effort solution. --- [1] Although it probably shouldn't be removed until the next major version. --- src/display/api.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index e47ecb064..545060958 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -27,7 +27,7 @@ import { unreachable, warn } from '../shared/util'; import { - DOMCanvasFactory, DOMCMapReaderFactory, loadScript, PageViewport, + deprecated, DOMCanvasFactory, DOMCMapReaderFactory, loadScript, PageViewport, releaseImageResources, RenderingCancelledException, StatTimer } from './display_utils'; import { FontFaceObject, FontLoader } from './font_loader'; @@ -43,7 +43,6 @@ import { WebGLContext } from './webgl'; const DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536 const RENDERING_CANCELLED_TIMEOUT = 100; // ms - /** * @typedef {function} IPDFStreamFactory * @param {DocumentInitParameters} params The document initialization @@ -1477,15 +1476,15 @@ const PDFWorker = (function PDFWorkerClosure() { // Workers aren't supported in Node.js, force-disabling them there. isWorkerDisabled = true; + if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) { + fallbackWorkerSrc = '../pdf.worker.js'; + } else { + fallbackWorkerSrc = './pdf.worker.js'; + } fakeWorkerFilesLoader = function() { return new Promise(function(resolve, reject) { try { - let worker; - if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) { - worker = __non_webpack_require__('../pdf.worker.js'); - } else { - worker = __non_webpack_require__('./pdf.worker.js'); - } + const worker = __non_webpack_require__(getWorkerSrc()); resolve(worker.WorkerMessageHandler); } catch (ex) { reject(ex); @@ -1507,6 +1506,9 @@ const PDFWorker = (function PDFWorkerClosure() { return GlobalWorkerOptions.workerSrc; } if (typeof fallbackWorkerSrc !== 'undefined') { + if (!isNodeJS) { + deprecated('No "GlobalWorkerOptions.workerSrc" specified.'); + } return fallbackWorkerSrc; } throw new Error('No "GlobalWorkerOptions.workerSrc" specified.');