File size: 1,537 Bytes
3144483
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { describe, expect, it } from "vitest";
import { createImageProcessor, createImageProcessorWithPixelLimits } from "./image-processor.js";

const image = Buffer.from(
  "iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAAEUlEQVR4nGMwSJiAFTEMLQkACmxIAasRElQAAAAASUVORK5CYII=",
  "base64",
);

describe("operation-specific pixel limits through the image worker", () => {
  it("keeps local probes, worker encodes and later default requests consistent", async () => {
    const restricted = createImageProcessorWithPixelLimits({ inputPixels: 32, outputPixels: 16 });
    expect(await restricted.probe(image)).toBeNull();
    await expect(
      restricted.encode(image, { format: "png", resize: { maxSide: 4 } }),
    ).rejects.toThrow();

    const downscale = createImageProcessorWithPixelLimits({ inputPixels: 64, outputPixels: 16 });
    expect(await downscale.probe(image)).toMatchObject({ width: 8, height: 8 });
    const encoded = await downscale.encode(image, { format: "png", resize: { maxSide: 4 } });
    expect(encoded).toMatchObject({ width: 4, height: 4, format: "png" });
    expect(encoded.data.length).toBeGreaterThan(0);
    await expect(downscale.encode(image, { format: "png" })).rejects.toMatchObject({
      code: "RASTERMILL_OUTPUT_TOO_LARGE",
    });

    // A scoped downscale limit must not change the shared worker's subsequent default policy.
    const normal = await createImageProcessor().encode(image, { format: "png" });
    expect(normal).toMatchObject({ width: 8, height: 8, format: "png" });
  });
});