Skip to content

Commit

Permalink
fix(metro/polyfill): correctly polyfill
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed May 25, 2024
1 parent f136597 commit 9bd47d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
26 changes: 7 additions & 19 deletions src/metro/caches.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { ClientInfoManager, MMKVManager } from "@lib/api/native/modules";
import { throttle } from "@lib/utils/throttle";

const CACHE_VERSION = 19;
const CACHE_VERSION = 24;
const BUNNY_METRO_CACHE_KEY = `__bunny_metro_cache_key_v${CACHE_VERSION}__`;

export enum ExportsFlags {
EXISTS = 1 << 0,
ES_MODULE = 1 << 1,
BLACKLISTED = 1 << 2,
FUNCTION = 1 << 3,
PRIMITIVE = 1 << 4,
BLACKLISTED = 1 << 1
}

type ModulesMap = {
Expand All @@ -25,7 +22,7 @@ function buildInitCache() {
const cache = {
_v: CACHE_VERSION,
_buildNumber: ClientInfoManager.Build as number,
exportsIndex: {} as Record<string, [number, number]>,
exportsIndex: {} as Record<string, number>,
findIndex: {} as Record<string, ModulesMap | undefined>,
polyfillIndex: {} as Record<string, ModulesMap | undefined>,
assetsIndex: {} as Record<string, number>
Expand Down Expand Up @@ -65,28 +62,19 @@ const saveCache = throttle(() => {
function extractExportsFlags(moduleExports: any) {
if (!moduleExports) return 0;

let bit = ExportsFlags.EXISTS;
bit |= moduleExports.__esModule ? ExportsFlags.ES_MODULE : 0;
bit |= typeof moduleExports === "function" ? ExportsFlags.FUNCTION : 0;
if (typeof moduleExports === "string" || typeof moduleExports === "number") {
bit |= ExportsFlags.PRIMITIVE;
}

const bit = ExportsFlags.EXISTS;
return bit;
}

export function indexExportsFlags(moduleId: number, moduleExports: any) {
const flags: [number, number] = moduleExports.default && moduleExports.__esModule
? [extractExportsFlags(moduleExports), extractExportsFlags(moduleExports.default)]
: [extractExportsFlags(moduleExports), 0];

if (flags[0] !== ExportsFlags.EXISTS && flags[1] !== ExportsFlags.EXISTS) {
const flags = extractExportsFlags(moduleExports);
if (flags !== ExportsFlags.EXISTS) {
_metroCache.exportsIndex[moduleId] = flags;
}
}

export function indexBlacklistFlag(id: number) {
_metroCache.exportsIndex[id] ??= [ExportsFlags.BLACKLISTED, 0];
_metroCache.exportsIndex[id] |= ExportsFlags.BLACKLISTED;
}

export function getCacherForUniq(uniq: string, allFind: boolean) {
Expand Down
6 changes: 3 additions & 3 deletions src/metro/modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ for (const key in metroModules) {
const metroModule = metroModules[id];

const cache = getMetroCache().exportsIndex[id];
if (cache?.[0] & ExportsFlags.BLACKLISTED) {
if (cache & ExportsFlags.BLACKLISTED) {
blacklistModule(id);
continue;
}
Expand Down Expand Up @@ -166,7 +166,7 @@ export function requireModule(id: Metro.ModuleID) {
if (!metroModules[0]?.isInitialized) metroRequire(0);
if (blacklistedIds.has(id)) return undefined;

if (Number(id) === -1) return require("@metro/polyfills/redesign");
if (Number(id) === -1) return require("@metro/polyfills/redesign").default;

if (metroModules[id]?.isInitialized && !metroModules[id]?.hasError) {
return metroRequire(id);
Expand All @@ -191,7 +191,7 @@ export function requireModule(id: Metro.ModuleID) {
}

export function* getModules(uniq: string, all = false) {
yield [-1, require("@metro/polyfills/redesign")];
yield [-1, require("@metro/polyfills/redesign").default];

let cache = getMetroCache().findIndex[uniq];
if (all && !cache?._) cache = undefined;
Expand Down
6 changes: 6 additions & 0 deletions src/metro/polyfills/redesign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ export default new Proxy({}, {

return _cache[prop] = bestCandidate[prop];
},
ownKeys() {
redesignProps.forEach(prop => {
this.get!({}, prop, {});
});
return Reflect.ownKeys(_cache);
}
});

0 comments on commit 9bd47d2

Please sign in to comment.