@buoy-gg/images 7.0.3
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 +58 -0
- package/lib/commonjs/capture/actions.js +227 -0
- package/lib/commonjs/capture/actions.js.map +1 -0
- package/lib/commonjs/capture/install.js +41 -0
- package/lib/commonjs/capture/install.js.map +1 -0
- package/lib/commonjs/capture/installExpoImage.js +99 -0
- package/lib/commonjs/capture/installExpoImage.js.map +1 -0
- package/lib/commonjs/capture/installRnImage.js +128 -0
- package/lib/commonjs/capture/installRnImage.js.map +1 -0
- package/lib/commonjs/capture/overrides.js +286 -0
- package/lib/commonjs/capture/overrides.js.map +1 -0
- package/lib/commonjs/capture/savings.js +193 -0
- package/lib/commonjs/capture/savings.js.map +1 -0
- package/lib/commonjs/capture/shared.js +367 -0
- package/lib/commonjs/capture/shared.js.map +1 -0
- package/lib/commonjs/components/ImageDetail.js +444 -0
- package/lib/commonjs/components/ImageDetail.js.map +1 -0
- package/lib/commonjs/components/ImageDetailFooter.js +232 -0
- package/lib/commonjs/components/ImageDetailFooter.js.map +1 -0
- package/lib/commonjs/components/ImageRow.js +164 -0
- package/lib/commonjs/components/ImageRow.js.map +1 -0
- package/lib/commonjs/components/ImagesHeader.js +63 -0
- package/lib/commonjs/components/ImagesHeader.js.map +1 -0
- package/lib/commonjs/components/ImagesIcon.js +59 -0
- package/lib/commonjs/components/ImagesIcon.js.map +1 -0
- package/lib/commonjs/components/ImagesListFooter.js +194 -0
- package/lib/commonjs/components/ImagesListFooter.js.map +1 -0
- package/lib/commonjs/components/ImagesModal.js +173 -0
- package/lib/commonjs/components/ImagesModal.js.map +1 -0
- package/lib/commonjs/components/ImagesRoot.js +24 -0
- package/lib/commonjs/components/ImagesRoot.js.map +1 -0
- package/lib/commonjs/components/InsightsBar.js +59 -0
- package/lib/commonjs/components/InsightsBar.js.map +1 -0
- package/lib/commonjs/export.js +81 -0
- package/lib/commonjs/export.js.map +1 -0
- package/lib/commonjs/format.js +117 -0
- package/lib/commonjs/format.js.map +1 -0
- package/lib/commonjs/hooks/useImageRecords.js +20 -0
- package/lib/commonjs/hooks/useImageRecords.js.map +1 -0
- package/lib/commonjs/index.js +236 -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/register.js +19 -0
- package/lib/commonjs/register.js.map +1 -0
- package/lib/commonjs/state.js +54 -0
- package/lib/commonjs/state.js.map +1 -0
- package/lib/commonjs/store/imageEventStore.js +160 -0
- package/lib/commonjs/store/imageEventStore.js.map +1 -0
- package/lib/commonjs/store/insights.js +76 -0
- package/lib/commonjs/store/insights.js.map +1 -0
- package/lib/commonjs/sync/imagesSyncAdapter.js +315 -0
- package/lib/commonjs/sync/imagesSyncAdapter.js.map +1 -0
- package/lib/commonjs/types.js +2 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/module/capture/actions.js +214 -0
- package/lib/module/capture/actions.js.map +1 -0
- package/lib/module/capture/install.js +36 -0
- package/lib/module/capture/install.js.map +1 -0
- package/lib/module/capture/installExpoImage.js +94 -0
- package/lib/module/capture/installExpoImage.js.map +1 -0
- package/lib/module/capture/installRnImage.js +122 -0
- package/lib/module/capture/installRnImage.js.map +1 -0
- package/lib/module/capture/overrides.js +269 -0
- package/lib/module/capture/overrides.js.map +1 -0
- package/lib/module/capture/savings.js +189 -0
- package/lib/module/capture/savings.js.map +1 -0
- package/lib/module/capture/shared.js +359 -0
- package/lib/module/capture/shared.js.map +1 -0
- package/lib/module/components/ImageDetail.js +440 -0
- package/lib/module/components/ImageDetail.js.map +1 -0
- package/lib/module/components/ImageDetailFooter.js +228 -0
- package/lib/module/components/ImageDetailFooter.js.map +1 -0
- package/lib/module/components/ImageRow.js +160 -0
- package/lib/module/components/ImageRow.js.map +1 -0
- package/lib/module/components/ImagesHeader.js +59 -0
- package/lib/module/components/ImagesHeader.js.map +1 -0
- package/lib/module/components/ImagesIcon.js +55 -0
- package/lib/module/components/ImagesIcon.js.map +1 -0
- package/lib/module/components/ImagesListFooter.js +190 -0
- package/lib/module/components/ImagesListFooter.js.map +1 -0
- package/lib/module/components/ImagesModal.js +169 -0
- package/lib/module/components/ImagesModal.js.map +1 -0
- package/lib/module/components/ImagesRoot.js +20 -0
- package/lib/module/components/ImagesRoot.js.map +1 -0
- package/lib/module/components/InsightsBar.js +55 -0
- package/lib/module/components/InsightsBar.js.map +1 -0
- package/lib/module/export.js +76 -0
- package/lib/module/export.js.map +1 -0
- package/lib/module/format.js +108 -0
- package/lib/module/format.js.map +1 -0
- package/lib/module/hooks/useImageRecords.js +16 -0
- package/lib/module/hooks/useImageRecords.js.map +1 -0
- package/lib/module/index.js +73 -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/register.js +19 -0
- package/lib/module/register.js.map +1 -0
- package/lib/module/state.js +50 -0
- package/lib/module/state.js.map +1 -0
- package/lib/module/store/imageEventStore.js +146 -0
- package/lib/module/store/imageEventStore.js.map +1 -0
- package/lib/module/store/insights.js +71 -0
- package/lib/module/store/insights.js.map +1 -0
- package/lib/module/sync/imagesSyncAdapter.js +311 -0
- package/lib/module/sync/imagesSyncAdapter.js.map +1 -0
- package/lib/module/types.js +2 -0
- package/lib/module/types.js.map +1 -0
- package/lib/typescript/capture/actions.d.ts +49 -0
- package/lib/typescript/capture/actions.d.ts.map +1 -0
- package/lib/typescript/capture/install.d.ts +15 -0
- package/lib/typescript/capture/install.d.ts.map +1 -0
- package/lib/typescript/capture/installExpoImage.d.ts +23 -0
- package/lib/typescript/capture/installExpoImage.d.ts.map +1 -0
- package/lib/typescript/capture/installRnImage.d.ts +24 -0
- package/lib/typescript/capture/installRnImage.d.ts.map +1 -0
- package/lib/typescript/capture/overrides.d.ts +83 -0
- package/lib/typescript/capture/overrides.d.ts.map +1 -0
- package/lib/typescript/capture/savings.d.ts +37 -0
- package/lib/typescript/capture/savings.d.ts.map +1 -0
- package/lib/typescript/capture/shared.d.ts +61 -0
- package/lib/typescript/capture/shared.d.ts.map +1 -0
- package/lib/typescript/components/ImageDetail.d.ts +13 -0
- package/lib/typescript/components/ImageDetail.d.ts.map +1 -0
- package/lib/typescript/components/ImageDetailFooter.d.ts +19 -0
- package/lib/typescript/components/ImageDetailFooter.d.ts.map +1 -0
- package/lib/typescript/components/ImageRow.d.ts +15 -0
- package/lib/typescript/components/ImageRow.d.ts.map +1 -0
- package/lib/typescript/components/ImagesHeader.d.ts +25 -0
- package/lib/typescript/components/ImagesHeader.d.ts.map +1 -0
- package/lib/typescript/components/ImagesIcon.d.ts +13 -0
- package/lib/typescript/components/ImagesIcon.d.ts.map +1 -0
- package/lib/typescript/components/ImagesListFooter.d.ts +8 -0
- package/lib/typescript/components/ImagesListFooter.d.ts.map +1 -0
- package/lib/typescript/components/ImagesModal.d.ts +17 -0
- package/lib/typescript/components/ImagesModal.d.ts.map +1 -0
- package/lib/typescript/components/ImagesRoot.d.ts +11 -0
- package/lib/typescript/components/ImagesRoot.d.ts.map +1 -0
- package/lib/typescript/components/InsightsBar.d.ts +13 -0
- package/lib/typescript/components/InsightsBar.d.ts.map +1 -0
- package/lib/typescript/export.d.ts +12 -0
- package/lib/typescript/export.d.ts.map +1 -0
- package/lib/typescript/format.d.ts +26 -0
- package/lib/typescript/format.d.ts.map +1 -0
- package/lib/typescript/hooks/useImageRecords.d.ts +11 -0
- package/lib/typescript/hooks/useImageRecords.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +33 -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/register.d.ts +15 -0
- package/lib/typescript/register.d.ts.map +1 -0
- package/lib/typescript/state.d.ts +54 -0
- package/lib/typescript/state.d.ts.map +1 -0
- package/lib/typescript/store/imageEventStore.d.ts +34 -0
- package/lib/typescript/store/imageEventStore.d.ts.map +1 -0
- package/lib/typescript/store/insights.d.ts +11 -0
- package/lib/typescript/store/insights.d.ts.map +1 -0
- package/lib/typescript/sync/imagesSyncAdapter.d.ts +205 -0
- package/lib/typescript/sync/imagesSyncAdapter.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +121 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/package.json +67 -0
- package/register.d.ts +1 -0
- package/register.js +15 -0
- package/src/capture/actions.ts +206 -0
- package/src/capture/install.ts +36 -0
- package/src/capture/installExpoImage.ts +115 -0
- package/src/capture/installRnImage.tsx +133 -0
- package/src/capture/overrides.ts +282 -0
- package/src/capture/savings.ts +193 -0
- package/src/capture/shared.ts +387 -0
- package/src/components/ImageDetail.tsx +456 -0
- package/src/components/ImageDetailFooter.tsx +270 -0
- package/src/components/ImageRow.tsx +148 -0
- package/src/components/ImagesHeader.tsx +77 -0
- package/src/components/ImagesIcon.tsx +66 -0
- package/src/components/ImagesListFooter.tsx +193 -0
- package/src/components/ImagesModal.tsx +195 -0
- package/src/components/ImagesRoot.tsx +18 -0
- package/src/components/InsightsBar.tsx +51 -0
- package/src/export.ts +100 -0
- package/src/format.ts +85 -0
- package/src/hooks/useImageRecords.ts +24 -0
- package/src/index.tsx +108 -0
- package/src/preset.tsx +35 -0
- package/src/register.ts +17 -0
- package/src/state.ts +88 -0
- package/src/store/imageEventStore.ts +162 -0
- package/src/store/insights.ts +66 -0
- package/src/sync/imagesSyncAdapter.ts +261 -0
- package/src/types.ts +111 -0
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Savings prover — Squoosh-style, on device.
|
|
3
|
+
*
|
|
4
|
+
* Instead of estimating what an oversized source wastes, PROVE it: re-encode
|
|
5
|
+
* the image at the displayed size (× pixel ratio) as WebP via
|
|
6
|
+
* expo-image-manipulator (verified: chainable resize → saveAsync WebP on BOTH
|
|
7
|
+
* platforms, available in Expo Go), stat the output file, and report real
|
|
8
|
+
* bytes vs the original. The optimized file:// URI can then be A/B'd in place
|
|
9
|
+
* through the existing swap-url override.
|
|
10
|
+
*
|
|
11
|
+
* Original size is resolved in tiers: observed download bytes (onProgress
|
|
12
|
+
* total) → expo disk-cache file size → HEAD Content-Length.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { neededPixels } from "../store/imageEventStore";
|
|
16
|
+
import type { ImageRecord } from "../types";
|
|
17
|
+
|
|
18
|
+
export interface SavingsResult {
|
|
19
|
+
ok: boolean;
|
|
20
|
+
message: string;
|
|
21
|
+
/** Real encoded bytes of the original (when determinable). */
|
|
22
|
+
originalBytes?: number;
|
|
23
|
+
originalBytesSource?: "download" | "cacheFile" | "head";
|
|
24
|
+
/** Real encoded bytes of the optimized WebP on disk. */
|
|
25
|
+
optimizedBytes?: number;
|
|
26
|
+
/** file:// URI of the optimized image (feed to swap-url to preview). */
|
|
27
|
+
optimizedUri?: string;
|
|
28
|
+
optimizedDims?: { width: number; height: number };
|
|
29
|
+
savedBytes?: number;
|
|
30
|
+
savedPct?: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
interface LegacyFs {
|
|
34
|
+
getInfoAsync?: (uri: string, options?: { size?: boolean }) => Promise<{
|
|
35
|
+
exists: boolean;
|
|
36
|
+
size?: number;
|
|
37
|
+
}>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function loadLegacyFs(): LegacyFs | null {
|
|
41
|
+
try {
|
|
42
|
+
const mod = require("expo-file-system/legacy");
|
|
43
|
+
const fs = mod?.default || mod;
|
|
44
|
+
if (fs?.getInfoAsync) return fs;
|
|
45
|
+
} catch {
|
|
46
|
+
// try main import
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
const mod = require("expo-file-system");
|
|
50
|
+
const fs = mod?.default || mod;
|
|
51
|
+
if (fs?.getInfoAsync) return fs;
|
|
52
|
+
} catch {
|
|
53
|
+
// not installed
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function fileSize(uri: string): Promise<number | null> {
|
|
59
|
+
const fs = loadLegacyFs();
|
|
60
|
+
if (!fs?.getInfoAsync) return null;
|
|
61
|
+
try {
|
|
62
|
+
const normalized = uri.startsWith("file://") ? uri : `file://${uri}`;
|
|
63
|
+
const info = await fs.getInfoAsync(normalized, { size: true });
|
|
64
|
+
return info.exists && typeof info.size === "number" ? info.size : null;
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function resolveOriginalBytes(
|
|
71
|
+
record: ImageRecord
|
|
72
|
+
): Promise<{ bytes: number; source: SavingsResult["originalBytesSource"] } | null> {
|
|
73
|
+
if (record.bytesTotal && record.bytesTotal > 0) {
|
|
74
|
+
return { bytes: record.bytesTotal, source: "download" };
|
|
75
|
+
}
|
|
76
|
+
// expo disk cache file
|
|
77
|
+
if (record.lib === "expo") {
|
|
78
|
+
try {
|
|
79
|
+
const expo = require("expo-image");
|
|
80
|
+
const path: string | null = await expo.Image.getCachePathAsync(record.uri);
|
|
81
|
+
if (path) {
|
|
82
|
+
const size = await fileSize(path);
|
|
83
|
+
if (size != null) return { bytes: size, source: "cacheFile" };
|
|
84
|
+
}
|
|
85
|
+
} catch {
|
|
86
|
+
// fall through
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// HEAD request (network sources only)
|
|
90
|
+
if (record.sourceKind === "network") {
|
|
91
|
+
try {
|
|
92
|
+
const response = await fetch(record.uri, { method: "HEAD" });
|
|
93
|
+
const length = Number(response.headers.get("content-length"));
|
|
94
|
+
if (Number.isFinite(length) && length > 0) return { bytes: length, source: "head" };
|
|
95
|
+
} catch {
|
|
96
|
+
// unknown original size is fine
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Re-encode `record`'s source at its displayed size as WebP and measure it.
|
|
104
|
+
* Requires expo-image-manipulator (graceful message when absent).
|
|
105
|
+
*/
|
|
106
|
+
export async function proveSavings(record: ImageRecord): Promise<SavingsResult> {
|
|
107
|
+
if (!record.uri || (record.sourceKind !== "network" && record.sourceKind !== "asset" && record.sourceKind !== "file")) {
|
|
108
|
+
return { ok: false, message: "No loadable source URI on this record" };
|
|
109
|
+
}
|
|
110
|
+
const needed = neededPixels(record);
|
|
111
|
+
if (!needed) {
|
|
112
|
+
return { ok: false, message: "No layout yet — the image must render once to know its displayed size" };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let manipulator: {
|
|
116
|
+
ImageManipulator?: { manipulate: (source: string) => any };
|
|
117
|
+
manipulateAsync?: (
|
|
118
|
+
uri: string,
|
|
119
|
+
actions: unknown[],
|
|
120
|
+
options: Record<string, unknown>
|
|
121
|
+
) => Promise<{ uri: string; width: number; height: number }>;
|
|
122
|
+
SaveFormat?: Record<string, string>;
|
|
123
|
+
};
|
|
124
|
+
try {
|
|
125
|
+
manipulator = require("expo-image-manipulator");
|
|
126
|
+
} catch {
|
|
127
|
+
return {
|
|
128
|
+
ok: false,
|
|
129
|
+
message: "expo-image-manipulator not installed — add it to enable real savings measurement",
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const webp = manipulator.SaveFormat?.WEBP ?? "webp";
|
|
134
|
+
const targetWidth = Math.min(needed.width, record.intrinsic?.width ?? needed.width);
|
|
135
|
+
|
|
136
|
+
let original: Awaited<ReturnType<typeof resolveOriginalBytes>> = null;
|
|
137
|
+
let optimized: { uri: string; width: number; height: number } | null = null;
|
|
138
|
+
try {
|
|
139
|
+
[original, optimized] = await Promise.all([
|
|
140
|
+
resolveOriginalBytes(record),
|
|
141
|
+
(async (): Promise<{ uri: string; width: number; height: number } | null> => {
|
|
142
|
+
// Preferred: context API (current). Fallback: deprecated manipulateAsync.
|
|
143
|
+
if (manipulator.ImageManipulator?.manipulate) {
|
|
144
|
+
const context = manipulator.ImageManipulator.manipulate(record.uri);
|
|
145
|
+
try {
|
|
146
|
+
const ref = await context.resize({ width: targetWidth }).renderAsync();
|
|
147
|
+
try {
|
|
148
|
+
return await ref.saveAsync({ format: webp, compress: 0.8 });
|
|
149
|
+
} finally {
|
|
150
|
+
ref.release?.();
|
|
151
|
+
}
|
|
152
|
+
} finally {
|
|
153
|
+
context.release?.();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (manipulator.manipulateAsync) {
|
|
157
|
+
return manipulator.manipulateAsync(record.uri, [{ resize: { width: targetWidth } }], {
|
|
158
|
+
compress: 0.8,
|
|
159
|
+
format: webp,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
})(),
|
|
164
|
+
]);
|
|
165
|
+
} catch (error) {
|
|
166
|
+
return { ok: false, message: `Re-encode failed: ${String((error as Error)?.message ?? error)}` };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (!optimized?.uri) {
|
|
170
|
+
return { ok: false, message: "Re-encode produced no output (unsupported source?)" };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const optimizedBytes = await fileSize(optimized.uri);
|
|
174
|
+
const result: SavingsResult = {
|
|
175
|
+
ok: true,
|
|
176
|
+
message: "",
|
|
177
|
+
optimizedUri: optimized.uri,
|
|
178
|
+
optimizedBytes: optimizedBytes ?? undefined,
|
|
179
|
+
optimizedDims: { width: optimized.width, height: optimized.height },
|
|
180
|
+
originalBytes: original?.bytes,
|
|
181
|
+
originalBytesSource: original?.source,
|
|
182
|
+
};
|
|
183
|
+
if (original?.bytes != null && optimizedBytes != null) {
|
|
184
|
+
result.savedBytes = Math.max(0, original.bytes - optimizedBytes);
|
|
185
|
+
result.savedPct = original.bytes > 0 ? Math.round((result.savedBytes / original.bytes) * 100) : 0;
|
|
186
|
+
result.message = `Right-sized WebP is ${result.savedPct}% smaller (real bytes, encoded on device)`;
|
|
187
|
+
} else if (optimizedBytes != null) {
|
|
188
|
+
result.message = "Optimized variant encoded — original size unknown (no download bytes, cache entry, or Content-Length)";
|
|
189
|
+
} else {
|
|
190
|
+
result.message = "Optimized variant encoded — could not stat the output file";
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capture core shared by the RN <Image> decorator and the expo-image patch.
|
|
3
|
+
*
|
|
4
|
+
* ImageInstanceTracker: one per live image component instance. It rolls a new
|
|
5
|
+
* ImageRecord whenever the instance's source URI changes, and exposes wrapped
|
|
6
|
+
* event handlers that feed the store while still calling through to whatever
|
|
7
|
+
* handlers the app passed.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import type { CacheVerdict, ImageLib, SourceKind } from "../types";
|
|
11
|
+
import { imagesState } from "../state";
|
|
12
|
+
import { dropActionState, settleReloadRequest } from "./actions";
|
|
13
|
+
import {
|
|
14
|
+
createRecord,
|
|
15
|
+
getRecord,
|
|
16
|
+
updateRecord,
|
|
17
|
+
} from "../store/imageEventStore";
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Images rendered by Buoy's own UI (e.g. thumbnails inside the Images modal)
|
|
21
|
+
* carry this testID prefix and are excluded from capture, so the tool doesn't
|
|
22
|
+
* pollute its own registry.
|
|
23
|
+
*/
|
|
24
|
+
export const INTERNAL_TEST_ID_PREFIX = "buoy-internal";
|
|
25
|
+
|
|
26
|
+
export function isInternalImage(testID: unknown): boolean {
|
|
27
|
+
return typeof testID === "string" && testID.startsWith(INTERNAL_TEST_ID_PREFIX);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* React Native's own devtools UI loads images too (LogBox icons resolve to
|
|
32
|
+
* Metro asset URLs under Libraries/LogBox). They're noise in an app-focused
|
|
33
|
+
* registry — skip them.
|
|
34
|
+
*/
|
|
35
|
+
export function isNoiseUri(uri: string): boolean {
|
|
36
|
+
return (
|
|
37
|
+
uri.includes("Libraries%2FLogBox") ||
|
|
38
|
+
uri.includes("Libraries/LogBox") ||
|
|
39
|
+
uri.includes("LogBoxImages")
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function classifyUri(uri: string): SourceKind {
|
|
44
|
+
if (uri.startsWith("http://") || uri.startsWith("https://")) return "network";
|
|
45
|
+
if (uri.startsWith("file://") || uri.startsWith("asset://")) return "file";
|
|
46
|
+
if (uri.startsWith("data:")) return "data";
|
|
47
|
+
return "other";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Best-effort URI extraction from any source prop shape (RN or expo-image):
|
|
52
|
+
* string, number (Metro asset), {uri}, arrays of those, ImageRef.
|
|
53
|
+
*/
|
|
54
|
+
export function resolveSourceUri(source: unknown): {
|
|
55
|
+
uri: string;
|
|
56
|
+
kind: SourceKind;
|
|
57
|
+
} {
|
|
58
|
+
if (source == null) return { uri: "", kind: "other" };
|
|
59
|
+
if (Array.isArray(source)) {
|
|
60
|
+
return resolveSourceUri(source.length > 0 ? source[0] : null);
|
|
61
|
+
}
|
|
62
|
+
if (typeof source === "string") {
|
|
63
|
+
return { uri: source, kind: classifyUri(source) };
|
|
64
|
+
}
|
|
65
|
+
if (typeof source === "number") {
|
|
66
|
+
// Metro asset id. Image is guaranteed evaluated by the time an instance
|
|
67
|
+
// renders, so requiring it lazily here can never latch the decorator late.
|
|
68
|
+
try {
|
|
69
|
+
const rn = require("react-native");
|
|
70
|
+
const resolved = rn.Image?.resolveAssetSource?.(source);
|
|
71
|
+
if (resolved?.uri) return { uri: resolved.uri, kind: "asset" };
|
|
72
|
+
} catch {
|
|
73
|
+
// fall through
|
|
74
|
+
}
|
|
75
|
+
return { uri: `asset:#${source}`, kind: "asset" };
|
|
76
|
+
}
|
|
77
|
+
if (typeof source === "object") {
|
|
78
|
+
const uri = (source as { uri?: unknown }).uri;
|
|
79
|
+
if (typeof uri === "string") {
|
|
80
|
+
const kind = classifyUri(uri);
|
|
81
|
+
// require()'d assets resolve to Metro dev-server URLs in dev — still
|
|
82
|
+
// report them as assets when they carry the packager marker.
|
|
83
|
+
if ((source as { __packager_asset?: boolean }).__packager_asset) {
|
|
84
|
+
return { uri, kind: "asset" };
|
|
85
|
+
}
|
|
86
|
+
return { uri, kind };
|
|
87
|
+
}
|
|
88
|
+
// expo-image ImageRef (native shared object) has width/height but no uri.
|
|
89
|
+
const width = (source as { width?: unknown }).width;
|
|
90
|
+
if (typeof width === "number") return { uri: "(ImageRef)", kind: "other" };
|
|
91
|
+
}
|
|
92
|
+
return { uri: "", kind: "other" };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** The event-handler subset we intercept, normalized across both libraries. */
|
|
96
|
+
export interface OriginalHandlers {
|
|
97
|
+
onLoadStart?: (event: unknown) => void;
|
|
98
|
+
onProgress?: (event: unknown) => void;
|
|
99
|
+
onLoad?: (event: unknown) => void;
|
|
100
|
+
onError?: (event: unknown) => void;
|
|
101
|
+
onLoadEnd?: (event: unknown) => void;
|
|
102
|
+
onDisplay?: (event: unknown) => void;
|
|
103
|
+
onLayout?: (event: unknown) => void;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type WrappedHandlers = Required<
|
|
107
|
+
Pick<
|
|
108
|
+
OriginalHandlers,
|
|
109
|
+
"onLoadStart" | "onProgress" | "onLoad" | "onError" | "onLayout"
|
|
110
|
+
>
|
|
111
|
+
> &
|
|
112
|
+
Pick<OriginalHandlers, "onLoadEnd" | "onDisplay">;
|
|
113
|
+
|
|
114
|
+
/** RN events arrive as {nativeEvent}; expo-image passes the payload directly. */
|
|
115
|
+
function payloadOf(event: unknown): Record<string, unknown> {
|
|
116
|
+
if (event && typeof event === "object") {
|
|
117
|
+
const native = (event as { nativeEvent?: unknown }).nativeEvent;
|
|
118
|
+
if (native && typeof native === "object") {
|
|
119
|
+
return native as Record<string, unknown>;
|
|
120
|
+
}
|
|
121
|
+
return event as Record<string, unknown>;
|
|
122
|
+
}
|
|
123
|
+
return {};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* After an RN-core load, classify the cache via Image.queryCache.
|
|
128
|
+
*
|
|
129
|
+
* Delayed + retried: NSURLCache commits fresh downloads asynchronously, so a
|
|
130
|
+
* query at the instant of onLoad reports a miss for images that ARE being
|
|
131
|
+
* cached (observed live). First probe after 600ms, one re-probe at 3s to
|
|
132
|
+
* catch slow cache writes.
|
|
133
|
+
*/
|
|
134
|
+
function queryRnCache(recordId: number, uri: string): void {
|
|
135
|
+
const probe = () => {
|
|
136
|
+
try {
|
|
137
|
+
const rn = require("react-native");
|
|
138
|
+
const queryCache: undefined | ((uris: string[]) => Promise<Record<string, string>>) =
|
|
139
|
+
rn.Image?.queryCache;
|
|
140
|
+
if (typeof queryCache !== "function" || !uri) return;
|
|
141
|
+
queryCache([uri])
|
|
142
|
+
.then((result) => {
|
|
143
|
+
const verdict = result?.[uri] as CacheVerdict | undefined;
|
|
144
|
+
if (verdict) {
|
|
145
|
+
updateRecord(recordId, {
|
|
146
|
+
cacheVerdict: verdict,
|
|
147
|
+
cacheVerdictSource: "queryCache",
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
.catch(() => {});
|
|
152
|
+
} catch {
|
|
153
|
+
// queryCache unavailable (web) — leave verdict unknown.
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
setTimeout(probe, 600);
|
|
157
|
+
setTimeout(() => {
|
|
158
|
+
const record = getRecord(recordId);
|
|
159
|
+
if (record && record.cacheVerdictSource !== "queryCache") probe();
|
|
160
|
+
}, 3000);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export class ImageInstanceTracker {
|
|
164
|
+
private readonly lib: ImageLib;
|
|
165
|
+
private recordId: number | null = null;
|
|
166
|
+
private uri: string | null = null;
|
|
167
|
+
private handlerCache: { key: unknown[]; out: WrappedHandlers } | null = null;
|
|
168
|
+
|
|
169
|
+
constructor(lib: ImageLib) {
|
|
170
|
+
this.lib = lib;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
getRecordId(): number | null {
|
|
174
|
+
return this.recordId;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Record accessibility presence (called per render; writes only on change). */
|
|
178
|
+
noteA11y(hasAltText: boolean): void {
|
|
179
|
+
if (this.recordId == null) return;
|
|
180
|
+
const record = getRecord(this.recordId);
|
|
181
|
+
if (record && record.hasAltText !== hasAltText) {
|
|
182
|
+
updateRecord(this.recordId, { hasAltText });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Call on every render with the current source prop. Rolls records on change. */
|
|
187
|
+
syncSource(source: unknown): void {
|
|
188
|
+
const { uri, kind } = resolveSourceUri(source);
|
|
189
|
+
if (uri === this.uri) return;
|
|
190
|
+
this.uri = uri;
|
|
191
|
+
this.handlerCache = null; // handlers close over recordId via getter, but roll anyway
|
|
192
|
+
if (this.recordId != null) dropActionState(this.recordId);
|
|
193
|
+
if (!uri || isNoiseUri(uri)) {
|
|
194
|
+
this.recordId = null;
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const record = createRecord({ lib: this.lib, uri, sourceKind: kind });
|
|
198
|
+
this.recordId = record.id;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
unmount(): void {
|
|
202
|
+
if (this.recordId != null) {
|
|
203
|
+
updateRecord(this.recordId, { mounted: false });
|
|
204
|
+
dropActionState(this.recordId);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Wrapped handlers for the current originals. Cached by handler identity so
|
|
210
|
+
* re-renders with unchanged props reuse the same functions.
|
|
211
|
+
*/
|
|
212
|
+
handlers(orig: OriginalHandlers): WrappedHandlers {
|
|
213
|
+
const key = [
|
|
214
|
+
orig.onLoadStart,
|
|
215
|
+
orig.onProgress,
|
|
216
|
+
orig.onLoad,
|
|
217
|
+
orig.onError,
|
|
218
|
+
orig.onLoadEnd,
|
|
219
|
+
orig.onDisplay,
|
|
220
|
+
orig.onLayout,
|
|
221
|
+
this.recordId,
|
|
222
|
+
];
|
|
223
|
+
const cached = this.handlerCache;
|
|
224
|
+
if (cached && cached.key.length === key.length && cached.key.every((k, i) => k === key[i])) {
|
|
225
|
+
return cached.out;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const recordId = this.recordId;
|
|
229
|
+
const lib = this.lib;
|
|
230
|
+
const uri = this.uri ?? "";
|
|
231
|
+
|
|
232
|
+
const out: WrappedHandlers = {
|
|
233
|
+
onLoadStart: (event) => {
|
|
234
|
+
if (recordId != null) {
|
|
235
|
+
const record = getRecord(recordId);
|
|
236
|
+
const now = Date.now();
|
|
237
|
+
// Out-of-order defense (seen live on iOS remounts/cache hits AND
|
|
238
|
+
// instant file:// failures): the native onLoadStart of a burst can
|
|
239
|
+
// arrive AFTER its onLoad/onError. A "start" right after a settled
|
|
240
|
+
// outcome belongs to that same burst — don't regress the record.
|
|
241
|
+
const settledAt = Math.max(record?.loadedAt ?? 0, record?.erroredAt ?? 0);
|
|
242
|
+
const settledJustNow = settledAt > 0 && now - settledAt < 300;
|
|
243
|
+
updateRecord(recordId, {
|
|
244
|
+
...(settledJustNow ? {} : { status: "loading", error: undefined, errorCode: undefined, errorHeaders: undefined }),
|
|
245
|
+
startedAt: now,
|
|
246
|
+
loadCount: (record?.loadCount ?? 0) + 1,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
orig.onLoadStart?.(event);
|
|
250
|
+
},
|
|
251
|
+
onProgress: (event) => {
|
|
252
|
+
if (recordId != null) {
|
|
253
|
+
const p = payloadOf(event);
|
|
254
|
+
updateRecord(recordId, {
|
|
255
|
+
progressSeen: true,
|
|
256
|
+
bytesLoaded: typeof p.loaded === "number" ? p.loaded : undefined,
|
|
257
|
+
bytesTotal: typeof p.total === "number" ? p.total : undefined,
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
orig.onProgress?.(event);
|
|
261
|
+
},
|
|
262
|
+
onLoad: (event) => {
|
|
263
|
+
if (recordId != null) {
|
|
264
|
+
const p = payloadOf(event);
|
|
265
|
+
const source = (p.source ?? {}) as Record<string, unknown>;
|
|
266
|
+
const now = Date.now();
|
|
267
|
+
const record = getRecord(recordId);
|
|
268
|
+
// Duration only counts from a start belonging to THIS load cycle: a
|
|
269
|
+
// start newer than the previous completion. Without one (instant
|
|
270
|
+
// cache hit whose onLoadStart arrives late/never), report 0ms.
|
|
271
|
+
const start =
|
|
272
|
+
record?.startedAt != null &&
|
|
273
|
+
record.startedAt <= now &&
|
|
274
|
+
record.startedAt > (record.loadedAt ?? record.createdAt - 1)
|
|
275
|
+
? record.startedAt
|
|
276
|
+
: record?.loadedAt == null
|
|
277
|
+
? (record?.startedAt ?? record?.createdAt ?? now)
|
|
278
|
+
: now;
|
|
279
|
+
const width = typeof source.width === "number" ? source.width : 0;
|
|
280
|
+
const height = typeof source.height === "number" ? source.height : 0;
|
|
281
|
+
updateRecord(recordId, {
|
|
282
|
+
status: "loaded",
|
|
283
|
+
loadedAt: now,
|
|
284
|
+
durationMs: Math.max(0, now - start),
|
|
285
|
+
error: undefined,
|
|
286
|
+
errorCode: undefined,
|
|
287
|
+
errorHeaders: undefined,
|
|
288
|
+
intrinsic: width > 0 && height > 0 ? { width, height } : record?.intrinsic,
|
|
289
|
+
cacheVerdict:
|
|
290
|
+
typeof p.cacheType === "string"
|
|
291
|
+
? (p.cacheType as CacheVerdict)
|
|
292
|
+
: record?.cacheVerdict,
|
|
293
|
+
cacheVerdictSource:
|
|
294
|
+
typeof p.cacheType === "string" ? "event" : record?.cacheVerdictSource,
|
|
295
|
+
mediaType:
|
|
296
|
+
typeof source.mediaType === "string" || source.mediaType === null
|
|
297
|
+
? (source.mediaType as string | null)
|
|
298
|
+
: record?.mediaType,
|
|
299
|
+
isAnimated:
|
|
300
|
+
typeof source.isAnimated === "boolean"
|
|
301
|
+
? source.isAnimated
|
|
302
|
+
: record?.isAnimated,
|
|
303
|
+
});
|
|
304
|
+
settleReloadRequest(recordId);
|
|
305
|
+
if (lib === "rn") queryRnCache(recordId, uri);
|
|
306
|
+
}
|
|
307
|
+
orig.onLoad?.(event);
|
|
308
|
+
},
|
|
309
|
+
onError: (event) => {
|
|
310
|
+
if (recordId != null) {
|
|
311
|
+
const p = payloadOf(event);
|
|
312
|
+
let message =
|
|
313
|
+
typeof p.error === "string"
|
|
314
|
+
? p.error
|
|
315
|
+
: String((p.error as { message?: string })?.message ?? "unknown error");
|
|
316
|
+
// Simulation sources produce raw file-not-found noise — translate to
|
|
317
|
+
// an honest label (state-based, so it works for both libs' error
|
|
318
|
+
// texts), in our record AND in the event the app receives.
|
|
319
|
+
const record0 = getRecord(recordId);
|
|
320
|
+
const s = imagesState();
|
|
321
|
+
const activeOverride = s.overrides.get(recordId) as
|
|
322
|
+
| { source?: { type?: string } }
|
|
323
|
+
| undefined;
|
|
324
|
+
if (activeOverride?.source?.type === "error") {
|
|
325
|
+
message = "Forced error (Buoy Images simulation)";
|
|
326
|
+
if (typeof p.error === "string") p.error = message;
|
|
327
|
+
} else if (
|
|
328
|
+
s.globalModes.network === "offline" &&
|
|
329
|
+
record0?.sourceKind === "network"
|
|
330
|
+
) {
|
|
331
|
+
message = "Offline simulation (Buoy Images): network image blocked";
|
|
332
|
+
if (typeof p.error === "string") p.error = message;
|
|
333
|
+
}
|
|
334
|
+
updateRecord(recordId, {
|
|
335
|
+
status: "error",
|
|
336
|
+
erroredAt: Date.now(),
|
|
337
|
+
error: message,
|
|
338
|
+
errorCode: typeof p.responseCode === "number" ? p.responseCode : undefined,
|
|
339
|
+
errorHeaders:
|
|
340
|
+
p.httpResponseHeaders && typeof p.httpResponseHeaders === "object"
|
|
341
|
+
? (p.httpResponseHeaders as Record<string, string>)
|
|
342
|
+
: undefined,
|
|
343
|
+
});
|
|
344
|
+
settleReloadRequest(recordId);
|
|
345
|
+
}
|
|
346
|
+
orig.onError?.(event);
|
|
347
|
+
},
|
|
348
|
+
onLayout: (event) => {
|
|
349
|
+
if (recordId != null) {
|
|
350
|
+
const layout = (payloadOf(event).layout ?? {}) as Record<string, unknown>;
|
|
351
|
+
const width = typeof layout.width === "number" ? layout.width : 0;
|
|
352
|
+
const height = typeof layout.height === "number" ? layout.height : 0;
|
|
353
|
+
if (width > 0 && height > 0) {
|
|
354
|
+
const record = getRecord(recordId);
|
|
355
|
+
const prev = record?.layout;
|
|
356
|
+
// Size change AFTER the image loaded = layout-shift suspect (the
|
|
357
|
+
// image arriving resized its box — reserve explicit dimensions).
|
|
358
|
+
const shifted =
|
|
359
|
+
prev != null &&
|
|
360
|
+
record?.loadedAt != null &&
|
|
361
|
+
(Math.abs(prev.width - width) > 1 || Math.abs(prev.height - height) > 1);
|
|
362
|
+
updateRecord(recordId, {
|
|
363
|
+
layout: { width, height },
|
|
364
|
+
...(shifted ? { layoutShifts: (record?.layoutShifts ?? 0) + 1 } : {}),
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
orig.onLayout?.(event);
|
|
369
|
+
},
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
// Only forward-wrap the optional ones so we don't attach handlers a
|
|
373
|
+
// library doesn't support (onDisplay is expo-only; onLoadEnd is RN-only).
|
|
374
|
+
out.onLoadEnd = orig.onLoadEnd
|
|
375
|
+
? (event) => orig.onLoadEnd?.(event)
|
|
376
|
+
: undefined;
|
|
377
|
+
if (this.lib === "expo") {
|
|
378
|
+
out.onDisplay = (event) => {
|
|
379
|
+
if (recordId != null) updateRecord(recordId, { displayedAt: Date.now() });
|
|
380
|
+
orig.onDisplay?.(event);
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
this.handlerCache = { key, out };
|
|
385
|
+
return out;
|
|
386
|
+
}
|
|
387
|
+
}
|