Skip to content

Commit 3afdd73

Browse files
committed
check creationTime's type, keep whole characters in cut strings and name renderSVG and renderText with their class
1 parent f4b44b9 commit 3afdd73

5 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ Upgrading from 3.x? Upgrade to the latest 3.x release first, fix the deprecation
136136

137137
### Fixes
138138

139+
- `renderSVG()` bounds the curves an arc between far-apart ends is drawn with and the points paths flatten to: a 139-byte path (`M 1e25 100 A 40 30 20 1 1 160 100`, stroked) ran the process out of memory. Too many points throw a `LimitError`.
140+
- `encodeJPEG()` and `encodeWebP()` check the image's size before going over its pixels; `metadata.creationTime` of another type than a `Date`, a number or a string throws a `TypeError` (`true` was 1970).
141+
- `Colors.gradient()` has the later stop's color at every hard edge (two stops at one position), like CSS, also inside the gradient and with 2 stops.
139142
- `encodeJPEG()` of an image with transparency composites it onto the background row by row instead of copying the image first: the peak memory of a 6000x6000 image is 378 MB instead of 485 MB, the same as without transparency, and the file is the same (images with a gain map still take the copy).
140143
- `encodeWebP({ optimize: false })` makes the same file as `webpEncoder()` for animations with frames WebP can't store as they are (stored as the canvas from the first such frame on, not all of them), and `Colors.gradient()` with 3 or more stops has its first and last stops' exact colors before and after them (a transparent end stop gave `0x00000000`) and takes 200,000 stops without overflowing the call stack.
141144
- `resize({ fit: 'inside' })` throws for a box side below 1 pixel, like the other fits, instead of making a 1x1 image; `pixels()` goes over the pixels the image has when it's called, also when it's resized before the first value is taken.

‎src/image.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,7 +3087,7 @@ export class Image {
30873087
*/
30883088
static renderSVG(svg: string | BinaryData, options?: RenderSVGOptions): Promise<Image>;
30893089
static async renderSVG(svg: string | BinaryData, options: RenderSVGOptions = {}): Promise<Image> {
3090-
checkOptions(options, 'renderSVG(svg, { width: 512 })');
3090+
checkOptions(options, 'Image.renderSVG(svg, { width: 512 })');
30913091
const { width: givenWidth, height: givenHeight, scale, maxPixels = MAX_PIXELS, font: fonts = [], images: givenImages = {} } = options;
30923092
pixelLimit(maxPixels);
30933093
const { maxLayerPixels = 4 * maxPixels, maxImagePixels = maxPixels } = options;
@@ -3171,8 +3171,9 @@ export class Image {
31713171
* allocated)
31723172
*/
31733173
static async renderText(text: string, options: RenderTextOptions): Promise<Image> {
3174-
if (typeof text !== 'string' || !isOptions(options)) throw new TypeError('renderText() takes the text and an options object, like renderText(\'Hello\', { font, size: 32 })');
3175-
if (options.font === undefined) throw new TypeError('renderText(text, options) needs the font in the options');
3174+
if (typeof text !== 'string') throw new TypeError(`Image.renderText() takes the text as a string, like Image.renderText('Hello', { font, size: 32 }), not ${shown(text)}`);
3175+
if (!isOptions(options)) throw new TypeError(`Image.renderText() takes an options object, like Image.renderText('Hello', { font, size: 32 }), not ${options === undefined ? 'undefined' : shown(options)}`);
3176+
if (options.font === undefined) throw new TypeError('Image.renderText(text, options) needs the font in the options');
31763177
const { font, color = 0xffffffff, strokeWidth = 0, strokeColor = 0x000000ff, hinting = true, features = {}, variations = {}, maxPixels = MAX_PIXELS } = options;
31773178
pixelLimit(maxPixels);
31783179
boolean(hinting, 'hinting');

‎src/metadata.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export function resolveMetadata(metadata: Metadata = {}): ResolvedMetadata {
8585
const { creationTime, software } = metadata;
8686
const resolved: ResolvedMetadata = {};
8787
if (creationTime !== undefined && creationTime !== null) {
88+
// (other types than these were turned into a date anyway: true was 1970)
89+
if (!(creationTime instanceof Date) && typeof creationTime !== 'number' && typeof creationTime !== 'string') {
90+
// eslint-disable-next-line no-restricted-syntax -- the one date in the options
91+
throw new TypeError(`creationTime must be a Date, a number or a string, not ${typeof creationTime === 'object' ? shown(creationTime) : typeof creationTime}`);
92+
}
8893
const time = new Date(creationTime);
8994
// eslint-disable-next-line no-restricted-syntax -- the one date in the options
9095
if (isNaN(time.getTime())) throw new RangeError(`creationTime must be a date, a timestamp or a date string, not ${shown(creationTime)}`);

‎src/util/options.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ export function shown(value: unknown): string {
33
if (value === null) return 'null';
44
if (Array.isArray(value)) return 'an array';
55
// (a long string is cut: a message shouldn't carry the megabytes someone passed by mistake)
6-
if (typeof value === 'string') return value.length > 40 ? `'${value.slice(0, 40)}...'` : `'${value}'`;
6+
// (40 characters, not UTF-16 units: a cut through a surrogate pair would leave half a character)
7+
if (typeof value === 'string') {
8+
const characters = Array.from(value.length > 80 ? value.slice(0, 82) : value);
9+
return characters.length > 40 ? `'${characters.slice(0, 40).join('')}...'` : `'${value}'`;
10+
}
711
if (typeof value === 'number') return String(value);
812
if (typeof value === 'object') return 'an object';
913
return typeof value;

‎tests/error-messages.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ const shape = /^[\w() ]+ (must be|must have|takes) .+, not .+$/;
131131
['font feature value', () => Image.renderText('A', {font, size: 20, features: {liga: 'x'}}), /not 'x'$/],
132132
['font variation value', () => Image.renderText('A', {font, size: 20, variations: {wght: 'x'}}), /not 'x'$/],
133133
['resize(null)', () => image().resize(null), /^resize\(\)/],
134+
// a creation time is a Date, a timestamp or a string: other types throw a TypeError, not a RangeError (and
135+
// true isn't 1970)
136+
['creationTime true', () => image().encodePNG({metadata: {creationTime: true}}), /^creationTime must be a Date, a number or a string, not boolean$/],
137+
['creationTime object', () => image().encodePNG({metadata: {creationTime: {}}}), /, not an object$/],
138+
['creationTime array', () => image().encodePNG({metadata: {creationTime: []}}), /, not an array$/],
139+
// a cut string keeps whole characters (no lone half of a surrogate pair)
140+
// statics are named with their class, like Image.decode()
141+
['renderSVG options', () => Image.renderSVG('<svg/>', 5), /^Image\.renderSVG\(\) takes an options object.*, not 5$/],
142+
['renderText options', () => Image.renderText('A', 5), /^Image\.renderText\(\) takes an options object.*, not 5$/],
143+
['renderText text', () => Image.renderText(5, {}), /^Image\.renderText\(\) takes the text as a string.*, not 5$/],
144+
['cut string', () => Image.decode('x'.repeat(39) + '\u{1F600}' + 'x'.repeat(10)), /not 'x{39}\u{1F600}\.\.\.'$/u],
134145
// the options check names the method, not its receiver
135146
['encodeWebP(5)', () => image().encodeWebP(5), /^encodeWebP\(\) takes an options object, like image\.encodeWebP\(/],
136147
['new GainMap(image, 5)', () => new GainMap(new Image(4, 4), 5), /^GainMap\(\) takes an options object/],

0 commit comments

Comments
 (0)