Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/image-rescale.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,13 @@ function loadJimp(): Promise<JimpModule | null> {
}

// force (#3701 byte-shed ladder): adopts ANY strict byte reduction (not just <80%).
// The 100 KiB trigger gate applies equally to both; a within-maxDim image is
// re-encoded only if it exceeds the trigger. The byte-shed caller pre-filters
// maxDim-fitting images before calling (egress-shed rung B).
// Above-maxDim images are always re-encoded (downscaled) — no trigger gate. For
// within-maxDim images the 100 KiB trigger gate IS applied as a cheap pre-filter:
// an image at/below the trigger is skipped (re-encoding a small image rarely pays),
// while one above it is re-encoded at the same dims and adopted only if strictly
// smaller. The byte-shed caller pre-filters known-dim maxDim-fitting images before
// calling (egress-shed rung B), so the within-maxDim branch is defensive — only hit
// when the header dim parse fails or disagrees with the authoritative Jimp.read decode.
async function doRescale(mime: RescaledMime, b64: string, origBytes: number, maxDim: number, force = false): Promise<RescaledImage | null> {
try {
const mod = await loadJimp();
Expand Down Expand Up @@ -220,10 +224,12 @@ export function clearRescaleEncodeCache(maxBytesForTest = ENCODE_CACHE_MAX_BYTES
}

/** #3701 byte-shed ladder step: forced re-encode at an explicit longest-edge dim.
* Unlike rescaleImage there is no trigger gate and no keep-ratio floor — any strict
* reduction is adopted; images already within maxDim are left alone. Returns the new
* base64 (same mime) or null when nothing was saved (unsupported format, jimp absent,
* or the re-encode did not shrink). Cached per hash+dim. */
* Unlike rescaleImage there is no keep-ratio floor — any strict reduction is adopted.
* Above-maxDim images are always re-encoded (downscaled). Within-maxDim images are
* skipped at/below the 100 KiB trigger (cheap pre-filter) and re-encoded at the same
* dims above it, adopted only if strictly smaller. Returns the new base64 (same mime)
* or null when nothing was saved (unsupported format, jimp absent, at/below the
* trigger, or the re-encode did not shrink). Cached per hash+dim. */
export async function shedDownscaleImage(mediaType: string, b64: string, maxDim: number): Promise<string | null> {
const mime = normalizeMime(mediaType);
if (!mime || typeof b64 !== "string" || b64.length === 0 || !Number.isFinite(maxDim) || maxDim <= 0) return null;
Expand Down
16 changes: 16 additions & 0 deletions tests/image-rescale-cache-budget.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,19 @@ test("a normal reset restores the production budget after a test override", asyn
await shedDownscaleImage("image/png", input(0), 1);
assert.equal(reads, 3, "the default reset restores normal cache admission");
});

test("forced within-maxDim: the 100 KiB trigger gate skips small, re-encodes large", async (t) => {
// A 1x1 image is within maxDim=1 (scale=1, no downscale). The forced path's
// trigger gate is a cheap pre-filter: below-trigger images are skipped entirely
// (no decode/re-encode), above-trigger images are re-encoded at the same dims and
// adopted only if strictly smaller. Pins both directions (#3617).
const pixels = new Jimp({ width: 1, height: 1 });
t.mock.method(pixels, "getBuffer", async () => Buffer.alloc(8));
t.mock.method(Jimp, "read", async () => pixels);

const below = "a".repeat(136532); // b64Bytes = 102399, below the 100 KiB trigger
const above = "a".repeat(136536); // b64Bytes = 102402, above the trigger

assert.equal(await shedDownscaleImage("image/png", below, 1), null, "below trigger is skipped");
assert.equal(await shedDownscaleImage("image/png", above, 1), Buffer.alloc(8).toString("base64"), "above trigger is re-encoded and adopted");
});
Loading