Skip to content

Commit

Permalink
Add module info for waitFor
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckyz committed Jun 20, 2024
1 parent a01ee40 commit 82952ec
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 69 deletions.
8 changes: 4 additions & 4 deletions src/api/ContextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export function findGroupChildrenByChildId(id: string | string[], children: Arra
}

interface ContextMenuProps {
contextMenuApiArguments?: Array<any>;
contextMenuAPIArguments?: Array<any>;
navId: string;
children: Array<ReactElement | null>;
"aria-label": string;
Expand All @@ -135,15 +135,15 @@ export function _usePatchContextMenu(props: ContextMenuProps) {
children: cloneMenuChildren(props.children),
};

props.contextMenuApiArguments ??= [];
props.contextMenuAPIArguments ??= [];
const contextMenuPatches = navPatches.get(props.navId);

if (!Array.isArray(props.children)) props.children = [props.children];

if (contextMenuPatches) {
for (const patch of contextMenuPatches) {
try {
patch(props.children, ...props.contextMenuApiArguments);
patch(props.children, ...props.contextMenuAPIArguments);
} catch (err) {
ContextMenuLogger.error(`Patch for ${props.navId} errored,`, err);
}
Expand All @@ -152,7 +152,7 @@ export function _usePatchContextMenu(props: ContextMenuProps) {

for (const patch of globalPatches) {
try {
patch(props.navId, props.children, ...props.contextMenuApiArguments);
patch(props.navId, props.children, ...props.contextMenuAPIArguments);
} catch (err) {
ContextMenuLogger.error("Global patch errored,", err);
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/_api/contextMenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default definePlugin({
all: true,
replacement: {
match: /Menu,{(?<=\.jsxs?\)\(\i\.Menu,{)/g,
replace: "$&contextMenuApiArguments:typeof arguments!=='undefined'?arguments:[],"
replace: "$&contextMenuAPIArguments:typeof arguments!=='undefined'?arguments:[],"
}
}
]
Expand Down
44 changes: 24 additions & 20 deletions src/webpack/patchWebpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { WebpackInstance } from "discord-types/other";

import { traceFunction } from "../debug/Tracer";
import { patches } from "../plugins";
import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, subscriptions, wreq } from ".";
import { _initWebpack, beforeInitListeners, factoryListeners, moduleListeners, waitForSubscriptions, wreq } from ".";

const logger = new Logger("WebpackInterceptor", "#8caaee");
const initCallbackRegex = canonicalizeMatch(/{return \i\(".+?"\)}/);
Expand Down Expand Up @@ -204,8 +204,7 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re
}

exports = module.exports;

if (!exports) return;
if (exports == null) return;

// There are (at the time of writing) 11 modules exporting the window
// Make these non enumerable to improve webpack search performance
Expand Down Expand Up @@ -240,32 +239,37 @@ function patchFactories(factories: Record<string, (module: any, exports: any, re

for (const callback of moduleListeners) {
try {
callback(exports, id);
callback(exports, { id, factory: originalMod });
} catch (err) {
logger.error("Error in Webpack module listener:\n", err, callback);
}
}

for (const [filter, callback] of subscriptions) {
for (const [filter, callback] of waitForSubscriptions) {
try {
if (exports && filter(exports)) {
subscriptions.delete(filter);
callback(exports, id);
} else if (typeof exports === "object") {
if (exports.default && filter(exports.default)) {
subscriptions.delete(filter);
callback(exports.default, id);
} else {
for (const nested in exports) if (nested.length <= 3) {
if (exports[nested] && filter(exports[nested])) {
subscriptions.delete(filter);
callback(exports[nested], id);
}
}
if (filter(exports)) {
waitForSubscriptions.delete(filter);
callback(exports, { id, factory: originalMod, exportKey: "" });
continue;
}

if (typeof exports !== "object") continue;

if (exports.default != null && filter(exports.default)) {
waitForSubscriptions.delete(filter);
callback(exports.default, { id, factory: originalMod, exportKey: "default" });
continue;
}

for (const key in exports) if (key.length <= 3) {
if (exports[key] != null && filter(exports[key])) {
waitForSubscriptions.delete(filter);
callback(exports[key], { id, factory: originalMod, exportKey: key });
break;
}
}
} catch (err) {
logger.error("Error while firing callback for Webpack subscription:\n", err, filter, callback);
logger.error("Error while firing callback for Webpack waitFor subscription:\n", err, filter, callback);
}
}
} as any as { toString: () => string, original: any, (...args: any[]): void; };
Expand Down
101 changes: 57 additions & 44 deletions src/webpack/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,21 @@ export const filters = {
}
};

export type CallbackFn = (mod: any, id: string) => void;
export type ModListenerInfo = {
id: PropertyKey;
factory: (module: any, exports: any, require: WebpackInstance) => void;
};

export type ModCallbackInfo = ModListenerInfo & {
exportKey: PropertyKey;
};

export type ModListenerFn = (module: any, info: ModListenerInfo) => void;
export type ModCallbackFn = (module: any, info: ModCallbackInfo) => void;

export const subscriptions = new Map<FilterFn, CallbackFn>();
export const moduleListeners = new Set<CallbackFn>();
export const factoryListeners = new Set<(factory: (module: any, exports: any, require: WebpackInstance) => void) => void>();
export const moduleListeners = new Set<ModListenerFn>();
export const waitForSubscriptions = new Map<FilterFn, ModCallbackFn>();
export const beforeInitListeners = new Set<(wreq: WebpackInstance) => void>();

export function _initWebpack(webpackRequire: WebpackInstance) {
Expand Down Expand Up @@ -106,24 +116,21 @@ export const find = traceFunction("find", function find(filter: FilterFn, { isIn

for (const key in cache) {
const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue;
if (!mod?.loaded || mod?.exports == null) continue;

if (filter(mod.exports)) {
return isWaitFor ? [mod.exports, key] : mod.exports;
}

if (typeof mod.exports !== "object") continue;

if (mod.exports.default && filter(mod.exports.default)) {
const found = mod.exports.default;
return isWaitFor ? [found, key] : found;
if (mod.exports.default != null && filter(mod.exports.default)) {
return isWaitFor ? [mod.exports.default, key] : mod.exports.default;
}

// the length check makes search about 20% faster
for (const nestedMod in mod.exports) if (nestedMod.length <= 3) {
const nested = mod.exports[nestedMod];
if (nested && filter(nested)) {
return isWaitFor ? [nested, key] : nested;
for (const key in mod.exports) if (key.length <= 3) {
if (mod.exports[key] != null && filter(mod.exports[key])) {
return isWaitFor ? [mod.exports[key], key] : mod.exports[key];
}
}
}
Expand All @@ -142,18 +149,25 @@ export function findAll(filter: FilterFn) {
const ret = [] as any[];
for (const key in cache) {
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);
else if (typeof mod.exports !== "object")
continue;
}

if (typeof mod.exports !== "object") continue;

if (mod.exports.default && filter(mod.exports.default))
if (mod.exports.default != null && filter(mod.exports.default)) {
ret.push(mod.exports.default);
else for (const nestedMod in mod.exports) if (nestedMod.length <= 3) {
const nested = mod.exports[nestedMod];
if (nested && filter(nested)) ret.push(nested);
continue;
}

for (const key in mod.exports) if (key.length <= 3) {
if (mod.exports[key] && filter(mod.exports[key])) {
ret.push(mod.exports[key]);
break;
}
}
}

Expand Down Expand Up @@ -190,7 +204,7 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
outer:
for (const key in cache) {
const mod = cache[key];
if (!mod.loaded || !mod?.exports) continue;
if (!mod.loaded || mod?.exports == null) continue;

for (let j = 0; j < length; j++) {
const filter = filters[j];
Expand All @@ -204,26 +218,23 @@ export const findBulk = traceFunction("findBulk", function findBulk(...filterFns
break;
}

if (typeof mod.exports !== "object")
continue;
if (typeof mod.exports !== "object") continue;

if (mod.exports.default && filter(mod.exports.default)) {
results[j] = mod.exports.default;
filters[j] = undefined;
if (++found === length) break outer;
break;
continue;
}

for (const nestedMod in mod.exports)
if (nestedMod.length <= 3) {
const nested = mod.exports[nestedMod];
if (nested && filter(nested)) {
results[j] = nested;
filters[j] = undefined;
if (++found === length) break outer;
continue outer;
}
for (const key in mod.exports) if (key.length <= 3) {
if (mod.exports[key] && filter(mod.exports[key])) {
results[j] = mod.exports[key];
filters[j] = undefined;
if (++found === length) break outer;
break;
}
}
}
}

Expand Down Expand Up @@ -293,7 +304,7 @@ export const lazyWebpackSearchHistory = [] as Array<["find" | "findByProps" | "f
* Note that the example below exists already as an api, see {@link findByPropsLazy}
* @example const mod = proxyLazy(() => findByProps("blah")); console.log(mod.blah);
*/
export function proxyLazyWebpack<T = any>(factory: () => any, attempts?: number) {
export function proxyLazyWebpack<T = any>(factory: () => T, attempts?: number) {
if (IS_REPORTER) lazyWebpackSearchHistory.push(["proxyLazyWebpack", [factory]]);

return proxyLazy<T>(factory, attempts);
Expand Down Expand Up @@ -446,25 +457,27 @@ export function findExportedComponentLazy<T extends object = any>(...props: stri
* })
*/
export const mapMangledModule = traceFunction("mapMangledModule", function mapMangledModule<S extends string>(code: string, mappers: Record<S, FilterFn>): Record<S, any> {
const exports = {} as Record<S, any>;
const mapping = {} as Record<S, any>;

const id = findModuleId(code);
if (id === null)
return exports;
if (id === null) return mapping;

const exports = wreq(id as any);

const mod = wreq(id as any);
outer:
for (const key in mod) {
const member = mod[key];
for (const key in exports) {
const value = exports[key];

for (const newName in mappers) {
// if the current mapper matches this module
if (mappers[newName](member)) {
exports[newName] = member;
if (mappers[newName](value)) {
mapping[newName] = value;
continue outer;
}
}
}
return exports;

return mapping;
});

/**
Expand Down Expand Up @@ -570,7 +583,7 @@ export function extractAndLoadChunksLazy(code: string[], matcher = DefaultExtrac
* Wait for a module that matches the provided filter to be registered,
* then call the callback with the module as the first argument
*/
export function waitFor(filter: string | string[] | FilterFn, callback: CallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
export function waitFor(filter: string | string[] | FilterFn, callback: ModCallbackFn, { isIndirect = false }: { isIndirect?: boolean; } = {}) {
if (IS_REPORTER && !isIndirect) lazyWebpackSearchHistory.push(["waitFor", Array.isArray(filter) ? filter : [filter]]);

if (typeof filter === "string")
Expand All @@ -585,7 +598,7 @@ export function waitFor(filter: string | string[] | FilterFn, callback: Callback
if (existing) return void callback(existing, id);
}

subscriptions.set(filter, callback);
waitForSubscriptions.set(filter, callback);
}

/**
Expand Down

0 comments on commit 82952ec

Please sign in to comment.