Skip to content

Commit aab0f91

Browse files
committed
[api-minor] Simplify the *fallback* fake worker loader code in src/display/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 see [Webpack issue 8826](https://github.com/webpack/webpack) and libraries such as [require-fool-webpack](https://github.com/sindresorhus/require-fool-webpack).
1 parent 693240c commit aab0f91

File tree

6 files changed

+13
-50
lines changed

6 files changed

+13
-50
lines changed

examples/browserify/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
var pdfjsLib = require('pdfjs-dist');
77

8-
var pdfPath = '../helloworld/helloworld.pdf';
8+
var pdfPath = '../learning/helloworld.pdf';
99

1010
// Setting worker path to worker bundle.
1111
pdfjsLib.GlobalWorkerOptions.workerSrc =

examples/webpack/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
var pdfjsLib = require('pdfjs-dist');
77

8-
var pdfPath = '../helloworld/helloworld.pdf';
8+
var pdfPath = '../learning/helloworld.pdf';
99

1010
// Setting worker path to worker bundle.
1111
pdfjsLib.GlobalWorkerOptions.workerSrc =

gulpfile.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,6 @@ gulp.task('dist-pre', gulp.series('generic', 'components', 'image_decoders',
13201320
bugs: DIST_BUGS_URL,
13211321
license: DIST_LICENSE,
13221322
dependencies: {
1323-
'node-ensure': '^0.0.0', // shim for node for require.ensure
13241323
'worker-loader': '^2.0.0', // used in external/dist/webpack.json
13251324
},
13261325
peerDependencies: {
@@ -1330,7 +1329,6 @@ gulp.task('dist-pre', gulp.series('generic', 'components', 'image_decoders',
13301329
'fs': false,
13311330
'http': false,
13321331
'https': false,
1333-
'node-ensure': false,
13341332
'url': false,
13351333
'zlib': false,
13361334
},

package-lock.json

Lines changed: 0 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"jstransformer-markdown-it": "^2.1.0",
3636
"merge-stream": "^1.0.1",
3737
"mkdirp": "^0.5.1",
38-
"node-ensure": "^0.0.0",
3938
"prettier": "^1.19.1",
4039
"rimraf": "^2.7.1",
4140
"streamqueue": "^1.1.2",

src/display/api.js

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License.
1414
*/
15-
/* globals requirejs, __non_webpack_require__ */
15+
/* globals __non_webpack_require__ */
1616
/* eslint no-var: error */
1717

1818
/**
@@ -34,6 +34,7 @@ import { FontFaceObject, FontLoader } from './font_loader';
3434
import { apiCompatibilityParams } from './api_compatibility';
3535
import { CanvasGraphics } from './canvas';
3636
import { GlobalWorkerOptions } from './worker_options';
37+
import { isNodeJS } from '../shared/is_node';
3738
import { MessageHandler } from '../shared/message_handler';
3839
import { Metadata } from './metadata';
3940
import { PDFDataTransportStream } from './transport_stream';
@@ -47,28 +48,12 @@ let fallbackWorkerSrc;
4748

4849
let fakeWorkerFilesLoader = null;
4950
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
50-
let useRequireEnsure = false;
51-
// For GENERIC build we need to add support for different fake file loaders
52-
// for different frameworks.
53-
if (typeof window === 'undefined') {
54-
// node.js - disable worker and set require.ensure.
51+
if (isNodeJS && typeof __non_webpack_require__ === 'function') {
52+
// Workers aren't supported in Node.js, force-disabling them there.
5553
isWorkerDisabled = true;
56-
if (typeof __non_webpack_require__.ensure === 'undefined') {
57-
__non_webpack_require__.ensure = __non_webpack_require__('node-ensure');
58-
}
59-
useRequireEnsure = true;
60-
} else if (typeof __non_webpack_require__ !== 'undefined' &&
61-
typeof __non_webpack_require__.ensure === 'function') {
62-
useRequireEnsure = true;
63-
}
64-
if (typeof requirejs !== 'undefined' && requirejs.toUrl) {
65-
fallbackWorkerSrc = requirejs.toUrl('pdfjs-dist/build/pdf.worker.js');
66-
}
67-
const dynamicLoaderSupported =
68-
typeof requirejs !== 'undefined' && requirejs.load;
69-
fakeWorkerFilesLoader = useRequireEnsure ? (function() {
70-
return new Promise(function(resolve, reject) {
71-
__non_webpack_require__.ensure([], function() {
54+
55+
fakeWorkerFilesLoader = function() {
56+
return new Promise(function(resolve, reject) {
7257
try {
7358
let worker;
7459
if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('LIB')) {
@@ -80,22 +65,9 @@ if (typeof PDFJSDev !== 'undefined' && PDFJSDev.test('GENERIC')) {
8065
} catch (ex) {
8166
reject(ex);
8267
}
83-
}, reject, 'pdfjsWorker');
84-
});
85-
}) : dynamicLoaderSupported ? (function() {
86-
return new Promise(function(resolve, reject) {
87-
requirejs(['pdfjs-dist/build/pdf.worker'], function(worker) {
88-
try {
89-
resolve(worker.WorkerMessageHandler);
90-
} catch (ex) {
91-
reject(ex);
92-
}
93-
}, reject);
94-
});
95-
}) : null;
96-
97-
if (!fallbackWorkerSrc && typeof document === 'object' &&
98-
'currentScript' in document) {
68+
});
69+
};
70+
} else if (typeof document === 'object' && 'currentScript' in document) {
9971
const pdfjsFilePath = document.currentScript && document.currentScript.src;
10072
if (pdfjsFilePath) {
10173
fallbackWorkerSrc =
@@ -1557,7 +1529,7 @@ const PDFWorker = (function PDFWorkerClosure() {
15571529

15581530
const mainWorkerMessageHandler = getMainThreadWorkerMessageHandler();
15591531
if (mainWorkerMessageHandler) {
1560-
// The worker was already loaded using a `<script>` tag.
1532+
// The worker was already loaded using e.g. a `<script>` tag.
15611533
fakeWorkerFilesLoadedCapability.resolve(mainWorkerMessageHandler);
15621534
return fakeWorkerFilesLoadedCapability.promise;
15631535
}

0 commit comments

Comments
 (0)