diff --git a/.eslintrc.json b/.eslintrc.json index 618b7f0e..20190246 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -21,6 +21,7 @@ "define": "readonly", "amplitude": "readonly", "opera": "readonly", - "ActiveXObject": "readonly" + "ActiveXObject": "readonly", + "globalThis": "readonly" } } diff --git a/src/amplitude-client.js b/src/amplitude-client.js index cda4ffc7..2c176f12 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -19,7 +19,6 @@ import getHost from './get-host'; import baseCookie from './base-cookie'; import { AmplitudeServerZone, getEventLogApi } from './server-zone'; import ConfigManager from './config-manager'; -import GlobalScope from './global-scope'; /** * AmplitudeClient SDK API - instance constructor. @@ -83,7 +82,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o _parseConfig(this.options, opt_config); if ( (isBrowserEnv() || utils.isWebWorkerEnvironment()) && - GlobalScope.Prototype !== undefined && + globalThis.Prototype !== undefined && Array.prototype.toJSON ) { prototypeJsFix(); @@ -248,7 +247,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o // Monitoring just page exits because that is the most requested feature for now // "If you're specifically trying to detect page unload events, the pagehide event is the best option." // https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event - GlobalScope.addEventListener( + globalThis.addEventListener( 'pagehide', () => { handleVisibilityChange(); @@ -763,7 +762,7 @@ AmplitudeClient.prototype._getReferrer = function _getReferrer() { * @private */ AmplitudeClient.prototype._getUrlParams = function _getUrlParams() { - return GlobalScope.location.search; + return globalThis.location.search; }; /** @@ -1785,7 +1784,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() { } this._sending = true; } - var protocol = this.options.forceHttps ? 'https' : 'https:' === GlobalScope.location.protocol ? 'https' : 'http'; + var protocol = this.options.forceHttps ? 'https' : 'https:' === globalThis.location.protocol ? 'https' : 'http'; var url = protocol + '://' + this.options.apiEndpoint; // fetch events to send diff --git a/src/base64.js b/src/base64.js index 4a085dfc..c10d3a08 100644 --- a/src/base64.js +++ b/src/base64.js @@ -1,5 +1,4 @@ import UTF8 from './utf8'; -import GlobalScope from './global-scope'; /* * Base64 encoder/decoder @@ -10,8 +9,8 @@ var Base64 = { encode: function (input) { try { - if (GlobalScope.btoa && GlobalScope.atob) { - return GlobalScope.btoa(unescape(encodeURIComponent(input))); + if (globalThis.btoa && globalThis.atob) { + return globalThis.btoa(unescape(encodeURIComponent(input))); } } catch (e) { //log(e); @@ -54,8 +53,8 @@ var Base64 = { decode: function (input) { try { - if (GlobalScope.btoa && GlobalScope.atob) { - return decodeURIComponent(escape(GlobalScope.atob(input))); + if (globalThis.btoa && globalThis.atob) { + return decodeURIComponent(escape(globalThis.atob(input))); } } catch (e) { //log(e); diff --git a/src/config-manager.js b/src/config-manager.js index 294133e2..e7946536 100644 --- a/src/config-manager.js +++ b/src/config-manager.js @@ -1,6 +1,5 @@ import Constants from './constants'; import { getDynamicConfigApi } from './server-zone'; -import GlobalScope from './global-scope'; /** * Dynamic Configuration * Find the best server url automatically based on app users' geo location. @@ -16,14 +15,14 @@ class ConfigManager { refresh(serverZone, forceHttps, callback) { let protocol = 'https'; - if (!forceHttps && 'https:' !== GlobalScope.location.protocol) { + if (!forceHttps && 'https:' !== globalThis.location.protocol) { protocol = 'http'; } const dynamicConfigUrl = protocol + '://' + getDynamicConfigApi(serverZone); const self = this; - const isIE = GlobalScope.XDomainRequest ? true : false; + const isIE = globalThis.XDomainRequest ? true : false; if (isIE) { - const xdr = new GlobalScope.XDomainRequest(); + const xdr = new globalThis.XDomainRequest(); xdr.open('GET', dynamicConfigUrl, true); xdr.onload = function () { const response = JSON.parse(xdr.responseText); diff --git a/src/cookiestorage.js b/src/cookiestorage.js index 367c4fea..1dae661e 100644 --- a/src/cookiestorage.js +++ b/src/cookiestorage.js @@ -6,7 +6,6 @@ import Cookie from './cookie'; import localStorage from './localstorage'; import baseCookie from './base-cookie'; -import GlobalScope from './global-scope'; var cookieStorage = function () { this.storage = null; @@ -44,7 +43,7 @@ cookieStorage.prototype.getStorage = function () { this._options.expirationDays = opts.expirationDays || this._options.expirationDays; // localStorage is specific to subdomains this._options.domain = - opts.domain || this._options.domain || (GlobalScope && GlobalScope.location && GlobalScope.location.hostname); + opts.domain || this._options.domain || (globalThis && globalThis.location && globalThis.location.hostname); return (this._options.secure = opts.secure || false); }, get: function (name) { diff --git a/src/get-host.js b/src/get-host.js index 8c717448..6c378030 100644 --- a/src/get-host.js +++ b/src/get-host.js @@ -1,18 +1,16 @@ -import GlobalScope from './global-scope'; - const getHost = (url) => { if (url) { if (typeof document !== 'undefined') { const a = document.createElement('a'); a.href = url; - return a.hostname || GlobalScope.location.hostname; + return a.hostname || globalThis.location.hostname; } if (typeof URL === 'function') { const u = new URL(url); - return u.hostname || GlobalScope.location.hostname; + return u.hostname || globalThis.location.hostname; } } - return GlobalScope.location.hostname; + return globalThis.location.hostname; }; export default getHost; diff --git a/src/get-location.js b/src/get-location.js index ad8ba3d4..1f78ccbd 100644 --- a/src/get-location.js +++ b/src/get-location.js @@ -1,7 +1,5 @@ -import GlobalScope from './global-scope'; - const getLocation = () => { - return GlobalScope.location; + return globalThis.location; }; export default getLocation; diff --git a/src/global-scope.js b/src/global-scope.js deleted file mode 100644 index f7aadec0..00000000 --- a/src/global-scope.js +++ /dev/null @@ -1,2 +0,0 @@ -const GlobalScope = typeof window !== 'undefined' ? window : self; -export default GlobalScope; diff --git a/src/index.js b/src/index.js index 591a6ff4..c2b26249 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,7 @@ // Entry point import Amplitude from './amplitude'; -import GlobalScope from './global-scope'; -const old = (typeof GlobalScope !== 'undefined' && GlobalScope.amplitude) || {}; +const old = (typeof globalThis !== 'undefined' && globalThis.amplitude) || {}; const newInstance = new Amplitude(); newInstance._q = old._q || []; diff --git a/src/localstorage.js b/src/localstorage.js index 53a2828d..cfe0d5f6 100644 --- a/src/localstorage.js +++ b/src/localstorage.js @@ -2,14 +2,13 @@ * Implement localStorage to support Firefox 2-3 and IE 5-7 */ -import GlobalScope from './global-scope'; import WorkerStorage from './worker-storage'; import utils from './utils'; var localStorage; if (!BUILD_COMPAT_LOCAL_STORAGE) { - localStorage = GlobalScope.localStorage; + localStorage = globalThis.localStorage; } if (BUILD_COMPAT_LOCAL_STORAGE) { @@ -18,9 +17,9 @@ if (BUILD_COMPAT_LOCAL_STORAGE) { var uid = new Date(); var result; try { - GlobalScope.localStorage.setItem(uid, uid); - result = GlobalScope.localStorage.getItem(uid) === String(uid); - GlobalScope.localStorage.removeItem(uid); + globalThis.localStorage.setItem(uid, uid); + result = globalThis.localStorage.getItem(uid) === String(uid); + globalThis.localStorage.removeItem(uid); return result; } catch (e) { // localStorage not available @@ -29,12 +28,12 @@ if (BUILD_COMPAT_LOCAL_STORAGE) { }; if (windowLocalStorageAvailable()) { - localStorage = GlobalScope.localStorage; - } else if (typeof GlobalScope !== 'undefined' && GlobalScope.globalStorage) { + localStorage = globalThis.localStorage; + } else if (typeof globalThis !== 'undefined' && globalThis.globalStorage) { // Firefox 2-3 use globalStorage // See https://developer.mozilla.org/en/dom/storage#globalStorage try { - localStorage = GlobalScope.globalStorage[GlobalScope.location.hostname]; + localStorage = globalThis.globalStorage[globalThis.location.hostname]; } catch (e) { // Something bad happened... } diff --git a/src/metadata-storage.js b/src/metadata-storage.js index c719a992..d044cdc9 100644 --- a/src/metadata-storage.js +++ b/src/metadata-storage.js @@ -10,7 +10,6 @@ import getLocation from './get-location'; import ampLocalStorage from './localstorage'; import topDomain from './top-domain'; import utils from './utils'; -import GlobalScope from './global-scope'; const storageOptionExists = { [Constants.STORAGE_COOKIES]: true, @@ -89,8 +88,8 @@ class MetadataStorage { switch (this.storage) { case Constants.STORAGE_SESSION: - if (GlobalScope.sessionStorage) { - GlobalScope.sessionStorage.setItem(this.storageKey, value); + if (globalThis.sessionStorage) { + globalThis.sessionStorage.setItem(this.storageKey, value); } break; case Constants.STORAGE_LOCAL: @@ -132,7 +131,7 @@ class MetadataStorage { } if (!str) { try { - str = GlobalScope.sessionStorage && GlobalScope.sessionStorage.getItem(this.storageKey); + str = globalThis.sessionStorage && globalThis.sessionStorage.getItem(this.storageKey); } catch (e) { utils.log.info(`window.sessionStorage unavailable. Reason: "${e}"`); } @@ -188,8 +187,8 @@ class MetadataStorage { } if (!str) { try { - str = GlobalScope.sessionStorage && GlobalScope.sessionStorage.getItem(this.storageKey); - GlobalScope.sessionStorage.clear(); + str = globalThis.sessionStorage && globalThis.sessionStorage.getItem(this.storageKey); + globalThis.sessionStorage.clear(); } catch (e) { utils.log.info(`window.sessionStorage unavailable. Reason: "${e}"`); } diff --git a/src/utils.js b/src/utils.js index a163576e..6a744e1f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,5 +1,4 @@ import constants from './constants'; -import GlobalScope from './global-scope'; import type from './type'; var logLevels = { @@ -55,7 +54,7 @@ var isEmptyString = function isEmptyString(str) { var sessionStorageEnabled = function sessionStorageEnabled() { try { - if (GlobalScope.sessionStorage) { + if (globalThis.sessionStorage) { return true; } } catch (e) { @@ -275,7 +274,7 @@ var getQueryParam = function getQueryParam(name, query) { }; const isWebWorkerEnvironment = () => { - return typeof WorkerGlobalScope !== 'undefined'; + return typeof WorkerglobalThis !== 'undefined'; }; export default { diff --git a/src/xhr.js b/src/xhr.js index 8a532314..a2c3136a 100644 --- a/src/xhr.js +++ b/src/xhr.js @@ -1,5 +1,4 @@ import queryString from 'query-string'; -import GlobalScope from './global-scope'; /* * Simple AJAX request object @@ -17,9 +16,9 @@ function setHeaders(xhr, headers) { } Request.prototype.send = function (callback) { - var isIE = GlobalScope.XDomainRequest ? true : false; + var isIE = globalThis.XDomainRequest ? true : false; if (isIE) { - var xdr = new GlobalScope.XDomainRequest(); + var xdr = new globalThis.XDomainRequest(); xdr.open('POST', this.url, true); xdr.onload = function () { callback(200, xdr.responseText); diff --git a/test/global-scope.js b/test/global-scope.js deleted file mode 100644 index 4906f590..00000000 --- a/test/global-scope.js +++ /dev/null @@ -1,7 +0,0 @@ -import GlobalScope from '../src/global-scope'; - -describe('GlobalScope', function () { - it('should return true', function () { - assert.isTrue(GlobalScope === window); - }); -}); diff --git a/test/tests.js b/test/tests.js index 4d48df49..c85df27f 100644 --- a/test/tests.js +++ b/test/tests.js @@ -17,4 +17,3 @@ import './base64Id.js'; import './server-zone.js'; import './config-manager.js'; import './worker-storage.js'; -import './global-scope.js';