Skip to content

Commit 1d76007

Browse files
committed
export LimitError
1 parent 6bc7a9d commit 1d76007

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎src/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type { AnimationOptions, AnimationCanvasOptions, AnimationEncodeOptions,
2525
export { AnimationEncoder } from './animation-encoder.ts';
2626
export type { TextLayoutOptions, WrapStyle, HorizontalAlign, VerticalAlign } from './text-layout.ts';
2727
export { ImageType } from './image-type.ts';
28+
export { LimitError } from './util/limits.ts';
2829
export type { ImageTypeName } from './image-type.ts';
2930
export type { ToneMapping } from './codecs/hdr.ts';
3031
export type { BinaryData } from './util/mem.ts';

‎src/util/limits.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// limits against decompression bombs: small files declaring huge images
22

3-
/** Thrown when an image exceeds the decode options' maxPixels */
4-
export class LimitError extends RangeError {}
3+
/** Thrown when an image exceeds a limit of the decode options (`maxPixels`) or the render options of SVGs */
4+
export class LimitError extends RangeError {
5+
/** `'LimitError'`: tells a limit apart from other `RangeError`s */
6+
override name = 'LimitError';
7+
}
58

69
/** The default of the decode options' maxPixels: 16383 squared (like sharp's limitInputPixels) */
710
export const MAX_PIXELS = 0x3fff * 0x3fff;

‎tests/regressions.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,15 @@ const rgba = (image, x, y) => Colors.toRGBA(image.getPixelAt(x, y));
960960
await tooLarge('tiff samples', () => Image.decode(tiff));
961961
await tooLarge('gif frames', () => Animation.decode(gif, {maxPixels: 4000 * 4000 * 10}));
962962
await tooLarge('maxPixels', () => Image.decode(png(100, 100), {maxPixels: 9999}));
963+
// (the limit's error is exported, and tells itself from other RangeErrors by its name)
964+
{
965+
let error = null;
966+
try { await Image.decode(png(100, 100), {maxPixels: 9999}); } catch (err) { error = err; }
967+
assert(error instanceof require('..').LimitError && error instanceof RangeError && error.name === 'LimitError', `LimitError: ${error && error.name}`);
968+
error = null;
969+
try { await Image.renderSVG('<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40"/>', {maxPixels: 15}); } catch (err) { error = err; }
970+
assert(error instanceof require('..').LimitError, `SVG maxPixels: ${error && error.constructor.name}`);
971+
}
963972

964973
// without a limit, the PNG gets as far as its data (which is too short)
965974
let error = null;

0 commit comments

Comments
 (0)