diff --git a/src/metro/caches.ts b/src/metro/caches.ts index 3bd77f6..1706335 100644 --- a/src/metro/caches.ts +++ b/src/metro/caches.ts @@ -7,24 +7,21 @@ type ModulesMap = { }; interface MetroCacheStore { - _v: 6; + _v: 10; _buildNumber: number; findIndex: Record; polyfillIndex: Record; assetsIndex: Record; } -const BUNNY_METRO_CACHE_KEY = "__bunny_metro_cache_key_v5__"; +const BUNNY_METRO_CACHE_KEY = "__bunny_metro_cache_key_v10__"; let _metroCache = null as unknown as MetroCacheStore; -export function getMetroCache() { - return _metroCache; -} -window.getMetroCache = getMetroCache; +export const getMetroCache = window.__getMetroCache = () => _metroCache; function buildInitCache() { _metroCache = { - _v: 6, + _v: 10, _buildNumber: ClientInfoManager.Build, findIndex: {}, polyfillIndex: {}, @@ -62,12 +59,12 @@ export function getCacherForUniq(uniq: string, allFind: boolean) { return { cacheId(moduleId: number) { - indexObject = _metroCache.findIndex[uniq] ??= {}; + indexObject ??= _metroCache.findIndex[uniq] ??= {}; indexObject[moduleId] = 1; saveCache(); }, finish() { - indexObject = _metroCache.findIndex[uniq] ??= {}; + indexObject ??= _metroCache.findIndex[uniq] ??= {}; if (allFind) indexObject._ = 1; saveCache(); } @@ -82,7 +79,7 @@ export function getPolyfillModuleCacher(name: string) { return require("@metro/modules").getCachedPolyfillModules(name); }, cacheId(moduleId: number) { - indexObject = _metroCache.polyfillIndex[name] ??= {}; + indexObject ??= _metroCache.polyfillIndex[name] ??= {}; indexObject[moduleId] = 1; saveCache(); } diff --git a/src/metro/finders.ts b/src/metro/finders.ts index bd447de..cff3f2b 100644 --- a/src/metro/finders.ts +++ b/src/metro/finders.ts @@ -4,10 +4,10 @@ import { FilterFn } from "./types"; function testExports(moduleExports: any, moduleId: number, filter: FilterFn) { if (moduleExports.default && moduleExports.__esModule && filter(moduleExports.default, moduleId, true)) { - return [filter.defaultFilter ? moduleExports : moduleExports.default, !filter.defaultFilter]; + return [filter.raw ? moduleExports : moduleExports.default, !filter.raw]; } - if (!filter.defaultFilter && filter(moduleExports, moduleId, false)) { + if (!filter.raw && filter(moduleExports, moduleId, false)) { return [moduleExports, false]; } @@ -47,7 +47,7 @@ export function findModuleId(filter: FilterFn) { */ export function findExports(filter: FilterFn) { const [id, defaultExp] = findModule(filter); - if (defaultExp == null) return; + if (id == null) return; return defaultExp ? requireModule(id).default : requireModule(id); } @@ -84,9 +84,9 @@ export function findAllModuleId(filter: FilterFn) { * @param filter findAll calls filter once for each enumerable module's exports, adding the exports to the returned array when filter returns a thruthy value. */ export function findAllExports(filter: FilterFn) { - return findAllModule(filter).map(e => { - const [id, defaultExp] = e; - if (defaultExp == null) return; + return findAllModule(filter).map(ret => { + if (!ret.length) return; + const [id, defaultExp] = ret; return defaultExp ? requireModule(id).default : requireModule(id); }); } diff --git a/src/metro/types.ts b/src/metro/types.ts index b343ea1..ebe5099 100644 --- a/src/metro/types.ts +++ b/src/metro/types.ts @@ -139,13 +139,13 @@ export interface FilterFn { (m: any, id: number, defaultCheck: boolean): boolean; filter: FilterCheckDef; args: A; - defaultFilter: boolean; + raw: boolean; uniq: string; } export interface FilterDefinition { (...args: A): FilterFn; - byDefault(...args: A): FilterFn; + byRaw(...args: A): FilterFn; uniqMaker(args: A): string; } diff --git a/src/metro/utils.ts b/src/metro/utils.ts index 4151eed..52d0e17 100644 --- a/src/metro/utils.ts +++ b/src/metro/utils.ts @@ -8,26 +8,26 @@ export function createFilterDefinition( fn: FilterCheckDef, uniqMaker: (args: A) => string ): FilterDefinition { - function createHolder(func: T, args: A, defaultFilter: boolean) { + function createHolder(func: T, args: A, raw: boolean) { return Object.assign(func, { filter: fn, - defaultFilter, + raw, args, uniq: [ - defaultFilter && "default::", + raw && "raw::", uniqMaker(args) ].filter(Boolean).join("") }); } - const curry = (defaultFilter: boolean) => (...args: A) => { + const curry = (raw: boolean) => (...args: A) => { return createHolder((m: ModuleExports, id: number, defaultCheck: boolean) => { return fn(args, m, id, defaultCheck); - }, args, defaultFilter); + }, args, raw); }; return Object.assign(curry(false), { - byDefault: curry(true), + byRaw: curry(true), uniqMaker }); } @@ -46,17 +46,17 @@ export const findByProps = (...props: string[]) => findExports(byProps(...props) export const findByPropsLazy = (...props: string[]) => proxyLazy(() => findByProps(...props)); export const findByPropsAll = (...props: string[]) => findAllExports(byProps(...props)); -export const findByName = (name: string, expDefault = true) => findExports(expDefault ? byName(name) : byName.byDefault(name)); +export const findByName = (name: string, expDefault = true) => findExports(expDefault ? byName(name) : byName.byRaw(name)); export const findByNameLazy = (name: string, expDefault = true) => proxyLazy(() => findByName(name, expDefault)); -export const findByNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byName(name) : byName.byDefault(name)); +export const findByNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byName(name) : byName.byRaw(name)); -export const findByDisplayName = (name: string, expDefault = true) => findExports(expDefault ? byDisplayName(name) : byDisplayName.byDefault(name)); +export const findByDisplayName = (name: string, expDefault = true) => findExports(expDefault ? byDisplayName(name) : byDisplayName.byRaw(name)); export const findByDisplayNameLazy = (name: string, expDefault = true) => proxyLazy(() => findByDisplayName(name, expDefault)); -export const findByDisplayNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byDisplayName(name) : byDisplayName.byDefault(name)); +export const findByDisplayNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byDisplayName(name) : byDisplayName.byRaw(name)); -export const findByTypeName = (name: string, expDefault = true) => findExports(expDefault ? byTypeName(name) : byTypeName.byDefault(name)); +export const findByTypeName = (name: string, expDefault = true) => findExports(expDefault ? byTypeName(name) : byTypeName.byRaw(name)); export const findByTypeNameLazy = (name: string, expDefault = true) => proxyLazy(() => findByTypeName(name, expDefault)); -export const findByTypeNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byTypeName(name) : byTypeName.byDefault(name)); +export const findByTypeNameAll = (name: string, expDefault = true) => findAllExports(expDefault ? byTypeName(name) : byTypeName.byRaw(name)); export const findByStoreName = (name: string) => findExports(byStoreName(name)); export const findByStoreNameLazy = (name: string) => proxyLazy(() => findByStoreName(name));