@dicebear/core 8.0.2 → 8.1.0

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/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2023 Florian Körner
3
+ Copyright (c) 2024 Florian Körner
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/lib/core.js CHANGED
@@ -2,8 +2,29 @@ import * as svgUtils from './utils/svg.js';
2
2
  import { merge as mergeOptions } from './utils/options.js';
3
3
  import { create as createPrng } from './utils/prng.js';
4
4
  import * as license from './utils/license.js';
5
- import { toFormat } from '@dicebear/converter';
5
+ import { toPng, toJpeg } from '@dicebear/converter';
6
6
  import { getBackgroundColors } from './utils/color.js';
7
+ const encoder = new TextEncoder();
8
+ function wrapWithToFile(result) {
9
+ return {
10
+ toDataUri: () => result.toDataUri(),
11
+ toArrayBuffer: () => result.toArrayBuffer(),
12
+ toFile: async (name) => {
13
+ if (typeof document !== 'undefined') {
14
+ const link = document.createElement('a');
15
+ link.href = await result.toDataUri();
16
+ link.download = name;
17
+ link.click();
18
+ link.remove();
19
+ }
20
+ else {
21
+ const { writeFile } = await import('node:fs/promises');
22
+ const buffer = await result.toArrayBuffer();
23
+ await writeFile(name, Buffer.from(buffer));
24
+ }
25
+ },
26
+ };
27
+ }
7
28
  export function createAvatar(style, options = {}) {
8
29
  var _a, _b, _c, _d, _e;
9
30
  options = mergeOptions(style, options);
@@ -41,7 +62,6 @@ export function createAvatar(style, options = {}) {
41
62
  }
42
63
  const attributes = svgUtils.createAttrString(result);
43
64
  const metadata = license.xml(style);
44
- const exif = license.exif(style);
45
65
  const svg = `<svg ${attributes}>${metadata}${result.body}</svg>`;
46
66
  return {
47
67
  toString: () => svg,
@@ -61,12 +81,15 @@ export function createAvatar(style, options = {}) {
61
81
  toDataUriSync: () => {
62
82
  return `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`;
63
83
  },
64
- ...toFormat(svg, 'svg'),
84
+ ...wrapWithToFile({
85
+ toDataUri: async () => `data:image/svg+xml;utf8,${encodeURIComponent(svg)}`,
86
+ toArrayBuffer: async () => encoder.encode(svg).buffer,
87
+ }),
65
88
  png: ({ includeExif = false } = {}) => {
66
- return toFormat(svg, 'png', includeExif ? exif : undefined);
89
+ return wrapWithToFile(toPng(svg, { includeExif }));
67
90
  },
68
91
  jpeg: ({ includeExif = false } = {}) => {
69
- return toFormat(svg, 'jpeg', includeExif ? exif : undefined);
92
+ return wrapWithToFile(toJpeg(svg, { includeExif }));
70
93
  },
71
94
  };
72
95
  }
package/lib/index.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  * DiceBear (@dicebear/core)
3
3
  *
4
4
  * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
- * Copyright (c) 2023 Florian Körner
5
+ * Copyright (c) 2024 Florian Körner
6
6
  */
7
7
  import * as license from './utils/license.js';
8
8
  import * as escape from './utils/escape.js';
package/lib/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * DiceBear (@dicebear/core)
3
3
  *
4
4
  * Code licensed under MIT (https://github.com/dicebear/dicebear/blob/main/LICENSE)
5
- * Copyright (c) 2023 Florian Körner
5
+ * Copyright (c) 2024 Florian Körner
6
6
  */
7
7
  import * as license from './utils/license.js';
8
8
  import * as escape from './utils/escape.js';
package/lib/types.d.ts CHANGED
@@ -1,8 +1,12 @@
1
1
  import type { JSONSchema7 } from 'json-schema';
2
- import type { Result as ConverterResult } from '@dicebear/converter';
3
2
  export interface ResultConvertOptions {
4
3
  includeExif?: boolean;
5
4
  }
5
+ export interface ConverterResult {
6
+ toDataUri(): Promise<string>;
7
+ toFile(name: string): Promise<void>;
8
+ toArrayBuffer(): Promise<ArrayBufferLike>;
9
+ }
6
10
  export interface Result extends ConverterResult {
7
11
  png(options?: ResultConvertOptions): ConverterResult;
8
12
  jpeg(options?: ResultConvertOptions): ConverterResult;
package/lib/utils/prng.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const MIN = -2147483648;
2
2
  const MAX = 2147483647;
3
+ const MAX_SEED_LENGTH = 1024;
3
4
  function xorshift(value) {
4
5
  value ^= value << 13;
5
6
  value ^= value >> 17;
@@ -15,8 +16,8 @@ function hashSeed(seed) {
15
16
  return hash;
16
17
  }
17
18
  export function create(seed = '') {
18
- // Ensure that seed is a string
19
- seed = seed.toString();
19
+ // Ensure that seed is a string and limit length to prevent CPU exhaustion
20
+ seed = seed.toString().slice(0, MAX_SEED_LENGTH);
20
21
  let value = hashSeed(seed) || 1;
21
22
  const next = () => (value = xorshift(value));
22
23
  const integer = (min, max) => {
package/lib/utils/svg.js CHANGED
@@ -15,7 +15,7 @@ export function getViewBox(result) {
15
15
  }
16
16
  export function addBackground(result, primaryColor, secondaryColor, type, rotation) {
17
17
  let { width, height, x, y } = getViewBox(result);
18
- const solidBackground = `<rect fill="${primaryColor}" width="${width}" height="${height}" x="${x}" y="${y}" />`;
18
+ const solidBackground = `<rect fill="${escape.xml(primaryColor)}" width="${width}" height="${height}" x="${x}" y="${y}" />`;
19
19
  switch (type) {
20
20
  case 'solid':
21
21
  return solidBackground + result.body;
@@ -23,8 +23,8 @@ export function addBackground(result, primaryColor, secondaryColor, type, rotati
23
23
  return (`<rect fill="url(#backgroundLinear)" width="${width}" height="${height}" x="${x}" y="${y}" />` +
24
24
  `<defs>` +
25
25
  `<linearGradient id="backgroundLinear" gradientTransform="rotate(${rotation} 0.5 0.5)">` +
26
- `<stop stop-color="${primaryColor}"/>` +
27
- `<stop offset="1" stop-color="${secondaryColor}"/>` +
26
+ `<stop stop-color="${escape.xml(primaryColor)}"/>` +
27
+ `<stop offset="1" stop-color="${escape.xml(secondaryColor)}"/>` +
28
28
  `</linearGradient>` +
29
29
  `</defs>` +
30
30
  result.body);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicebear/core",
3
- "version": "8.0.2",
3
+ "version": "8.1.0",
4
4
  "description": "An avatar library for designers and developers.",
5
5
  "keywords": [
6
6
  "avatar",
@@ -32,7 +32,7 @@
32
32
  "test": "uvu tests"
33
33
  },
34
34
  "dependencies": {
35
- "@dicebear/converter": "8.0.2",
35
+ "@dicebear/converter": "^9.4.2",
36
36
  "@types/json-schema": "^7.0.11"
37
37
  },
38
38
  "devDependencies": {
@@ -47,5 +47,5 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  },
50
- "gitHead": "a81631f25e0723edc73cf40c1423f7b5242fafe5"
50
+ "gitHead": "f60d993c2998e9767108372035855cc68aa23b15"
51
51
  }