chore: cleanup questionable code

This commit is contained in:
PalmDevs 2025-07-11 15:31:53 +07:00
parent 07c3425693
commit c66dace21b
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
10 changed files with 28 additions and 101 deletions

View file

@ -87,7 +87,7 @@ export function getModule(
if (max === 1) {
const [exports, id] = lookupModule(filter, options!)
if (id != null) {
if (id !== undefined) {
callback(exports, id)
return noopFalse
}
@ -131,7 +131,7 @@ export function getModuleByImportedPath<T>(
callback: GetModuleCallback<T>,
): GetModuleUnsubscribeFunction {
const [exports, id] = lookupModuleByImportedPath(path)
if (id != null) {
if (id !== undefined) {
callback(exports, id)
return noopFalse
}

View file

@ -2,9 +2,9 @@ import { getCurrentStack } from '@revenge-mod/utils/errors'
import { proxify } from '@revenge-mod/utils/proxy'
import { cache } from '../caches'
import {
mDeps,
mImportedPaths,
mInitialized,
mList,
mUninitialized,
} from '../metro/_internal'
import {
@ -194,7 +194,7 @@ export function* lookupModules(filter: Filter, options?: LookupModulesOptions) {
// Full lookup
if (options?.all) {
for (let id = 0; id < mDeps.length; id++) {
for (const id of mList.keys()) {
if (cached?.has(id)) continue
const exports = getInitializedModuleExports(id)
@ -318,7 +318,7 @@ export function lookupModule(filter: Filter, options?: LookupModulesOptions) {
// Full lookup
if (options?.all) {
for (let id = 0; id < mDeps.length; id++) {
for (const id of mList.keys()) {
const exports = getInitializedModuleExports(id)
const flag = runFilter(filter, id, exports, options)
if (flag) {

View file

@ -160,7 +160,7 @@ export function waitForModuleByImportedPath<T = any>(
}
}
: (id, cmpPath) => {
if (path === cmpPath && mInitialized.has(id)) {
if (mInitialized.has(id) && path === cmpPath) {
unsub()
callback(getInitializedModuleExports(id), id)
}

View file

@ -89,7 +89,7 @@ function handleFactoryCall(
factory: Metro.FactoryFn,
moduleObject: Metro.Module,
) {
const prevIId = mInitializingId
const prevId = mInitializingId
mInitializingId = moduleObject.id!
executeRequireSubscriptions(mInitializingId)
@ -105,16 +105,13 @@ function handleFactoryCall(
mDeps[mInitializingId],
)
const { exports: actualExports } = moduleObject
const { exports } = moduleObject
if (exports != null) mInitialized.add(mInitializingId)
// Add the module to the initialized set only if the factory doesn't error or the exports aren't bad
// Don't use exports here, as modules can set module.exports to a different object
if (actualExports != null) mInitialized.add(mInitializingId)
executeInitializeSubscriptions(mInitializingId, actualExports)
executeInitializeSubscriptions(mInitializingId, exports)
} finally {
mUninitialized.delete(mInitializingId)
mInitializingId = prevIId
mInitializingId = prevId
}
}

View file

@ -1,77 +1,12 @@
import { objectDefineProperty } from './patches/objects'
import type { AnyObject } from './types'
// Heavily inspired by uwu/shelter
// https://github.com/uwu/shelter/blob/main/packages/shelter/src/exfiltrate.ts
// Patch to fix intercepting properties on Object.prototype
Object.defineProperty = (target, p, d) => {
if (target === Object.prototype && _intercepting.has(p)) {
_intercepting.set(p, d)
return target
}
return objectDefineProperty(target, p, d)
}
const _intercepting = new Map<PropertyKey, PropertyDescriptor | undefined>()
export type InterceptPropertyCallback = (target: any, value: any) => any
/**
* Intercept a property when set on an object to modify its value. **Only one callback can be set for a property at a time.**
*
* @param prop The property to intercept.
* @param callback The callback to call when the property is set, can return a non-null value to set the property to.
* @returns The function to unintercept the property.
*/
export function interceptProperty(
prop: PropertyKey,
callback: (target: object, value: any) => any,
) {
if (_intercepting.has(prop))
throw new Error(`Property ${String(prop)} is already being intercepted`)
const proto = Object.prototype
_intercepting.set(prop, Object.getOwnPropertyDescriptor(proto, prop))
objectDefineProperty(proto, prop, {
configurable: true,
set(value) {
if (this === proto) {
_intercepting.set(prop, { configurable: true, value })
return
}
objectDefineProperty(this, prop, {
configurable: true,
writable: true,
enumerable: true,
value: callback(this, value) ?? value,
})
},
get() {
const desc = _intercepting.get(prop)
return desc && (desc.value ?? desc.get?.call(this))
},
})
return () => {
// @ts-expect-error
delete proto[prop]
const desc = _intercepting.get(prop)
if (desc) objectDefineProperty(proto, prop, desc)
return _intercepting.delete(prop)
}
}
/**
* Simple check if to see if value is an object.
*
* @param val The value to check.
*/
export function isObject(val: any): val is AnyObject {
return val && typeof val === 'object' && !Array.isArray(val)
return val?.__proto__ === Object.prototype
}
/**

View file

@ -1,6 +1,5 @@
// Retain reference to original functions
export const objectFreeze = Object.freeze
export const objectDefineProperty = Object.defineProperty
// Patch to prevent the use of Object.freeze
Object.freeze = (o: any) => o

View file

@ -1,10 +1,9 @@
export const _targets = new WeakMap<object, object>()
export const pTargets = new WeakMap<object, object>()
const OriginalProxy = globalThis.Proxy
globalThis.Proxy = new Proxy(OriginalProxy, {
construct(_target, args) {
const prox = new OriginalProxy(args[0], args[1])
_targets.set(prox, args[0])
globalThis.Proxy = new Proxy(globalThis.Proxy, {
construct(target, args) {
const prox = Reflect.construct(target, args)
pTargets.set(prox, args[0])
return prox
},
})

View file

@ -4,7 +4,7 @@
*/
import { getCurrentStack } from './errors'
import { _targets } from './patches/proxy'
import { pTargets } from './patches/proxy'
/**
* Returns whether the object is a proxy.
@ -12,7 +12,7 @@ import { _targets } from './patches/proxy'
* @param obj The object to check
*/
export function isProxy(obj: object) {
return _targets.has(obj)
return pTargets.has(obj)
}
/**
@ -31,7 +31,7 @@ export function isProxified(obj: object) {
* @returns The target of the proxy
*/
export function getProxyTarget(obj: object) {
return _targets.get(obj)
return pTargets.get(obj)
}
// Heavily inspired by Wintry's lazy utils, but more optimized and stripped down, with a few fixes.

View file

@ -4,16 +4,12 @@ import type { ElementType } from 'react'
const { jsx: _jsx, jsxs: _jsxs, Fragment } = ReactJSXRuntime
const jsx = ((type, ...args) =>
_jsx(unproxy(type as object), ...args)) as typeof _jsx
const jsx: typeof _jsx = (type, props, key) => _jsx(unproxy(type), props, key)
const jsxs: typeof _jsxs = (type, props, key) =>
_jsxs(unproxy(type), props, key)
const jsxs = ((type, ...args) =>
_jsxs(unproxy(type as object), ...args)) as typeof _jsxs
function unproxy(type: object): ElementType {
return (isProxified(type)
? unproxify(type)
: type) as unknown as ElementType
function unproxy(type: any): ElementType {
return isProxified(type) ? unproxify(type) : type
}
module.exports = { jsx, jsxs, Fragment }

View file

@ -6,7 +6,7 @@ import { instead } from '@revenge-mod/patcher'
import { InternalPluginFlags, registerPlugin } from '@revenge-mod/plugins/_'
import { PluginFlags } from '@revenge-mod/plugins/constants'
import { noop } from '@revenge-mod/utils/callbacks'
import { getErrorStack } from '@revenge-mod/utils/errors'
import { getCurrentStack } from '@revenge-mod/utils/errors'
// TODO(plugins/no-track): Block Sentry native-side
registerPlugin(
@ -57,6 +57,7 @@ registerPlugin(
// Discord uses ReactNavigationInstrumentation to track navigation
// Discord also uses Profiler to track performance, but we blocked that by patching profiledRootComponent
// Note that to actually find Sentry, we just need to add 'init' to byProps
const unsubSentryInst = waitForModules(
byProps('ReactNavigationInstrumentation'),
exports => {
@ -75,7 +76,7 @@ registerPlugin(
if (__DEV__)
Object.defineProperty(globalThis, '__SENTRY__', {
set: () => warnSetSentry(getErrorStack(new Error())!),
set: () => warnSetSentry(getCurrentStack()),
})
else
Object.defineProperty(globalThis, '__SENTRY__', {