-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnativeModuleInit.js
More file actions
executable file
·188 lines (164 loc) · 6.63 KB
/
Copy pathnativeModuleInit.js
File metadata and controls
executable file
·188 lines (164 loc) · 6.63 KB
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
/**
* Native Module Initialization
* This file ensures that all native modules are properly initialized before use
*/
// CRITICAL FIX: Use require instead of import to avoid Hermes issues
const ReactNative = require('react-native');
const { NativeModules, LogBox } = ReactNative;
// Store NativeModules in global for access across the app
if (typeof global !== 'undefined') {
global.ReactNativeModules = NativeModules || {};
console.log('Stored NativeModules in global.ReactNativeModules');
}
// Ignore specific warnings related to native modules
LogBox.ignoreLogs([
'NativeModule',
'Required dispatch_sync to load constants',
'RCTBridge required dispatch_sync',
'Module RNLlama requires',
'Module ExpoFileSystem requires',
'Module ExpoCrypto requires',
'Tried to insert a NativeModule',
'View #579 of type RCTView has a shadow set but cannot calculate shadow efficiently', // Shadow performance warning
]);
// Log the current platform
console.log('Platform: android');
// Check if we're running in Hermes
const isHermes = () => typeof global !== 'undefined' && !!global.HermesInternal;
console.log('Running in Hermes:', isHermes());
// Log available native modules
console.log('Available NativeModules in nativeModuleInit:', NativeModules ? Object.keys(NativeModules).length : 0);
const initializeNativeModules = () => {
// Check for Hermes and log its status
if (isHermes()) {
console.log('Hermes is enabled, ensuring compatibility...');
}
// CRITICAL FIX: Get NativeModules from global if available
let SafeNativeModules;
if (typeof global !== 'undefined' && global.ReactNativeModules) {
console.log('Using NativeModules from global.ReactNativeModules');
SafeNativeModules = global.ReactNativeModules;
} else {
// Ensure NativeModules is defined and initialized
if (!NativeModules) {
console.error('NativeModules is undefined! This may indicate a deeper issue with the React Native bridge initialization or timing.');
// Try to get NativeModules directly from react-native
try {
const reactNative = require('react-native');
SafeNativeModules = reactNative.NativeModules || {};
console.log('Got NativeModules directly from require("react-native")');
} catch (error) {
console.error('Failed to get NativeModules from require("react-native"):', error);
SafeNativeModules = {};
}
} else {
// Create a safe version of NativeModules that won't crash if modules are missing
SafeNativeModules = { ...(NativeModules || {}) };
}
}
// Log all available native modules for debugging
console.log('Available NativeModules:', Object.keys(SafeNativeModules).length);
// Create a safe getter for native modules that won't throw if the module is missing
const getSafeModule = (moduleName) => {
try {
return SafeNativeModules[moduleName];
} catch (error) {
console.warn(`Error accessing ${moduleName}:`, error);
return null;
}
};
// Check if specific modules are available using the safe getter
const ExpoFileSystemModule = getSafeModule('ExpoFileSystem');
const ExpoCryptoModule = getSafeModule('ExpoCrypto');
const RNLlamaModule = getSafeModule('RNLlama');
const hasExpoFileSystem = !!ExpoFileSystemModule;
const hasExpoCrypto = !!ExpoCryptoModule;
const hasLlama = !!RNLlamaModule;
console.log('ExpoFileSystem available:', hasExpoFileSystem);
console.log('ExpoCrypto available:', hasExpoCrypto);
console.log('RNLlama available:', hasLlama);
// Create fallbacks for missing modules
const ExpoFileSystemFallback = {
documentDirectory: 'file:///data/user/0/org.darkgridai.app/files/',
cacheDirectory: 'file:///data/user/0/org.darkgridai.app/cache/',
getInfoAsync: () => Promise.resolve({ exists: false }),
readAsStringAsync: () => Promise.resolve(''),
writeAsStringAsync: () => Promise.resolve(),
deleteAsync: () => Promise.resolve(),
createDownloadResumable: () => ({
downloadAsync: () => Promise.resolve({ uri: '' })
})
};
const ExpoCryptoFallback = {
digestStringAsync: () => Promise.resolve(''),
CryptoDigestAlgorithm: { SHA256: 'sha256' }
};
const RNLlamaFallback = {
initLlama: () => Promise.resolve({}),
completion: () => Promise.resolve({ text: 'Error: Model failed to initialize' }),
release: () => {}
};
// Create safe versions of the modules
const SafeExpoFileSystem = hasExpoFileSystem ? ExpoFileSystemModule : ExpoFileSystemFallback;
const SafeExpoCrypto = hasExpoCrypto ? ExpoCryptoModule : ExpoCryptoFallback;
const SafeRNLlama = hasLlama ? RNLlamaModule : RNLlamaFallback;
// Create a safe version of NativeModules with our fallbacks
// Use a try-catch block to handle potential errors when creating the enhanced modules object
let EnhancedNativeModules;
try {
EnhancedNativeModules = {
...SafeNativeModules,
ExpoFileSystem: SafeExpoFileSystem,
ExpoCrypto: SafeExpoCrypto,
RNLlama: SafeRNLlama
};
console.log('Successfully created EnhancedNativeModules');
// CRITICAL FIX: Store EnhancedNativeModules in global for access across the app
if (typeof global !== 'undefined') {
global.EnhancedNativeModules = EnhancedNativeModules;
console.log('Stored EnhancedNativeModules in global.EnhancedNativeModules');
}
} catch (error) {
console.error('Error creating EnhancedNativeModules:', error);
// Create a minimal fallback if the above fails
EnhancedNativeModules = {
ExpoFileSystem: ExpoFileSystemFallback,
ExpoCrypto: ExpoCryptoFallback,
RNLlama: RNLlamaFallback
};
// CRITICAL FIX: Store fallback EnhancedNativeModules in global
if (typeof global !== 'undefined') {
global.EnhancedNativeModules = EnhancedNativeModules;
console.log('Stored fallback EnhancedNativeModules in global.EnhancedNativeModules');
}
}
// Create a safe check function that won't throw errors
const safeCheckNativeModules = () => {
try {
return {
expoFileSystem: hasExpoFileSystem,
expoCrypto: hasExpoCrypto,
llama: hasLlama
};
} catch (error) {
console.error('Error in checkNativeModules:', error);
return {
expoFileSystem: false,
expoCrypto: false,
llama: false
};
}
};
// Return the enhanced modules and the check function
return {
EnhancedNativeModules,
checkNativeModules: safeCheckNativeModules
};
};
// Initialize the modules immediately
const { EnhancedNativeModules, checkNativeModules } = initializeNativeModules();
// Export the initialized modules and check function using CommonJS
module.exports = {
default: EnhancedNativeModules,
checkNativeModules
};