forked from rainbow-me/rainbow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shim.js
196 lines (171 loc) Β· 5.66 KB
/
shim.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import 'react-native-get-random-values';
import '@ethersproject/shims';
import AsyncStorage from '@react-native-community/async-storage';
import ReactNative from 'react-native';
import Animated from 'react-native-reanimated';
import Storage from 'react-native-storage';
// import { debugLayoutAnimations } from './src/config/debug';
import toLocaleStringPolyfill from '@rainbow-me/helpers/toLocaleStringPolyfill';
import logger from 'logger';
if (typeof btoa === 'undefined') {
global.btoa = function (str) {
return new Buffer(str, 'binary').toString('base64');
};
}
if (typeof atob === 'undefined') {
global.atob = function (b64Encoded) {
return new Buffer(b64Encoded, 'base64').toString('binary');
};
}
toLocaleStringPolyfill();
// https://github.com/facebook/react-native/commit/1049835b504cece42ee43ac5b554687891da1349
// https://github.com/facebook/react-native/commit/035718ba97bb44c68f2a4ccdd95e537e3d28690
if (ReactNative.Keyboard.removeEventListener) {
ReactNative.Keyboard.removeListener =
ReactNative.Keyboard.removeEventListener;
}
ReactNative.Platform.OS === 'ios' &&
Animated.addWhitelistedNativeProps({ d: true });
const storage = new Storage({
defaultExpires: null,
enableCache: ReactNative.Platform.OS === 'ios',
size: 10000,
storageBackend: AsyncStorage,
});
if (ReactNative.Platform.OS === 'android') {
ReactNative.UIManager.setLayoutAnimationEnabledExperimental?.(true);
}
if (
!global.__reanimatedModuleProxy &&
!ReactNative.TurboModuleRegistry.get('NativeReanimated')
) {
global.__reanimatedModuleProxy = {
__shimmed: true,
installCoreFunctions() {},
makeMutable(init) {
return { value: init };
},
makeRemote() {},
makeShareable() {
return () => {};
},
registerEventHandler() {},
startMapper() {},
stopMapper() {},
unregisterEventHandler() {},
};
}
global.storage = storage;
// shimming for reanimated need to happen before importing globalVariables.js
// eslint-disable-next-line import/no-commonjs
for (let variable of Object.entries(require('./globalVariables').default)) {
Object.defineProperty(global, variable[0], {
get: () => variable[1],
set: () => {
logger.sentry(`Trying to override internal Rainbow var ${variable[0]}`);
},
});
}
const SHORTEN_PROP_TYPES_ERROR = true;
if (SHORTEN_PROP_TYPES_ERROR) {
const oldConsoleError = console.error; // eslint-disable-line no-console
// eslint-disable-next-line no-console
console.error = function () {
if (
typeof arguments[0] === 'string' &&
arguments[0].startsWith('Warning: Failed prop type')
) {
// eslint-disable-next-line no-console
console.log(
`PropTypes error in: ${arguments[0]
.match(/\w+.js:[0-9]+/g)
.slice(0, 6)
.join(' in ')}`
);
return;
}
if (
typeof arguments[0] === 'string' &&
arguments[0].startsWith(
'VirtualizedLists should never be nested inside plain ScrollViews'
)
) {
return;
}
oldConsoleError?.apply(this, arguments);
};
}
if (typeof __dirname === 'undefined') global.__dirname = '/';
if (typeof __filename === 'undefined') global.__filename = '';
if (typeof process === 'undefined') {
global.process = require('process');
} else {
const bProcess = require('process');
for (var p in bProcess) {
if (!(p in process)) {
process[p] = bProcess[p];
}
}
}
export const dismissingScreenListener = { current: undefined };
global.__rainbowDismissScreen = () => dismissingScreenListener.current?.();
process.browser = false;
if (typeof Buffer === 'undefined') global.Buffer = require('buffer').Buffer;
// global.location = global.location || { port: 80 }
const isDev = typeof __DEV__ === 'boolean' && __DEV__;
process.env.NODE_ENV = isDev ? 'development' : 'production';
if (typeof localStorage !== 'undefined') {
localStorage.debug = isDev ? '*' : '';
}
ReactNative.LayoutAnimation.configureNext = () => null;
// const oldConfigureNext = ReactNative.LayoutAnimation.configureNext;
// if (
// !ReactNative.LayoutAnimation.configureNext.__shimmed &&
// debugLayoutAnimations
// ) {
// ReactNative.LayoutAnimation.configureNext = (...args) => {
// logger.sentry('LayoutAnimation.configureNext', args);
// oldConfigureNext(...args);
// };
// ReactNative.LayoutAnimation.configureNext.__shimmed = true;
// }
if (!ReactNative.InteractionManager._shimmed) {
const oldCreateInteractionHandle =
ReactNative.InteractionManager.createInteractionHandle;
ReactNative.InteractionManager.createInteractionHandle = function (
finishAutomatically = true
) {
const handle = oldCreateInteractionHandle();
if (finishAutomatically) {
setTimeout(() => {
ReactNative.InteractionManager.clearInteractionHandle(handle);
logger.sentry(`Interaction finished automatically`);
}, 3000);
}
return handle;
};
ReactNative.InteractionManager._shimmed = true;
}
// If using the crypto shim, uncomment the following line to ensure
// crypto is loaded first, so it can populate global.crypto
// eslint-disable-next-line import/no-commonjs
require('crypto');
const description = Object.getOwnPropertyDescriptor(
ReactNative,
'requireNativeComponent'
);
if (!description.writable) {
Object.defineProperty(ReactNative, 'requireNativeComponent', {
value: (function () {
const cache = {};
const _requireNativeComponent = ReactNative.requireNativeComponent;
return function requireNativeComponent(nativeComponent) {
if (!cache[nativeComponent]) {
cache[nativeComponent] = _requireNativeComponent(nativeComponent);
}
return cache[nativeComponent];
};
})(),
writable: true,
});
}