Skip to content

Commit

Permalink
feat: replace GlobalScope with globalThis
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlubner committed Dec 22, 2021
1 parent aeed7bb commit 0c07a36
Show file tree
Hide file tree
Showing 15 changed files with 35 additions and 57 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"define": "readonly",
"amplitude": "readonly",
"opera": "readonly",
"ActiveXObject": "readonly"
"ActiveXObject": "readonly",
"globalThis": "readonly"
}
}
9 changes: 4 additions & 5 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -763,7 +762,7 @@ AmplitudeClient.prototype._getReferrer = function _getReferrer() {
* @private
*/
AmplitudeClient.prototype._getUrlParams = function _getUrlParams() {
return GlobalScope.location.search;
return globalThis.location.search;
};

/**
Expand Down Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/base64.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import UTF8 from './utf8';
import GlobalScope from './global-scope';

/*
* Base64 encoder/decoder
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/config-manager.js
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions src/cookiestorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 3 additions & 5 deletions src/get-host.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 1 addition & 3 deletions src/get-location.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import GlobalScope from './global-scope';

const getLocation = () => {
return GlobalScope.location;
return globalThis.location;
};

export default getLocation;
2 changes: 0 additions & 2 deletions src/global-scope.js

This file was deleted.

3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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 || [];

Expand Down
15 changes: 7 additions & 8 deletions src/localstorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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...
}
Expand Down
11 changes: 5 additions & 6 deletions src/metadata-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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}"`);
}
Expand Down Expand Up @@ -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}"`);
}
Expand Down
5 changes: 2 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import constants from './constants';
import GlobalScope from './global-scope';
import type from './type';

var logLevels = {
Expand Down Expand Up @@ -55,7 +54,7 @@ var isEmptyString = function isEmptyString(str) {

var sessionStorageEnabled = function sessionStorageEnabled() {
try {
if (GlobalScope.sessionStorage) {
if (globalThis.sessionStorage) {
return true;
}
} catch (e) {
Expand Down Expand Up @@ -275,7 +274,7 @@ var getQueryParam = function getQueryParam(name, query) {
};

const isWebWorkerEnvironment = () => {
return typeof WorkerGlobalScope !== 'undefined';
return typeof WorkerglobalThis !== 'undefined';
};

export default {
Expand Down
5 changes: 2 additions & 3 deletions src/xhr.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import queryString from 'query-string';
import GlobalScope from './global-scope';

/*
* Simple AJAX request object
Expand All @@ -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);
Expand Down
7 changes: 0 additions & 7 deletions test/global-scope.js

This file was deleted.

1 change: 0 additions & 1 deletion test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ import './base64Id.js';
import './server-zone.js';
import './config-manager.js';
import './worker-storage.js';
import './global-scope.js';

0 comments on commit 0c07a36

Please sign in to comment.