From a8fc306b6e1d78eac43d841713c9dc5281a9ca3e Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 8 Dec 2019 13:46:21 +0100 Subject: [PATCH 1/2] Replace `globalScope` with the standard `globalThis` property instead Please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis and note that most (reasonably) modern browsers have supported this for a while now, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/globalThis#Browser_compatibility Since ESLint doesn't support this new global yet, it was added to the `globals` list in the top-level configuration file to prevent issues. Finally, for older browsers a polyfill was added in `ssrc/shared/compatibility.js`. --- .eslintrc | 1 + src/display/api.js | 13 ++++++------- src/display/text_layer.js | 5 ++--- src/shared/compatibility.js | 39 +++++++++++++++++++++---------------- src/shared/global_scope.js | 24 ----------------------- 5 files changed, 31 insertions(+), 51 deletions(-) delete mode 100644 src/shared/global_scope.js diff --git a/.eslintrc b/.eslintrc index ff2bd1019..0f50c6e55 100644 --- a/.eslintrc +++ b/.eslintrc @@ -19,6 +19,7 @@ }, "globals": { + "globalThis": false, "PDFJSDev": false, "exports": false, "SystemJS": false, diff --git a/src/display/api.js b/src/display/api.js index 4d91fd432..c1aa7c8cc 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -33,7 +33,6 @@ import { import { FontFaceObject, FontLoader } from './font_loader'; import { apiCompatibilityParams } from './api_compatibility'; import { CanvasGraphics } from './canvas'; -import { globalScope } from '../shared/global_scope'; import { GlobalWorkerOptions } from './worker_options'; import { MessageHandler } from '../shared/message_handler'; import { Metadata } from './metadata'; @@ -2103,11 +2102,11 @@ class WorkerTransport { } let fontRegistry = null; - if (params.pdfBug && globalScope.FontInspector && - globalScope.FontInspector.enabled) { + if (params.pdfBug && globalThis.FontInspector && + globalThis.FontInspector.enabled) { fontRegistry = { registerFont(font, url) { - globalScope['FontInspector'].fontAdded(font, url); + globalThis.FontInspector.fontAdded(font, url); }, }; } @@ -2608,9 +2607,9 @@ const InternalRenderTask = (function InternalRenderTaskClosure() { canvasInRendering.add(this._canvas); } - if (this._pdfBug && globalScope.StepperManager && - globalScope.StepperManager.enabled) { - this.stepper = globalScope.StepperManager.create(this.pageNumber - 1); + if (this._pdfBug && globalThis.StepperManager && + globalThis.StepperManager.enabled) { + this.stepper = globalThis.StepperManager.create(this.pageNumber - 1); this.stepper.init(this.operatorList); this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint(); } diff --git a/src/display/text_layer.js b/src/display/text_layer.js index c850bc8b0..bc24762c8 100644 --- a/src/display/text_layer.js +++ b/src/display/text_layer.js @@ -14,7 +14,6 @@ */ import { AbortException, createPromiseCapability, Util } from '../shared/util'; -import { globalScope } from '../shared/global_scope'; /** * Text layer render parameters. @@ -469,8 +468,8 @@ var renderTextLayer = (function renderTextLayerClosure() { this._textDivs = textDivs || []; this._textContentItemsStr = textContentItemsStr || []; this._enhanceTextSelection = !!enhanceTextSelection; - this._fontInspectorEnabled = !!(globalScope.FontInspector && - globalScope.FontInspector.enabled); + this._fontInspectorEnabled = !!(globalThis.FontInspector && + globalThis.FontInspector.enabled); this._reader = null; this._layoutTextLastFontSize = null; diff --git a/src/shared/compatibility.js b/src/shared/compatibility.js index 2bc09b1cc..41849c617 100644 --- a/src/shared/compatibility.js +++ b/src/shared/compatibility.js @@ -14,13 +14,18 @@ */ /* eslint no-var: error */ -const { globalScope, } = require('./global_scope'); - // Skip compatibility checks for modern builds and if we already ran the module. if ((typeof PDFJSDev === 'undefined' || !PDFJSDev.test('SKIP_BABEL')) && - !globalScope._pdfjsCompatibilityChecked) { + (typeof globalThis === 'undefined' || + !globalThis._pdfjsCompatibilityChecked)) { -globalScope._pdfjsCompatibilityChecked = true; +// Provides support for globalThis in legacy browsers. +// Support: IE11/Edge, Opera +if (typeof globalThis === 'undefined' || globalThis.Math !== Math) { + // eslint-disable-next-line no-global-assign + globalThis = require('core-js/es/global-this'); +} +globalThis._pdfjsCompatibilityChecked = true; const { isNodeJS, } = require('./is_node'); @@ -31,10 +36,10 @@ const isIE = /Trident/.test(userAgent); // Support: Node.js (function checkNodeBtoa() { - if (globalScope.btoa || !isNodeJS) { + if (globalThis.btoa || !isNodeJS) { return; } - globalScope.btoa = function(chars) { + globalThis.btoa = function(chars) { // eslint-disable-next-line no-undef return Buffer.from(chars, 'binary').toString('base64'); }; @@ -42,10 +47,10 @@ const isIE = /Trident/.test(userAgent); // Support: Node.js (function checkNodeAtob() { - if (globalScope.atob || !isNodeJS) { + if (globalThis.atob || !isNodeJS) { return; } - globalScope.atob = function(input) { + globalThis.atob = function(input) { // eslint-disable-next-line no-undef return Buffer.from(input, 'base64').toString('binary'); }; @@ -223,11 +228,11 @@ const isIE = /Trident/.test(userAgent); // need to be polyfilled for the IMAGE_DECODERS build target. return; } - if (globalScope.Promise && (globalScope.Promise.prototype && - globalScope.Promise.prototype.finally)) { + if (globalThis.Promise && (globalThis.Promise.prototype && + globalThis.Promise.prototype.finally)) { return; } - globalScope.Promise = require('core-js/es/promise/index'); + globalThis.Promise = require('core-js/es/promise/index'); })(); // Support: IE @@ -241,23 +246,23 @@ const isIE = /Trident/.test(userAgent); // The `URL` constructor is assumed to be available in the extension builds. return; } - globalScope.URL = require('core-js/web/url'); + globalThis.URL = require('core-js/web/url'); })(); // Support: IE<11, Safari<8, Chrome<36 (function checkWeakMap() { - if (globalScope.WeakMap) { + if (globalThis.WeakMap) { return; } - globalScope.WeakMap = require('core-js/es/weak-map/index'); + globalThis.WeakMap = require('core-js/es/weak-map/index'); })(); // Support: IE11 (function checkWeakSet() { - if (globalScope.WeakSet) { + if (globalThis.WeakSet) { return; } - globalScope.WeakSet = require('core-js/es/weak-set/index'); + globalThis.WeakSet = require('core-js/es/weak-set/index'); })(); // Provides support for String.codePointAt in legacy browsers. @@ -280,7 +285,7 @@ const isIE = /Trident/.test(userAgent); // Support: IE (function checkSymbol() { - if (globalScope.Symbol) { + if (globalThis.Symbol) { return; } require('core-js/es/symbol/index'); diff --git a/src/shared/global_scope.js b/src/shared/global_scope.js deleted file mode 100644 index 5ddc95b17..000000000 --- a/src/shared/global_scope.js +++ /dev/null @@ -1,24 +0,0 @@ -/* Copyright 2017 Mozilla Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -const globalScope = - (typeof window !== 'undefined' && window.Math === Math) ? window : - // eslint-disable-next-line no-undef - (typeof global !== 'undefined' && global.Math === Math) ? global : - (typeof self !== 'undefined' && self.Math === Math) ? self : {}; - -export { - globalScope, -}; From 71d61e4c6fc74d94c2cdd6d6b522697c92777504 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 8 Dec 2019 14:03:32 +0100 Subject: [PATCH 2/2] Re-factor `getMainThreadWorkerMessageHandler` to support arbitrary global scopes, rather than only `window` --- src/display/api.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index c1aa7c8cc..b83292872 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1540,12 +1540,12 @@ const PDFWorker = (function PDFWorkerClosure() { } function getMainThreadWorkerMessageHandler() { + let mainWorkerMessageHandler; try { - if (typeof window !== 'undefined') { - return (window.pdfjsWorker && window.pdfjsWorker.WorkerMessageHandler); - } - } catch (ex) { } - return null; + mainWorkerMessageHandler = + globalThis.pdfjsWorker && globalThis.pdfjsWorker.WorkerMessageHandler; + } catch (ex) { /* Ignore errors. */ } + return mainWorkerMessageHandler || null; } // Loads worker code into main thread.