diff --git a/examples/browserify/main.js b/examples/browserify/main.js index 6c4e144b1af4d..8554ffc9b3980 100644 --- a/examples/browserify/main.js +++ b/examples/browserify/main.js @@ -5,7 +5,7 @@ var pdfjsLib = require('pdfjs-dist'); -var pdfPath = '../helloworld/helloworld.pdf'; +var pdfPath = '../learning/helloworld.pdf'; // Setting worker path to worker bundle. pdfjsLib.GlobalWorkerOptions.workerSrc = diff --git a/examples/webpack/main.js b/examples/webpack/main.js index f8bfd179bbe1e..bc21b5f32e4b5 100644 --- a/examples/webpack/main.js +++ b/examples/webpack/main.js @@ -5,7 +5,7 @@ var pdfjsLib = require('pdfjs-dist'); -var pdfPath = '../helloworld/helloworld.pdf'; +var pdfPath = '../learning/helloworld.pdf'; // Setting worker path to worker bundle. pdfjsLib.GlobalWorkerOptions.workerSrc = diff --git a/gulpfile.js b/gulpfile.js index 6558a66183600..bbe9fa7d695cc 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1320,7 +1320,6 @@ gulp.task('dist-pre', gulp.series('generic', 'components', 'image_decoders', bugs: DIST_BUGS_URL, license: DIST_LICENSE, dependencies: { - 'node-ensure': '^0.0.0', // shim for node for require.ensure 'worker-loader': '^2.0.0', // used in external/dist/webpack.json }, peerDependencies: { @@ -1330,7 +1329,6 @@ gulp.task('dist-pre', gulp.series('generic', 'components', 'image_decoders', 'fs': false, 'http': false, 'https': false, - 'node-ensure': false, 'url': false, 'zlib': false, }, diff --git a/package-lock.json b/package-lock.json index 9442257c2096d..78c1bb848bc5d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6682,12 +6682,6 @@ "integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ==", "dev": true }, - "node-ensure": { - "version": "0.0.0", - "resolved": "https://registry.npmjs.org/node-ensure/-/node-ensure-0.0.0.tgz", - "integrity": "sha1-7K52QVDemYYexcgQ/V0Jaxg5Mqc=", - "dev": true - }, "node-libs-browser": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/node-libs-browser/-/node-libs-browser-2.1.0.tgz", diff --git a/package.json b/package.json index 87bce96c0d550..46fdf0801ec62 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "jstransformer-markdown-it": "^2.1.0", "merge-stream": "^1.0.1", "mkdirp": "^0.5.1", - "node-ensure": "^0.0.0", "prettier": "^1.19.1", "rimraf": "^2.7.1", "streamqueue": "^1.1.2", diff --git a/src/display/api.js b/src/display/api.js index b832928727388..262f16f3c6507 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -12,7 +12,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -/* globals requirejs, __non_webpack_require__ */ /* eslint no-var: error */ /** @@ -27,13 +26,14 @@ 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'; import { apiCompatibilityParams } from './api_compatibility'; import { CanvasGraphics } from './canvas'; import { GlobalWorkerOptions } from './worker_options'; +import { isNodeJS } from '../shared/is_node'; import { MessageHandler } from '../shared/message_handler'; import { Metadata } from './metadata'; import { PDFDataTransportStream } from './transport_stream'; @@ -42,68 +42,6 @@ import { WebGLContext } from './webgl'; const DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536 const RENDERING_CANCELLED_TIMEOUT = 100; // ms -let isWorkerDisabled = false; -let fallbackWorkerSrc; - -let fakeWorkerFilesLoader = null; -if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) { - let useRequireEnsure = false; - // For GENERIC build we need to add support for different fake file loaders - // for different frameworks. - if (typeof window === 'undefined') { - // node.js - disable worker and set require.ensure. - isWorkerDisabled = true; - if (typeof __non_webpack_require__.ensure === 'undefined') { - __non_webpack_require__.ensure = __non_webpack_require__('node-ensure'); - } - useRequireEnsure = true; - } else if (typeof __non_webpack_require__ !== 'undefined' && - typeof __non_webpack_require__.ensure === 'function') { - useRequireEnsure = true; - } - if (typeof requirejs !== 'undefined' && requirejs.toUrl) { - fallbackWorkerSrc = requirejs.toUrl('pdfjs-dist/build/pdf.worker.js'); - } - const dynamicLoaderSupported = - typeof requirejs !== 'undefined' && requirejs.load; - fakeWorkerFilesLoader = useRequireEnsure ? (function() { - return new Promise(function(resolve, reject) { - __non_webpack_require__.ensure([], function() { - 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'); - } - resolve(worker.WorkerMessageHandler); - } catch (ex) { - reject(ex); - } - }, reject, 'pdfjsWorker'); - }); - }) : dynamicLoaderSupported ? (function() { - return new Promise(function(resolve, reject) { - requirejs(['pdfjs-dist/build/pdf.worker'], function(worker) { - try { - resolve(worker.WorkerMessageHandler); - } catch (ex) { - reject(ex); - } - }, reject); - }); - }) : null; - - if (!fallbackWorkerSrc && typeof document === 'object' && - 'currentScript' in document) { - const pdfjsFilePath = document.currentScript && document.currentScript.src; - if (pdfjsFilePath) { - fallbackWorkerSrc = - pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2'); - } - } -} - /** * @typedef {function} IPDFStreamFactory * @param {DocumentInitParameters} params The document initialization @@ -1526,14 +1464,40 @@ class LoopbackPort { const PDFWorker = (function PDFWorkerClosure() { const pdfWorkerPorts = new WeakMap(); + let isWorkerDisabled = false; + let fallbackWorkerSrc; let nextFakeWorkerId = 0; - let fakeWorkerFilesLoadedCapability; + let fakeWorkerCapability; + + if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) { + // eslint-disable-next-line no-undef + if (isNodeJS && typeof __non_webpack_require__ === 'function') { + // 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'; + } + } else if (typeof document === 'object' && 'currentScript' in document) { + const pdfjsFilePath = document.currentScript && + document.currentScript.src; + if (pdfjsFilePath) { + fallbackWorkerSrc = + pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2'); + } + } + } function getWorkerSrc() { 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.'); @@ -1550,39 +1514,50 @@ const PDFWorker = (function PDFWorkerClosure() { // Loads worker code into main thread. function setupFakeWorkerGlobal() { - if (fakeWorkerFilesLoadedCapability) { - return fakeWorkerFilesLoadedCapability.promise; - } - fakeWorkerFilesLoadedCapability = createPromiseCapability(); - - const mainWorkerMessageHandler = getMainThreadWorkerMessageHandler(); - if (mainWorkerMessageHandler) { - // The worker was already loaded using a `