@@ -104,11 +104,11 @@ export function isMatchingPattern(value: string, pattern: RegExp | string): bool
104
104
return false ;
105
105
}
106
106
107
- type GlobalWithBase64Helpers = {
108
- // browser
107
+ export type GlobalBase64Helpers = {
108
+ // browser and Node 16+
109
109
atob ?: ( base64String : string ) => string ;
110
110
btoa ?: ( utf8String : string ) => string ;
111
- // Node
111
+ // all versions of Node
112
112
Buffer ?: { from : ( input : string , encoding : string ) => { toString : ( encoding : string ) => string } } ;
113
113
} ;
114
114
@@ -120,7 +120,7 @@ type GlobalWithBase64Helpers = {
120
120
* @returns A base64-encoded version of the string
121
121
*/
122
122
export function unicodeToBase64 ( plaintext : string ) : string {
123
- const globalObject = getGlobalObject < GlobalWithBase64Helpers > ( ) ;
123
+ const globalObject = getGlobalObject < GlobalBase64Helpers > ( ) ;
124
124
125
125
// To account for the fact that different platforms use different character encodings natively, our `tracestate`
126
126
// spec calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder.
@@ -129,7 +129,7 @@ export function unicodeToBase64(plaintext: string): string {
129
129
throw new Error ( `Input must be a string. Received input of type '${ typeof plaintext } '.` ) ;
130
130
}
131
131
132
- // browser
132
+ // browser and Node 16+
133
133
if ( 'btoa' in globalObject ) {
134
134
// encode using UTF-8
135
135
const bytes = new TextEncoder ( ) . encode ( plaintext ) ;
@@ -142,7 +142,7 @@ export function unicodeToBase64(plaintext: string): string {
142
142
return globalObject . btoa ! ( bytesAsString ) ;
143
143
}
144
144
145
- // Node
145
+ // fallback for Node <= 14
146
146
if ( 'Buffer' in globalObject ) {
147
147
// encode using UTF-8
148
148
// TODO: if TS ever learns about "in", we can get rid of the non-null assertion
@@ -173,7 +173,7 @@ export function unicodeToBase64(plaintext: string): string {
173
173
* @returns A Unicode string
174
174
*/
175
175
export function base64ToUnicode ( base64String : string ) : string {
176
- const globalObject = getGlobalObject < GlobalWithBase64Helpers > ( ) ;
176
+ const globalObject = getGlobalObject < GlobalBase64Helpers > ( ) ;
177
177
178
178
// To account for the fact that different platforms use different character encodings natively, our `tracestate` spec
179
179
// calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder. So to reverse
@@ -183,7 +183,7 @@ export function base64ToUnicode(base64String: string): string {
183
183
throw new Error ( `Input must be a string. Received input of type '${ typeof base64String } '.` ) ;
184
184
}
185
185
186
- // browser
186
+ // browser and Node 16+
187
187
if ( 'atob' in globalObject ) {
188
188
// `atob` returns a string rather than bytes, so we first need to encode using the native encoding (UTF-16)
189
189
// TODO: if TS ever learns about "in", we can get rid of the non-null assertion
@@ -195,7 +195,7 @@ export function base64ToUnicode(base64String: string): string {
195
195
return new TextDecoder ( ) . decode ( Uint8Array . from ( bytes ) ) ;
196
196
}
197
197
198
- // Node
198
+ // fallback for Node <= 14
199
199
if ( 'Buffer' in globalObject ) {
200
200
// unlike the browser, Node can go straight from base64 to bytes
201
201
// TODO: if TS ever learns about "in", we can get rid of the non-null assertion
0 commit comments