fix: address review feedback from #1333 and #1335 (#1343)

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
This commit is contained in:
Matt Van Horn
2026-06-12 16:18:18 -07:00
committed by GitHub
co-authored by Matt Van Horn
parent 1ea0ed55e3
commit 28e2ab9d5b
6 changed files with 78 additions and 39 deletions
+9
View File
@@ -71,6 +71,15 @@ describe("parseAnimatedGifMetadata", () => {
expect(metadata?.durationSeconds).toBe(0.2);
});
it("clamps all-zero frame delays to the browser playback minimum", () => {
const frames = Array.from({ length: 10 }, () => frame(0)).flat();
const metadata = parseAnimatedGifMetadata(gif(frames, 0));
expect(metadata?.animated).toBe(true);
expect(metadata?.delaysCentiseconds).toEqual(Array.from({ length: 10 }, () => 10));
expect(metadata?.durationSeconds).toBe(1);
});
it("reads Netscape loop metadata", () => {
const metadata = parseAnimatedGifMetadata(gif([...frame(8), ...frame(8)], 0));
+10 -3
View File
@@ -9,6 +9,14 @@ export interface AnimatedGifMetadata {
animated: boolean;
}
const BROWSER_MIN_DELAY_CENTISECONDS = 10;
function normalizeDelayCentiseconds(delay: number): number {
// Chrome clamps GIF frame delays <= 1cs to 10cs (100ms); mirror browser playback timing.
if (delay <= 1) return BROWSER_MIN_DELAY_CENTISECONDS;
return Math.max(0, delay);
}
function readAscii(bytes: Uint8Array, start: number, length: number): string {
if (start + length > bytes.length) return "";
let value = "";
@@ -104,7 +112,7 @@ export function parseAnimatedGifMetadata(bytes: Uint8Array): AnimatedGifMetadata
if (blockSize !== 4 || pos + 6 > bytes.length) return null;
const delay = readU16LE(bytes, pos + 2);
if (delay == null) return null;
delaysCentiseconds.push(delay);
delaysCentiseconds.push(normalizeDelayCentiseconds(delay));
pos += 1 + blockSize;
if (bytes[pos] !== 0) return null;
pos += 1;
@@ -144,8 +152,7 @@ export function parseAnimatedGifMetadata(bytes: Uint8Array): AnimatedGifMetadata
return null;
}
const durationSeconds =
delaysCentiseconds.reduce((total, delay) => total + Math.max(0, delay), 0) / 100;
const durationSeconds = delaysCentiseconds.reduce((total, delay) => total + delay, 0) / 100;
return {
width,