2025-04-11 18:38:38 +07:00
|
|
|
import { isProxy } from '@revenge-mod/utils/proxy'
|
2025-05-09 19:05:31 +07:00
|
|
|
import { _inits, _metas, _uninits } from './_internal'
|
|
|
|
|
import type { Metro } from '../types'
|
2025-04-11 18:38:38 +07:00
|
|
|
|
|
|
|
|
/**
|
2025-04-16 14:23:48 +07:00
|
|
|
* Returns whether an uninitialized module has bad exports.
|
2025-04-15 15:06:48 +07:00
|
|
|
*
|
2025-04-16 14:23:48 +07:00
|
|
|
* @see {@link isModuleExportsBad} for more information on what is considered bad module exports.
|
2025-04-15 15:06:48 +07:00
|
|
|
*
|
|
|
|
|
* @param id The module ID.
|
2025-04-11 18:38:38 +07:00
|
|
|
*/
|
2025-04-16 14:23:48 +07:00
|
|
|
export function uninitializedModuleHasBadExports(id: Metro.ModuleID): boolean {
|
2025-05-09 19:05:31 +07:00
|
|
|
return !_uninits.has(id)
|
2025-04-11 18:38:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-16 14:23:48 +07:00
|
|
|
* Returns whether an initialized module has bad exports.
|
2025-04-15 15:06:48 +07:00
|
|
|
*
|
2025-04-16 14:23:48 +07:00
|
|
|
* @see {@link isModuleExportsBad} for more information on what is considered bad module exports.
|
2025-04-15 15:06:48 +07:00
|
|
|
*
|
|
|
|
|
* @param id The module ID.
|
2025-04-11 18:38:38 +07:00
|
|
|
*/
|
2025-04-16 14:23:48 +07:00
|
|
|
export function initializedModuleHasBadExports(id: Metro.ModuleID): boolean {
|
2025-05-09 19:05:31 +07:00
|
|
|
return !_inits.has(id)
|
2025-04-11 18:38:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the dependencies of a module.
|
|
|
|
|
* @param id The module ID.
|
|
|
|
|
*/
|
2025-06-03 16:24:58 +07:00
|
|
|
export function getModuleDependencies(
|
|
|
|
|
id: Metro.ModuleID,
|
|
|
|
|
): Metro.DependencyMap | undefined {
|
2025-05-09 19:05:31 +07:00
|
|
|
return _metas.get(id)?.[0]
|
2025-04-11 18:38:38 +07:00
|
|
|
}
|
|
|
|
|
|
2025-04-16 14:23:48 +07:00
|
|
|
/**
|
|
|
|
|
* Returns whether a module is initialized.
|
|
|
|
|
* @param id The module ID.
|
|
|
|
|
*/
|
|
|
|
|
export function isModuleInitialized(id: Metro.ModuleID): boolean {
|
2025-05-09 19:05:31 +07:00
|
|
|
return _metas.get(id)?.[1]!
|
2025-04-16 14:23:48 +07:00
|
|
|
}
|
|
|
|
|
|
2025-04-11 18:38:38 +07:00
|
|
|
/**
|
|
|
|
|
* Returns the exports of an initialized module.
|
|
|
|
|
* @param id The module ID.
|
|
|
|
|
*/
|
2025-06-03 16:24:58 +07:00
|
|
|
export function getInitializedModuleExports(
|
|
|
|
|
id: Metro.ModuleID,
|
|
|
|
|
): Metro.ModuleExports | undefined {
|
2025-05-09 19:05:31 +07:00
|
|
|
return _metas.get(id)?.[2]?.exports
|
2025-04-11 18:38:38 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether a particular module export is bad. This is used for filter functions to check whether an export is filterable.
|
|
|
|
|
* @param exp The export to check.
|
|
|
|
|
*/
|
2025-06-03 16:24:58 +07:00
|
|
|
export function isModuleExportBad(
|
|
|
|
|
exp: Metro.ModuleExports[PropertyKey],
|
|
|
|
|
): boolean {
|
2025-04-11 18:38:38 +07:00
|
|
|
return (
|
|
|
|
|
// Nullish?
|
|
|
|
|
exp == null ||
|
|
|
|
|
// Is it a proxy? (discord-intl has proxy exports)
|
2025-04-27 14:42:24 +07:00
|
|
|
isProxy(exp)
|
2025-04-11 18:38:38 +07:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-04-16 14:23:48 +07:00
|
|
|
* Returns whether the module has bad exports. If it does, it will be "blacklisted" to avoid filtering issues.
|
2025-04-15 15:06:48 +07:00
|
|
|
*
|
|
|
|
|
* **Which module exports are considered bad?** Anything not an object or function, or an empty object.
|
|
|
|
|
*
|
2025-04-11 18:38:38 +07:00
|
|
|
* @param exports The exports of the module.
|
|
|
|
|
*/
|
2025-04-15 15:06:48 +07:00
|
|
|
export function isModuleExportsBad(exports: Metro.ModuleExports): boolean {
|
2025-04-11 18:38:38 +07:00
|
|
|
return (
|
|
|
|
|
// Nullish?
|
|
|
|
|
exports == null ||
|
2025-04-15 15:06:48 +07:00
|
|
|
// Isn't an object or function?
|
|
|
|
|
// - Number exports are not useful, usually just an asset ID
|
|
|
|
|
// - String, Boolean, Symbol, BigInt exports are not useful (who would do `module.exports = ...`?)
|
2025-04-16 16:17:43 +07:00
|
|
|
!(typeof exports === 'object' || typeof exports === 'function') ||
|
2025-04-11 18:38:38 +07:00
|
|
|
// Checking if the object is empty
|
2025-06-03 16:24:58 +07:00
|
|
|
(exports.__proto__ === Object.prototype &&
|
|
|
|
|
!Reflect.ownKeys(exports).length) ||
|
2025-04-27 14:42:24 +07:00
|
|
|
// Can't run isProxy() on this because this isn't your typical proxy:
|
2025-05-09 19:05:31 +07:00
|
|
|
// https://github.com/facebook/react-native/blob/master/packages/react-native/ReactCommon/react/nativemodule/core/ReactCommon/TurboModuleBinding.cpp
|
2025-04-27 14:42:24 +07:00
|
|
|
exports === nativeModuleProxy
|
2025-04-11 18:38:38 +07:00
|
|
|
)
|
|
|
|
|
}
|