-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathglobals.js
60 lines (51 loc) · 1.81 KB
/
globals.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Shim global JS objects, hopefully this won't be necessary forever
// https://github.com/facebook/hermes/discussions/1072
global.process = global.process ?? {}
global.process.version = 'v20.12.2'
import '@azure/core-asynciterator-polyfill'
import 'react-native-get-random-values'
import 'weakmap-polyfill'
import { TextEncoder, TextDecoder } from 'text-encoding'
import { EventTarget, Event } from 'event-target-shim'
import { Buffer } from '@craftzdog/react-native-buffer'
import { Crypto } from '@peculiar/webcrypto'
global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder
global.EventTarget = EventTarget
global.Event = Event
/**
* CustomEvent is a standard event but it's not supported by react-native
*
* Ref: https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent
* Ref: https://github.com/facebook/react-native/issues/38004
*/
class CustomEventPolyfill extends Event {
constructor (message, data) {
super(message, data)
this.detail = data?.detail
}
}
global.CustomEvent = CustomEventPolyfill
global.AbortSignal.timeout = (ms) => {
const controller = new AbortController()
setTimeout(() => {
controller.abort(new Error('Aborted'))
}, ms)
}
global.AbortSignal.prototype.throwIfAborted = () => {
if (this.aborted) {
throw new Error('Aborted')
}
}
global.Buffer = Buffer
global.crypto.subtle = new Crypto().subtle
// this is not necessary for your app to run, but it helps when
// tracking down broken polyfill modules
if (global.__fbBatchedBridge) {
const origMessageQueue = global.__fbBatchedBridge;
const modules = origMessageQueue._remoteModuleTable;
const methods = origMessageQueue._remoteMethodTable;
global.findModuleByModuleAndMethodIds = (moduleId, methodId) => {
console.log(`The problematic line code is in: ${modules[moduleId]}.${methods[moduleId][methodId]}`)
}
}