@dicebear/core 8.0.3 → 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/lib/core.js +28 -5
- package/lib/types.d.ts +5 -1
- package/lib/utils/prng.js +3 -2
- package/package.json +3 -3
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 {
|
|
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
|
-
...
|
|
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
|
|
89
|
+
return wrapWithToFile(toPng(svg, { includeExif }));
|
|
67
90
|
},
|
|
68
91
|
jpeg: ({ includeExif = false } = {}) => {
|
|
69
|
-
return
|
|
92
|
+
return wrapWithToFile(toJpeg(svg, { includeExif }));
|
|
70
93
|
},
|
|
71
94
|
};
|
|
72
95
|
}
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dicebear/core",
|
|
3
|
-
"version": "8.0
|
|
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": "
|
|
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": "
|
|
50
|
+
"gitHead": "f60d993c2998e9767108372035855cc68aa23b15"
|
|
51
51
|
}
|