@@ -6,26 +6,39 @@ export function getGlobalObject<T = typeof globalThis>(): T {
6
6
if ( typeof globalThis === 'object' ) {
7
7
return globalThis as unknown as T
8
8
}
9
- Object . defineProperty ( Object . prototype , '_dd_temp_' , {
10
- get ( ) {
11
- return this as object
12
- } ,
13
- configurable : true ,
14
- } )
15
- // @ts -ignore _dd_temp is defined using defineProperty
16
- let globalObject : unknown = _dd_temp_
17
- // @ts -ignore _dd_temp is defined using defineProperty
18
- delete Object . prototype . _dd_temp_
19
- if ( typeof globalObject !== 'object' ) {
20
- // on safari _dd_temp_ is available on window but not globally
21
- // fallback on other browser globals check
22
- if ( typeof self === 'object' ) {
23
- globalObject = self
24
- } else if ( typeof window === 'object' ) {
25
- globalObject = window
26
- } else {
27
- globalObject = { }
9
+
10
+ // Polyfill globalThis if it doesn't exist
11
+ if ( typeof globalThis === 'undefined' ) {
12
+ Object . defineProperty ( Object . prototype , '_dd_temp_' , {
13
+ get ( ) {
14
+ return this as object
15
+ } ,
16
+ configurable : true ,
17
+ } )
18
+ // @ts -ignore _dd_temp is defined using defineProperty
19
+ let globalObject : unknown = _dd_temp_
20
+ // @ts -ignore _dd_temp is defined using defineProperty
21
+ delete Object . prototype . _dd_temp_
22
+ if ( typeof globalObject !== 'object' ) {
23
+ // on safari _dd_temp_ is available on window but not globally
24
+ // fallback on other browser globals check
25
+ if ( typeof self === 'object' ) {
26
+ globalObject = self
27
+ } else if ( typeof window === 'object' ) {
28
+ globalObject = window
29
+ } else {
30
+ globalObject = { }
31
+ }
28
32
}
33
+
34
+ // Define globalThis if it doesn't exist
35
+ Object . defineProperty ( globalObject as object , 'globalThis' , {
36
+ value : globalObject ,
37
+ writable : true ,
38
+ enumerable : false ,
39
+ configurable : true ,
40
+ } )
29
41
}
30
- return globalObject as T
42
+
43
+ return globalThis as unknown as T
31
44
}
0 commit comments