@buoy-gg/assets 7.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +58 -0
- package/README.md +55 -0
- package/lib/commonjs/capture/expo.js +46 -0
- package/lib/commonjs/capture/expo.js.map +1 -0
- package/lib/commonjs/capture/install.js +78 -0
- package/lib/commonjs/capture/install.js.map +1 -0
- package/lib/commonjs/capture/measure.js +186 -0
- package/lib/commonjs/capture/measure.js.map +1 -0
- package/lib/commonjs/capture/metroGraph.js +148 -0
- package/lib/commonjs/capture/metroGraph.js.map +1 -0
- package/lib/commonjs/capture/registry.js +124 -0
- package/lib/commonjs/capture/registry.js.map +1 -0
- package/lib/commonjs/components/AssetDetail.js +205 -0
- package/lib/commonjs/components/AssetDetail.js.map +1 -0
- package/lib/commonjs/components/AssetRow.js +246 -0
- package/lib/commonjs/components/AssetRow.js.map +1 -0
- package/lib/commonjs/components/AssetsFilterBar.js +92 -0
- package/lib/commonjs/components/AssetsFilterBar.js.map +1 -0
- package/lib/commonjs/components/AssetsHeader.js +66 -0
- package/lib/commonjs/components/AssetsHeader.js.map +1 -0
- package/lib/commonjs/components/AssetsIcon.js +19 -0
- package/lib/commonjs/components/AssetsIcon.js.map +1 -0
- package/lib/commonjs/components/AssetsListControls.js +94 -0
- package/lib/commonjs/components/AssetsListControls.js.map +1 -0
- package/lib/commonjs/components/AssetsModal.js +338 -0
- package/lib/commonjs/components/AssetsModal.js.map +1 -0
- package/lib/commonjs/components/BaselineBar.js +110 -0
- package/lib/commonjs/components/BaselineBar.js.map +1 -0
- package/lib/commonjs/export.js +62 -0
- package/lib/commonjs/export.js.map +1 -0
- package/lib/commonjs/format.js +59 -0
- package/lib/commonjs/format.js.map +1 -0
- package/lib/commonjs/hooks/useAssetRecords.js +22 -0
- package/lib/commonjs/hooks/useAssetRecords.js.map +1 -0
- package/lib/commonjs/index.js +260 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/preset.js +45 -0
- package/lib/commonjs/preset.js.map +1 -0
- package/lib/commonjs/remote.js +65 -0
- package/lib/commonjs/remote.js.map +1 -0
- package/lib/commonjs/state.js +51 -0
- package/lib/commonjs/state.js.map +1 -0
- package/lib/commonjs/store/assetStore.js +234 -0
- package/lib/commonjs/store/assetStore.js.map +1 -0
- package/lib/commonjs/store/baseline.js +165 -0
- package/lib/commonjs/store/baseline.js.map +1 -0
- package/lib/commonjs/store/insights.js +129 -0
- package/lib/commonjs/store/insights.js.map +1 -0
- package/lib/commonjs/sync/assetsSyncAdapter.js +157 -0
- package/lib/commonjs/sync/assetsSyncAdapter.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/capture/expo.js +41 -0
- package/lib/module/capture/expo.js.map +1 -0
- package/lib/module/capture/install.js +72 -0
- package/lib/module/capture/install.js.map +1 -0
- package/lib/module/capture/measure.js +179 -0
- package/lib/module/capture/measure.js.map +1 -0
- package/lib/module/capture/metroGraph.js +141 -0
- package/lib/module/capture/metroGraph.js.map +1 -0
- package/lib/module/capture/registry.js +117 -0
- package/lib/module/capture/registry.js.map +1 -0
- package/lib/module/components/AssetDetail.js +201 -0
- package/lib/module/components/AssetDetail.js.map +1 -0
- package/lib/module/components/AssetRow.js +242 -0
- package/lib/module/components/AssetRow.js.map +1 -0
- package/lib/module/components/AssetsFilterBar.js +88 -0
- package/lib/module/components/AssetsFilterBar.js.map +1 -0
- package/lib/module/components/AssetsHeader.js +62 -0
- package/lib/module/components/AssetsHeader.js.map +1 -0
- package/lib/module/components/AssetsIcon.js +12 -0
- package/lib/module/components/AssetsIcon.js.map +1 -0
- package/lib/module/components/AssetsListControls.js +90 -0
- package/lib/module/components/AssetsListControls.js.map +1 -0
- package/lib/module/components/AssetsModal.js +333 -0
- package/lib/module/components/AssetsModal.js.map +1 -0
- package/lib/module/components/BaselineBar.js +106 -0
- package/lib/module/components/BaselineBar.js.map +1 -0
- package/lib/module/export.js +57 -0
- package/lib/module/export.js.map +1 -0
- package/lib/module/format.js +51 -0
- package/lib/module/format.js.map +1 -0
- package/lib/module/hooks/useAssetRecords.js +18 -0
- package/lib/module/hooks/useAssetRecords.js.map +1 -0
- package/lib/module/index.js +59 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/preset.js +40 -0
- package/lib/module/preset.js.map +1 -0
- package/lib/module/remote.js +58 -0
- package/lib/module/remote.js.map +1 -0
- package/lib/module/state.js +47 -0
- package/lib/module/state.js.map +1 -0
- package/lib/module/store/assetStore.js +219 -0
- package/lib/module/store/assetStore.js.map +1 -0
- package/lib/module/store/baseline.js +157 -0
- package/lib/module/store/baseline.js.map +1 -0
- package/lib/module/store/insights.js +125 -0
- package/lib/module/store/insights.js.map +1 -0
- package/lib/module/sync/assetsSyncAdapter.js +153 -0
- package/lib/module/sync/assetsSyncAdapter.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/capture/expo.d.ts +18 -0
- package/lib/typescript/capture/expo.d.ts.map +1 -0
- package/lib/typescript/capture/install.d.ts +17 -0
- package/lib/typescript/capture/install.d.ts.map +1 -0
- package/lib/typescript/capture/measure.d.ts +38 -0
- package/lib/typescript/capture/measure.d.ts.map +1 -0
- package/lib/typescript/capture/metroGraph.d.ts +33 -0
- package/lib/typescript/capture/metroGraph.d.ts.map +1 -0
- package/lib/typescript/capture/registry.d.ts +45 -0
- package/lib/typescript/capture/registry.d.ts.map +1 -0
- package/lib/typescript/components/AssetDetail.d.ts +17 -0
- package/lib/typescript/components/AssetDetail.d.ts.map +1 -0
- package/lib/typescript/components/AssetRow.d.ts +32 -0
- package/lib/typescript/components/AssetRow.d.ts.map +1 -0
- package/lib/typescript/components/AssetsFilterBar.d.ts +16 -0
- package/lib/typescript/components/AssetsFilterBar.d.ts.map +1 -0
- package/lib/typescript/components/AssetsHeader.d.ts +22 -0
- package/lib/typescript/components/AssetsHeader.d.ts.map +1 -0
- package/lib/typescript/components/AssetsIcon.d.ts +9 -0
- package/lib/typescript/components/AssetsIcon.d.ts.map +1 -0
- package/lib/typescript/components/AssetsListControls.d.ts +18 -0
- package/lib/typescript/components/AssetsListControls.d.ts.map +1 -0
- package/lib/typescript/components/AssetsModal.d.ts +24 -0
- package/lib/typescript/components/AssetsModal.d.ts.map +1 -0
- package/lib/typescript/components/BaselineBar.d.ts +13 -0
- package/lib/typescript/components/BaselineBar.d.ts.map +1 -0
- package/lib/typescript/export.d.ts +8 -0
- package/lib/typescript/export.d.ts.map +1 -0
- package/lib/typescript/format.d.ts +13 -0
- package/lib/typescript/format.d.ts.map +1 -0
- package/lib/typescript/hooks/useAssetRecords.d.ts +11 -0
- package/lib/typescript/hooks/useAssetRecords.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +26 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/preset.d.ts +38 -0
- package/lib/typescript/preset.d.ts.map +1 -0
- package/lib/typescript/remote.d.ts +17 -0
- package/lib/typescript/remote.d.ts.map +1 -0
- package/lib/typescript/state.d.ts +41 -0
- package/lib/typescript/state.d.ts.map +1 -0
- package/lib/typescript/store/assetStore.d.ts +51 -0
- package/lib/typescript/store/assetStore.d.ts.map +1 -0
- package/lib/typescript/store/baseline.d.ts +22 -0
- package/lib/typescript/store/baseline.d.ts.map +1 -0
- package/lib/typescript/store/insights.d.ts +7 -0
- package/lib/typescript/store/insights.d.ts.map +1 -0
- package/lib/typescript/sync/assetsSyncAdapter.d.ts +83 -0
- package/lib/typescript/sync/assetsSyncAdapter.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +162 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/package.json +77 -0
- package/src/capture/expo.ts +45 -0
- package/src/capture/install.ts +73 -0
- package/src/capture/measure.ts +167 -0
- package/src/capture/metroGraph.ts +146 -0
- package/src/capture/registry.ts +134 -0
- package/src/components/AssetDetail.tsx +207 -0
- package/src/components/AssetRow.tsx +243 -0
- package/src/components/AssetsFilterBar.tsx +99 -0
- package/src/components/AssetsHeader.tsx +75 -0
- package/src/components/AssetsIcon.tsx +9 -0
- package/src/components/AssetsListControls.tsx +101 -0
- package/src/components/AssetsModal.tsx +368 -0
- package/src/components/BaselineBar.tsx +98 -0
- package/src/export.ts +108 -0
- package/src/format.ts +53 -0
- package/src/hooks/useAssetRecords.ts +26 -0
- package/src/index.tsx +87 -0
- package/src/preset.tsx +35 -0
- package/src/remote.ts +65 -0
- package/src/state.ts +84 -0
- package/src/store/assetStore.ts +209 -0
- package/src/store/baseline.ts +152 -0
- package/src/store/insights.ts +145 -0
- package/src/sync/assetsSyncAdapter.ts +151 -0
- package/src/types.ts +160 -0
package/package.json
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@buoy-gg/assets",
|
|
3
|
+
"version": "7.0.16",
|
|
4
|
+
"description": "See every asset shipped in your React Native app — bundled images, fonts, video and audio with dimensions, scale variants, byte sizes, duplicate detection and WebP-savings audit. Part of Buoy devtools.",
|
|
5
|
+
"main": "lib/commonjs/index",
|
|
6
|
+
"module": "lib/module/index",
|
|
7
|
+
"types": "lib/typescript/index.d.ts",
|
|
8
|
+
"react-native": "src/index.tsx",
|
|
9
|
+
"source": "src/index.tsx",
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"lib"
|
|
13
|
+
],
|
|
14
|
+
"sideEffects": [
|
|
15
|
+
"**/index.*"
|
|
16
|
+
],
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@buoy-gg/shared-ui": "^7.0.16"
|
|
19
|
+
},
|
|
20
|
+
"peerDependencies": {
|
|
21
|
+
"expo-asset": "*",
|
|
22
|
+
"react": "*",
|
|
23
|
+
"react-native": "*"
|
|
24
|
+
},
|
|
25
|
+
"peerDependenciesMeta": {
|
|
26
|
+
"expo-asset": {
|
|
27
|
+
"optional": true
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/react": "^19.1.0",
|
|
32
|
+
"@types/react-native": "^0.73.0",
|
|
33
|
+
"typescript": "~5.8.3"
|
|
34
|
+
},
|
|
35
|
+
"react-native-builder-bob": {
|
|
36
|
+
"source": "src",
|
|
37
|
+
"output": "lib",
|
|
38
|
+
"targets": [
|
|
39
|
+
"commonjs",
|
|
40
|
+
"module",
|
|
41
|
+
"typescript"
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/LovesWorking/react-native-buoy.git",
|
|
47
|
+
"directory": "packages/assets"
|
|
48
|
+
},
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/LovesWorking/react-native-buoy/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/LovesWorking/react-native-buoy/tree/main/packages/assets#readme",
|
|
53
|
+
"publishConfig": {
|
|
54
|
+
"access": "public",
|
|
55
|
+
"tag": "latest"
|
|
56
|
+
},
|
|
57
|
+
"keywords": [
|
|
58
|
+
"react-native",
|
|
59
|
+
"expo",
|
|
60
|
+
"devtools",
|
|
61
|
+
"debugging",
|
|
62
|
+
"react-native-devtools",
|
|
63
|
+
"flipper-alternative",
|
|
64
|
+
"assets",
|
|
65
|
+
"bundle-size",
|
|
66
|
+
"images",
|
|
67
|
+
"fonts",
|
|
68
|
+
"app-size"
|
|
69
|
+
],
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "bob build",
|
|
72
|
+
"typecheck": "tsc --noEmit",
|
|
73
|
+
"clean": "rimraf lib",
|
|
74
|
+
"test": "pnpm run typecheck",
|
|
75
|
+
"postinstall": "echo \"Run pnpm build to compile this package\""
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Expo enrichment — all optional, all read through globalThis.expo guards so
|
|
3
|
+
* bare RN apps (no expo-modules) silently get none of it and no imports are
|
|
4
|
+
* added for anyone.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
interface ExpoGlobal {
|
|
8
|
+
modules?: {
|
|
9
|
+
ExpoFontLoader?: { getLoadedFonts?: () => string[] };
|
|
10
|
+
ExpoUpdates?: { localAssets?: Record<string, string> };
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function expoModules(): ExpoGlobal["modules"] {
|
|
15
|
+
return (globalThis as { expo?: ExpoGlobal }).expo?.modules;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Every loaded font family — build-time embedded (config plugin / UIAppFonts /
|
|
20
|
+
* android assets/fonts) AND runtime Font.loadAsync, straight from the native
|
|
21
|
+
* module Expo documents as a stable global. [] when unavailable (bare RN).
|
|
22
|
+
*/
|
|
23
|
+
export function getLoadedFontFamilies(): string[] {
|
|
24
|
+
try {
|
|
25
|
+
const fonts = expoModules()?.ExpoFontLoader?.getLoadedFonts?.();
|
|
26
|
+
return Array.isArray(fonts) ? [...fonts].sort() : [];
|
|
27
|
+
} catch {
|
|
28
|
+
return [];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* expo-updates' per-file-hash → local URI map: the COMPLETE shipped asset set
|
|
34
|
+
* in Expo Go / OTA environments, independent of what JS has evaluated.
|
|
35
|
+
* null when not running in an updates-managed environment.
|
|
36
|
+
*/
|
|
37
|
+
export function getLocalAssets(): Record<string, string> | null {
|
|
38
|
+
try {
|
|
39
|
+
const map = expoModules()?.ExpoUpdates?.localAssets;
|
|
40
|
+
if (map && typeof map === "object" && Object.keys(map).length > 0) return map;
|
|
41
|
+
return null;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capture install + scan orchestration.
|
|
3
|
+
*
|
|
4
|
+
* installAssetCapture() is cheap and synchronous (registry walk + patch) and
|
|
5
|
+
* runs at package import. The expensive passes — Metro graph fetch and byte
|
|
6
|
+
* measurement — run in rescan(), triggered when the modal opens or via the
|
|
7
|
+
* `rescan` sync action, so merely installing Buoy never hits the network.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { assetsState } from "../state";
|
|
11
|
+
import { invalidate } from "../store/assetStore";
|
|
12
|
+
import { getLoadedFontFamilies, getLocalAssets } from "./expo";
|
|
13
|
+
import { measureSizes } from "./measure";
|
|
14
|
+
import { fetchAssetGraph, getDevServerOrigin } from "./metroGraph";
|
|
15
|
+
import { getRegistryModule, patchRegisterAsset, walkRegistry } from "./registry";
|
|
16
|
+
import type { AssetsScanStatus } from "../types";
|
|
17
|
+
|
|
18
|
+
export function installAssetCapture(): void {
|
|
19
|
+
const s = assetsState();
|
|
20
|
+
if (s.captureDisabled || s.installed) return;
|
|
21
|
+
s.installed = true;
|
|
22
|
+
s.registryAvailable = getRegistryModule() != null;
|
|
23
|
+
s.patched = patchRegisterAsset();
|
|
24
|
+
walkRegistry();
|
|
25
|
+
refreshEnrichment();
|
|
26
|
+
s.lastScanAt = Date.now();
|
|
27
|
+
invalidate();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function refreshEnrichment(): void {
|
|
31
|
+
const s = assetsState();
|
|
32
|
+
s.fontFamilies = getLoadedFontFamilies();
|
|
33
|
+
const localAssets = getLocalAssets();
|
|
34
|
+
s.localAssetCount = localAssets ? Object.keys(localAssets).length : null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Full scan: registry re-walk (catches anything the patch missed), Metro
|
|
39
|
+
* graph merge (dev), Expo enrichment refresh, then size measurement.
|
|
40
|
+
*/
|
|
41
|
+
export async function rescan(): Promise<AssetsScanStatus> {
|
|
42
|
+
const s = assetsState();
|
|
43
|
+
if (s.captureDisabled) return getScanStatus();
|
|
44
|
+
installAssetCapture();
|
|
45
|
+
walkRegistry();
|
|
46
|
+
refreshEnrichment();
|
|
47
|
+
await fetchAssetGraph();
|
|
48
|
+
s.lastScanAt = Date.now();
|
|
49
|
+
invalidate();
|
|
50
|
+
void measureSizes();
|
|
51
|
+
return getScanStatus();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function getScanStatus(): AssetsScanStatus {
|
|
55
|
+
const s = assetsState();
|
|
56
|
+
// Mirrors report the synced DEVICE's status, not the dashboard's.
|
|
57
|
+
if (s.mirrorScanStatus) return s.mirrorScanStatus;
|
|
58
|
+
let registryCount = 0;
|
|
59
|
+
for (const record of s.records) if (record.registryId != null) registryCount++;
|
|
60
|
+
return {
|
|
61
|
+
registryAvailable: s.registryAvailable,
|
|
62
|
+
patched: s.patched,
|
|
63
|
+
registryCount,
|
|
64
|
+
graphSupported: s.graphSupported || getDevServerOrigin() != null,
|
|
65
|
+
graphCount: s.graphCount,
|
|
66
|
+
graphError: s.graphError,
|
|
67
|
+
lastScanAt: s.lastScanAt,
|
|
68
|
+
measuring: s.measuring,
|
|
69
|
+
measuredCount: s.measuredCount,
|
|
70
|
+
fontFamilies: s.fontFamilies,
|
|
71
|
+
localAssetCount: s.localAssetCount,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte-size measurement.
|
|
3
|
+
*
|
|
4
|
+
* Dev only in practice: each scale variant is fetched from the Metro dev
|
|
5
|
+
* server (HEAD first for Content-Length, GET-blob fallback since Metro
|
|
6
|
+
* doesn't reliably answer HEAD) and the per-variant sizes are summed onto
|
|
7
|
+
* the record. Android release resource ids and iOS file:// URIs are not
|
|
8
|
+
* measurable from JS — those records keep `estDecodedBytes` as their only
|
|
9
|
+
* size signal and the UI marks them as estimates.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { assetsState } from "../state";
|
|
13
|
+
import { getSnapshot, invalidate, updateRecord } from "../store/assetStore";
|
|
14
|
+
import { getDevServerOrigin } from "./metroGraph";
|
|
15
|
+
import type { AssetRecord } from "../types";
|
|
16
|
+
|
|
17
|
+
const CONCURRENCY = 4;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Best displayable URL for a record: the resolved runtime URI, or (dev) the
|
|
21
|
+
* same dev-server URL the measurement pass uses — this is what gives graph-
|
|
22
|
+
* only (never-loaded) images real thumbnails.
|
|
23
|
+
*/
|
|
24
|
+
export function previewURL(record: AssetRecord): string | null {
|
|
25
|
+
if (record.resolvedUri) return record.resolvedUri;
|
|
26
|
+
const variants = variantURLs(record);
|
|
27
|
+
return variants.length ? variants[0]!.url : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Image source for thumbnails/previews. On device the NUMERIC registry id is
|
|
32
|
+
* preferred (resolves natively — works for Android release resource ids too).
|
|
33
|
+
* On dashboard mirrors that id belongs to the DEVICE's registry and rn-web
|
|
34
|
+
* throws "asset with ID N could not be found" — use the device's http(s)
|
|
35
|
+
* URL instead (the Metro dev server is reachable from the desktop).
|
|
36
|
+
*/
|
|
37
|
+
export function displaySource(
|
|
38
|
+
record: AssetRecord
|
|
39
|
+
): number | { uri: string } | null {
|
|
40
|
+
const mirror = assetsState().captureDisabled;
|
|
41
|
+
if (!mirror && record.registryId != null) return record.registryId;
|
|
42
|
+
const uri = previewURL(record);
|
|
43
|
+
if (!uri) return null;
|
|
44
|
+
if (mirror && !/^https?:\/\//.test(uri)) return null;
|
|
45
|
+
return { uri };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** All scale-variant URLs for a record, derived without re-encoding pitfalls. */
|
|
49
|
+
export function variantURLs(record: AssetRecord): Array<{ scale: number; url: string }> {
|
|
50
|
+
const scales = record.scales.length ? record.scales : [1];
|
|
51
|
+
|
|
52
|
+
// Preferred: string surgery on the URL resolveAssetSource already proved
|
|
53
|
+
// works (handles Expo's ?unstable_path= form and monorepo rewrites).
|
|
54
|
+
const resolved = record.resolvedUri;
|
|
55
|
+
if (resolved && /^https?:\/\//.test(resolved) && record.resolvedScale != null) {
|
|
56
|
+
const pickedSuffix = record.resolvedScale === 1 ? "" : `@${record.resolvedScale}x`;
|
|
57
|
+
const marker = `${pickedSuffix}.${record.type}`;
|
|
58
|
+
if (resolved.includes(marker) || pickedSuffix === "") {
|
|
59
|
+
return scales.map((scale) => {
|
|
60
|
+
const suffix = scale === 1 ? "" : `@${scale}x`;
|
|
61
|
+
const url = (
|
|
62
|
+
pickedSuffix === ""
|
|
63
|
+
? resolved.replace(`.${record.type}`, `${suffix}.${record.type}`)
|
|
64
|
+
: resolved.replace(marker, `${suffix}.${record.type}`)
|
|
65
|
+
).replace(/ /g, "%20"); // RN's asset path builder doesn't encode spaces
|
|
66
|
+
return { scale, url };
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Graph-only records: rebuild the dev-server URL the way Metro's
|
|
72
|
+
// AssetSourceResolver does (plain concatenation), encoding only spaces —
|
|
73
|
+
// full encodeURI would double-encode Expo's pre-escaped unstable_path.
|
|
74
|
+
const origin = getDevServerOrigin();
|
|
75
|
+
if (!origin || !record.httpServerLocation) return [];
|
|
76
|
+
return scales.map((scale) => {
|
|
77
|
+
const suffix = scale === 1 ? "" : `@${scale}x`;
|
|
78
|
+
const path = `${record.httpServerLocation}/${record.name}${suffix}.${record.type}`;
|
|
79
|
+
const sep = path.includes("?") ? "&" : "?";
|
|
80
|
+
const url = `${origin}${path}${sep}platform=${platformName()}${
|
|
81
|
+
record.hash ? `&hash=${record.hash}` : ""
|
|
82
|
+
}`.replace(/ /g, "%20");
|
|
83
|
+
return { scale, url };
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function platformName(): string {
|
|
88
|
+
try {
|
|
89
|
+
return require("react-native").Platform.OS;
|
|
90
|
+
} catch {
|
|
91
|
+
return "ios";
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function fetchSize(url: string): Promise<number | null> {
|
|
96
|
+
try {
|
|
97
|
+
const head = await fetch(url, { method: "HEAD" });
|
|
98
|
+
if (head.ok) {
|
|
99
|
+
const length = Number(head.headers.get("content-length"));
|
|
100
|
+
if (Number.isFinite(length) && length > 0) return length;
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
// fall through to GET
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const response = await fetch(url);
|
|
107
|
+
if (!response.ok) return null;
|
|
108
|
+
const blob = await response.blob();
|
|
109
|
+
return blob.size > 0 ? blob.size : null;
|
|
110
|
+
} catch {
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function measureRecord(record: AssetRecord): Promise<void> {
|
|
116
|
+
const variants = variantURLs(record);
|
|
117
|
+
if (!variants.length) {
|
|
118
|
+
updateRecord(record.id, { sizeSource: "estimate" });
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
const sizesByScale: Record<string, number> = {};
|
|
122
|
+
let total = 0;
|
|
123
|
+
for (const { scale, url } of variants) {
|
|
124
|
+
const size = await fetchSize(url);
|
|
125
|
+
if (size != null) {
|
|
126
|
+
sizesByScale[String(scale)] = size;
|
|
127
|
+
total += size;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (total > 0) {
|
|
131
|
+
updateRecord(record.id, { sizesByScale, sizeBytes: total, sizeSource: "measured" });
|
|
132
|
+
} else {
|
|
133
|
+
updateRecord(record.id, { sizeSource: "estimate" });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Measure every unmeasured record; safe to re-run (skips measured ones). */
|
|
138
|
+
export async function measureSizes(): Promise<{ measured: number; total: number }> {
|
|
139
|
+
const s = assetsState();
|
|
140
|
+
if (s.measuring) return { measured: s.measuredCount, total: getSnapshot().length };
|
|
141
|
+
const pending = getSnapshot().filter((r) => r.sizeSource !== "measured");
|
|
142
|
+
s.measuring = true;
|
|
143
|
+
invalidate();
|
|
144
|
+
try {
|
|
145
|
+
let index = 0;
|
|
146
|
+
const worker = async () => {
|
|
147
|
+
for (;;) {
|
|
148
|
+
const next = pending[index++];
|
|
149
|
+
if (!next) return;
|
|
150
|
+
await measureRecord(next);
|
|
151
|
+
s.measuredCount = getSnapshot().filter(
|
|
152
|
+
(r) => r.sizeSource === "measured"
|
|
153
|
+
).length;
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
await Promise.all(
|
|
157
|
+
Array.from({ length: Math.min(CONCURRENCY, pending.length) }, worker)
|
|
158
|
+
);
|
|
159
|
+
} finally {
|
|
160
|
+
s.measuring = false;
|
|
161
|
+
invalidate();
|
|
162
|
+
}
|
|
163
|
+
return {
|
|
164
|
+
measured: s.measuredCount,
|
|
165
|
+
total: getSnapshot().length,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metro dev-server graph fetch.
|
|
3
|
+
*
|
|
4
|
+
* The dev server serves `<entry>.assets` next to `<entry>.bundle` — the full
|
|
5
|
+
* dependency graph's AssetData[], INCLUDING assets never required at runtime
|
|
6
|
+
* and the `fileSystemLocation`/`files` fields Metro strips from runtime
|
|
7
|
+
* records. Deriving the URL by swapping `.bundle` → `.assets` on the running
|
|
8
|
+
* bundle's scriptURL keeps the entry path and platform/dev query params
|
|
9
|
+
* intact for both RN CLI (index.bundle) and Expo (…/entry.bundle) layouts.
|
|
10
|
+
*
|
|
11
|
+
* Graph-only records (in the graph, not the registry) are exactly the
|
|
12
|
+
* "shipped but never loaded" set. Dev-only: no server, no graph.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { assetsState } from "../state";
|
|
16
|
+
import { assetKey, invalidate, upsertRecord } from "../store/assetStore";
|
|
17
|
+
|
|
18
|
+
interface GraphAssetData {
|
|
19
|
+
__packager_asset?: boolean;
|
|
20
|
+
fileSystemLocation?: string;
|
|
21
|
+
httpServerLocation: string;
|
|
22
|
+
width?: number | null;
|
|
23
|
+
height?: number | null;
|
|
24
|
+
scales: number[];
|
|
25
|
+
files?: string[];
|
|
26
|
+
fileHashes?: string[];
|
|
27
|
+
hash: string;
|
|
28
|
+
name: string;
|
|
29
|
+
type: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function getScriptURL(): string | null {
|
|
33
|
+
try {
|
|
34
|
+
const { NativeModules } = require("react-native");
|
|
35
|
+
const sourceCode = NativeModules?.SourceCode;
|
|
36
|
+
const url: unknown =
|
|
37
|
+
sourceCode?.getConstants?.().scriptURL ?? sourceCode?.scriptURL;
|
|
38
|
+
return typeof url === "string" ? url : null;
|
|
39
|
+
} catch {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** http(s) origin of the Metro dev server, or null when running embedded. */
|
|
45
|
+
export function getDevServerOrigin(): string | null {
|
|
46
|
+
const scriptURL = getScriptURL();
|
|
47
|
+
const match = scriptURL?.match(/^https?:\/\/[^/]+/);
|
|
48
|
+
return match ? match[0] : null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Candidate graph URLs, most-likely first. The straight `.bundle`→`.assets`
|
|
53
|
+
* swap works for RN CLI, but Expo dev builds serve a VIRTUAL entry
|
|
54
|
+
* (.expo/.virtual-metro-entry.bundle) that only exists for .bundle requests —
|
|
55
|
+
* its .assets twin 404s — so the real entry modules are tried next, keeping
|
|
56
|
+
* the original query string (platform/dev/etc.) intact.
|
|
57
|
+
*/
|
|
58
|
+
export function getGraphURLCandidates(): string[] {
|
|
59
|
+
const scriptURL = getScriptURL();
|
|
60
|
+
if (!scriptURL || !/^https?:\/\//.test(scriptURL)) return [];
|
|
61
|
+
const origin = getDevServerOrigin();
|
|
62
|
+
const queryIndex = scriptURL.indexOf("?");
|
|
63
|
+
const query = queryIndex >= 0 ? scriptURL.slice(queryIndex) : "?platform=ios&dev=true";
|
|
64
|
+
const candidates: string[] = [];
|
|
65
|
+
if (scriptURL.includes(".bundle")) {
|
|
66
|
+
candidates.push(scriptURL.replace(".bundle", ".assets"));
|
|
67
|
+
}
|
|
68
|
+
if (origin) {
|
|
69
|
+
candidates.push(
|
|
70
|
+
`${origin}/node_modules/expo-router/entry.assets${query}`,
|
|
71
|
+
`${origin}/node_modules/expo/AppEntry.assets${query}`,
|
|
72
|
+
`${origin}/index.assets${query}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return candidates;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Fetch the full asset graph from the dev server and merge it into the store. */
|
|
79
|
+
export async function fetchAssetGraph(): Promise<
|
|
80
|
+
{ ok: true; count: number } | { ok: false; message: string }
|
|
81
|
+
> {
|
|
82
|
+
const s = assetsState();
|
|
83
|
+
const candidates = getGraphURLCandidates();
|
|
84
|
+
if (!candidates.length) {
|
|
85
|
+
s.graphSupported = false;
|
|
86
|
+
s.graphError = "No Metro dev server (embedded bundle)";
|
|
87
|
+
invalidate();
|
|
88
|
+
return { ok: false, message: s.graphError };
|
|
89
|
+
}
|
|
90
|
+
if (s.graphFetchInFlight) return { ok: false, message: "Graph fetch already running" };
|
|
91
|
+
s.graphFetchInFlight = true;
|
|
92
|
+
try {
|
|
93
|
+
let data: unknown = null;
|
|
94
|
+
let lastError = "";
|
|
95
|
+
for (const url of candidates) {
|
|
96
|
+
try {
|
|
97
|
+
const response = await fetch(url);
|
|
98
|
+
if (!response.ok) {
|
|
99
|
+
lastError = `Metro responded ${response.status} for ${url}`;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const parsed: unknown = await response.json();
|
|
103
|
+
if (Array.isArray(parsed)) {
|
|
104
|
+
data = parsed;
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
lastError = "Unexpected .assets payload shape";
|
|
108
|
+
} catch (error) {
|
|
109
|
+
lastError = String((error as Error)?.message ?? error);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!Array.isArray(data)) throw new Error(lastError || "No graph endpoint answered");
|
|
113
|
+
const graph = data as GraphAssetData[];
|
|
114
|
+
for (const asset of graph) {
|
|
115
|
+
if (!asset || typeof asset.name !== "string" || typeof asset.type !== "string") {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
upsertRecord({
|
|
119
|
+
key: assetKey(asset.httpServerLocation, asset.name, asset.type),
|
|
120
|
+
name: asset.name,
|
|
121
|
+
type: asset.type,
|
|
122
|
+
origin: "graph",
|
|
123
|
+
hash: asset.hash,
|
|
124
|
+
httpServerLocation: asset.httpServerLocation,
|
|
125
|
+
width: asset.width ?? undefined,
|
|
126
|
+
height: asset.height ?? undefined,
|
|
127
|
+
scales: asset.scales,
|
|
128
|
+
fileSystemLocation: asset.fileSystemLocation,
|
|
129
|
+
files: asset.files,
|
|
130
|
+
fileHashes: asset.fileHashes,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
s.graphSupported = true;
|
|
134
|
+
s.graphCount = graph.length;
|
|
135
|
+
s.graphError = null;
|
|
136
|
+
invalidate();
|
|
137
|
+
return { ok: true, count: graph.length };
|
|
138
|
+
} catch (error) {
|
|
139
|
+
s.graphSupported = getDevServerOrigin() != null;
|
|
140
|
+
s.graphError = String((error as Error)?.message ?? error);
|
|
141
|
+
invalidate();
|
|
142
|
+
return { ok: false, message: s.graphError };
|
|
143
|
+
} finally {
|
|
144
|
+
s.graphFetchInFlight = false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime access to @react-native/assets-registry.
|
|
3
|
+
*
|
|
4
|
+
* Asset ids are contiguous 1..N (`assets.push` return value), so a plain
|
|
5
|
+
* getAssetByID walk enumerates every asset registered so far — including
|
|
6
|
+
* everything registered BEFORE this tool was evaluated. That's why, unlike
|
|
7
|
+
* the images tool, there is no "must be the first import" requirement here:
|
|
8
|
+
* the walk backfills whatever the patch missed.
|
|
9
|
+
*
|
|
10
|
+
* registerAsset is patched (plain non-frozen CommonJS export, looked up at
|
|
11
|
+
* call time by Metro's generated asset modules) purely for LIVE updates as
|
|
12
|
+
* lazily-required assets register later.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { upsertRecord, assetKey } from "../store/assetStore";
|
|
16
|
+
import type { AssetRecord } from "../types";
|
|
17
|
+
|
|
18
|
+
/** Runtime shape of a registry entry (fileSystemLocation/files are stripped by Metro). */
|
|
19
|
+
export interface PackagerAsset {
|
|
20
|
+
__packager_asset: boolean;
|
|
21
|
+
httpServerLocation: string;
|
|
22
|
+
width?: number | null;
|
|
23
|
+
height?: number | null;
|
|
24
|
+
scales: number[];
|
|
25
|
+
hash: string;
|
|
26
|
+
name: string;
|
|
27
|
+
type: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface RegistryModule {
|
|
31
|
+
registerAsset: (asset: PackagerAsset) => number;
|
|
32
|
+
getAssetByID: (assetId: number) => PackagerAsset | undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let cachedRegistry: RegistryModule | null | undefined;
|
|
36
|
+
|
|
37
|
+
export function getRegistryModule(): RegistryModule | null {
|
|
38
|
+
if (cachedRegistry !== undefined) return cachedRegistry;
|
|
39
|
+
let mod: RegistryModule | null = null;
|
|
40
|
+
try {
|
|
41
|
+
mod = require("@react-native/assets-registry/registry");
|
|
42
|
+
} catch {
|
|
43
|
+
try {
|
|
44
|
+
// Older RN layouts re-export from here; same module instance when both exist.
|
|
45
|
+
mod = require("react-native/Libraries/Image/AssetRegistry");
|
|
46
|
+
} catch {
|
|
47
|
+
mod = null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
cachedRegistry =
|
|
51
|
+
mod && typeof mod.getAssetByID === "function" ? mod : null;
|
|
52
|
+
return cachedRegistry;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** resolveAssetSource(id) → this environment's URI (dev URL / file:// / res id). */
|
|
56
|
+
export function resolveRegistryAsset(
|
|
57
|
+
registryId: number
|
|
58
|
+
): { uri: string; scale: number } | null {
|
|
59
|
+
try {
|
|
60
|
+
const { Image } = require("react-native");
|
|
61
|
+
const resolved = Image.resolveAssetSource(registryId);
|
|
62
|
+
if (resolved && typeof resolved.uri === "string") {
|
|
63
|
+
return { uri: resolved.uri, scale: resolved.scale ?? 1 };
|
|
64
|
+
}
|
|
65
|
+
} catch {
|
|
66
|
+
// resolution is enrichment — never let it break the walk
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function upsertFromPackagerAsset(
|
|
72
|
+
registryId: number,
|
|
73
|
+
asset: PackagerAsset
|
|
74
|
+
): AssetRecord {
|
|
75
|
+
const resolved = resolveRegistryAsset(registryId);
|
|
76
|
+
return upsertRecord({
|
|
77
|
+
key: assetKey(asset.httpServerLocation, asset.name, asset.type),
|
|
78
|
+
name: asset.name,
|
|
79
|
+
type: asset.type,
|
|
80
|
+
origin: "registry",
|
|
81
|
+
registryId,
|
|
82
|
+
hash: asset.hash,
|
|
83
|
+
httpServerLocation: asset.httpServerLocation,
|
|
84
|
+
width: asset.width ?? undefined,
|
|
85
|
+
height: asset.height ?? undefined,
|
|
86
|
+
scales: asset.scales,
|
|
87
|
+
resolvedUri: resolved?.uri,
|
|
88
|
+
resolvedScale: resolved?.scale,
|
|
89
|
+
loaded: true,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** Enumerate every asset registered so far. Returns the highest id seen. */
|
|
94
|
+
export function walkRegistry(): number {
|
|
95
|
+
const registry = getRegistryModule();
|
|
96
|
+
if (!registry) return 0;
|
|
97
|
+
let id = 0;
|
|
98
|
+
for (;;) {
|
|
99
|
+
const asset = registry.getAssetByID(id + 1);
|
|
100
|
+
if (!asset) break;
|
|
101
|
+
id++;
|
|
102
|
+
upsertFromPackagerAsset(id, asset);
|
|
103
|
+
}
|
|
104
|
+
return id;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const PATCH_MARKER = "__buoyAssetsPatched";
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Wrap registerAsset so future registrations stream in live. Safe to call
|
|
111
|
+
* more than once (marker guard); the walk covers anything registered before.
|
|
112
|
+
*/
|
|
113
|
+
export function patchRegisterAsset(): boolean {
|
|
114
|
+
const registry = getRegistryModule();
|
|
115
|
+
if (!registry) return false;
|
|
116
|
+
const original = registry.registerAsset;
|
|
117
|
+
if ((original as unknown as Record<string, unknown>)[PATCH_MARKER]) return true;
|
|
118
|
+
const wrapped = (asset: PackagerAsset): number => {
|
|
119
|
+
const id = original.call(registry, asset);
|
|
120
|
+
try {
|
|
121
|
+
upsertFromPackagerAsset(id, asset);
|
|
122
|
+
} catch {
|
|
123
|
+
// never break app asset registration
|
|
124
|
+
}
|
|
125
|
+
return id;
|
|
126
|
+
};
|
|
127
|
+
(wrapped as unknown as Record<string, unknown>)[PATCH_MARKER] = true;
|
|
128
|
+
try {
|
|
129
|
+
registry.registerAsset = wrapped;
|
|
130
|
+
} catch {
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
return true;
|
|
134
|
+
}
|