Move the workerSrc option from the global PDFJS object and into GlobalWorkerOptions instead

This commit is contained in:
Jonas Jenwald 2018-02-14 14:49:24 +01:00
parent 45adf33187
commit c3c1fc511d
20 changed files with 69 additions and 60 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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.
//

View File

@ -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.
//

View File

@ -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.
//

View File

@ -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) {

View File

@ -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

View File

@ -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.

View File

@ -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,

View File

@ -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;

View File

@ -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/';

View File

@ -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)

View File

@ -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);

View File

@ -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() {

View File

@ -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':

View File

@ -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.

View File

@ -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;

View File

@ -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) {

View File

@ -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();
});

View File

@ -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/';
}