Skip to content

Commit

Permalink
Proxy modules object for patching
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckyz committed May 20, 2024
1 parent 5f8b96d commit 2a77941
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 241 deletions.
6 changes: 6 additions & 0 deletions scripts/generateReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,12 @@ async function runtime(token: string) {
}
}));

// Call the getter for all the values in the modules object
// So modules that were not required get patched by our proxy
for (const id in wreq!.m) {
wreq!.m[id];
}

console.log("[PUP_DEBUG]", "Finished loading all chunks!");

for (const patch of Vencord.Plugins.patches) {
Expand Down
1 change: 0 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

export const WEBPACK_CHUNK = "webpackChunkdiscord_app";
export const REACT_GLOBAL = "Vencord.Webpack.Common.React";
export const SUPPORT_CHANNEL_ID = "1026515880080842772";

Expand Down
10 changes: 4 additions & 6 deletions src/utils/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { UNCONFIGURABLE_PROPERTIES } from "./misc";

export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
let tries = 0;
let cache: T;
Expand All @@ -29,10 +31,6 @@ export function makeLazy<T>(factory: () => T, attempts = 5): () => T {
};
}

// Proxies demand that these properties be unmodified, so proxyLazy
// will always return the function default for them.
const unconfigurable = ["arguments", "caller", "prototype"];

const handler: ProxyHandler<any> = {};

const kGET = Symbol.for("vencord.lazy.get");
Expand All @@ -59,14 +57,14 @@ for (const method of [
handler.ownKeys = target => {
const v = target[kGET]();
const keys = Reflect.ownKeys(v);
for (const key of unconfigurable) {
for (const key of UNCONFIGURABLE_PROPERTIES) {
if (!keys.includes(key)) keys.push(key);
}
return keys;
};

handler.getOwnPropertyDescriptor = (target, p) => {
if (typeof p === "string" && unconfigurable.includes(p))
if (typeof p === "string" && UNCONFIGURABLE_PROPERTIES.includes(p))
return Reflect.getOwnPropertyDescriptor(target, p);

const descriptor = Reflect.getOwnPropertyDescriptor(target[kGET](), p);
Expand Down
3 changes: 3 additions & 0 deletions src/utils/misc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,6 @@ export const isPluginDev = (id: string) => Object.hasOwn(DevsById, id);
export function pluralise(amount: number, singular: string, plural = singular + "s") {
return amount === 1 ? `${amount} ${singular}` : `${amount} ${plural}`;
}

/** Unconfigurable properties for proxies */
export const UNCONFIGURABLE_PROPERTIES = ["arguments", "caller", "prototype"];
Loading

0 comments on commit 2a77941

Please sign in to comment.