fix(lib/modules): only do hot blacklisting, fix bad module checks

This commit is contained in:
PalmDevs 2025-04-27 14:42:24 +07:00
parent f3f9dbbd17
commit 727afef487
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
3 changed files with 9 additions and 45 deletions

View file

@ -53,21 +53,21 @@ export type FilterResultFlag = (typeof FilterResultFlags)[keyof typeof FilterRes
// Currently, we only have options that are relevant for checking exports
// If we add more options later on, do NOT forget about
// adding them here, and passing them in the lookup* functions
export function runFilter(filter: Filter<any, false>, id: Metro.ModuleID): FilterResultFlag | false
export function runFilter(filter: Filter<any, false>, id: Metro.ModuleID): FilterResultFlag | undefined
export function runFilter(
filter: Filter,
id: Metro.ModuleID,
exports: Metro.ModuleExports,
options?: RunFilterOptions,
): FilterResultFlag | false
): FilterResultFlag | undefined
export function runFilter(
filter: Filter,
id: Metro.ModuleID,
exports?: Metro.ModuleExports,
options?: RunFilterOptions,
): FilterResultFlag | false {
): FilterResultFlag | undefined {
if (exports?.__esModule) {
const { default: defaultExport } = exports
@ -86,7 +86,7 @@ export function runFilter(
return FilterResultFlags.Found
}
return false
return
}
export function exportsFromFilterResultFlag(

View file

@ -38,7 +38,6 @@ export function patchMetroDefine(metroDefine: Metro.DefineFn) {
// Add the module to the initialized set only if the factory doesn't error or the exports aren't bad
_mInited.add(id)
} catch {
_blacklist(id)
} finally {
_mInitingId = prevIId
_mUninited.delete(id)
@ -51,38 +50,3 @@ export function patchMetroDefine(metroDefine: Metro.DefineFn) {
)
}) satisfies Metro.DefineFn
}
/**
* Blacklists a module. A module can be blacklisted for several reasons:
*
* - If the module is already initialized, it either means:
* - The module factory threw an error during initialization
* - The module exports are bad after initialization
* - Otherwise, this module is known to be bad from previous caches
*
* @param id The module ID to blacklist.
* @internal
*/
export function _blacklist(id: Metro.ModuleID) {
// In case blacklisting happens before the module is initialized
_mUninited.delete(id)
// Usually we'd also remove the module from _mInited too,
// but we already have an else block in the patchMetroDefine function that does that for us.
// _mInited.delete(id)
// TODO(modules): caching
}
/// BLACKLIST RESTORATION
if (globalThis.__REVENGE_CACHE_MODULE_BLACKLIST__)
for (const id of globalThis.__REVENGE_CACHE_MODULE_BLACKLIST__) _blacklist(id)
// @ts-expect-error
// biome-ignore lint/performance/noDelete: no
delete globalThis.__REVENGE_CACHE_MODULE_BLACKLIST__
declare namespace globalThis {
const __REVENGE_CACHE_MODULE_BLACKLIST__: Metro.ModuleID[] | undefined
}

View file

@ -58,10 +58,7 @@ export function isModuleExportBad(exp: Metro.ModuleExports[PropertyKey]): boolea
// Nullish?
exp == null ||
// Is it a proxy? (discord-intl has proxy exports)
isProxy(exp) ||
// Can't run isProxy() on this because this isn't your typical proxy:
// https://github.com/discord/react-native/blob/master/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp
exp === nativeModuleProxy
isProxy(exp)
)
}
@ -81,6 +78,9 @@ export function isModuleExportsBad(exports: Metro.ModuleExports): boolean {
// - String, Boolean, Symbol, BigInt exports are not useful (who would do `module.exports = ...`?)
!(typeof exports === 'object' || typeof exports === 'function') ||
// Checking if the object is empty
(exports.__proto__ === Object.prototype && !Reflect.ownKeys(exports).length)
(exports.__proto__ === Object.prototype && !Reflect.ownKeys(exports).length) ||
// Can't run isProxy() on this because this isn't your typical proxy:
// https://github.com/discord/react-native/blob/master/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp
exports === nativeModuleProxy
)
}