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`.
This commit is contained in:
Jonas Jenwald 2019-12-08 13:46:21 +01:00
parent 7b503c8923
commit a8fc306b6e
5 changed files with 31 additions and 51 deletions

View File

@ -19,6 +19,7 @@
},
"globals": {
"globalThis": false,
"PDFJSDev": false,
"exports": false,
"SystemJS": false,

View File

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

View File

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

View File

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

View File

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