2026-05-09 07:35:27 +08:00
|
|
|
import sharp from "sharp";
|
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
|
import { decodeHeic, ensureSharpCompat } from "../../../apps/api/src/lib/heic-converter.js";
|
2026-06-20 04:20:17 +08:00
|
|
|
import { fixtures, readFixture } from "../../fixtures/index.js";
|
2026-05-09 07:35:27 +08:00
|
|
|
|
|
|
|
|
function isPng(buf: Buffer): boolean {
|
|
|
|
|
return buf[0] === 0x89 && buf[1] === 0x50 && buf[2] === 0x4e && buf[3] === 0x47;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function makeHeicHeader(brand: string): Buffer {
|
|
|
|
|
const buf = Buffer.alloc(12);
|
|
|
|
|
buf.writeUInt32BE(12, 0);
|
|
|
|
|
buf.write("ftyp", 4, 4, "ascii");
|
|
|
|
|
buf.write(brand, 8, 4, "ascii");
|
|
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 03:34:24 +08:00
|
|
|
function makeIspeBox(width: number, height: number): Buffer {
|
|
|
|
|
const box = Buffer.alloc(20);
|
|
|
|
|
box.writeUInt32BE(20, 0);
|
|
|
|
|
box.write("ispe", 4, 4, "ascii");
|
|
|
|
|
box.writeUInt32BE(width, 12);
|
|
|
|
|
box.writeUInt32BE(height, 16);
|
|
|
|
|
return box;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-09 07:35:27 +08:00
|
|
|
describe("decodeHeic", () => {
|
2026-07-15 03:34:24 +08:00
|
|
|
it("rejects source dimensions over a caller-supplied pixel cap", async () => {
|
|
|
|
|
const heicBuf = readFixture(fixtures.image.formats("heic"));
|
|
|
|
|
|
|
|
|
|
await expect(decodeHeic(heicBuf, { maxPixels: 1 })).rejects.toThrow(/pixel safety limit/i);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("checks every image extent instead of trusting a small first thumbnail", async () => {
|
|
|
|
|
const multiImage = Buffer.concat([
|
|
|
|
|
makeHeicHeader("heic"),
|
|
|
|
|
makeIspeBox(1, 1),
|
|
|
|
|
makeIspeBox(10_000, 10_000),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await expect(decodeHeic(multiImage, { maxPixels: 40_000_000 })).rejects.toThrow(
|
|
|
|
|
/pixel safety limit/i,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects an extreme image side from encoded metadata before starting a decoder", async () => {
|
|
|
|
|
const extremeImage = Buffer.concat([
|
|
|
|
|
makeHeicHeader("heic"),
|
|
|
|
|
makeIspeBox(1_000, 1_000),
|
|
|
|
|
makeIspeBox(40_001, 1),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
decodeHeic(extremeImage, { maxDimension: 40_000, maxPixels: 40_000_000 }),
|
|
|
|
|
).rejects.toThrow(/dimension safety limit.*40,001x1/i);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("honors an already-aborted request", async () => {
|
|
|
|
|
const controller = new AbortController();
|
|
|
|
|
controller.abort();
|
|
|
|
|
|
|
|
|
|
await expect(
|
|
|
|
|
decodeHeic(readFixture(fixtures.image.formats("heic")), { signal: controller.signal }),
|
|
|
|
|
).rejects.toMatchObject({ name: "AbortError" });
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-09 07:35:27 +08:00
|
|
|
it("decodes sample.heic to a valid PNG buffer", async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const heicBuf = readFixture(fixtures.image.formats("heic"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const result = await decodeHeic(heicBuf);
|
|
|
|
|
expect(isPng(result)).toBe(true);
|
|
|
|
|
const meta = await sharp(result).metadata();
|
|
|
|
|
expect(meta.width).toBeGreaterThan(0);
|
|
|
|
|
expect(meta.height).toBeGreaterThan(0);
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-11 22:48:05 +08:00
|
|
|
it("decodes sample.heif to a valid PNG buffer", { timeout: 60_000 }, async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const heifBuf = readFixture(fixtures.image.formats("heif"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const result = await decodeHeic(heifBuf);
|
|
|
|
|
expect(isPng(result)).toBe(true);
|
|
|
|
|
const meta = await sharp(result).metadata();
|
|
|
|
|
expect(meta.width).toBeGreaterThan(0);
|
|
|
|
|
expect(meta.height).toBeGreaterThan(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("preserves image dimensions after decoding", async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const heicBuf = readFixture(fixtures.image.formats("heic"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const result = await decodeHeic(heicBuf);
|
|
|
|
|
const meta = await sharp(result).metadata();
|
|
|
|
|
expect(meta.width).toBeGreaterThan(0);
|
|
|
|
|
expect(meta.height).toBeGreaterThan(0);
|
|
|
|
|
expect(meta.format).toBe("png");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects invalid/corrupt data", async () => {
|
|
|
|
|
const garbage = Buffer.from("this is not a heic file at all");
|
|
|
|
|
await expect(decodeHeic(garbage)).rejects.toThrow();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("rejects an empty buffer", async () => {
|
|
|
|
|
await expect(decodeHeic(Buffer.alloc(0))).rejects.toThrow();
|
|
|
|
|
});
|
|
|
|
|
|
2026-05-11 22:48:05 +08:00
|
|
|
it("cleans up temp files after success", { timeout: 60_000 }, async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const heicBuf = readFixture(fixtures.image.formats("heic"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const { tmpdir } = await import("node:os");
|
|
|
|
|
const { readdirSync } = await import("node:fs");
|
2026-06-28 18:57:53 +08:00
|
|
|
// decodeHeic stamps its temp files with this process's PID. Scope the check
|
|
|
|
|
// to those so temp files created by other concurrent test-worker processes
|
|
|
|
|
// (different PID) in the shared tmpdir cannot make this flaky.
|
|
|
|
|
const mine = (f: string) =>
|
|
|
|
|
f.startsWith(`heic-in-${process.pid}-`) || f.startsWith(`heic-out-${process.pid}-`);
|
|
|
|
|
const beforeSet = new Set(readdirSync(tmpdir()).filter(mine));
|
2026-05-09 07:35:27 +08:00
|
|
|
await decodeHeic(heicBuf);
|
2026-06-28 18:57:53 +08:00
|
|
|
const leftover = readdirSync(tmpdir())
|
|
|
|
|
.filter(mine)
|
|
|
|
|
.filter((f) => !beforeSet.has(f));
|
2026-06-13 10:15:23 +08:00
|
|
|
expect(leftover).toHaveLength(0);
|
2026-05-09 07:35:27 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("ensureSharpCompat", () => {
|
|
|
|
|
it("decodes a HEIC buffer to PNG", async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const heicBuf = readFixture(fixtures.image.formats("heic"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const result = await ensureSharpCompat(heicBuf);
|
|
|
|
|
expect(isPng(result)).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("passes through a PNG buffer unchanged", async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const pngBuf = readFixture(fixtures.image.formats("png"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const result = await ensureSharpCompat(pngBuf);
|
|
|
|
|
expect(result).toBe(pngBuf);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("passes through a JPEG buffer unchanged", async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const jpgBuf = readFixture(fixtures.image.formats("jpg"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const result = await ensureSharpCompat(jpgBuf);
|
|
|
|
|
expect(result).toBe(jpgBuf);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("passes through a WebP buffer unchanged", async () => {
|
2026-06-20 04:20:17 +08:00
|
|
|
const webpBuf = readFixture(fixtures.image.formats("webp"));
|
2026-05-09 07:35:27 +08:00
|
|
|
const result = await ensureSharpCompat(webpBuf);
|
|
|
|
|
expect(result).toBe(webpBuf);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("detects heic brand in ftyp box", async () => {
|
|
|
|
|
const fakeBuf = makeHeicHeader("heic");
|
|
|
|
|
await expect(ensureSharpCompat(fakeBuf)).rejects.toThrow();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("detects mif1 brand in ftyp box", async () => {
|
|
|
|
|
const fakeBuf = makeHeicHeader("mif1");
|
|
|
|
|
await expect(ensureSharpCompat(fakeBuf)).rejects.toThrow();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not treat a short buffer as HEIF", async () => {
|
|
|
|
|
const shortBuf = Buffer.from("tiny");
|
|
|
|
|
const result = await ensureSharpCompat(shortBuf);
|
|
|
|
|
expect(result).toBe(shortBuf);
|
|
|
|
|
});
|
|
|
|
|
});
|