@buoy-gg/images 7.0.29 → 7.0.30
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/package.json +5 -6
- package/src/capture/actions.ts +0 -215
- package/src/capture/install.ts +0 -40
- package/src/capture/installExpoImage.ts +0 -115
- package/src/capture/installRnImage.tsx +0 -133
- package/src/capture/overrides.ts +0 -332
- package/src/capture/savings.ts +0 -204
- package/src/capture/shared.ts +0 -387
- package/src/components/ImageDetail.tsx +0 -466
- package/src/components/ImageDetailFooter.tsx +0 -271
- package/src/components/ImageRow.tsx +0 -148
- package/src/components/ImagesHeader.tsx +0 -77
- package/src/components/ImagesIcon.tsx +0 -9
- package/src/components/ImagesListFooter.tsx +0 -194
- package/src/components/ImagesModal.tsx +0 -266
- package/src/components/ImagesRoot.tsx +0 -18
- package/src/components/InsightsBar.tsx +0 -51
- package/src/export.ts +0 -100
- package/src/format.ts +0 -85
- package/src/hooks/useImageRecords.ts +0 -24
- package/src/index.tsx +0 -120
- package/src/preset.tsx +0 -35
- package/src/register.ts +0 -17
- package/src/remote.ts +0 -42
- package/src/state.ts +0 -103
- package/src/store/imageEventStore.ts +0 -236
- package/src/store/insights.ts +0 -66
- package/src/sync/__tests__/imagesSyncAdapter.test.ts +0 -114
- package/src/sync/imagesSyncAdapter.ts +0 -312
- package/src/types.ts +0 -155
package/src/capture/overrides.ts
DELETED
|
@@ -1,332 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Simulation overrides — the "we own the props" tier of actions.
|
|
3
|
-
*
|
|
4
|
-
* Because the RN decorator and the expo-image render patch control every
|
|
5
|
-
* mounted image's props, anything the component accepts is a simulation:
|
|
6
|
-
* forced errors (instant, offline-safe file:// source), forced loading
|
|
7
|
-
* (blackhole-IP source that never resolves), URL swaps, blanking, style
|
|
8
|
-
* merges (locate-flash border), and app-wide modes (offline images /
|
|
9
|
-
* cold-start / blank images).
|
|
10
|
-
*
|
|
11
|
-
* Re-render plumbing: every mutation bumps a per-record or global version.
|
|
12
|
-
* RN wrappers subscribe via useSyncExternalStore with a per-record snapshot
|
|
13
|
-
* (only the touched image re-renders); expo class instances are re-rendered
|
|
14
|
-
* directly via forceUpdate() from the per-record instance registry.
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import { remoteImagesDispatch } from "../remote";
|
|
18
|
-
import { imagesState } from "../state";
|
|
19
|
-
import { getRecord, getSnapshot, updateRecord } from "../store/imageEventStore";
|
|
20
|
-
import type { ImageLib } from "../types";
|
|
21
|
-
|
|
22
|
-
// ---------------------------------------------------------------------------
|
|
23
|
-
// Override model
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
|
|
26
|
-
export type OverrideSource =
|
|
27
|
-
| { type: "url"; uri: string }
|
|
28
|
-
/** Nonexistent file:// — native fires onError immediately, no network needed. */
|
|
29
|
-
| { type: "error" }
|
|
30
|
-
/** Blackhole IP — the request hangs, onLoad never fires (loading forever). */
|
|
31
|
-
| { type: "hang" }
|
|
32
|
-
/** No source at all (expo keeps showing its placeholder). */
|
|
33
|
-
| { type: "blank" };
|
|
34
|
-
|
|
35
|
-
export interface ImageOverride {
|
|
36
|
-
source?: OverrideSource;
|
|
37
|
-
/** Merged over the app's style (size experiments etc.). */
|
|
38
|
-
style?: Record<string, unknown>;
|
|
39
|
-
/** Extra props merged last (expo contentFit, priority, cachePolicy…). */
|
|
40
|
-
extraProps?: Record<string, unknown>;
|
|
41
|
-
/** Shown as the record's override badge, e.g. "Forced error". */
|
|
42
|
-
label: string;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
const FORCED_ERROR_URI = "file:///buoy-images-forced-error.png";
|
|
46
|
-
const HANG_URI = "https://10.255.255.1/buoy-images-hang.png";
|
|
47
|
-
/** Offline mode replaces network sources with this (fails immediately). */
|
|
48
|
-
const OFFLINE_URI = "file:///buoy-images-offline-no-network.png";
|
|
49
|
-
const FLASH_STYLE = { borderWidth: 3, borderColor: "#FF453A" } as const;
|
|
50
|
-
/**
|
|
51
|
-
* RN core has NO safe "empty source": undefined resolves to the app bundle
|
|
52
|
-
* path and [] does too on RN 0.85 — both fire a bogus onError ("The file …
|
|
53
|
-
* couldn't be opened", observed live). So RN images blank via opacity
|
|
54
|
-
* instead (visual-only; the decoded image stays resident). expo-image
|
|
55
|
-
* supports a true `source={null}` and keeps showing its placeholder.
|
|
56
|
-
*/
|
|
57
|
-
const RN_BLANK_STYLE = { opacity: 0 } as const;
|
|
58
|
-
|
|
59
|
-
export type NetworkMode = "normal" | "offline" | "cold";
|
|
60
|
-
|
|
61
|
-
// ---------------------------------------------------------------------------
|
|
62
|
-
// Version bus (re-render triggers)
|
|
63
|
-
// ---------------------------------------------------------------------------
|
|
64
|
-
|
|
65
|
-
function emit(): void {
|
|
66
|
-
for (const l of imagesState().actionListeners) l();
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function forceUpdateExpo(recordId?: number): void {
|
|
70
|
-
const s = imagesState();
|
|
71
|
-
if (recordId != null) {
|
|
72
|
-
const instance = s.expoInstances.get(recordId) as
|
|
73
|
-
| { forceUpdate?: () => void }
|
|
74
|
-
| undefined;
|
|
75
|
-
instance?.forceUpdate?.();
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
for (const instance of s.expoInstances.values()) {
|
|
79
|
-
(instance as { forceUpdate?: () => void })?.forceUpdate?.();
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Bump one record's render version (re-renders just that image). */
|
|
84
|
-
export function touchRecord(recordId: number): void {
|
|
85
|
-
const s = imagesState();
|
|
86
|
-
s.recordVersions.set(recordId, ++s.versionCounter);
|
|
87
|
-
emit();
|
|
88
|
-
forceUpdateExpo(recordId);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** Bump the global render version (re-renders every wrapped image). */
|
|
92
|
-
export function touchGlobal(): void {
|
|
93
|
-
const s = imagesState();
|
|
94
|
-
s.globalVersion = ++s.versionCounter;
|
|
95
|
-
emit();
|
|
96
|
-
forceUpdateExpo();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/** Snapshot for the RN wrapper's useSyncExternalStore. */
|
|
100
|
-
export function getRenderVersion(recordId: number | null): number {
|
|
101
|
-
const s = imagesState();
|
|
102
|
-
const record = recordId != null ? (s.recordVersions.get(recordId) ?? 0) : 0;
|
|
103
|
-
return Math.max(record, s.globalVersion);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// ---------------------------------------------------------------------------
|
|
107
|
-
// Per-record overrides
|
|
108
|
-
// ---------------------------------------------------------------------------
|
|
109
|
-
|
|
110
|
-
/** Remote param shape for the adapter's `setOverride` action. */
|
|
111
|
-
function overrideParams(
|
|
112
|
-
recordId: number,
|
|
113
|
-
source: OverrideSource
|
|
114
|
-
): Record<string, unknown> {
|
|
115
|
-
return source.type === "url"
|
|
116
|
-
? { id: recordId, kind: "url", uri: source.uri }
|
|
117
|
-
: { id: recordId, kind: source.type };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Local bookkeeping only (no remote forward) — mirrors keep this as an
|
|
122
|
-
* optimistic UI update; the next device snapshot is the source of truth.
|
|
123
|
-
*/
|
|
124
|
-
function setOverrideLocal(recordId: number, override: ImageOverride): void {
|
|
125
|
-
imagesState().overrides.set(recordId, override);
|
|
126
|
-
updateRecord(recordId, { overrideLabel: override.label });
|
|
127
|
-
touchRecord(recordId);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function setOverride(recordId: number, override: ImageOverride): void {
|
|
131
|
-
if (override.source) {
|
|
132
|
-
void remoteImagesDispatch()?.(
|
|
133
|
-
"setOverride",
|
|
134
|
-
overrideParams(recordId, override.source)
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
setOverrideLocal(recordId, override);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function clearOverrideLocal(recordId: number): void {
|
|
141
|
-
imagesState().overrides.delete(recordId);
|
|
142
|
-
updateRecord(recordId, { overrideLabel: undefined });
|
|
143
|
-
touchRecord(recordId);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export function clearOverride(recordId: number): void {
|
|
147
|
-
void remoteImagesDispatch()?.("clearOverride", { id: recordId });
|
|
148
|
-
clearOverrideLocal(recordId);
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
export function getOverride(recordId: number | null): ImageOverride | undefined {
|
|
152
|
-
if (recordId == null) return undefined;
|
|
153
|
-
return imagesState().overrides.get(recordId) as ImageOverride | undefined;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// ---------------------------------------------------------------------------
|
|
157
|
-
// Mass actions (every mounted image at once)
|
|
158
|
-
// ---------------------------------------------------------------------------
|
|
159
|
-
|
|
160
|
-
/** Adapter `massAction` kind for a mass override (per-record 'hang' is mass 'loading'). */
|
|
161
|
-
function massKind(source: OverrideSource): string {
|
|
162
|
-
return source.type === "hang" ? "loading" : source.type;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/** Apply one override to every mounted record. Returns how many were hit. */
|
|
166
|
-
export function setOverrideForAllMounted(source: OverrideSource, label: string): number {
|
|
167
|
-
// One mass action on the device, not N per-record forwards.
|
|
168
|
-
void remoteImagesDispatch()?.("massAction", { kind: massKind(source) });
|
|
169
|
-
let count = 0;
|
|
170
|
-
for (const record of getSnapshot()) {
|
|
171
|
-
if (!record.mounted) continue;
|
|
172
|
-
setOverrideLocal(record.id, { source, label });
|
|
173
|
-
count++;
|
|
174
|
-
}
|
|
175
|
-
return count;
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/** Remove every active override. Returns how many were cleared. */
|
|
179
|
-
export function clearAllOverrides(): number {
|
|
180
|
-
void remoteImagesDispatch()?.("massAction", { kind: "restore" });
|
|
181
|
-
// Mirrors track labels on records, not the overrides map — clear both.
|
|
182
|
-
const ids = new Set<number>(imagesState().overrides.keys());
|
|
183
|
-
for (const record of getSnapshot()) {
|
|
184
|
-
if (record.overrideLabel != null) ids.add(record.id);
|
|
185
|
-
}
|
|
186
|
-
for (const id of ids) clearOverrideLocal(id);
|
|
187
|
-
return ids.size;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
/** Flash every mounted image (locate everything the tool tracks). */
|
|
191
|
-
export function flashAllMounted(durationMs = 2500): number {
|
|
192
|
-
void remoteImagesDispatch()?.("massAction", { kind: "flash" });
|
|
193
|
-
let count = 0;
|
|
194
|
-
for (const record of getSnapshot()) {
|
|
195
|
-
if (!record.mounted) continue;
|
|
196
|
-
flashRecordLocal(record.id, durationMs);
|
|
197
|
-
count++;
|
|
198
|
-
}
|
|
199
|
-
return count;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function flashRecordLocal(recordId: number, durationMs = 2500): void {
|
|
203
|
-
const s = imagesState();
|
|
204
|
-
s.flashUntil.set(recordId, Date.now() + durationMs);
|
|
205
|
-
touchRecord(recordId);
|
|
206
|
-
setTimeout(() => {
|
|
207
|
-
imagesState().flashUntil.delete(recordId);
|
|
208
|
-
touchRecord(recordId);
|
|
209
|
-
}, durationMs + 50);
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/** Flash a border on the on-screen image so it can be located visually. */
|
|
213
|
-
export function flashRecord(recordId: number, durationMs = 2500): void {
|
|
214
|
-
void remoteImagesDispatch()?.("flash", { id: recordId });
|
|
215
|
-
flashRecordLocal(recordId, durationMs);
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
// ---------------------------------------------------------------------------
|
|
219
|
-
// Global simulation modes
|
|
220
|
-
// ---------------------------------------------------------------------------
|
|
221
|
-
|
|
222
|
-
/**
|
|
223
|
-
* offline — every NETWORK image load fails immediately (both libs, cache or
|
|
224
|
-
* not); bundled/local assets still load, like a real offline device showing
|
|
225
|
-
* shipped images. Blunt on purpose: toggle + reload = images visibly gone.
|
|
226
|
-
* (The subtler `only-if-cached` variant was correct-but-confusing, and its
|
|
227
|
-
* uncached requests HANG on iOS instead of erroring — bad for QA.)
|
|
228
|
-
* cold — every image bypasses caches (RN `cache:'reload'`, expo
|
|
229
|
-
* `cachePolicy:'none'`): first-launch behavior without clearing anything.
|
|
230
|
-
*/
|
|
231
|
-
export function setNetworkMode(mode: NetworkMode): void {
|
|
232
|
-
void remoteImagesDispatch()?.("setNetworkMode", { mode });
|
|
233
|
-
imagesState().globalModes.network = mode;
|
|
234
|
-
touchGlobal();
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/** Chrome-style "disable images": render every image without a source. */
|
|
238
|
-
export function setBlankImages(enabled: boolean): void {
|
|
239
|
-
void remoteImagesDispatch()?.("setBlankImages", { enabled });
|
|
240
|
-
imagesState().globalModes.blank = enabled;
|
|
241
|
-
touchGlobal();
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
export function getGlobalModes(): { network: NetworkMode; blank: boolean } {
|
|
245
|
-
return imagesState().globalModes;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// ---------------------------------------------------------------------------
|
|
249
|
-
// Application (called from the RN wrapper + expo render patch)
|
|
250
|
-
// ---------------------------------------------------------------------------
|
|
251
|
-
|
|
252
|
-
/** Inject a `cache` policy into an RN source (object or array shape). */
|
|
253
|
-
export function patchSourceCache(source: unknown, cache: string): unknown {
|
|
254
|
-
if (Array.isArray(source)) {
|
|
255
|
-
return source.map((entry) =>
|
|
256
|
-
entry && typeof entry === "object" && typeof (entry as { uri?: unknown }).uri === "string"
|
|
257
|
-
? { ...(entry as object), cache }
|
|
258
|
-
: entry
|
|
259
|
-
);
|
|
260
|
-
}
|
|
261
|
-
if (source && typeof source === "object" && typeof (source as { uri?: unknown }).uri === "string") {
|
|
262
|
-
return { ...(source as object), cache };
|
|
263
|
-
}
|
|
264
|
-
return source; // numeric assets etc. — nothing to patch
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
function resolveOverrideSource(source: OverrideSource, lib: ImageLib): unknown {
|
|
268
|
-
switch (source.type) {
|
|
269
|
-
case "url":
|
|
270
|
-
return { uri: source.uri };
|
|
271
|
-
case "error":
|
|
272
|
-
return { uri: FORCED_ERROR_URI };
|
|
273
|
-
case "hang":
|
|
274
|
-
return { uri: HANG_URI };
|
|
275
|
-
case "blank":
|
|
276
|
-
// Only reached for expo — RN blanking is style-based (see RN_BLANK_STYLE).
|
|
277
|
-
return null;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
/**
|
|
282
|
-
* Merge global modes + per-record override + locate-flash into an
|
|
283
|
-
* already-copied props object. Mutates and returns `out`.
|
|
284
|
-
*/
|
|
285
|
-
export function applyOverrides(
|
|
286
|
-
out: Record<string, unknown>,
|
|
287
|
-
recordId: number | null,
|
|
288
|
-
lib: ImageLib
|
|
289
|
-
): Record<string, unknown> {
|
|
290
|
-
const s = imagesState();
|
|
291
|
-
const modes = s.globalModes;
|
|
292
|
-
|
|
293
|
-
if (modes.network === "offline") {
|
|
294
|
-
// Hard offline: network sources fail outright (both libs); bundled/local
|
|
295
|
-
// assets keep loading like a real offline device.
|
|
296
|
-
const record = recordId != null ? getRecord(recordId) : undefined;
|
|
297
|
-
if (record?.sourceKind === "network") {
|
|
298
|
-
out.source = { uri: OFFLINE_URI };
|
|
299
|
-
}
|
|
300
|
-
} else if (modes.network === "cold") {
|
|
301
|
-
if (lib === "rn") {
|
|
302
|
-
out.source = patchSourceCache(out.source, "reload");
|
|
303
|
-
} else {
|
|
304
|
-
out.cachePolicy = "none";
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
if (modes.blank) {
|
|
308
|
-
if (lib === "expo") out.source = null;
|
|
309
|
-
else out.style = [out.style, RN_BLANK_STYLE];
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
const override = getOverride(recordId);
|
|
313
|
-
if (override) {
|
|
314
|
-
if (override.source) {
|
|
315
|
-
if (override.source.type === "blank" && lib === "rn") {
|
|
316
|
-
out.style = [out.style, RN_BLANK_STYLE];
|
|
317
|
-
} else {
|
|
318
|
-
out.source = resolveOverrideSource(override.source, lib);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
if (override.extraProps) Object.assign(out, override.extraProps);
|
|
322
|
-
if (override.style) out.style = [out.style, override.style];
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
if (recordId != null) {
|
|
326
|
-
const until = s.flashUntil.get(recordId);
|
|
327
|
-
if (until != null && until > Date.now()) {
|
|
328
|
-
out.style = [out.style, FLASH_STYLE];
|
|
329
|
-
}
|
|
330
|
-
}
|
|
331
|
-
return out;
|
|
332
|
-
}
|
package/src/capture/savings.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
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 { remoteImagesDispatch } from "../remote";
|
|
16
|
-
import { neededPixels } from "../store/imageEventStore";
|
|
17
|
-
import type { ImageRecord } from "../types";
|
|
18
|
-
|
|
19
|
-
export interface SavingsResult {
|
|
20
|
-
ok: boolean;
|
|
21
|
-
message: string;
|
|
22
|
-
/** Real encoded bytes of the original (when determinable). */
|
|
23
|
-
originalBytes?: number;
|
|
24
|
-
originalBytesSource?: "download" | "cacheFile" | "head";
|
|
25
|
-
/** Real encoded bytes of the optimized WebP on disk. */
|
|
26
|
-
optimizedBytes?: number;
|
|
27
|
-
/** file:// URI of the optimized image (feed to swap-url to preview). */
|
|
28
|
-
optimizedUri?: string;
|
|
29
|
-
optimizedDims?: { width: number; height: number };
|
|
30
|
-
savedBytes?: number;
|
|
31
|
-
savedPct?: number;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
interface LegacyFs {
|
|
35
|
-
getInfoAsync?: (uri: string, options?: { size?: boolean }) => Promise<{
|
|
36
|
-
exists: boolean;
|
|
37
|
-
size?: number;
|
|
38
|
-
}>;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function loadLegacyFs(): LegacyFs | null {
|
|
42
|
-
try {
|
|
43
|
-
const mod = require("expo-file-system/legacy");
|
|
44
|
-
const fs = mod?.default || mod;
|
|
45
|
-
if (fs?.getInfoAsync) return fs;
|
|
46
|
-
} catch {
|
|
47
|
-
// try main import
|
|
48
|
-
}
|
|
49
|
-
try {
|
|
50
|
-
const mod = require("expo-file-system");
|
|
51
|
-
const fs = mod?.default || mod;
|
|
52
|
-
if (fs?.getInfoAsync) return fs;
|
|
53
|
-
} catch {
|
|
54
|
-
// not installed
|
|
55
|
-
}
|
|
56
|
-
return null;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
async function fileSize(uri: string): Promise<number | null> {
|
|
60
|
-
const fs = loadLegacyFs();
|
|
61
|
-
if (!fs?.getInfoAsync) return null;
|
|
62
|
-
try {
|
|
63
|
-
const normalized = uri.startsWith("file://") ? uri : `file://${uri}`;
|
|
64
|
-
const info = await fs.getInfoAsync(normalized, { size: true });
|
|
65
|
-
return info.exists && typeof info.size === "number" ? info.size : null;
|
|
66
|
-
} catch {
|
|
67
|
-
return null;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
async function resolveOriginalBytes(
|
|
72
|
-
record: ImageRecord
|
|
73
|
-
): Promise<{ bytes: number; source: SavingsResult["originalBytesSource"] } | null> {
|
|
74
|
-
if (record.bytesTotal && record.bytesTotal > 0) {
|
|
75
|
-
return { bytes: record.bytesTotal, source: "download" };
|
|
76
|
-
}
|
|
77
|
-
// expo disk cache file
|
|
78
|
-
if (record.lib === "expo") {
|
|
79
|
-
try {
|
|
80
|
-
const expo = require("expo-image");
|
|
81
|
-
const path: string | null = await expo.Image.getCachePathAsync(record.uri);
|
|
82
|
-
if (path) {
|
|
83
|
-
const size = await fileSize(path);
|
|
84
|
-
if (size != null) return { bytes: size, source: "cacheFile" };
|
|
85
|
-
}
|
|
86
|
-
} catch {
|
|
87
|
-
// fall through
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
// HEAD request (network sources only)
|
|
91
|
-
if (record.sourceKind === "network") {
|
|
92
|
-
try {
|
|
93
|
-
const response = await fetch(record.uri, { method: "HEAD" });
|
|
94
|
-
const length = Number(response.headers.get("content-length"));
|
|
95
|
-
if (Number.isFinite(length) && length > 0) return { bytes: length, source: "head" };
|
|
96
|
-
} catch {
|
|
97
|
-
// unknown original size is fine
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
return null;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Re-encode `record`'s source at its displayed size as WebP and measure it.
|
|
105
|
-
* Requires expo-image-manipulator (graceful message when absent).
|
|
106
|
-
*/
|
|
107
|
-
export async function proveSavings(record: ImageRecord): Promise<SavingsResult> {
|
|
108
|
-
// Mirror mode: the re-encode must happen ON DEVICE (that's the whole point —
|
|
109
|
-
// real bytes from the real encoder); forward and relay the result.
|
|
110
|
-
const remote = remoteImagesDispatch();
|
|
111
|
-
if (remote) {
|
|
112
|
-
try {
|
|
113
|
-
return (await remote("proveSavings", { id: record.id })) as SavingsResult;
|
|
114
|
-
} catch (error) {
|
|
115
|
-
return { ok: false, message: String((error as Error)?.message ?? error) };
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (!record.uri || (record.sourceKind !== "network" && record.sourceKind !== "asset" && record.sourceKind !== "file")) {
|
|
119
|
-
return { ok: false, message: "No loadable source URI on this record" };
|
|
120
|
-
}
|
|
121
|
-
const needed = neededPixels(record);
|
|
122
|
-
if (!needed) {
|
|
123
|
-
return { ok: false, message: "No layout yet — the image must render once to know its displayed size" };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
let manipulator: {
|
|
127
|
-
ImageManipulator?: { manipulate: (source: string) => any };
|
|
128
|
-
manipulateAsync?: (
|
|
129
|
-
uri: string,
|
|
130
|
-
actions: unknown[],
|
|
131
|
-
options: Record<string, unknown>
|
|
132
|
-
) => Promise<{ uri: string; width: number; height: number }>;
|
|
133
|
-
SaveFormat?: Record<string, string>;
|
|
134
|
-
};
|
|
135
|
-
try {
|
|
136
|
-
manipulator = require("expo-image-manipulator");
|
|
137
|
-
} catch {
|
|
138
|
-
return {
|
|
139
|
-
ok: false,
|
|
140
|
-
message: "expo-image-manipulator not installed — add it to enable real savings measurement",
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
const webp = manipulator.SaveFormat?.WEBP ?? "webp";
|
|
145
|
-
const targetWidth = Math.min(needed.width, record.intrinsic?.width ?? needed.width);
|
|
146
|
-
|
|
147
|
-
let original: Awaited<ReturnType<typeof resolveOriginalBytes>> = null;
|
|
148
|
-
let optimized: { uri: string; width: number; height: number } | null = null;
|
|
149
|
-
try {
|
|
150
|
-
[original, optimized] = await Promise.all([
|
|
151
|
-
resolveOriginalBytes(record),
|
|
152
|
-
(async (): Promise<{ uri: string; width: number; height: number } | null> => {
|
|
153
|
-
// Preferred: context API (current). Fallback: deprecated manipulateAsync.
|
|
154
|
-
if (manipulator.ImageManipulator?.manipulate) {
|
|
155
|
-
const context = manipulator.ImageManipulator.manipulate(record.uri);
|
|
156
|
-
try {
|
|
157
|
-
const ref = await context.resize({ width: targetWidth }).renderAsync();
|
|
158
|
-
try {
|
|
159
|
-
return await ref.saveAsync({ format: webp, compress: 0.8 });
|
|
160
|
-
} finally {
|
|
161
|
-
ref.release?.();
|
|
162
|
-
}
|
|
163
|
-
} finally {
|
|
164
|
-
context.release?.();
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
if (manipulator.manipulateAsync) {
|
|
168
|
-
return manipulator.manipulateAsync(record.uri, [{ resize: { width: targetWidth } }], {
|
|
169
|
-
compress: 0.8,
|
|
170
|
-
format: webp,
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
return null;
|
|
174
|
-
})(),
|
|
175
|
-
]);
|
|
176
|
-
} catch (error) {
|
|
177
|
-
return { ok: false, message: `Re-encode failed: ${String((error as Error)?.message ?? error)}` };
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
if (!optimized?.uri) {
|
|
181
|
-
return { ok: false, message: "Re-encode produced no output (unsupported source?)" };
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
const optimizedBytes = await fileSize(optimized.uri);
|
|
185
|
-
const result: SavingsResult = {
|
|
186
|
-
ok: true,
|
|
187
|
-
message: "",
|
|
188
|
-
optimizedUri: optimized.uri,
|
|
189
|
-
optimizedBytes: optimizedBytes ?? undefined,
|
|
190
|
-
optimizedDims: { width: optimized.width, height: optimized.height },
|
|
191
|
-
originalBytes: original?.bytes,
|
|
192
|
-
originalBytesSource: original?.source,
|
|
193
|
-
};
|
|
194
|
-
if (original?.bytes != null && optimizedBytes != null) {
|
|
195
|
-
result.savedBytes = Math.max(0, original.bytes - optimizedBytes);
|
|
196
|
-
result.savedPct = original.bytes > 0 ? Math.round((result.savedBytes / original.bytes) * 100) : 0;
|
|
197
|
-
result.message = `Right-sized WebP is ${result.savedPct}% smaller (real bytes, encoded on device)`;
|
|
198
|
-
} else if (optimizedBytes != null) {
|
|
199
|
-
result.message = "Optimized variant encoded — original size unknown (no download bytes, cache entry, or Content-Length)";
|
|
200
|
-
} else {
|
|
201
|
-
result.message = "Optimized variant encoded — could not stat the output file";
|
|
202
|
-
}
|
|
203
|
-
return result;
|
|
204
|
-
}
|