@@ -85,56 +85,77 @@ const byteArrayToString = function(bytes: number[]): string {
85
85
return out . join ( '' ) ;
86
86
} ;
87
87
88
+ interface Base64 {
89
+ byteToCharMap_ : { [ key : number ] : string } | null ;
90
+ charToByteMap_ : { [ key : string ] : number } | null ;
91
+ byteToCharMapWebSafe_ : { [ key : number ] : string } | null ;
92
+ charToByteMapWebSafe_ : { [ key : string ] : number } | null ;
93
+ ENCODED_VALS_BASE : string ;
94
+ readonly ENCODED_VALS : string ;
95
+ readonly ENCODED_VALS_WEBSAFE : string ;
96
+ HAS_NATIVE_SUPPORT : boolean ;
97
+ encodeByteArray ( input : number [ ] | Uint8Array , webSafe ?: boolean ) : string ;
98
+ encodeString ( input : string , webSafe ?: boolean ) : string ;
99
+ decodeString ( input : string , webSafe : boolean ) : string ;
100
+ decodeStringToByteArray ( input : string , webSafe : boolean ) : number [ ] ;
101
+ init_ ( ) : void ;
102
+ }
103
+
104
+ // We define it as an object literal instead of a class because a class compiled down to es5 can't
105
+ // be treeshaked. https://github.com/rollup/rollup/issues/1691
88
106
// Static lookup maps, lazily populated by init_()
89
- class Base64 {
107
+ export const base64 : Base64 = {
90
108
/**
91
109
* Maps bytes to characters.
92
110
*/
93
- byteToCharMap_ : { [ key : number ] : string } | null = null ;
111
+ byteToCharMap_ : null ,
94
112
95
113
/**
96
114
* Maps characters to bytes.
97
115
*/
98
- charToByteMap_ : { [ key : string ] : number } | null = null ;
116
+ charToByteMap_ : null ,
99
117
100
118
/**
101
119
* Maps bytes to websafe characters.
120
+ * @private
102
121
*/
103
- byteToCharMapWebSafe_ : { [ key : number ] : string } | null = null ;
122
+ byteToCharMapWebSafe_ : null ,
104
123
105
124
/**
106
125
* Maps websafe characters to bytes.
126
+ * @private
107
127
*/
108
- charToByteMapWebSafe_ : { [ key : string ] : number } | null = null ;
128
+ charToByteMapWebSafe_ : null ,
109
129
110
130
/**
111
- * Our default alphabet shared between
131
+ * Our default alphabet, shared between
112
132
* ENCODED_VALS and ENCODED_VALS_WEBSAFE
113
133
*/
114
- ENCODED_VALS_BASE : string =
115
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789' ;
134
+ ENCODED_VALS_BASE :
135
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789' ,
116
136
117
137
/**
118
138
* Our default alphabet. Value 64 (=) is special; it means "nothing."
119
139
*/
120
- get ENCODED_VALS ( ) : string {
140
+ get ENCODED_VALS ( ) {
121
141
return this . ENCODED_VALS_BASE + '+/=' ;
122
- }
142
+ } ,
123
143
124
144
/**
125
145
* Our websafe alphabet.
126
146
*/
127
- get ENCODED_VALS_WEBSAFE ( ) : string {
147
+ get ENCODED_VALS_WEBSAFE ( ) {
128
148
return this . ENCODED_VALS_BASE + '-_.' ;
129
- }
149
+ } ,
130
150
131
151
/**
132
152
* Whether this browser supports the atob and btoa functions. This extension
133
153
* started at Mozilla but is now implemented by many browsers. We use the
134
154
* ASSUME_* variables to avoid pulling in the full useragent detection library
135
155
* but still allowing the standard per-browser compilations.
156
+ *
136
157
*/
137
- HAS_NATIVE_SUPPORT : boolean = typeof atob === 'function' ;
158
+ HAS_NATIVE_SUPPORT : typeof atob === 'function' ,
138
159
139
160
/**
140
161
* Base64-encode an array of bytes.
@@ -187,7 +208,7 @@ class Base64 {
187
208
}
188
209
189
210
return output . join ( '' ) ;
190
- }
211
+ } ,
191
212
192
213
/**
193
214
* Base64-encode a string.
@@ -204,7 +225,7 @@ class Base64 {
204
225
return btoa ( input ) ;
205
226
}
206
227
return this . encodeByteArray ( stringToByteArray ( input ) , webSafe ) ;
207
- }
228
+ } ,
208
229
209
230
/**
210
231
* Base64-decode a string.
@@ -221,7 +242,7 @@ class Base64 {
221
242
return atob ( input ) ;
222
243
}
223
244
return byteArrayToString ( this . decodeStringToByteArray ( input , webSafe ) ) ;
224
- }
245
+ } ,
225
246
226
247
/**
227
248
* Base64-decode a string.
@@ -281,14 +302,14 @@ class Base64 {
281
302
}
282
303
283
304
return output ;
284
- }
305
+ } ,
285
306
286
307
/**
287
308
* Lazy static initialization function. Called before
288
309
* accessing any of the static map variables.
289
310
* @private
290
311
*/
291
- init_ ( ) : void {
312
+ init_ ( ) {
292
313
if ( ! this . byteToCharMap_ ) {
293
314
this . byteToCharMap_ = { } ;
294
315
this . charToByteMap_ = { } ;
@@ -310,7 +331,7 @@ class Base64 {
310
331
}
311
332
}
312
333
}
313
- }
334
+ } ;
314
335
315
336
/**
316
337
* URL-safe base64 encoding
@@ -337,5 +358,3 @@ export const base64Decode = function(str: string): string | null {
337
358
}
338
359
return null ;
339
360
} ;
340
-
341
- export const base64 = new Base64 ( ) ;
0 commit comments