Browse Source

fix: add 10s timeout to getImageDimensions to prevent promise hang

- Add resolved flag and safeResolve guard pattern from getVideoDimensions
- Add cleanup() function to null event handlers and clear src
- Add 10-second timeout that resolves with null if neither onload nor onerror fires
- Prevents promise from hanging indefinitely on corrupt/malformed images
handoff-20260429-1057
shrimbly 5 months ago
parent
commit
23db4d1cf1
  1. 19
      src/utils/nodeDimensions.ts

19
src/utils/nodeDimensions.ts

@ -16,20 +16,29 @@ export function getImageDimensions(
return;
}
let resolved = false;
const img = new Image();
const cleanup = () => {
img.onload = null;
img.onerror = null;
img.src = "";
};
img.onload = () => {
const dims = { width: img.naturalWidth, height: img.naturalHeight };
const safeResolve = (value: { width: number; height: number } | null) => {
if (resolved) return;
resolved = true;
cleanup();
resolve(dims);
resolve(value);
};
const timeout = setTimeout(() => safeResolve(null), 10_000);
img.onload = () => {
clearTimeout(timeout);
safeResolve({ width: img.naturalWidth, height: img.naturalHeight });
};
img.onerror = () => {
cleanup();
resolve(null);
clearTimeout(timeout);
safeResolve(null);
};
img.src = base64DataUrl;
});

Loading…
Cancel
Save