From 93381714dde65c2ab74b11b025eb5ca04905c3cf Mon Sep 17 00:00:00 2001 From: legendecas Date: Mon, 8 Nov 2021 16:57:52 +0800 Subject: [PATCH] fix: align globalThis fallbacks with otel-core (#126) Co-authored-by: Valentin Marchaud --- api/src/platform/browser/globalThis.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/api/src/platform/browser/globalThis.ts b/api/src/platform/browser/globalThis.ts index 34a8254b888..ebea1f48c59 100644 --- a/api/src/platform/browser/globalThis.ts +++ b/api/src/platform/browser/globalThis.ts @@ -14,6 +14,22 @@ * limitations under the License. */ +// Updates to this file should also be replicated to @opentelemetry/api-metrics and +// @opentelemetry/core too. + +/** + * - globalThis (New standard) + * - self (Will return the current window instance for supported browsers) + * - window (fallback for older browser implementations) + * - global (NodeJS implementation) + * - (When all else fails) + */ + /** only globals that common to node and browsers are allowed */ // eslint-disable-next-line node/no-unsupported-features/es-builtins, no-undef -export const _globalThis = typeof globalThis === 'object' ? globalThis : window; +export const _globalThis: typeof globalThis = + typeof globalThis === 'object' ? globalThis : + typeof self === 'object' ? self : + typeof window === 'object' ? window : + typeof global === 'object' ? global : + {} as typeof globalThis;