@dicebear/converter 5.1.3 → 5.1.5

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 CHANGED
@@ -1,51 +1,46 @@
1
1
  import { getMimeType } from './utils/mime-type.js';
2
2
  import { ensureSize } from './utils/svg.js';
3
- const encoder = new TextEncoder();
4
- const decoder = new TextDecoder();
3
+ import { getEncoder } from './utils/text.js';
5
4
  export const toFormat = function (svg, format, exif) {
6
5
  return {
7
- toDataUri: async () => {
8
- return toDataUri(await toArrayBuffer(svg, format, exif), format);
9
- },
10
- toFile: async (name) => {
11
- return toFile(await toArrayBuffer(svg, format, exif), format, name);
12
- },
13
- toArrayBuffer: async () => {
14
- return toArrayBuffer(svg, format, exif);
15
- },
6
+ toDataUri: () => toDataUri(svg, format, exif),
7
+ toFile: (name) => toFile(name, svg, format, exif),
8
+ toArrayBuffer: () => toArrayBuffer(svg, format, exif),
16
9
  };
17
10
  };
18
- async function toDataUri(arrayBuffer, format) {
19
- if (format === 'svg') {
20
- return `data:${getMimeType('svg')};utf8,${encodeURIComponent(decoder.decode(arrayBuffer))}`;
11
+ async function toDataUri(svg, format, exif) {
12
+ if ('svg' === format) {
13
+ return `data:${getMimeType(format)};utf8,${encodeURIComponent(svg)}`;
21
14
  }
22
- else {
23
- let binary = '';
24
- const bytes = new Uint8Array(arrayBuffer);
25
- const len = bytes.byteLength;
26
- for (let i = 0; i < len; i++) {
27
- binary += String.fromCharCode(bytes[i]);
28
- }
29
- return `data:${getMimeType(format)};base64,${window.btoa(binary)}`;
15
+ const canvas = await toCanvas(svg, format, exif);
16
+ return canvas.toDataURL(getMimeType(format));
17
+ }
18
+ async function toArrayBuffer(rawSvg, format, exif) {
19
+ if ('svg' === format) {
20
+ return getEncoder().encode(rawSvg);
30
21
  }
22
+ const canvas = await toCanvas(rawSvg, format, exif);
23
+ return await new Promise((resolve, reject) => {
24
+ canvas.toBlob((blob) => {
25
+ blob
26
+ ? resolve(blob.arrayBuffer())
27
+ : reject(new Error('Could not create blob'));
28
+ }, getMimeType(format));
29
+ });
31
30
  }
32
- async function toFile(arrayBuffer, format, name) {
31
+ async function toFile(name, svg, format, exif) {
33
32
  const link = document.createElement('a');
34
- link.href = await toDataUri(arrayBuffer, format);
33
+ link.href = await toDataUri(svg, format, exif);
35
34
  link.download = name;
36
35
  link.click();
37
36
  link.remove();
38
37
  }
39
- async function toArrayBuffer(rawSvg, format, exif) {
38
+ async function toCanvas(rawSvg, format, exif) {
40
39
  if (exif) {
41
- console.warn('The `exif` option is not supported in the browser version of `@dicebear/converter`.');
42
- console.warn('Please use the node version of `@dicebear/converter` to generate images with exif data.');
43
- }
44
- if (format === 'svg') {
45
- return encoder.encode(rawSvg);
40
+ console.warn('The `exif` option is not supported in the browser version of `@dicebear/converter`. \n' +
41
+ 'Please use the node version of `@dicebear/converter` to generate images with exif data.');
46
42
  }
47
43
  let { svg, size } = ensureSize(rawSvg);
48
- const svgArrayBuffer = encoder.encode(svg);
49
44
  const canvas = document.createElement('canvas');
50
45
  canvas.width = size;
51
46
  canvas.height = size;
@@ -60,15 +55,11 @@ async function toArrayBuffer(rawSvg, format, exif) {
60
55
  var img = document.createElement('img');
61
56
  img.width = size;
62
57
  img.height = size;
63
- img.setAttribute('src', await toDataUri(svgArrayBuffer, 'svg'));
58
+ img.setAttribute('src', await toDataUri(svg, 'svg'));
64
59
  return new Promise((resolve, reject) => {
65
60
  img.onload = () => {
66
61
  context.drawImage(img, 0, 0, size, size);
67
- canvas.toBlob((blob) => {
68
- blob
69
- ? resolve(blob.arrayBuffer())
70
- : reject(new Error('Could not create blob'));
71
- }, `image/${format}`);
62
+ resolve(canvas);
72
63
  };
73
64
  img.onerror = (e) => reject(e);
74
65
  });
package/lib/node/core.js CHANGED
@@ -3,40 +3,35 @@ import { getMimeType } from '../utils/mime-type.js';
3
3
  import { ensureSize } from '../utils/svg.js';
4
4
  import * as tmp from 'tmp-promise';
5
5
  import { ensurePackage } from '../utils/package-helper.js';
6
- const encoder = new TextEncoder();
7
- const decoder = new TextDecoder();
6
+ import { getEncoder } from '../utils/text.js';
8
7
  export const toFormat = function (svg, format, exif) {
9
8
  return {
10
- toDataUri: async () => {
11
- return toDataUri(await toArrayBuffer(svg, format, exif), format);
12
- },
13
- toFile: async (name) => {
14
- return toFile(await toArrayBuffer(svg, format, exif), name);
15
- },
16
- toArrayBuffer: async () => {
17
- return toArrayBuffer(svg, format, exif);
18
- },
9
+ toDataUri: () => toDataUri(svg, format, exif),
10
+ toFile: (name) => toFile(name, svg, format, exif),
11
+ toArrayBuffer: () => toArrayBuffer(svg, format, exif),
19
12
  };
20
13
  };
21
- async function toDataUri(arrayBuffer, format) {
14
+ async function toDataUri(svg, format, exif) {
22
15
  if (format === 'svg') {
23
- return `data:${getMimeType(format)};utf8,${encodeURIComponent(decoder.decode(arrayBuffer))}`;
24
- }
25
- else {
26
- const buffer = Buffer.from(arrayBuffer);
27
- return `data:${getMimeType(format)};base64,${buffer.toString('base64')}`;
16
+ return `data:${getMimeType(format)};utf8,${encodeURIComponent(svg)}`;
28
17
  }
18
+ const buffer = await toBuffer(svg, format, exif);
19
+ return `data:${getMimeType(format)};base64,${buffer.toString('base64')}`;
29
20
  }
30
- async function toFile(arrayBuffer, name) {
31
- return fs.writeFile(name, Buffer.from(arrayBuffer));
21
+ async function toFile(name, svg, format, exif) {
22
+ if (format === 'svg') {
23
+ await fs.writeFile(name, svg);
24
+ return;
25
+ }
26
+ await fs.writeFile(name, await toBuffer(svg, format, exif));
32
27
  }
33
28
  async function toArrayBuffer(rawSvg, format, exif) {
34
29
  if (format === 'svg') {
35
- if (exif) {
36
- console.warn('Exif is ignored when converting to svg.');
37
- }
38
- return encoder.encode(rawSvg);
30
+ return getEncoder().encode(rawSvg);
39
31
  }
32
+ return (await toBuffer(rawSvg, format, exif)).buffer;
33
+ }
34
+ async function toBuffer(rawSvg, format, exif) {
40
35
  await ensurePackage('@resvg/resvg-js', '2.0.0');
41
36
  const { renderAsync } = await import('@resvg/resvg-js');
42
37
  const interRegular = new URL('../../fonts/inter/inter-regular.otf', import.meta.url);
@@ -49,7 +44,7 @@ async function toArrayBuffer(rawSvg, format, exif) {
49
44
  fontFiles: [interRegular.pathname, interBold.pathname],
50
45
  },
51
46
  })).asPng();
52
- if (format === 'jpeg') {
47
+ if ('jpeg' === format) {
53
48
  await ensurePackage('sharp', '0.30.0');
54
49
  const sharp = (await import('sharp')).default;
55
50
  buffer = await sharp(buffer)
@@ -66,5 +61,5 @@ async function toArrayBuffer(rawSvg, format, exif) {
66
61
  buffer = await fs.readFile(path);
67
62
  });
68
63
  }
69
- return buffer.buffer;
64
+ return buffer;
70
65
  }
package/lib/types.d.ts CHANGED
@@ -2,7 +2,7 @@ export type ToFormat = (svg: string, format: Format, exif?: Exif) => Result;
2
2
  export interface Result {
3
3
  toDataUri(): Promise<string>;
4
4
  toFile(name: string): Promise<void>;
5
- toArrayBuffer(): Promise<ArrayBuffer>;
5
+ toArrayBuffer(): Promise<ArrayBufferLike>;
6
6
  }
7
7
  export interface Exif {
8
8
  [key: string]: string;
@@ -1,9 +1,11 @@
1
1
  export function getMimeType(format) {
2
2
  switch (format) {
3
+ case 'svg':
4
+ return 'image/svg+xml';
3
5
  case 'png':
4
6
  case 'jpeg':
5
7
  return `image/${format}`;
6
8
  default:
7
- return 'image/svg+xml';
9
+ throw new Error(`Unsupported format: ${format}`);
8
10
  }
9
11
  }
@@ -0,0 +1 @@
1
+ export declare function getEncoder(): TextEncoder;
@@ -0,0 +1,7 @@
1
+ let encoder;
2
+ export function getEncoder() {
3
+ if (!encoder) {
4
+ encoder = new TextEncoder();
5
+ }
6
+ return encoder;
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dicebear/converter",
3
- "version": "5.1.3",
3
+ "version": "5.1.5",
4
4
  "description": "SVG Converter for DiceBear",
5
5
  "keywords": [
6
6
  "dicebear"
@@ -71,5 +71,5 @@
71
71
  "publishConfig": {
72
72
  "access": "public"
73
73
  },
74
- "gitHead": "3ea70708b220547b7e4f3db911abf9d30ef2d9ba"
74
+ "gitHead": "cd0ba272057aeb321db66e63a483d55557082344"
75
75
  }