@@ -12,38 +12,58 @@ import { Buffer } from '@craftzdog/react-native-buffer'
12
12
import { Crypto } from '@peculiar/webcrypto'
13
13
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
14
14
15
- global . EventTarget = EventTarget
16
- global . Event = Event
15
+ /**
16
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget
17
+ */
18
+ global . EventTarget = global . EventTarget ?? EventTarget
19
+
20
+ /**
21
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/Event
22
+ */
23
+ global . Event = global . Event ?? Event
17
24
18
25
/**
19
26
* CustomEvent is a standard event but it's not supported by react-native
20
27
*
21
28
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
22
29
* Ref: https://github.com/facebook/react-native/issues/38004
23
30
*/
24
- class CustomEventPolyfill extends Event {
31
+ global . CustomEvent = global . CustomEvent ?? class CustomEventPolyfill extends Event {
25
32
constructor ( message , data ) {
26
33
super ( message , data )
27
34
28
35
this . detail = data ?. detail
29
36
}
30
37
}
31
38
32
- global . CustomEvent = global . CustomEvent ?? CustomEventPolyfill
33
-
39
+ /**
40
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static
41
+ */
34
42
global . AbortSignal . timeout = global . AbortSignal . timeout ?? ( ( ms ) => {
35
43
const controller = new AbortController ( )
36
44
setTimeout ( ( ) => {
37
45
controller . abort ( new Error ( 'Aborted' ) )
38
46
} , ms )
39
47
} )
48
+
49
+ /**
50
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/throwIfAborted
51
+ */
40
52
global . AbortSignal . prototype . throwIfAborted = global . AbortSignal . prototype . throwIfAborted ?? ( ( ) => {
41
53
if ( this . aborted ) {
42
54
throw new Error ( 'Aborted' )
43
55
}
44
56
} )
45
57
58
+ /**
59
+ * Some older Node.js modules use `Buffer` from the global scope instead of
60
+ * importing it from `node:buffer`.
61
+ */
46
62
global . Buffer = global . Buffer ?? Buffer
63
+
64
+ /**
65
+ * Ref: https://developer.mozilla.org/en-US/docs/Web/API/Crypto
66
+ */
47
67
global . crypto = global . crypto ?? { }
48
68
global . crypto . subtle = global . crypto . subtle ?? new Crypto ( ) . subtle
49
69
@@ -68,8 +88,19 @@ global.Promise.withResolvers = global.Promise.withResolvers ?? (() => {
68
88
}
69
89
} )
70
90
71
- // this is not necessary for your app to run, but it helps when tracking down
72
- // broken polyfill modules
91
+ /**
92
+ * Sometimes this is undefined, though perhaps only in the simulator
93
+ *
94
+ * Ref: https://github.com/craftzdog/react-native-quick-base64/issues/25
95
+ */
96
+ global . base64FromArrayBuffer = global . base64FromArrayBuffer ?? ( ( buf ) => {
97
+ return uint8ArrayToString ( new Uint8Array ( buf , 0 , buf . byteLength ) , 'base64' )
98
+ } )
99
+
100
+ /**
101
+ * this is not necessary for your app to run, but it helps when tracking down
102
+ * broken polyfill modules
103
+ */
73
104
if ( global . __fbBatchedBridge ) {
74
105
const origMessageQueue = global . __fbBatchedBridge ;
75
106
const modules = origMessageQueue . _remoteModuleTable ;
@@ -78,9 +109,3 @@ if (global.__fbBatchedBridge) {
78
109
console . log ( `The problematic line code is in: ${ modules [ moduleId ] } .${ methods [ moduleId ] [ methodId ] } ` )
79
110
}
80
111
}
81
-
82
- // sometimes this is undefined, though perhaps only in the simulator
83
- // ref: https://github.com/craftzdog/react-native-quick-base64/issues/25
84
- global . base64FromArrayBuffer = global . base64FromArrayBuffer ?? ( ( buf ) => {
85
- return uint8ArrayToString ( new Uint8Array ( buf , 0 , buf . byteLength ) , 'base64' )
86
- } )
0 commit comments