@dicebear/core 11.0.0-rc.3 → 11.0.0-rc.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/Options.d.ts CHANGED
@@ -13,9 +13,18 @@ import type { StyleOptions, StyleOptionsFlipValue, StyleOptionsColorFillValue, S
13
13
  export declare class Options<D = unknown> {
14
14
  #private;
15
15
  constructor(data?: StyleOptions<D>);
16
+ /**
17
+ * The seed, with every lone surrogate replaced by U+FFFD: ill-formed text
18
+ * has no meaning another port could reproduce, so the hash and the initials
19
+ * read the same text everywhere.
20
+ */
16
21
  seed(): string | undefined;
17
22
  size(): number | undefined;
18
23
  idRandomization(): boolean | undefined;
24
+ /**
25
+ * The title, with every lone surrogate replaced by U+FFFD, so no ill-formed
26
+ * text reaches the SVG.
27
+ */
19
28
  title(): string | undefined;
20
29
  flip(): readonly StyleOptionsFlipValue[];
21
30
  fontFamily(): readonly string[];
@@ -38,7 +47,7 @@ export declare class Options<D = unknown> {
38
47
  */
39
48
  animationSpeedFor(name: string): Range | undefined;
40
49
  animationDelay(): Range | undefined;
41
- animationMotionQuery(): boolean | undefined;
50
+ animationRespectReducedMotion(): boolean | undefined;
42
51
  /**
43
52
  * Returns the `${name}AnimationDelay` option for one animation name as a
44
53
  * range, or `undefined` when unset.
package/lib/Options.js CHANGED
@@ -9,7 +9,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _Options_instances, _Options_data, _Options_tags, _Options_dynamic, _Options_asArray, _Options_toRange;
12
+ var _Options_instances, _Options_data, _Options_tags, _Options_dynamic, _Options_asArray, _Options_wellFormed, _Options_toRange;
13
+ import { Text } from './Utils/Text.js';
13
14
  /**
14
15
  * Validates the raw user-supplied options and exposes them through typed
15
16
  * accessors. Each accessor returns the user's input in a normalized form
@@ -27,8 +28,13 @@ export class Options {
27
28
  _Options_tags.set(this, void 0);
28
29
  __classPrivateFieldSet(this, _Options_data, structuredClone(data), "f");
29
30
  }
31
+ /**
32
+ * The seed, with every lone surrogate replaced by U+FFFD: ill-formed text
33
+ * has no meaning another port could reproduce, so the hash and the initials
34
+ * read the same text everywhere.
35
+ */
30
36
  seed() {
31
- return __classPrivateFieldGet(this, _Options_data, "f").seed;
37
+ return __classPrivateFieldGet(this, _Options_instances, "m", _Options_wellFormed).call(this, __classPrivateFieldGet(this, _Options_data, "f").seed);
32
38
  }
33
39
  size() {
34
40
  return __classPrivateFieldGet(this, _Options_data, "f").size;
@@ -36,8 +42,12 @@ export class Options {
36
42
  idRandomization() {
37
43
  return __classPrivateFieldGet(this, _Options_data, "f").idRandomization;
38
44
  }
45
+ /**
46
+ * The title, with every lone surrogate replaced by U+FFFD, so no ill-formed
47
+ * text reaches the SVG.
48
+ */
39
49
  title() {
40
- return __classPrivateFieldGet(this, _Options_data, "f").title;
50
+ return __classPrivateFieldGet(this, _Options_instances, "m", _Options_wellFormed).call(this, __classPrivateFieldGet(this, _Options_data, "f").title);
41
51
  }
42
52
  flip() {
43
53
  return __classPrivateFieldGet(this, _Options_instances, "m", _Options_asArray).call(this, __classPrivateFieldGet(this, _Options_data, "f").flip);
@@ -86,8 +96,8 @@ export class Options {
86
96
  animationDelay() {
87
97
  return __classPrivateFieldGet(this, _Options_instances, "m", _Options_toRange).call(this, __classPrivateFieldGet(this, _Options_data, "f").animationDelay);
88
98
  }
89
- animationMotionQuery() {
90
- return __classPrivateFieldGet(this, _Options_data, "f").animationMotionQuery;
99
+ animationRespectReducedMotion() {
100
+ return __classPrivateFieldGet(this, _Options_data, "f").animationRespectReducedMotion;
91
101
  }
92
102
  /**
93
103
  * Returns the `${name}AnimationDelay` option for one animation name as a
@@ -169,6 +179,8 @@ _Options_data = new WeakMap(), _Options_tags = new WeakMap(), _Options_instances
169
179
  return value;
170
180
  }
171
181
  return [value];
182
+ }, _Options_wellFormed = function _Options_wellFormed(value) {
183
+ return typeof value === 'string' ? Text.wellFormed(value) : value;
172
184
  }, _Options_toRange = function _Options_toRange(value) {
173
185
  if (value === undefined) {
174
186
  return undefined;
@@ -115,7 +115,7 @@ _a = OptionsDescriptor, _OptionsDescriptor_descriptor = new WeakMap(), _OptionsD
115
115
  result.animation = { type: 'boolean' };
116
116
  result.animationSpeed = { type: 'range', min: 0.1, max: 10 };
117
117
  result.animationDelay = { type: 'range', min: -3600, max: 3600 };
118
- result.animationMotionQuery = { type: 'boolean' };
118
+ result.animationRespectReducedMotion = { type: 'boolean' };
119
119
  for (const name of __classPrivateFieldGet(this, _OptionsDescriptor_style, "f").animationNames()) {
120
120
  result[`${name}Animation`] = { type: 'boolean' };
121
121
  result[`${name}AnimationSpeed`] = { type: 'range', min: 0.1, max: 10 };
package/lib/Renderer.js CHANGED
@@ -359,7 +359,7 @@ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap
359
359
  // Without the media query the rules apply to every viewer. A render that
360
360
  // keeps the query must not pick them up on the same page, so the two get
361
361
  // different names. The default adds nothing and leaves the hash as it was.
362
- const motion = __classPrivateFieldGet(this, _Renderer_resolver, "f").animationMotionQuery()
362
+ const motion = __classPrivateFieldGet(this, _Renderer_resolver, "f").animationRespectReducedMotion()
363
363
  ? ''
364
364
  : ':nomotionquery';
365
365
  return (__classPrivateFieldSet(this, _Renderer_cachedAnimationHash, Fnv1a.hex((__classPrivateFieldGet(this, _Renderer_style, "f").meta().source().name() ?? '') +
@@ -503,7 +503,7 @@ _a = Renderer, _Renderer_style = new WeakMap(), _Renderer_resolver = new WeakMap
503
503
  key += '-';
504
504
  }
505
505
  const css = __classPrivateFieldGet(this, _Renderer_keyframesCss, "f").join('') + __classPrivateFieldGet(this, _Renderer_animationCss, "f").join('');
506
- __classPrivateFieldGet(this, _Renderer_defs, "f").set(key, __classPrivateFieldGet(this, _Renderer_resolver, "f").animationMotionQuery()
506
+ __classPrivateFieldGet(this, _Renderer_defs, "f").set(key, __classPrivateFieldGet(this, _Renderer_resolver, "f").animationRespectReducedMotion()
507
507
  ? `<style>@media (prefers-reduced-motion:no-preference){${css}}</style>`
508
508
  : `<style>${css}</style>`);
509
509
  };
package/lib/Resolver.d.ts CHANGED
@@ -45,7 +45,7 @@ export declare class Resolver<D = unknown> {
45
45
  * query. On by default. Renderers that ignore media queries need it off,
46
46
  * and the app then decides about `animation` from the system setting.
47
47
  */
48
- animationMotionQuery(): boolean;
48
+ animationRespectReducedMotion(): boolean;
49
49
  title(): string | undefined;
50
50
  flip(): StyleOptionsFlipValue;
51
51
  fontFamily(): string;
package/lib/Resolver.js CHANGED
@@ -102,8 +102,8 @@ export class Resolver {
102
102
  * query. On by default. Renderers that ignore media queries need it off,
103
103
  * and the app then decides about `animation` from the system setting.
104
104
  */
105
- animationMotionQuery() {
106
- return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'animationMotionQuery', () => __classPrivateFieldGet(this, _Resolver_options, "f").animationMotionQuery() ?? true);
105
+ animationRespectReducedMotion() {
106
+ return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'animationRespectReducedMotion', () => __classPrivateFieldGet(this, _Resolver_options, "f").animationRespectReducedMotion() ?? true);
107
107
  }
108
108
  title() {
109
109
  return __classPrivateFieldGet(this, _Resolver_instances, "m", _Resolver_memo).call(this, 'title', () => __classPrivateFieldGet(this, _Resolver_options, "f").title());
@@ -46,7 +46,7 @@ export interface StyleOptionsBase {
46
46
  readonly animation?: boolean;
47
47
  readonly animationSpeed?: number | readonly [number, number];
48
48
  readonly animationDelay?: number | readonly [number, number];
49
- readonly animationMotionQuery?: boolean;
49
+ readonly animationRespectReducedMotion?: boolean;
50
50
  }
51
51
  type ComponentVariantOption<D, K extends string> = VariantNames<D, K> | readonly VariantNames<D, K>[] | Readonly<Partial<Record<VariantNames<D, K>, number>>>;
52
52
  type IsAlias<D, C extends string> = D extends {
@@ -0,0 +1,26 @@
1
+ /**
2
+ * The nesting limit for style definitions and options.
3
+ *
4
+ * A port may turn down a value nested deeper than {@link Depth.MAX} levels
5
+ * before it runs the schema, and some must: their schema validators run out of
6
+ * stack on a few thousand levels. Every port applies the same limit, so a
7
+ * definition renders in all of them or in none. Valid options nest two levels
8
+ * and the deepest style definition uses less than a quarter of the limit.
9
+ */
10
+ export declare class Depth {
11
+ #private;
12
+ /**
13
+ * How many levels of objects and arrays a value may nest, counting the value
14
+ * itself as the first.
15
+ */
16
+ static readonly MAX = 64;
17
+ /**
18
+ * What a validator reports when {@link Depth.exceeds} holds.
19
+ */
20
+ static readonly MESSAGE: string;
21
+ /**
22
+ * Returns whether `value` nests objects and arrays deeper than
23
+ * {@link Depth.MAX} levels. A value that contains itself does.
24
+ */
25
+ static exceeds(value: unknown): boolean;
26
+ }
@@ -0,0 +1,47 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var _a, _Depth_exceeds;
7
+ /**
8
+ * The nesting limit for style definitions and options.
9
+ *
10
+ * A port may turn down a value nested deeper than {@link Depth.MAX} levels
11
+ * before it runs the schema, and some must: their schema validators run out of
12
+ * stack on a few thousand levels. Every port applies the same limit, so a
13
+ * definition renders in all of them or in none. Valid options nest two levels
14
+ * and the deepest style definition uses less than a quarter of the limit.
15
+ */
16
+ export class Depth {
17
+ /**
18
+ * Returns whether `value` nests objects and arrays deeper than
19
+ * {@link Depth.MAX} levels. A value that contains itself does.
20
+ */
21
+ static exceeds(value) {
22
+ return __classPrivateFieldGet(_a, _a, "m", _Depth_exceeds).call(_a, value, 1, new Map());
23
+ }
24
+ }
25
+ _a = Depth, _Depth_exceeds = function _Depth_exceeds(value, depth, checked) {
26
+ if (value === null || typeof value !== 'object') {
27
+ return false;
28
+ }
29
+ if (depth > _a.MAX) {
30
+ return true;
31
+ }
32
+ if ((checked.get(value) ?? 0) >= depth) {
33
+ return false;
34
+ }
35
+ checked.set(value, depth);
36
+ const items = Array.isArray(value) ? value : Object.values(value);
37
+ return items.some((item) => __classPrivateFieldGet(_a, _a, "m", _Depth_exceeds).call(_a, item, depth + 1, checked));
38
+ };
39
+ /**
40
+ * How many levels of objects and arrays a value may nest, counting the value
41
+ * itself as the first.
42
+ */
43
+ Depth.MAX = 64;
44
+ /**
45
+ * What a validator reports when {@link Depth.exceeds} holds.
46
+ */
47
+ Depth.MESSAGE = `nesting is deeper than the ${_a.MAX} levels this port reads`;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Makes user text well-formed before the core reads it.
3
+ *
4
+ * A JS string can hold a lone UTF-16 surrogate, for example half of an emoji
5
+ * cut off by a slice. Ports that keep strings as UTF-8 can hold ill-formed
6
+ * bytes instead. Neither has a meaning another port could reproduce, so every
7
+ * port replaces them with U+FFFD first. Here that is each lone surrogate, the
8
+ * same as `String.prototype.toWellFormed()`.
9
+ */
10
+ export declare class Text {
11
+ /**
12
+ * Returns `value` with every lone surrogate replaced by U+FFFD. A
13
+ * well-formed string comes back unchanged.
14
+ */
15
+ static wellFormed(value: string): string;
16
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Makes user text well-formed before the core reads it.
3
+ *
4
+ * A JS string can hold a lone UTF-16 surrogate, for example half of an emoji
5
+ * cut off by a slice. Ports that keep strings as UTF-8 can hold ill-formed
6
+ * bytes instead. Neither has a meaning another port could reproduce, so every
7
+ * port replaces them with U+FFFD first. Here that is each lone surrogate, the
8
+ * same as `String.prototype.toWellFormed()`.
9
+ */
10
+ export class Text {
11
+ /**
12
+ * Returns `value` with every lone surrogate replaced by U+FFFD. A
13
+ * well-formed string comes back unchanged.
14
+ */
15
+ static wellFormed(value) {
16
+ let result = '';
17
+ let start = 0;
18
+ for (let i = 0; i < value.length; i++) {
19
+ const unit = value.charCodeAt(i);
20
+ if (unit < 0xd800 || unit > 0xdfff) {
21
+ continue;
22
+ }
23
+ const next = i + 1 < value.length ? value.charCodeAt(i + 1) : 0;
24
+ if (unit <= 0xdbff && next >= 0xdc00 && next <= 0xdfff) {
25
+ // A valid pair: keep both units.
26
+ i++;
27
+ continue;
28
+ }
29
+ result += value.slice(start, i) + '�';
30
+ start = i + 1;
31
+ }
32
+ return start === 0 ? value : result + value.slice(start);
33
+ }
34
+ }
@@ -1192,11 +1192,11 @@ const validate = (function () {
1192
1192
  errorCount++;
1193
1193
  }
1194
1194
  }
1195
- if (data.animationMotionQuery !== undefined && hasOwn(data, "animationMotionQuery")) {
1196
- if (!(typeof data.animationMotionQuery === "boolean")) {
1195
+ if (data.animationRespectReducedMotion !== undefined && hasOwn(data, "animationRespectReducedMotion")) {
1196
+ if (!(typeof data.animationRespectReducedMotion === "boolean")) {
1197
1197
  if (validate.errors === null)
1198
1198
  validate.errors = [];
1199
- validate.errors.push({ keywordLocation: "#/properties/animationMotionQuery/type", instanceLocation: "#/animationMotionQuery" });
1199
+ validate.errors.push({ keywordLocation: "#/properties/animationRespectReducedMotion/type", instanceLocation: "#/animationRespectReducedMotion" });
1200
1200
  errorCount++;
1201
1201
  }
1202
1202
  }
@@ -1600,7 +1600,7 @@ const validate = (function () {
1600
1600
  }
1601
1601
  }
1602
1602
  for (const key4 of Object.keys(data)) {
1603
- if (key4 !== "seed" && key4 !== "size" && key4 !== "idRandomization" && key4 !== "title" && key4 !== "flip" && key4 !== "rotate" && key4 !== "scale" && key4 !== "translateX" && key4 !== "translateY" && key4 !== "borderRadius" && key4 !== "fontFamily" && key4 !== "fontWeight" && key4 !== "tags" && key4 !== "animation" && key4 !== "animationSpeed" && key4 !== "animationDelay" && key4 !== "animationMotionQuery" && !pattern2.test(key4) && !pattern4.test(key4) && !pattern5.test(key4) && !pattern7.test(key4) && !pattern8.test(key4) && !pattern9.test(key4) && !pattern10.test(key4) && !pattern11.test(key4) && !pattern12.test(key4) && !pattern13.test(key4)) {
1603
+ if (key4 !== "seed" && key4 !== "size" && key4 !== "idRandomization" && key4 !== "title" && key4 !== "flip" && key4 !== "rotate" && key4 !== "scale" && key4 !== "translateX" && key4 !== "translateY" && key4 !== "borderRadius" && key4 !== "fontFamily" && key4 !== "fontWeight" && key4 !== "tags" && key4 !== "animation" && key4 !== "animationSpeed" && key4 !== "animationDelay" && key4 !== "animationRespectReducedMotion" && !pattern2.test(key4) && !pattern4.test(key4) && !pattern5.test(key4) && !pattern7.test(key4) && !pattern8.test(key4) && !pattern9.test(key4) && !pattern10.test(key4) && !pattern11.test(key4) && !pattern12.test(key4) && !pattern13.test(key4)) {
1604
1604
  if (validate.errors === null)
1605
1605
  validate.errors = [];
1606
1606
  validate.errors.push({ keywordLocation: "#/additionalProperties", instanceLocation: "#/" + pointerPart(key4) });
package/lib/index.js CHANGED
@@ -2,6 +2,9 @@ import { Style as StyleBase } from './Style.js';
2
2
  import { Avatar as AvatarBase } from './Avatar.js';
3
3
  import { StyleValidator } from './Validator/StyleValidator.js';
4
4
  import { OptionsValidator } from './Validator/OptionsValidator.js';
5
+ import { StyleValidationError } from './Error/StyleValidationError.js';
6
+ import { OptionsValidationError } from './Error/OptionsValidationError.js';
7
+ import { Depth } from './Utils/Depth.js';
5
8
  /**
6
9
  * A style whose definition is checked against the definition schema before
7
10
  * it is parsed. `@dicebear/core/lite` exports the same class without the
@@ -9,6 +12,9 @@ import { OptionsValidator } from './Validator/OptionsValidator.js';
9
12
  */
10
13
  export class Style extends StyleBase {
11
14
  constructor(data) {
15
+ if (Depth.exceeds(data)) {
16
+ throw new StyleValidationError([{ message: Depth.MESSAGE }]);
17
+ }
12
18
  StyleValidator.validate(data);
13
19
  super(data);
14
20
  }
@@ -20,6 +26,9 @@ export class Style extends StyleBase {
20
26
  */
21
27
  export class Avatar extends AvatarBase {
22
28
  constructor(style, optionsInput) {
29
+ if (Depth.exceeds(optionsInput)) {
30
+ throw new OptionsValidationError([{ message: Depth.MESSAGE }]);
31
+ }
23
32
  OptionsValidator.validate(optionsInput ?? {});
24
33
  super(style, optionsInput);
25
34
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicebear/core",
3
- "version": "11.0.0-rc.3",
3
+ "version": "11.0.0-rc.4",
4
4
  "description": "Unique avatars from dozens of styles — deterministic, customizable, vector-based.",
5
5
  "keywords": [
6
6
  "avatar",
@@ -42,7 +42,7 @@
42
42
  "test": "node --test"
43
43
  },
44
44
  "devDependencies": {
45
- "@dicebear/schema": "^2.1.0",
45
+ "@dicebear/schema": "^3.0.0-rc.1",
46
46
  "@exodus/schemasafe": "^1.3.0",
47
47
  "@tsconfig/recommended": "^1.0.13",
48
48
  "del-cli": "^7.0.0",