Avoid checking for default twice

This commit is contained in:
Nuckyz 2025-01-29 14:41:29 -03:00
parent 172c372e40
commit 8602ec2611
No known key found for this signature in database
GPG key ID: 440BF8296E1C4AD9
2 changed files with 10 additions and 35 deletions

View file

@ -12,7 +12,7 @@ import { PatchReplacement } from "@utils/types";
import { traceFunctionWithResults } from "../debug/Tracer"; import { traceFunctionWithResults } from "../debug/Tracer";
import { patches } from "../plugins"; import { patches } from "../plugins";
import { _initWebpack, AnyModuleFactory, AnyWebpackRequire, factoryListeners, findModuleId, ModuleExports, moduleListeners, subscriptions, WebpackRequire, WrappedModuleFactory, wreq } from "."; import { _initWebpack, AnyModuleFactory, AnyWebpackRequire, factoryListeners, findModuleId, ModuleExports, moduleListeners, waitForSubscriptions, WebpackRequire, WrappedModuleFactory, wreq } from ".";
const logger = new Logger("WebpackInterceptor", "#8caaee"); const logger = new Logger("WebpackInterceptor", "#8caaee");
@ -345,11 +345,6 @@ function wrapAndPatchFactory(id: PropertyKey, originalFactory: AnyModuleFactory)
break nonEnumerableChecking; break nonEnumerableChecking;
} }
if (exports.default === window || exports.default?.[Symbol.toStringTag] === "DOMTokenList") {
shouldMakeNonEnumerable = true;
break nonEnumerableChecking;
}
for (const exportKey in exports) { for (const exportKey in exports) {
if (exports[exportKey] === window || exports[exportKey]?.[Symbol.toStringTag] === "DOMTokenList") { if (exports[exportKey] === window || exports[exportKey]?.[Symbol.toStringTag] === "DOMTokenList") {
shouldMakeNonEnumerable = true; shouldMakeNonEnumerable = true;
@ -380,11 +375,11 @@ function wrapAndPatchFactory(id: PropertyKey, originalFactory: AnyModuleFactory)
} }
} }
for (const [filter, callback] of subscriptions) { for (const [filter, callback] of waitForSubscriptions) {
try { try {
if (filter(exports)) { if (filter(exports)) {
subscriptions.delete(filter); waitForSubscriptions.delete(filter);
callback(exports, id); callback(exports, id);
continue; continue;
} }
@ -393,17 +388,11 @@ function wrapAndPatchFactory(id: PropertyKey, originalFactory: AnyModuleFactory)
continue; continue;
} }
if (exports.default != null && filter(exports.default)) {
subscriptions.delete(filter);
callback(exports.default, id);
continue;
}
for (const exportKey in exports) { for (const exportKey in exports) {
const exportValue = exports[exportKey]; const exportValue = exports[exportKey];
if (exportValue != null && filter(exportValue)) { if (exportValue != null && filter(exportValue)) {
subscriptions.delete(filter); waitForSubscriptions.delete(filter);
callback(exportValue, id); callback(exportValue, id);
break; break;
} }

View file

@ -86,7 +86,7 @@ export const filters = {
export type CallbackFn = (module: ModuleExports, id: PropertyKey) => void; export type CallbackFn = (module: ModuleExports, id: PropertyKey) => void;
export type FactoryListernFn = (factory: AnyModuleFactory) => void; export type FactoryListernFn = (factory: AnyModuleFactory) => void;
export const subscriptions = new Map<FilterFn, CallbackFn>(); export const waitForSubscriptions = new Map<FilterFn, CallbackFn>();
export const moduleListeners = new Set<CallbackFn>(); export const moduleListeners = new Set<CallbackFn>();
export const factoryListeners = new Set<FactoryListernFn>(); export const factoryListeners = new Set<FactoryListernFn>();
@ -130,7 +130,7 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
for (const key in cache) { for (const key in cache) {
const mod = cache[key]; const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue; if (!mod?.loaded || mod.exports == null) continue;
if (filter(mod.exports)) { if (filter(mod.exports)) {
return isWaitFor ? [mod.exports, key] : mod.exports; return isWaitFor ? [mod.exports, key] : mod.exports;
@ -138,11 +138,6 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn
if (typeof mod.exports !== "object") continue; if (typeof mod.exports !== "object") continue;
if (mod.exports.default && filter(mod.exports.default)) {
const found = mod.exports.default;
return isWaitFor ? [found, key] : found;
}
for (const nestedMod in mod.exports) { for (const nestedMod in mod.exports) {
const nested = mod.exports[nestedMod]; const nested = mod.exports[nestedMod];
if (nested && filter(nested)) { if (nested && filter(nested)) {
@ -165,16 +160,14 @@ export function findAll(filter: FilterFn) {
const ret = [] as any[]; const ret = [] as any[];
for (const key in cache) { for (const key in cache) {
const mod = cache[key]; const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue; if (!mod?.loaded || mod.exports == null) continue;
if (filter(mod.exports)) if (filter(mod.exports))
ret.push(mod.exports); ret.push(mod.exports);
else if (typeof mod.exports !== "object") else if (typeof mod.exports !== "object")
continue; continue;
if (mod.exports.default && filter(mod.exports.default)) for (const nestedMod in mod.exports) {
ret.push(mod.exports.default);
else for (const nestedMod in mod.exports) {
const nested = mod.exports[nestedMod]; const nested = mod.exports[nestedMod];
if (nested && filter(nested)) ret.push(nested); if (nested && filter(nested)) ret.push(nested);
} }
@ -213,7 +206,7 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
outer: outer:
for (const key in cache) { for (const key in cache) {
const mod = cache[key]; const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue; if (!mod?.loaded || mod.exports == null) continue;
for (let j = 0; j < length; j++) { for (let j = 0; j < length; j++) {
const filter = filters[j]; const filter = filters[j];
@ -230,13 +223,6 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
if (typeof mod.exports !== "object") if (typeof mod.exports !== "object")
continue; continue;
if (mod.exports.default && filter(mod.exports.default)) {
results[j] = mod.exports.default;
filters[j] = undefined;
if (++found === length) break outer;
break;
}
for (const nestedMod in mod.exports) { for (const nestedMod in mod.exports) {
const nested = mod.exports[nestedMod]; const nested = mod.exports[nestedMod];
if (nested && filter(nested)) { if (nested && filter(nested)) {
@ -605,7 +591,7 @@ export function waitFor(filter: string | PropsFilter | FilterFn, callback: Callb
if (existing) return void callback(existing, id); if (existing) return void callback(existing, id);
} }
subscriptions.set(filter, callback); waitForSubscriptions.set(filter, callback);
} }
/** /**