Skip to content

Commit 766100a

Browse files
Set parameter validation Metadata for GenerateImage operations (#2611)
1 parent 89feb7f commit 766100a

1 file changed

Lines changed: 18 additions & 10 deletions

File tree

src/core/operations/GenerateImage.mjs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ import { toBase64 } from "../lib/Base64.mjs";
1212
import { isWorkerEnvironment } from "../Utils.mjs";
1313
import { Jimp, JimpMime, ResizeStrategy, rgbaToInt } from "jimp";
1414

15+
// arbitrary limits to prevent resource exhaustion
16+
// scale factor of 64 is big enough to likely result in scaling in the display
17+
// window anyway
18+
// pixels per row is harder to come up with a figure that won't inconvenience
19+
// someone. 2048 feels like a reasonable compromise
20+
const MAX_PIXEL_SCALE_FACTOR = 64;
21+
const MAX_PIXELS_PER_ROW = 2048;
22+
1523
/**
1624
* Generate Image operation
1725
*/
@@ -40,11 +48,17 @@ class GenerateImage extends Operation {
4048
name: "Pixel Scale Factor",
4149
type: "number",
4250
value: 8,
51+
integer: true,
52+
min: 1,
53+
max: MAX_PIXEL_SCALE_FACTOR,
4354
},
4455
{
4556
name: "Pixels per row",
4657
type: "number",
4758
value: 64,
59+
integer: true,
60+
min: 1,
61+
max: MAX_PIXELS_PER_ROW,
4862
},
4963
];
5064
}
@@ -58,14 +72,6 @@ class GenerateImage extends Operation {
5872
const [mode, scale, width] = args;
5973
input = new Uint8Array(input);
6074

61-
if (scale <= 0) {
62-
throw new OperationError("Pixel Scale Factor needs to be > 0");
63-
}
64-
65-
if (width <= 0) {
66-
throw new OperationError("Pixels per Row needs to be > 0");
67-
}
68-
6975
const bytePerPixelMap = {
7076
Greyscale: 1,
7177
RG: 2,
@@ -167,8 +173,10 @@ class GenerateImage extends Operation {
167173
}
168174

169175
try {
170-
const imageBuffer = await image.getBuffer(JimpMime.png);
171-
return imageBuffer.buffer;
176+
// see https://nodejs.org/docs/latest-v24.x/api/buffer.html#bufbyteoffset
177+
// for why we can't just return result.buffer
178+
const result = await image.getBuffer(JimpMime.png);
179+
return result.buffer.slice(result.byteOffset, result.byteOffset + result.byteLength);
172180
} catch (err) {
173181
throw new OperationError(`Error generating image. (${err})`);
174182
}

0 commit comments

Comments
 (0)