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; return;
} }
let resolved = false;
const img = new Image(); const img = new Image();
const cleanup = () => { const cleanup = () => {
img.onload = null; img.onload = null;
img.onerror = null; img.onerror = null;
img.src = ""; img.src = "";
}; };
img.onload = () => { const safeResolve = (value: { width: number; height: number } | null) => {
const dims = { width: img.naturalWidth, height: img.naturalHeight }; if (resolved) return;
resolved = true;
cleanup(); 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 = () => { img.onerror = () => {
cleanup(); clearTimeout(timeout);
resolve(null); safeResolve(null);
}; };
img.src = base64DataUrl; img.src = base64DataUrl;
}); });

Loading…
Cancel
Save