Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(image): fix dataUri with type svg-base64 in browsers #3144

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eslint.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const config: ReturnType<typeof tseslint.config> = tseslint.config(
eqeqeq: ['error', 'always', { null: 'ignore' }],
'logical-assignment-operators': 'error',
'no-else-return': 'error',
'no-restricted-globals': ['error', 'Intl'],
'prefer-exponentiation-operator': 'error',
'prefer-template': 'error',
},
Expand Down Expand Up @@ -205,6 +204,7 @@ const config: ReturnType<typeof tseslint.config> = tseslint.config(
files: ['src/**/*.ts'],
rules: {
'jsdoc/require-jsdoc': 'error',
'no-restricted-globals': ['error', 'Intl', 'Buffer'],
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
},
},
{
Expand Down
21 changes: 21 additions & 0 deletions src/internal/base64.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* This works the same as `Buffer.from(input).toString('base64')`
* to work on both Node.js and browser environment.
*
* @internal
*
* @param input The string to encode to Base64.
*
* @returns Base64 encoded string.
*
* @see https://datatracker.ietf.org/doc/html/rfc4648
*
* @example const encodedHeader = toBase64(JSON.stringify(header));
*/
export function toBase64(input: string): string {
const utf8Bytes = new TextEncoder().encode(input);
const binaryString = Array.from(utf8Bytes, (byte) =>
String.fromCodePoint(byte)
).join('');
return btoa(binaryString);
}
5 changes: 2 additions & 3 deletions src/modules/image/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { toBase64 } from '../../internal/base64';
import { deprecated } from '../../internal/deprecated';
import { ModuleBase } from '../../internal/module-base';

Expand Down Expand Up @@ -388,8 +389,6 @@ export class ImageModule extends ModuleBase {

return type === 'svg-uri'
? `data:image/svg+xml;charset=UTF-8,${encodeURIComponent(svgString)}`
: `data:image/svg+xml;base64,${Buffer.from(svgString).toString(
'base64'
)}`;
: `data:image/svg+xml;base64,${toBase64(svgString)}`;
}
}
17 changes: 17 additions & 0 deletions test/internal/base64.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest';
import { faker } from '../../src';
import { toBase64 } from '../../src/internal/base64';

describe('toBase64', () => {
it.each(
faker.helpers.multiple(
() => faker.string.alphanumeric({ length: { min: 0, max: 100 } }),
{ count: 5 }
)
)(
"should behave the same as `Buffer.from(value).toString('base64')`",
(value: string) => {
expect(toBase64(value)).toBe(Buffer.from(value).toString('base64'));
}
);
});