From c3c1fc511db59f6a7de1409e6e552a337faf947e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 14 Feb 2018 14:49:24 +0100 Subject: [PATCH] Move the `workerSrc` option from the global `PDFJS` object and into `GlobalWorkerOptions` instead --- examples/acroforms/acroforms.js | 3 ++- examples/browserify/main.js | 3 ++- examples/components/pageviewer.js | 3 ++- examples/components/simpleviewer.js | 3 ++- examples/components/singlepageviewer.js | 3 ++- examples/helloworld/hello.js | 8 +++++--- examples/learning/helloworld.html | 3 ++- examples/learning/helloworld64.html | 3 ++- examples/learning/prevnext.html | 3 ++- examples/mobile-viewer/viewer.js | 4 +++- examples/svgviewer/viewer.js | 9 +++++++-- examples/text-only/pdf2svg.js | 3 ++- examples/webpack/main.js | 3 ++- src/display/api.js | 16 ++++++++-------- src/display/dom_utils.js | 2 -- src/display/global.js | 10 ---------- test/driver.js | 10 ++++++---- test/unit/api_spec.js | 20 ++++++++++---------- test/unit/jasmine-boot.js | 10 +++++----- web/app.js | 10 +++++----- 20 files changed, 69 insertions(+), 60 deletions(-) diff --git a/examples/acroforms/acroforms.js b/examples/acroforms/acroforms.js index 1941b746b..44932e796 100644 --- a/examples/acroforms/acroforms.js +++ b/examples/acroforms/acroforms.js @@ -15,7 +15,8 @@ 'use strict'; -PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; +PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; var DEFAULT_URL = '../../test/pdfs/f1040.pdf'; var DEFAULT_SCALE = 1.0; diff --git a/examples/browserify/main.js b/examples/browserify/main.js index 42c640ef0..5d20aa05b 100644 --- a/examples/browserify/main.js +++ b/examples/browserify/main.js @@ -8,7 +8,8 @@ require('pdfjs-dist'); var pdfPath = '../helloworld/helloworld.pdf'; // Setting worker path to worker bundle. -PDFJS.workerSrc = '../../build/browserify/pdf.worker.bundle.js'; +PDFJS.GlobalWorkerOptions.workerSrc = + '../../build/browserify/pdf.worker.bundle.js'; // Loading a document. var loadingTask = PDFJS.getDocument(pdfPath); diff --git a/examples/components/pageviewer.js b/examples/components/pageviewer.js index 0c4899137..0d128ebc9 100644 --- a/examples/components/pageviewer.js +++ b/examples/components/pageviewer.js @@ -22,7 +22,8 @@ if (!PDFJS.PDFViewer || !PDFJS.getDocument) { // The workerSrc property shall be specified. // -PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; +PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; // Some PDFs need external cmaps. // diff --git a/examples/components/simpleviewer.js b/examples/components/simpleviewer.js index 5fe597b5b..5f9da0fcc 100644 --- a/examples/components/simpleviewer.js +++ b/examples/components/simpleviewer.js @@ -22,7 +22,8 @@ if (!PDFJS.PDFViewer || !PDFJS.getDocument) { // The workerSrc property shall be specified. // -PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; +PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; // Some PDFs need external cmaps. // diff --git a/examples/components/singlepageviewer.js b/examples/components/singlepageviewer.js index 44ec1d84f..90900741e 100644 --- a/examples/components/singlepageviewer.js +++ b/examples/components/singlepageviewer.js @@ -22,7 +22,8 @@ if (!PDFJS.PDFSinglePageViewer || !PDFJS.getDocument) { // The workerSrc property shall be specified. // -PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; +PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; // Some PDFs need external cmaps. // diff --git a/examples/helloworld/hello.js b/examples/helloworld/hello.js index 49a0e4247..08ce2ff29 100644 --- a/examples/helloworld/hello.js +++ b/examples/helloworld/hello.js @@ -2,17 +2,19 @@ // In production, the bundled pdf.js shall be used instead of SystemJS. Promise.all([System.import('pdfjs/display/api'), - System.import('pdfjs/display/global'), + System.import('pdfjs/display/worker_options'), System.import('pdfjs/display/network'), System.resolve('pdfjs/worker_loader')]) .then(function (modules) { - var api = modules[0], global = modules[1], network = modules[2]; + var api = modules[0]; + var GlobalWorkerOptions = modules[1].GlobalWorkerOptions; + var network = modules[2]; api.setPDFNetworkStreamFactory((params) => { return new network.PDFNetworkStream(params); }); // In production, change this to point to the built `pdf.worker.js` file. - global.PDFJS.workerSrc = modules[3]; + GlobalWorkerOptions.workerSrc = modules[3]; // Fetch the PDF document from the URL using promises. api.getDocument('helloworld.pdf').then(function (pdf) { diff --git a/examples/learning/helloworld.html b/examples/learning/helloworld.html index c2988c287..5657b73e0 100644 --- a/examples/learning/helloworld.html +++ b/examples/learning/helloworld.html @@ -22,7 +22,8 @@ // // The workerSrc property shall be specified. // - PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; + PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; // // Asynchronous download PDF diff --git a/examples/learning/helloworld64.html b/examples/learning/helloworld64.html index 69420495a..263ba905a 100644 --- a/examples/learning/helloworld64.html +++ b/examples/learning/helloworld64.html @@ -34,7 +34,8 @@ // // The workerSrc property shall be specified. // - PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; + PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; // Opening PDF by passing its binary data as a string. It is still preferable // to use Uint8Array, but string or array-like structure will work too. diff --git a/examples/learning/prevnext.html b/examples/learning/prevnext.html index f7e68f2b5..a315a2118 100644 --- a/examples/learning/prevnext.html +++ b/examples/learning/prevnext.html @@ -33,7 +33,8 @@ // pdf.js's one, or the pdf.js is executed via eval(), the workerSrc property // shall be specified. // - // PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; + // PDFJS.GlobalWorkerOptions.workerSrc = + // '../../node_modules/pdfjs-dist/build/pdf.worker.js'; var pdfDoc = null, pageNum = 1, diff --git a/examples/mobile-viewer/viewer.js b/examples/mobile-viewer/viewer.js index dc3276ce4..2501dbdfa 100644 --- a/examples/mobile-viewer/viewer.js +++ b/examples/mobile-viewer/viewer.js @@ -24,10 +24,12 @@ if (typeof PDFJS === 'undefined' || !PDFJS.PDFViewer || !PDFJS.getDocument) { var USE_ONLY_CSS_ZOOM = true; var TEXT_LAYER_MODE = 0; // DISABLE PDFJS.maxImageSize = 1024 * 1024; -PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/'; PDFJS.cMapPacked = true; +PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; + var DEFAULT_URL = '../../web/compressed.tracemonkey-pldi-09.pdf'; var DEFAULT_SCALE_DELTA = 1.1; var MIN_SCALE = 0.25; diff --git a/examples/svgviewer/viewer.js b/examples/svgviewer/viewer.js index a6370da51..04f92f8a2 100644 --- a/examples/svgviewer/viewer.js +++ b/examples/svgviewer/viewer.js @@ -39,16 +39,21 @@ function renderDocument(pdf, svgLib) { Promise.all([System.import('pdfjs/display/api'), System.import('pdfjs/display/svg'), System.import('pdfjs/display/global'), + System.import('pdfjs/display/worker_options'), System.import('pdfjs/display/network'), System.resolve('pdfjs/worker_loader')]) .then(function (modules) { - var api = modules[0], svg = modules[1], global = modules[2], network = modules[3]; + var api = modules[0]; + var svg = modules[1]; + var global = modules[2]; + var GlobalWorkerOptions = modules[3].GlobalWorkerOptions; + var network = modules[4]; api.setPDFNetworkStreamFactory((params) => { return new network.PDFNetworkStream(params); }); // In production, change this to point to the built `pdf.worker.js` file. - global.PDFJS.workerSrc = modules[4]; + GlobalWorkerOptions.workerSrc = modules[5]; // In production, change this to point to where the cMaps are placed. global.PDFJS.cMapUrl = '../../external/bcmaps/'; diff --git a/examples/text-only/pdf2svg.js b/examples/text-only/pdf2svg.js index ca7cf8183..eeb53bfb6 100644 --- a/examples/text-only/pdf2svg.js +++ b/examples/text-only/pdf2svg.js @@ -18,7 +18,8 @@ var PAGE_NUMBER = 1; var PAGE_SCALE = 1.5; var SVG_NS = 'http://www.w3.org/2000/svg'; -PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js'; +PDFJS.GlobalWorkerOptions.workerSrc = + '../../node_modules/pdfjs-dist/build/pdf.worker.js'; function buildSVG(viewport, textContent) { // Building SVG with size of the viewport (for simplicity) diff --git a/examples/webpack/main.js b/examples/webpack/main.js index d1beb101c..30c1e4d5e 100644 --- a/examples/webpack/main.js +++ b/examples/webpack/main.js @@ -8,7 +8,8 @@ var pdfjsLib = require('pdfjs-dist'); var pdfPath = '../helloworld/helloworld.pdf'; // Setting worker path to worker bundle. -pdfjsLib.PDFJS.workerSrc = '../../build/webpack/pdf.worker.bundle.js'; +pdfjsLib.GlobalWorkerOptions.workerSrc = + '../../build/webpack/pdf.worker.bundle.js'; // Loading a document. var loadingTask = pdfjsLib.getDocument(pdfPath); diff --git a/src/display/api.js b/src/display/api.js index c91b21384..ef72b6694 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -34,11 +34,11 @@ import { WebGLContext } from './webgl'; var DEFAULT_RANGE_CHUNK_SIZE = 65536; // 2^16 = 65536 -var isWorkerDisabled = false; -var workerSrc; +let isWorkerDisabled = false; +let workerSrc; var isPostMessageTransfersDisabled = false; -var pdfjsFilePath = +const pdfjsFilePath = typeof PDFJSDev !== 'undefined' && PDFJSDev.test('PRODUCTION && !(MOZCENTRAL || FIREFOX)') && typeof document !== 'undefined' && document.currentScript ? @@ -47,8 +47,8 @@ var pdfjsFilePath = var fakeWorkerFilesLoader = null; var useRequireEnsure = false; if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) { - // For GENERIC build we need add support of different fake file loaders - // for different frameworks. + // 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; @@ -1202,8 +1202,8 @@ var PDFWorker = (function PDFWorkerClosure() { let nextFakeWorkerId = 0; function getWorkerSrc() { - if (getDefaultSetting('workerSrc')) { - return getDefaultSetting('workerSrc'); + if (GlobalWorkerOptions.workerSrc) { + return GlobalWorkerOptions.workerSrc; } if (typeof workerSrc !== 'undefined') { return workerSrc; @@ -1213,7 +1213,7 @@ var PDFWorker = (function PDFWorkerClosure() { pdfjsFilePath) { return pdfjsFilePath.replace(/(\.(?:min\.)?js)(\?.*)?$/i, '.worker$1$2'); } - throw new Error('No PDFJS.workerSrc specified'); + throw new Error('No "GlobalWorkerOptions.workerSrc" specified.'); } function getMainThreadWorkerMessageHandler() { diff --git a/src/display/dom_utils.js b/src/display/dom_utils.js index 8bf31dfff..20cb5ec97 100644 --- a/src/display/dom_utils.js +++ b/src/display/dom_utils.js @@ -351,8 +351,6 @@ function getDefaultSetting(id) { return globalSettings ? globalSettings.cMapPacked : false; case 'postMessageTransfers': return globalSettings ? globalSettings.postMessageTransfers : true; - case 'workerSrc': - return globalSettings ? globalSettings.workerSrc : null; case 'maxImageSize': return globalSettings ? globalSettings.maxImageSize : -1; case 'isEvalSupported': diff --git a/src/display/global.js b/src/display/global.js index de3d0f0fe..7c00d194f 100644 --- a/src/display/global.js +++ b/src/display/global.js @@ -112,16 +112,6 @@ PDFJS.cMapPacked = PDFJS.cMapPacked === undefined ? false : PDFJS.cMapPacked; PDFJS.disableFontFace = (PDFJS.disableFontFace === undefined ? false : PDFJS.disableFontFace); -/** - * Path and filename of the worker file. Required when the worker is enabled - * in development mode. If unspecified in the production build, the worker - * will be loaded based on the location of the pdf.js file. It is recommended - * that the workerSrc is set in a custom application to prevent issues caused - * by third-party frameworks and libraries. - * @var {string} - */ -PDFJS.workerSrc = (PDFJS.workerSrc === undefined ? null : PDFJS.workerSrc); - /** * Disable range request loading of PDF files. When enabled and if the server * supports partial content requests then the PDF will be fetched in chunks. diff --git a/test/driver.js b/test/driver.js index 40f09a628..91cd268a6 100644 --- a/test/driver.js +++ b/test/driver.js @@ -16,9 +16,10 @@ 'use strict'; -var WAITING_TIME = 100; // ms -var PDF_TO_CSS_UNITS = 96.0 / 72.0; +const WAITING_TIME = 100; // ms +const PDF_TO_CSS_UNITS = 96.0 / 72.0; const IMAGE_RESOURCES_PATH = '/web/images/'; +const WORKER_SRC = '../build/generic/build/pdf.worker.js'; /** * @class @@ -267,8 +268,9 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars * @param {DriverOptions} options */ function Driver(options) { - // Configure the global PDFJS object - PDFJS.workerSrc = '../build/generic/build/pdf.worker.js'; + // Configure the global worker options. + PDFJS.GlobalWorkerOptions.workerSrc = WORKER_SRC; + PDFJS.cMapPacked = true; PDFJS.cMapUrl = '../external/bcmaps/'; PDFJS.pdfBug = true; diff --git a/test/unit/api_spec.js b/test/unit/api_spec.js index ff5f9f2c8..efa24ae90 100644 --- a/test/unit/api_spec.js +++ b/test/unit/api_spec.js @@ -24,10 +24,10 @@ import { DOMCanvasFactory, RenderingCancelledException } from '../../src/display/dom_utils'; import { - getDocument, PDFDocumentProxy, PDFPageProxy, PDFWorker + getDocument, PDFDataRangeTransport, PDFDocumentProxy, PDFPageProxy, PDFWorker } from '../../src/display/api'; +import { GlobalWorkerOptions } from '../../src/display/worker_options'; import isNodeJS from '../../src/shared/is_node'; -import { PDFJS } from '../../src/display/global'; describe('api', function() { let basicApiFileName = 'basicapi.pdf'; @@ -324,7 +324,7 @@ describe('api', function() { } it('worker created or destroyed', function (done) { - var worker = new PDFJS.PDFWorker('test1'); + var worker = new PDFWorker('test1'); worker.promise.then(function () { expect(worker.name).toEqual('test1'); expect(!!worker.port).toEqual(true); @@ -361,7 +361,7 @@ describe('api', function() { }); }); it('worker created and can be used in getDocument', function (done) { - var worker = new PDFJS.PDFWorker('test1'); + var worker = new PDFWorker('test1'); var loadingTask = getDocument( buildGetDocumentParams(basicApiFileName, { worker, @@ -386,9 +386,9 @@ describe('api', function() { }); }); it('creates more than one worker', function (done) { - var worker1 = new PDFJS.PDFWorker('test1'); - var worker2 = new PDFJS.PDFWorker('test2'); - var worker3 = new PDFJS.PDFWorker('test3'); + var worker1 = new PDFWorker('test1'); + var worker2 = new PDFWorker('test2'); + var worker3 = new PDFWorker('test3'); var ready = Promise.all([worker1.promise, worker2.promise, worker3.promise]); ready.then(function () { @@ -406,7 +406,7 @@ describe('api', function() { it('gets current workerSrc', function() { let workerSrc = PDFWorker.getWorkerSrc(); expect(typeof workerSrc).toEqual('string'); - expect(workerSrc).toEqual(PDFJS.workerSrc); + expect(workerSrc).toEqual(GlobalWorkerOptions.workerSrc); }); }); describe('PDFDocument', function() { @@ -1305,7 +1305,7 @@ describe('api', function() { var fetches = 0; var getDocumentPromise = getDocumentData().then(function (data) { var initialData = data.subarray(0, initialDataLength); - transport = new PDFJS.PDFDataRangeTransport(data.length, initialData); + transport = new PDFDataRangeTransport(data.length, initialData); transport.requestDataRange = function (begin, end) { fetches++; waitSome(function () { @@ -1339,7 +1339,7 @@ describe('api', function() { var fetches = 0; var getDocumentPromise = getDocumentData().then(function (data) { var initialData = data.subarray(0, initialDataLength); - transport = new PDFJS.PDFDataRangeTransport(data.length, initialData); + transport = new PDFDataRangeTransport(data.length, initialData); transport.requestDataRange = function (begin, end) { fetches++; if (fetches === 1) { diff --git a/test/unit/jasmine-boot.js b/test/unit/jasmine-boot.js index 4f2f5169a..cab5df38f 100644 --- a/test/unit/jasmine-boot.js +++ b/test/unit/jasmine-boot.js @@ -42,8 +42,8 @@ function initializePDFJS(callback) { Promise.all([ - 'pdfjs/display/global', 'pdfjs/display/api', + 'pdfjs/display/worker_options', 'pdfjs/display/network', 'pdfjs/display/fetch_stream', 'pdfjs/shared/is_node', @@ -77,9 +77,9 @@ function initializePDFJS(callback) { 'pdfjs-test/unit/util_stream_spec', ].map(function (moduleName) { return SystemJS.import(moduleName); - })).then(function (modules) { - var displayGlobal = modules[0]; - var displayApi = modules[1]; + })).then(function(modules) { + var displayApi = modules[0]; + const GlobalWorkerOptions = modules[1].GlobalWorkerOptions; var PDFNetworkStream = modules[2].PDFNetworkStream; var PDFFetchStream = modules[3].PDFFetchStream; const isNodeJS = modules[4]; @@ -101,7 +101,7 @@ function initializePDFJS(callback) { } // Configure the worker. - displayGlobal.PDFJS.workerSrc = '../../build/generic/build/pdf.worker.js'; + GlobalWorkerOptions.workerSrc = '../../build/generic/build/pdf.worker.js'; callback(); }); diff --git a/web/app.js b/web/app.js index e1891650d..7a7461081 100644 --- a/web/app.js +++ b/web/app.js @@ -21,9 +21,9 @@ import { ProgressBar, RendererType, TextLayerMode } from './ui_utils'; import { - build, createBlob, getDocument, getFilenameFromUrl, InvalidPDFException, - LinkTarget, MissingPDFException, OPS, PDFJS, PDFWorker, shadow, - UnexpectedResponseException, UNSUPPORTED_FEATURES, version + build, createBlob, getDocument, getFilenameFromUrl, GlobalWorkerOptions, + InvalidPDFException, LinkTarget, MissingPDFException, OPS, PDFJS, PDFWorker, + shadow, UnexpectedResponseException, UNSUPPORTED_FEATURES, version } from 'pdfjs-lib'; import { CursorTool, PDFCursorTools } from './pdf_cursor_tools'; import { PDFRenderingQueue, RenderingStates } from './pdf_rendering_queue'; @@ -54,11 +54,11 @@ function configure(PDFJS) { PDFJS.imageResourcesPath = './images/'; if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('FIREFOX || MOZCENTRAL || GENERIC || CHROME')) { - PDFJS.workerSrc = '../build/pdf.worker.js'; + GlobalWorkerOptions.workerSrc = '../build/pdf.worker.js'; } if (typeof PDFJSDev === 'undefined' || !PDFJSDev.test('PRODUCTION')) { PDFJS.cMapUrl = '../external/bcmaps/'; - PDFJS.workerSrc = '../src/worker_loader.js'; + GlobalWorkerOptions.workerSrc = '../src/worker_loader.js'; } else { PDFJS.cMapUrl = '../web/cmaps/'; }