2024-06-01 21:39:01 +00:00
|
|
|
/*
|
|
|
|
* Vencord, a Discord client mod
|
|
|
|
* Copyright (c) 2024 Vendicated and contributors
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Logger } from "@utils/Logger";
|
|
|
|
import { canonicalizeMatch } from "@utils/patches";
|
|
|
|
import * as Webpack from "@webpack";
|
|
|
|
import { wreq } from "@webpack";
|
|
|
|
|
|
|
|
const LazyChunkLoaderLogger = new Logger("LazyChunkLoader");
|
|
|
|
|
|
|
|
export async function loadLazyChunks() {
|
|
|
|
try {
|
|
|
|
LazyChunkLoaderLogger.log("Loading all chunks...");
|
|
|
|
|
2024-07-19 03:46:48 +00:00
|
|
|
const validChunks = new Set<number>();
|
|
|
|
const invalidChunks = new Set<number>();
|
|
|
|
const deferredRequires = new Set<number>();
|
2024-06-01 21:39:01 +00:00
|
|
|
|
|
|
|
let chunksSearchingResolve: (value: void | PromiseLike<void>) => void;
|
|
|
|
const chunksSearchingDone = new Promise<void>(r => chunksSearchingResolve = r);
|
|
|
|
|
|
|
|
// True if resolved, false otherwise
|
|
|
|
const chunksSearchPromises = [] as Array<() => boolean>;
|
|
|
|
|
2024-06-19 05:16:29 +00:00
|
|
|
const LazyChunkRegex = canonicalizeMatch(/(?:(?:Promise\.all\(\[)?(\i\.e\("?[^)]+?"?\)[^\]]*?)(?:\]\))?)\.then\(\i\.bind\(\i,"?([^)]+?)"?\)\)/g);
|
2024-06-01 21:39:01 +00:00
|
|
|
|
|
|
|
async function searchAndLoadLazyChunks(factoryCode: string) {
|
|
|
|
const lazyChunks = factoryCode.matchAll(LazyChunkRegex);
|
2024-07-19 03:46:48 +00:00
|
|
|
const validChunkGroups = new Set<[chunkIds: number[], entryPoint: number]>();
|
2024-06-01 21:39:01 +00:00
|
|
|
|
|
|
|
// Workaround for a chunk that depends on the ChannelMessage component but may be be force loaded before
|
|
|
|
// the chunk containing the component
|
|
|
|
const shouldForceDefer = factoryCode.includes(".Messages.GUILD_FEED_UNFEATURE_BUTTON_TEXT");
|
|
|
|
|
|
|
|
await Promise.all(Array.from(lazyChunks).map(async ([, rawChunkIds, entryPoint]) => {
|
2024-07-19 03:46:48 +00:00
|
|
|
const chunkIds = rawChunkIds ? Array.from(rawChunkIds.matchAll(Webpack.ChunkIdsRegex)).map(m => Number(m[1])) : [];
|
2024-06-01 21:39:01 +00:00
|
|
|
|
|
|
|
if (chunkIds.length === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let invalidChunkGroup = false;
|
|
|
|
|
|
|
|
for (const id of chunkIds) {
|
|
|
|
if (wreq.u(id) == null || wreq.u(id) === "undefined.js") continue;
|
|
|
|
|
2024-06-06 03:47:57 +00:00
|
|
|
const isWorkerAsset = await fetch(wreq.p + wreq.u(id))
|
2024-06-01 21:39:01 +00:00
|
|
|
.then(r => r.text())
|
2024-06-06 03:47:57 +00:00
|
|
|
.then(t => t.includes("importScripts("));
|
2024-06-01 21:39:01 +00:00
|
|
|
|
2024-06-06 03:47:57 +00:00
|
|
|
if (isWorkerAsset) {
|
2024-06-01 21:39:01 +00:00
|
|
|
invalidChunks.add(id);
|
|
|
|
invalidChunkGroup = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
validChunks.add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!invalidChunkGroup) {
|
2024-07-19 03:46:48 +00:00
|
|
|
validChunkGroups.add([chunkIds, Number(entryPoint)]);
|
2024-06-01 21:39:01 +00:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Loads all found valid chunk groups
|
|
|
|
await Promise.all(
|
|
|
|
Array.from(validChunkGroups)
|
|
|
|
.map(([chunkIds]) =>
|
|
|
|
Promise.all(chunkIds.map(id => wreq.e(id as any).catch(() => { })))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Requires the entry points for all valid chunk groups
|
|
|
|
for (const [, entryPoint] of validChunkGroups) {
|
|
|
|
try {
|
|
|
|
if (shouldForceDefer) {
|
|
|
|
deferredRequires.add(entryPoint);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wreq.m[entryPoint]) wreq(entryPoint as any);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// setImmediate to only check if all chunks were loaded after this function resolves
|
|
|
|
// We check if all chunks were loaded every time a factory is loaded
|
|
|
|
// If we are still looking for chunks in the other factories, the array will have that factory's chunk search promise not resolved
|
|
|
|
// But, if all chunk search promises are resolved, this means we found every lazy chunk loaded by Discord code and manually loaded them
|
|
|
|
setTimeout(() => {
|
|
|
|
let allResolved = true;
|
|
|
|
|
|
|
|
for (let i = 0; i < chunksSearchPromises.length; i++) {
|
|
|
|
const isResolved = chunksSearchPromises[i]();
|
|
|
|
|
|
|
|
if (isResolved) {
|
|
|
|
// Remove finished promises to avoid having to iterate through a huge array everytime
|
|
|
|
chunksSearchPromises.splice(i--, 1);
|
|
|
|
} else {
|
|
|
|
allResolved = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allResolved) chunksSearchingResolve();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Webpack.factoryListeners.add(factory => {
|
|
|
|
let isResolved = false;
|
|
|
|
searchAndLoadLazyChunks(factory.toString()).then(() => isResolved = true);
|
|
|
|
|
|
|
|
chunksSearchPromises.push(() => isResolved);
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const factoryId in wreq.m) {
|
|
|
|
let isResolved = false;
|
|
|
|
searchAndLoadLazyChunks(wreq.m[factoryId].toString()).then(() => isResolved = true);
|
|
|
|
|
|
|
|
chunksSearchPromises.push(() => isResolved);
|
|
|
|
}
|
|
|
|
|
|
|
|
await chunksSearchingDone;
|
|
|
|
|
|
|
|
// Require deferred entry points
|
|
|
|
for (const deferredRequire of deferredRequires) {
|
|
|
|
wreq!(deferredRequire as any);
|
|
|
|
}
|
|
|
|
|
|
|
|
// All chunks Discord has mapped to asset files, even if they are not used anymore
|
2024-07-19 03:46:48 +00:00
|
|
|
const allChunks = [] as number[];
|
2024-06-01 21:39:01 +00:00
|
|
|
|
|
|
|
// Matches "id" or id:
|
2024-08-23 06:02:04 +00:00
|
|
|
for (const currentMatch of wreq!.u.toString().matchAll(/(?:"([\deE]+?)"(?![,}]))|(?:([\deE]+?):)/g)) {
|
2024-06-01 21:39:01 +00:00
|
|
|
const id = currentMatch[1] ?? currentMatch[2];
|
|
|
|
if (id == null) continue;
|
|
|
|
|
2024-07-19 03:46:48 +00:00
|
|
|
allChunks.push(Number(id));
|
2024-06-01 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (allChunks.length === 0) throw new Error("Failed to get all chunks");
|
|
|
|
|
|
|
|
// Chunks that are not loaded (not used) by Discord code anymore
|
|
|
|
const chunksLeft = allChunks.filter(id => {
|
|
|
|
return !(validChunks.has(id) || invalidChunks.has(id));
|
|
|
|
});
|
|
|
|
|
|
|
|
await Promise.all(chunksLeft.map(async id => {
|
2024-06-06 03:47:57 +00:00
|
|
|
const isWorkerAsset = await fetch(wreq.p + wreq.u(id))
|
2024-06-01 21:39:01 +00:00
|
|
|
.then(r => r.text())
|
2024-06-06 03:47:57 +00:00
|
|
|
.then(t => t.includes("importScripts("));
|
2024-06-01 21:39:01 +00:00
|
|
|
|
|
|
|
// Loads and requires a chunk
|
2024-06-06 03:47:57 +00:00
|
|
|
if (!isWorkerAsset) {
|
2024-06-01 21:39:01 +00:00
|
|
|
await wreq.e(id as any);
|
2024-06-06 03:47:57 +00:00
|
|
|
// Technically, the id of the chunk does not match the entry point
|
|
|
|
// But, still try it because we have no way to get the actual entry point
|
2024-06-01 21:39:01 +00:00
|
|
|
if (wreq.m[id]) wreq(id as any);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
LazyChunkLoaderLogger.log("Finished loading all chunks!");
|
|
|
|
} catch (e) {
|
|
|
|
LazyChunkLoaderLogger.log("A fatal error occurred:", e);
|
|
|
|
}
|
|
|
|
}
|