Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 36 additions & 5 deletions src/renderer/modules/webpack/get-modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ export function getExports<T>(m: RawModule): T | undefined {
function* iterateModuleExports(
m: unknown,
secondLevel?: boolean,
): IterableIterator<Record<PropertyKey, unknown>> {
): IterableIterator<[string | null, Record<PropertyKey, unknown>]> {
// if m is null or not an object/function, then it will obviously not have the props
// if it has no props, then it definitely has no children either
try {
if (m && (typeof m === "object" || typeof m === "function")) {
yield m as Record<PropertyKey, unknown>;
yield [null, m as Record<PropertyKey, unknown>];
for (const key in m) {
// This could throw an error ("illegal invocation") if val === DOMTokenList.prototype
// and key === "length"
// There could be other cases too, hence this try-catch instead of a specific exclusion
const val = (m as Record<PropertyKey, unknown>)[key];
if (val && (typeof val === "object" || typeof val === "function")) {
yield val as Record<PropertyKey, unknown>;
yield [key, val as Record<PropertyKey, unknown>];
if (secondLevel && typeof val === "object") {
for (const subKey in val) {
const subVal = (val as Record<PropertyKey, unknown>)[subKey];
if (subVal && (typeof subVal === "object" || typeof subVal === "function")) {
yield subVal as Record<PropertyKey, unknown>;
yield [subKey, subVal as Record<PropertyKey, unknown>];
continue;
}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ export function getExportsForProps<T, P extends PropertyKey = keyof T>(
// Loop over the module and its exports at the top level
// Return the first thing that has all the indicated props
// Checks only in prototypes if specified, usually to look for functions
for (const exported of iterateModuleExports(m, byPrototype)) {
for (const [_, exported] of iterateModuleExports(m, byPrototype)) {
if (
props.every((p) =>
byPrototype
Expand All @@ -86,6 +86,37 @@ export function getExportsForProps<T, P extends PropertyKey = keyof T>(

// This doesn't have anywhere else to go

/**
* Retrieves the key of first export from a module that contains all the specified properties.
* @template T The expected type of the export that matches the specified properties.
* @template P The type of the property keys to look for.
* @param m The module to search through.
* @param props An array of property keys to look for in the exports.
* @param byPrototype Whether to search only in the prototype of the exports. Defaults to `false`.
* @returns The key of first export that contains all the specified properties, or `undefined` if none is found.
*/
export function getExportsKeyForProps<T, P extends PropertyKey = keyof T>(
m: unknown,
props: P[],
byPrototype?: boolean,
): string | undefined {
// Loop over the module and its exports at the top level
// Return the first thing that has all the indicated props
// Checks only in prototypes if specified, usually to look for functions
for (const [key, exported] of iterateModuleExports(m, byPrototype)) {
if (
key &&
props.every((p) =>
byPrototype
? exported.prototype && p in (exported.prototype as Record<P, unknown>)
: p in (exported as Record<P, unknown>),
)
) {
return key;
}
}
}

export function getById<T>(id: number | string, raw?: false): T | undefined;
export function getById<T>(id: number | string, raw: true): RawModule<T> | undefined;
export function getById<T>(id: number | string, raw?: boolean): T | RawModule<T> | undefined;
Expand Down
4 changes: 3 additions & 1 deletion src/renderer/modules/webpack/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export { mapModule } from "./mapper";

export { waitForModule } from "./lazy";

export { getComponentBySource, getFunctionBySource, getFunctionKeyBySource } from "./inner-search";

export { getById, getExportsForProps, getModule } from "./get-modules";
export { getById, getExportsForProps, getExportsKeyForProps, getModule } from "./get-modules";

/**
* A collection of filter functions to be used with {@link getModule}.
Expand Down
24 changes: 24 additions & 0 deletions src/renderer/modules/webpack/mapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function mapModule<T = Record<string, unknown>, M = T & { symbol: unknown }>(
mod: Record<string, unknown> | undefined,
map: Record<keyof T, string>,
): M {
const mapped = { [Symbol.for("raw")]: mod } as M;
if (!mod) return mapped;

for (const string in map) {
const key = map[string];
Object.defineProperty(mapped, string, {
configurable: true,
enumerable: true,
get() {
const val = mod[key];
return typeof val === "function" ? val.bind(mod) : val;
},
set(newValue) {
mod[key] = newValue;
},
});
}

return mapped;
}