forked from rainbow-me/rainbow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shim.js
157 lines (134 loc) Β· 5.12 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
import 'react-native-get-random-values';
import 'react-native-url-polyfill/auto';
import '@ethersproject/shims';
import AsyncStorage from '@react-native-async-storage/async-storage';
import ReactNative from 'react-native';
import Storage from 'react-native-storage';
// import { debugLayoutAnimations } from './src/config/debug';
import { mmkvStorageBackend } from '@/handlers/localstorage/mmkvStorageBackend';
import { logger } from '@/logger';
import 'fast-text-encoding';
import globalVariables from './globalVariables';
if (typeof BigInt === 'undefined') global.BigInt = require('big-integer');
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');
};
}
// 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;
}
const storage = new Storage({
defaultExpires: null,
size: 10000,
// TODO (RNBW-3969): Migrate to mmkv on iOS too
storageBackend: ReactNative.Platform.OS === 'ios' ? AsyncStorage : mmkvStorageBackend,
});
if (ReactNative.Platform.OS === 'android') {
ReactNative.UIManager.setLayoutAnimationEnabledExperimental?.(true);
}
global.storage = storage;
for (const [key, value] of Object.entries(globalVariables)) {
Object.defineProperty(global, key, {
get: () => value,
set: () => {
logger.debug(`[shim]: Trying to override internal Rainbow var ${key}`);
},
});
}
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 (const 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.debug('[shim]: 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.debug(`[shim]: 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,
});
}