Skip to content

Commit 003bd40

Browse files
committed
Introduce a GlobalWorkerOptions object for (basic) Worker configuration
Compared to most other options currently/previously residing on the global `PDFJS` object, some of the Worker specific ones (e.g. `workerPort`/`workerSrc`) probably cannot be moved into options provided directly when initializing e.g. `PDFWorker`. The reason is that in some cases, e.g. the Webpack examples, we try to provide Worker auto-configuration and I cannot see a good solution for that use-case if we completely remove the globally available Worker configuration. However inline with previous patches for PDF.js version `2.0`, it does seem like a worthwhile goal to move away from storing options directly on the global `PDFJS` object, since that is a pattern we should avoid going forward. Especially since one of the (eventual) goals is to attempt to *completely* remove the global `PDFJS` object, and rely solely on exporting/importing the needed functionality. By introducing the `GlobalWorkerOptions` we thus have larger flexibility in the future, if/when the global `PDFJS` object will be removed.
1 parent a89071b commit 003bd40

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

src/display/global.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
} from './api';
2727
import { AnnotationLayer } from './annotation_layer';
2828
import globalScope from '../shared/global_scope';
29+
import { GlobalWorkerOptions } from './worker_options';
2930
import { Metadata } from './metadata';
3031
import { renderTextLayer } from './text_layer';
3132
import { SVGGraphics } from './svg';
@@ -208,6 +209,7 @@ PDFJS.getDocument = getDocument;
208209
PDFJS.LoopbackPort = LoopbackPort;
209210
PDFJS.PDFDataRangeTransport = PDFDataRangeTransport;
210211
PDFJS.PDFWorker = PDFWorker;
212+
PDFJS.GlobalWorkerOptions = GlobalWorkerOptions;
211213

212214
PDFJS.getFilenameFromUrl = getFilenameFromUrl;
213215

src/display/worker_options.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Copyright 2018 Mozilla Foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
const GlobalWorkerOptions = Object.create(null);
17+
18+
/**
19+
* Defines global port for worker process. Overrides the `workerSrc` option.
20+
* @var {Object}
21+
*/
22+
GlobalWorkerOptions.workerPort = (GlobalWorkerOptions.workerPort === undefined ?
23+
null : GlobalWorkerOptions.workerPort);
24+
25+
/**
26+
* Path and filename of the worker file. Required when workers are enabled in
27+
* development mode. If unspecified in production builds, the worker will be
28+
* loaded based on the location of the `pdf.js` file.
29+
*
30+
* NOTE: The `workerSrc` should always be set in custom applications, in order
31+
* to prevent issues caused by third-party frameworks and libraries.
32+
* @var {string}
33+
*/
34+
GlobalWorkerOptions.workerSrc = (GlobalWorkerOptions.workerSrc === undefined ?
35+
'' : GlobalWorkerOptions.workerSrc);
36+
37+
export {
38+
GlobalWorkerOptions,
39+
};

src/pdf.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ var pdfjsDisplayTextLayer = require('./display/text_layer.js');
2828
var pdfjsDisplayAnnotationLayer = require('./display/annotation_layer.js');
2929
var pdfjsDisplayDOMUtils = require('./display/dom_utils.js');
3030
var pdfjsDisplaySVG = require('./display/svg.js');
31+
let pdfjsDisplayWorkerOptions = require('./display/worker_options.js');
3132

3233
if (typeof PDFJSDev === 'undefined' || PDFJSDev.test('GENERIC')) {
3334
const isNodeJS = require('./shared/is_node.js');
@@ -93,3 +94,4 @@ exports.RenderingCancelledException =
9394
exports.getFilenameFromUrl = pdfjsDisplayDOMUtils.getFilenameFromUrl;
9495
exports.LinkTarget = pdfjsDisplayDOMUtils.LinkTarget;
9596
exports.addLinkAttributes = pdfjsDisplayDOMUtils.addLinkAttributes;
97+
exports.GlobalWorkerOptions = pdfjsDisplayWorkerOptions.GlobalWorkerOptions;

0 commit comments

Comments
 (0)