aboutsummaryrefslogtreecommitdiff
path: root/src/internal
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-09-26 18:00:14 +0200
committerGitHub <[email protected]>2024-09-26 16:00:14 +0000
commit78b2a3a8b85679eeff27beccebcc404b0a5ff3cf (patch)
tree50012521180747094a55a862dfe7a0ba164feef4 /src/internal
parentd6bceb662de6559446fedb11de53a632a525aba0 (diff)
downloadfaker-78b2a3a8b85679eeff27beccebcc404b0a5ff3cf.tar.xz
faker-78b2a3a8b85679eeff27beccebcc404b0a5ff3cf.zip
fix(image): fix dataUri with type svg-base64 in browsers (#3144)
Diffstat (limited to 'src/internal')
-rw-r--r--src/internal/base64.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/internal/base64.ts b/src/internal/base64.ts
new file mode 100644
index 00000000..b346538a
--- /dev/null
+++ b/src/internal/base64.ts
@@ -0,0 +1,24 @@
+/**
+ * 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 const toBase64: (input: string) => string =
+ typeof Buffer === 'undefined'
+ ? (input) => {
+ const utf8Bytes = new TextEncoder().encode(input);
+ const binaryString = Array.from(utf8Bytes, (byte) =>
+ String.fromCodePoint(byte)
+ ).join('');
+ return btoa(binaryString);
+ }
+ : (input) => Buffer.from(input).toString('base64');