From 21c8dd4842b7cc4e36a9c9c7c39c8eb77e51f1c8 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 7 Oct 2018 14:28:16 +0200 Subject: [PATCH] Combine the `pdfjsFilePath` and fallback `workerSrc` handling in `src/display/api.js` With the way that the `getWorkerSrc()` helper function is implemented now, there's no longer a particularly strong reason for keeping the global `pdfjsFilePath` variable around. With this patch the fallback `workerSrc` will thus, assuming is wasn't already set, be set to the "pdfjsFilePath" which simplifies the `getWorkerSrc()` function and reduces the amount of global state. Finally, the global `workerSrc` variable was renamed to prevent shadowing. --- src/display/api.js | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 79b1dd0c5..1ecbdcb08 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -34,16 +34,10 @@ import { Metadata } from './metadata'; import { PDFDataTransportStream } from './transport_stream'; import { WebGLContext } from './webgl'; -var DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536 +const DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536 let isWorkerDisabled = false; -let workerSrc; - -const pdfjsFilePath = - typeof PDFJSDev !== 'undefined' && - PDFJSDev.test('PRODUCTION && !(MOZCENTRAL || FIREFOX)') && - typeof document !== 'undefined' && document.currentScript ? - document.currentScript.src : null; +let fallbackWorkerSrc; let fakeWorkerFilesLoader = null; if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) { @@ -62,7 +56,7 @@ if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) { useRequireEnsure = true; } if (typeof requirejs !== 'undefined' && requirejs.toUrl) { - workerSrc = requirejs.toUrl('pdfjs-dist/build/pdf.worker.js'); + fallbackWorkerSrc = requirejs.toUrl('pdfjs-dist/build/pdf.worker.js'); } const dynamicLoaderSupported = typeof requirejs !== 'undefined' && requirejs.load; @@ -93,6 +87,14 @@ if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) { }, reject); }); }) : null; + + if (!fallbackWorkerSrc && typeof document !== 'undefined') { + const pdfjsFilePath = document.currentScript && document.currentScript.src; + if (pdfjsFilePath) { + fallbackWorkerSrc = + pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2'); + } + } } /** @@ -1346,13 +1348,8 @@ var PDFWorker = (function PDFWorkerClosure() { if (GlobalWorkerOptions.workerSrc) { return GlobalWorkerOptions.workerSrc; } - if (typeof workerSrc !== 'undefined') { - return workerSrc; - } - if (typeof PDFJSDev !== 'undefined' && - PDFJSDev.test('PRODUCTION && !(MOZCENTRAL || FIREFOX)') && - pdfjsFilePath) { - return pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2'); + if (typeof fallbackWorkerSrc !== 'undefined') { + return fallbackWorkerSrc; } throw new Error('No "GlobalWorkerOptions.workerSrc" specified.'); } @@ -1480,7 +1477,7 @@ var PDFWorker = (function PDFWorkerClosure() { // Uint8Array as it arrives on the worker. (Chrome added this with v.15.) if (typeof Worker !== 'undefined' && !isWorkerDisabled && !getMainThreadWorkerMessageHandler()) { - var workerSrc = getWorkerSrc(); + let workerSrc = getWorkerSrc(); try { // Wraps workerSrc path into blob URL, if the former does not belong