Skip to content

Commit e089c2e

Browse files
committed
Separate base64 encoding function
1 parent d6a73ed commit e089c2e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ const base64Decode = data => {
2020
return Buffer.from(data, 'base64').toString('binary')
2121
}
2222

23+
const base64Encode = data => {
24+
if (runningOnBrowser) {
25+
return btoa(data)
26+
}
27+
// btoa polyfill for Node
28+
return Buffer.from(data).toString('base64')
29+
}
30+
2331
const base64ToUint8Array = base64String => {
2432
// base64 sanitizing
2533
const base64 = base64urlToBase64(base64String)
@@ -32,10 +40,7 @@ const base64ToUint8Array = base64String => {
3240
}
3341

3442
const typedArrayToBase64 = typedArray => {
35-
if (runningOnBrowser) {
36-
return btoa(String.fromCharCode(...typedArray))
37-
}
38-
return Buffer.from(typedArray).toString('base64')
43+
return base64Encode(String.fromCharCode(...typedArray))
3944
}
4045

4146
const uint8ArrayToBase64 = typedArrayToBase64

0 commit comments

Comments
 (0)