Skip to content

Commit

Permalink
[api-minor] Simplify the *fallback* fake worker loader code in `src/d…
Browse files Browse the repository at this point in the history
…isplay/api.js`

For performance reasons, and to avoid hanging the browser UI, the PDF.js library should *always* be used with web workers enabled.
At this point in time all of the supported browsers should have proper worker support, and Node.js is thus the only environment where workers aren't supported. Hence it no longer seems relevant/necessary to provide, by default, fake worker loaders for various JS builders/bundlers/frameworks in the PDF.js code itself.[1]

In order to simplify things, the fake worker loader code is thus simplified to now *only* support Node.js usage respectively "normal" browser usage out-of-the-box.[2]

*Please note:* The officially intended way of using the PDF.js library is with workers enabled, which can be done by setting `GlobalWorkerOptions.workerSrc`, `GlobalWorkerOptions.workerPort`, or manually providing a `PDFWorker` instance when calling `getDocument`.

---
[1] Note that it's still possible to *manually* disable workers, simply my manually loading the built `pdf.worker.js` file into the (current) global scope, however this's mostly intended for testing/debugging purposes.

[2] Unfortunately some bundlers such as Webpack, when used with third-party deployments of the PDF.js library, will start to print `Critical dependency: ...` warnings when run against the built `pdf.js` file from this patch. The reason is that despite the `require` calls being protected by *runtime* `isNodeJS` checks, it's not possible to simply tell Webpack to just ignore the `require`; please note [Webpack issue 8826](webpack/webpack#8826) and libraries such as [require-fool-webpack](https://github.com/sindresorhus/require-fool-webpack).
  • Loading branch information
Snuffleupagus committed Dec 19, 2019
1 parent cf3f342 commit 2db9289
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 48 deletions.
2 changes: 0 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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,
},
Expand Down
6 changes: 0 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
50 changes: 11 additions & 39 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals requirejs, __non_webpack_require__ */
/* globals __non_webpack_require__ */
/* eslint no-var: error */

/**
Expand All @@ -34,6 +34,7 @@ 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';
Expand All @@ -47,28 +48,12 @@ 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.
if (isNodeJS && typeof __non_webpack_require__ === 'function') {
// Workers aren't supported in Node.js, force-disabling them there.
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() {

fakeWorkerFilesLoader = function() {
return new Promise(function(resolve, reject) {
try {
let worker;
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) {
Expand All @@ -80,22 +65,9 @@ if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
} 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) {
});
};
} else if (typeof document === 'object' && 'currentScript' in document) {
const pdfjsFilePath = document.currentScript && document.currentScript.src;
if (pdfjsFilePath) {
fallbackWorkerSrc =
Expand Down Expand Up @@ -1557,7 +1529,7 @@ const PDFWorker = (function PDFWorkerClosure() {

const mainWorkerMessageHandler = getMainThreadWorkerMessageHandler();
if (mainWorkerMessageHandler) {
// The worker was already loaded using a `<script>` tag.
// The worker was already loaded using e.g. a `<script>` tag.
fakeWorkerFilesLoadedCapability.resolve(mainWorkerMessageHandler);
return fakeWorkerFilesLoadedCapability.promise;
}
Expand Down

0 comments on commit 2db9289

Please sign in to comment.