Skip to content

Commit bf259d6

Browse files
committed
chore: add docs
1 parent 5f14b24 commit bf259d6

File tree

1 file changed

+38
-13
lines changed

1 file changed

+38
-13
lines changed

globals.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,58 @@ import { Buffer } from '@craftzdog/react-native-buffer'
1212
import { Crypto } from '@peculiar/webcrypto'
1313
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
1414

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
1724

1825
/**
1926
* CustomEvent is a standard event but it's not supported by react-native
2027
*
2128
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
2229
* Ref: https://github.com/facebook/react-native/issues/38004
2330
*/
24-
class CustomEventPolyfill extends Event {
31+
global.CustomEvent = global.CustomEvent ?? class CustomEventPolyfill extends Event {
2532
constructor (message, data) {
2633
super(message, data)
2734

2835
this.detail = data?.detail
2936
}
3037
}
3138

32-
global.CustomEvent = global.CustomEvent ?? CustomEventPolyfill
33-
39+
/**
40+
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/timeout_static
41+
*/
3442
global.AbortSignal.timeout = global.AbortSignal.timeout ?? ((ms) => {
3543
const controller = new AbortController()
3644
setTimeout(() => {
3745
controller.abort(new Error('Aborted'))
3846
}, ms)
3947
})
48+
49+
/**
50+
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal/throwIfAborted
51+
*/
4052
global.AbortSignal.prototype.throwIfAborted = global.AbortSignal.prototype.throwIfAborted ?? (() => {
4153
if (this.aborted) {
4254
throw new Error('Aborted')
4355
}
4456
})
4557

58+
/**
59+
* Some older Node.js modules use `Buffer` from the global scope instead of
60+
* importing it from `node:buffer`.
61+
*/
4662
global.Buffer = global.Buffer ?? Buffer
63+
64+
/**
65+
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/Crypto
66+
*/
4767
global.crypto = global.crypto ?? {}
4868
global.crypto.subtle = global.crypto.subtle ?? new Crypto().subtle
4969

@@ -68,8 +88,19 @@ global.Promise.withResolvers = global.Promise.withResolvers ?? (() => {
6888
}
6989
})
7090

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+
*/
73104
if (global.__fbBatchedBridge) {
74105
const origMessageQueue = global.__fbBatchedBridge;
75106
const modules = origMessageQueue._remoteModuleTable;
@@ -78,9 +109,3 @@ if (global.__fbBatchedBridge) {
78109
console.log(`The problematic line code is in: ${modules[moduleId]}.${methods[moduleId][methodId]}`)
79110
}
80111
}
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

Comments
 (0)