From 0c07a36f162aaddd4d8b06c7e5e8cbf56bd48f77 Mon Sep 17 00:00:00 2001 From: Matt Lubner Date: Wed, 22 Dec 2021 13:03:41 -0800 Subject: [PATCH 1/5] feat: replace GlobalScope with globalThis --- .eslintrc.json | 3 ++- src/amplitude-client.js | 9 ++++----- src/base64.js | 9 ++++----- src/config-manager.js | 7 +++---- src/cookiestorage.js | 3 +-- src/get-host.js | 8 +++----- src/get-location.js | 4 +--- src/global-scope.js | 2 -- src/index.js | 3 +-- src/localstorage.js | 15 +++++++-------- src/metadata-storage.js | 11 +++++------ src/utils.js | 5 ++--- src/xhr.js | 5 ++--- test/global-scope.js | 7 ------- test/tests.js | 1 - 15 files changed, 35 insertions(+), 57 deletions(-) delete mode 100644 src/global-scope.js delete mode 100644 test/global-scope.js 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'; From e27925c85a213b752a48ccd71ae32f47e5c101b2 Mon Sep 17 00:00:00 2001 From: Matt Lubner Date: Wed, 22 Dec 2021 13:14:17 -0800 Subject: [PATCH 2/5] fix: restore WorkerGlobalScope --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 6a744e1f..2555e8c5 100644 --- a/src/utils.js +++ b/src/utils.js @@ -274,7 +274,7 @@ var getQueryParam = function getQueryParam(name, query) { }; const isWebWorkerEnvironment = () => { - return typeof WorkerglobalThis !== 'undefined'; + return typeof WorkerGlobalScope !== 'undefined'; }; export default { From 155dcbcc83ba71c05e3645aaee7554461bfc0796 Mon Sep 17 00:00:00 2001 From: Matt Lubner Date: Wed, 22 Dec 2021 16:16:47 -0800 Subject: [PATCH 3/5] Revert "fix: restore WorkerGlobalScope" This reverts commit e27925c85a213b752a48ccd71ae32f47e5c101b2. --- src/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.js b/src/utils.js index 2555e8c5..6a744e1f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -274,7 +274,7 @@ var getQueryParam = function getQueryParam(name, query) { }; const isWebWorkerEnvironment = () => { - return typeof WorkerGlobalScope !== 'undefined'; + return typeof WorkerglobalThis !== 'undefined'; }; export default { From 7d631fa97e573039b7bb9053c704c03f8fa3663d Mon Sep 17 00:00:00 2001 From: Matt Lubner Date: Wed, 22 Dec 2021 16:16:51 -0800 Subject: [PATCH 4/5] Revert "feat: replace GlobalScope with globalThis" This reverts commit 0c07a36f162aaddd4d8b06c7e5e8cbf56bd48f77. --- .eslintrc.json | 3 +-- src/amplitude-client.js | 9 +++++---- src/base64.js | 9 +++++---- src/config-manager.js | 7 ++++--- src/cookiestorage.js | 3 ++- src/get-host.js | 8 +++++--- src/get-location.js | 4 +++- src/global-scope.js | 2 ++ src/index.js | 3 ++- src/localstorage.js | 15 ++++++++------- src/metadata-storage.js | 11 ++++++----- src/utils.js | 5 +++-- src/xhr.js | 5 +++-- test/global-scope.js | 7 +++++++ test/tests.js | 1 + 15 files changed, 57 insertions(+), 35 deletions(-) create mode 100644 src/global-scope.js create mode 100644 test/global-scope.js diff --git a/.eslintrc.json b/.eslintrc.json index 20190246..618b7f0e 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -21,7 +21,6 @@ "define": "readonly", "amplitude": "readonly", "opera": "readonly", - "ActiveXObject": "readonly", - "globalThis": "readonly" + "ActiveXObject": "readonly" } } diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 2c176f12..cda4ffc7 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -19,6 +19,7 @@ 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. @@ -82,7 +83,7 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o _parseConfig(this.options, opt_config); if ( (isBrowserEnv() || utils.isWebWorkerEnvironment()) && - globalThis.Prototype !== undefined && + GlobalScope.Prototype !== undefined && Array.prototype.toJSON ) { prototypeJsFix(); @@ -247,7 +248,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 - globalThis.addEventListener( + GlobalScope.addEventListener( 'pagehide', () => { handleVisibilityChange(); @@ -762,7 +763,7 @@ AmplitudeClient.prototype._getReferrer = function _getReferrer() { * @private */ AmplitudeClient.prototype._getUrlParams = function _getUrlParams() { - return globalThis.location.search; + return GlobalScope.location.search; }; /** @@ -1784,7 +1785,7 @@ AmplitudeClient.prototype.sendEvents = function sendEvents() { } this._sending = true; } - var protocol = this.options.forceHttps ? 'https' : 'https:' === globalThis.location.protocol ? 'https' : 'http'; + var protocol = this.options.forceHttps ? 'https' : 'https:' === GlobalScope.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 c10d3a08..4a085dfc 100644 --- a/src/base64.js +++ b/src/base64.js @@ -1,4 +1,5 @@ import UTF8 from './utf8'; +import GlobalScope from './global-scope'; /* * Base64 encoder/decoder @@ -9,8 +10,8 @@ var Base64 = { encode: function (input) { try { - if (globalThis.btoa && globalThis.atob) { - return globalThis.btoa(unescape(encodeURIComponent(input))); + if (GlobalScope.btoa && GlobalScope.atob) { + return GlobalScope.btoa(unescape(encodeURIComponent(input))); } } catch (e) { //log(e); @@ -53,8 +54,8 @@ var Base64 = { decode: function (input) { try { - if (globalThis.btoa && globalThis.atob) { - return decodeURIComponent(escape(globalThis.atob(input))); + if (GlobalScope.btoa && GlobalScope.atob) { + return decodeURIComponent(escape(GlobalScope.atob(input))); } } catch (e) { //log(e); diff --git a/src/config-manager.js b/src/config-manager.js index e7946536..294133e2 100644 --- a/src/config-manager.js +++ b/src/config-manager.js @@ -1,5 +1,6 @@ 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. @@ -15,14 +16,14 @@ class ConfigManager { refresh(serverZone, forceHttps, callback) { let protocol = 'https'; - if (!forceHttps && 'https:' !== globalThis.location.protocol) { + if (!forceHttps && 'https:' !== GlobalScope.location.protocol) { protocol = 'http'; } const dynamicConfigUrl = protocol + '://' + getDynamicConfigApi(serverZone); const self = this; - const isIE = globalThis.XDomainRequest ? true : false; + const isIE = GlobalScope.XDomainRequest ? true : false; if (isIE) { - const xdr = new globalThis.XDomainRequest(); + const xdr = new GlobalScope.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 1dae661e..367c4fea 100644 --- a/src/cookiestorage.js +++ b/src/cookiestorage.js @@ -6,6 +6,7 @@ import Cookie from './cookie'; import localStorage from './localstorage'; import baseCookie from './base-cookie'; +import GlobalScope from './global-scope'; var cookieStorage = function () { this.storage = null; @@ -43,7 +44,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 || (globalThis && globalThis.location && globalThis.location.hostname); + opts.domain || this._options.domain || (GlobalScope && GlobalScope.location && GlobalScope.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 6c378030..8c717448 100644 --- a/src/get-host.js +++ b/src/get-host.js @@ -1,16 +1,18 @@ +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 || globalThis.location.hostname; + return a.hostname || GlobalScope.location.hostname; } if (typeof URL === 'function') { const u = new URL(url); - return u.hostname || globalThis.location.hostname; + return u.hostname || GlobalScope.location.hostname; } } - return globalThis.location.hostname; + return GlobalScope.location.hostname; }; export default getHost; diff --git a/src/get-location.js b/src/get-location.js index 1f78ccbd..ad8ba3d4 100644 --- a/src/get-location.js +++ b/src/get-location.js @@ -1,5 +1,7 @@ +import GlobalScope from './global-scope'; + const getLocation = () => { - return globalThis.location; + return GlobalScope.location; }; export default getLocation; diff --git a/src/global-scope.js b/src/global-scope.js new file mode 100644 index 00000000..f7aadec0 --- /dev/null +++ b/src/global-scope.js @@ -0,0 +1,2 @@ +const GlobalScope = typeof window !== 'undefined' ? window : self; +export default GlobalScope; diff --git a/src/index.js b/src/index.js index c2b26249..591a6ff4 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,8 @@ // Entry point import Amplitude from './amplitude'; +import GlobalScope from './global-scope'; -const old = (typeof globalThis !== 'undefined' && globalThis.amplitude) || {}; +const old = (typeof GlobalScope !== 'undefined' && GlobalScope.amplitude) || {}; const newInstance = new Amplitude(); newInstance._q = old._q || []; diff --git a/src/localstorage.js b/src/localstorage.js index cfe0d5f6..53a2828d 100644 --- a/src/localstorage.js +++ b/src/localstorage.js @@ -2,13 +2,14 @@ * 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 = globalThis.localStorage; + localStorage = GlobalScope.localStorage; } if (BUILD_COMPAT_LOCAL_STORAGE) { @@ -17,9 +18,9 @@ if (BUILD_COMPAT_LOCAL_STORAGE) { var uid = new Date(); var result; try { - globalThis.localStorage.setItem(uid, uid); - result = globalThis.localStorage.getItem(uid) === String(uid); - globalThis.localStorage.removeItem(uid); + GlobalScope.localStorage.setItem(uid, uid); + result = GlobalScope.localStorage.getItem(uid) === String(uid); + GlobalScope.localStorage.removeItem(uid); return result; } catch (e) { // localStorage not available @@ -28,12 +29,12 @@ if (BUILD_COMPAT_LOCAL_STORAGE) { }; if (windowLocalStorageAvailable()) { - localStorage = globalThis.localStorage; - } else if (typeof globalThis !== 'undefined' && globalThis.globalStorage) { + localStorage = GlobalScope.localStorage; + } else if (typeof GlobalScope !== 'undefined' && GlobalScope.globalStorage) { // Firefox 2-3 use globalStorage // See https://developer.mozilla.org/en/dom/storage#globalStorage try { - localStorage = globalThis.globalStorage[globalThis.location.hostname]; + localStorage = GlobalScope.globalStorage[GlobalScope.location.hostname]; } catch (e) { // Something bad happened... } diff --git a/src/metadata-storage.js b/src/metadata-storage.js index d044cdc9..c719a992 100644 --- a/src/metadata-storage.js +++ b/src/metadata-storage.js @@ -10,6 +10,7 @@ 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, @@ -88,8 +89,8 @@ class MetadataStorage { switch (this.storage) { case Constants.STORAGE_SESSION: - if (globalThis.sessionStorage) { - globalThis.sessionStorage.setItem(this.storageKey, value); + if (GlobalScope.sessionStorage) { + GlobalScope.sessionStorage.setItem(this.storageKey, value); } break; case Constants.STORAGE_LOCAL: @@ -131,7 +132,7 @@ class MetadataStorage { } if (!str) { try { - str = globalThis.sessionStorage && globalThis.sessionStorage.getItem(this.storageKey); + str = GlobalScope.sessionStorage && GlobalScope.sessionStorage.getItem(this.storageKey); } catch (e) { utils.log.info(`window.sessionStorage unavailable. Reason: "${e}"`); } @@ -187,8 +188,8 @@ class MetadataStorage { } if (!str) { try { - str = globalThis.sessionStorage && globalThis.sessionStorage.getItem(this.storageKey); - globalThis.sessionStorage.clear(); + str = GlobalScope.sessionStorage && GlobalScope.sessionStorage.getItem(this.storageKey); + GlobalScope.sessionStorage.clear(); } catch (e) { utils.log.info(`window.sessionStorage unavailable. Reason: "${e}"`); } diff --git a/src/utils.js b/src/utils.js index 6a744e1f..a163576e 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,4 +1,5 @@ import constants from './constants'; +import GlobalScope from './global-scope'; import type from './type'; var logLevels = { @@ -54,7 +55,7 @@ var isEmptyString = function isEmptyString(str) { var sessionStorageEnabled = function sessionStorageEnabled() { try { - if (globalThis.sessionStorage) { + if (GlobalScope.sessionStorage) { return true; } } catch (e) { @@ -274,7 +275,7 @@ var getQueryParam = function getQueryParam(name, query) { }; const isWebWorkerEnvironment = () => { - return typeof WorkerglobalThis !== 'undefined'; + return typeof WorkerGlobalScope !== 'undefined'; }; export default { diff --git a/src/xhr.js b/src/xhr.js index a2c3136a..8a532314 100644 --- a/src/xhr.js +++ b/src/xhr.js @@ -1,4 +1,5 @@ import queryString from 'query-string'; +import GlobalScope from './global-scope'; /* * Simple AJAX request object @@ -16,9 +17,9 @@ function setHeaders(xhr, headers) { } Request.prototype.send = function (callback) { - var isIE = globalThis.XDomainRequest ? true : false; + var isIE = GlobalScope.XDomainRequest ? true : false; if (isIE) { - var xdr = new globalThis.XDomainRequest(); + var xdr = new GlobalScope.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 new file mode 100644 index 00000000..4906f590 --- /dev/null +++ b/test/global-scope.js @@ -0,0 +1,7 @@ +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 c85df27f..4d48df49 100644 --- a/test/tests.js +++ b/test/tests.js @@ -17,3 +17,4 @@ import './base64Id.js'; import './server-zone.js'; import './config-manager.js'; import './worker-storage.js'; +import './global-scope.js'; From 910b1688a55b86a369d4ba4598481a22848aefb4 Mon Sep 17 00:00:00 2001 From: Matt Lubner Date: Wed, 22 Dec 2021 16:27:13 -0800 Subject: [PATCH 5/5] fix: global-scope reference error in nodejs --- src/global-scope.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/global-scope.js b/src/global-scope.js index f7aadec0..efa9efc5 100644 --- a/src/global-scope.js +++ b/src/global-scope.js @@ -1,2 +1,17 @@ -const GlobalScope = typeof window !== 'undefined' ? window : self; +/* global globalThis */ +const GlobalScope = (() => { + if (typeof globalThis !== 'undefined') { + return globalThis; + } + if (typeof window !== 'undefined') { + return window; + } + if (typeof self !== 'undefined') { + return self; + } + if (typeof global !== 'undefined') { + return global; + } +})(); + export default GlobalScope;