From c66dace21ba6584fc2d77f7a014e168ee66eb25e Mon Sep 17 00:00:00 2001 From: PalmDevs Date: Fri, 11 Jul 2025 15:31:53 +0700 Subject: [PATCH] chore: cleanup questionable code --- lib/modules/src/finders/get.ts | 4 +- lib/modules/src/finders/lookup.ts | 6 +-- lib/modules/src/finders/wait.ts | 2 +- lib/modules/src/metro/_internal.ts | 13 ++---- lib/utils/src/objects.ts | 67 +-------------------------- lib/utils/src/patches/objects.ts | 1 - lib/utils/src/patches/proxy.ts | 11 ++--- lib/utils/src/proxy.ts | 6 +-- shims/react~jsx-runtime.ts | 14 ++---- src/plugins/preinit/no-track/index.ts | 5 +- 10 files changed, 28 insertions(+), 101 deletions(-) diff --git a/lib/modules/src/finders/get.ts b/lib/modules/src/finders/get.ts index 3adadbb..09e8c2f 100644 --- a/lib/modules/src/finders/get.ts +++ b/lib/modules/src/finders/get.ts @@ -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( callback: GetModuleCallback, ): GetModuleUnsubscribeFunction { const [exports, id] = lookupModuleByImportedPath(path) - if (id != null) { + if (id !== undefined) { callback(exports, id) return noopFalse } diff --git a/lib/modules/src/finders/lookup.ts b/lib/modules/src/finders/lookup.ts index 89ee23c..f5eb889 100644 --- a/lib/modules/src/finders/lookup.ts +++ b/lib/modules/src/finders/lookup.ts @@ -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) { diff --git a/lib/modules/src/finders/wait.ts b/lib/modules/src/finders/wait.ts index e3f8be2..3560160 100644 --- a/lib/modules/src/finders/wait.ts +++ b/lib/modules/src/finders/wait.ts @@ -160,7 +160,7 @@ export function waitForModuleByImportedPath( } } : (id, cmpPath) => { - if (path === cmpPath && mInitialized.has(id)) { + if (mInitialized.has(id) && path === cmpPath) { unsub() callback(getInitializedModuleExports(id), id) } diff --git a/lib/modules/src/metro/_internal.ts b/lib/modules/src/metro/_internal.ts index 535fcdd..6442a59 100644 --- a/lib/modules/src/metro/_internal.ts +++ b/lib/modules/src/metro/_internal.ts @@ -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 } } diff --git a/lib/utils/src/objects.ts b/lib/utils/src/objects.ts index 860c564..ec0378a 100644 --- a/lib/utils/src/objects.ts +++ b/lib/utils/src/objects.ts @@ -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() - -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 } /** diff --git a/lib/utils/src/patches/objects.ts b/lib/utils/src/patches/objects.ts index 5e48a35..ec4c67c 100644 --- a/lib/utils/src/patches/objects.ts +++ b/lib/utils/src/patches/objects.ts @@ -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 diff --git a/lib/utils/src/patches/proxy.ts b/lib/utils/src/patches/proxy.ts index 636b3c3..29d51b7 100644 --- a/lib/utils/src/patches/proxy.ts +++ b/lib/utils/src/patches/proxy.ts @@ -1,10 +1,9 @@ -export const _targets = new WeakMap() +export const pTargets = new WeakMap() -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 }, }) diff --git a/lib/utils/src/proxy.ts b/lib/utils/src/proxy.ts index b02cb97..814816b 100644 --- a/lib/utils/src/proxy.ts +++ b/lib/utils/src/proxy.ts @@ -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. diff --git a/shims/react~jsx-runtime.ts b/shims/react~jsx-runtime.ts index 9385cae..54a6033 100644 --- a/shims/react~jsx-runtime.ts +++ b/shims/react~jsx-runtime.ts @@ -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 } diff --git a/src/plugins/preinit/no-track/index.ts b/src/plugins/preinit/no-track/index.ts index 94a11cd..56205de 100644 --- a/src/plugins/preinit/no-track/index.ts +++ b/src/plugins/preinit/no-track/index.ts @@ -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__', {