File tree Expand file tree Collapse file tree 1 file changed +12
-13
lines changed Expand file tree Collapse file tree 1 file changed +12
-13
lines changed Original file line number Diff line number Diff line change 1
1
const runningOnBrowser = typeof window !== 'undefined'
2
2
3
- // Pad out with standard base64 required padding characters if missing
4
- const base64AddPadding = base64String => {
5
- const missingPadding = '=' . repeat ( ( 4 - base64String . length % 4 ) % 4 )
6
- return base64String + missingPadding
7
- }
8
-
9
- // Replace non-url compatible chars with base64 standard chars
10
- const base64UrlDecode = base64UrlString => {
11
- return base64UrlString
3
+ const base64urlToBase64 = base64UrlString => {
4
+ // Replace non-url compatible chars with base64 standard chars
5
+ const base64 = base64UrlString
12
6
. replace ( / - / g, '+' )
13
7
. replace ( / _ / g, '/' )
8
+
9
+ // Pad out with standard base64 required padding characters if missing
10
+ const missingPadding = '=' . repeat ( ( 4 - base64 . length % 4 ) % 4 )
11
+
12
+ return base64 + missingPadding
14
13
}
15
14
16
- const atob = data => {
15
+ const base64Decode = data => {
17
16
if ( runningOnBrowser ) {
18
- return window . atob ( data )
17
+ return atob ( data )
19
18
}
20
19
// atob polyfill for Node
21
20
return Buffer . from ( data , 'base64' ) . toString ( 'binary' )
22
21
}
23
22
24
23
const base64ToUint8Array = base64String => {
25
24
// base64 sanitizing
26
- const base64 = base64UrlDecode ( base64AddPadding ( base64String ) )
25
+ const base64 = base64urlToBase64 ( base64String )
27
26
28
27
// base64 decoding
29
- const rawData = atob ( base64 )
28
+ const rawData = base64Decode ( base64 )
30
29
31
30
// Converting raw data to Uint8Array
32
31
return Uint8Array . from ( rawData , char => char . charCodeAt ( 0 ) )
You can’t perform that action at this time.
0 commit comments