@@ -114,12 +114,6 @@ export function isMatchingPattern(value: string, pattern: RegExp | string): bool
114
114
export function unicodeToBase64 ( plaintext : string ) : string {
115
115
const global = getGlobalObject ( ) ;
116
116
117
- // Cast to a string just in case we're given something else
118
- const stringifiedInput = String ( plaintext ) ;
119
- const errMsg = `Unable to convert to base64: ${
120
- stringifiedInput . length > 256 ? `${ stringifiedInput . slice ( 0 , 256 ) } ...` : stringifiedInput
121
- } `;
122
-
123
117
// To account for the fact that different platforms use different character encodings natively, our `tracestate`
124
118
// spec calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder.
125
119
try {
@@ -142,12 +136,17 @@ export function unicodeToBase64(plaintext: string): string {
142
136
// unlike the browser, Node can go straight from bytes to base64
143
137
return bytes . toString ( 'base64' ) ;
144
138
}
139
+
140
+ // we shouldn't ever get here, because one of `btoa` and `Buffer` should exist, but just in case...
141
+ throw new SentryError ( 'Neither `window.btoa` nor `global.Buffer` is defined.' ) ;
145
142
} catch ( err ) {
143
+ // Cast to a string just in case we're given something else
144
+ const stringifiedInput = String ( plaintext ) ;
145
+ const errMsg = `Unable to convert to base64: ${
146
+ stringifiedInput . length > 256 ? `${ stringifiedInput . slice ( 0 , 256 ) } ...` : stringifiedInput
147
+ } `;
146
148
throw new SentryError ( `${ errMsg } \nGot error: ${ err } ` ) ;
147
149
}
148
-
149
- // we shouldn't ever get here, because one of `btoa` and `Buffer` should exist, but just in case...
150
- throw new SentryError ( errMsg ) ;
151
150
}
152
151
153
152
/**
@@ -160,12 +159,6 @@ export function unicodeToBase64(plaintext: string): string {
160
159
export function base64ToUnicode ( base64String : string ) : string {
161
160
const globalObject = getGlobalObject ( ) ;
162
161
163
- // we cast to a string just in case we're given something else
164
- const stringifiedInput = String ( base64String ) ;
165
- const errMsg = `Unable to convert from base64: ${
166
- stringifiedInput . length > 256 ? `${ stringifiedInput . slice ( 0 , 256 ) } ...` : stringifiedInput
167
- } `;
168
-
169
162
// To account for the fact that different platforms use different character encodings natively, our `tracestate` spec
170
163
// calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder. So to reverse
171
164
// the process, decode from base64 to bytes, then feed those bytes to a UTF-8 decoder.
@@ -188,10 +181,15 @@ export function base64ToUnicode(base64String: string): string {
188
181
// decode using UTF-8
189
182
return bytes . toString ( 'utf-8' ) ;
190
183
}
184
+
185
+ // we shouldn't ever get here, because one of `atob` and `Buffer` should exist, but just in case...
186
+ throw new SentryError ( 'Neither `window.atob` nor `global.Buffer` is defined.' ) ;
191
187
} catch ( err ) {
188
+ // we cast to a string just in case we're given something else
189
+ const stringifiedInput = String ( base64String ) ;
190
+ const errMsg = `Unable to convert from base64: ${
191
+ stringifiedInput . length > 256 ? `${ stringifiedInput . slice ( 0 , 256 ) } ...` : stringifiedInput
192
+ } `;
192
193
throw new SentryError ( `${ errMsg } \nGot error: ${ err } ` ) ;
193
194
}
194
-
195
- // we shouldn't ever get here, because one of `atob` and `Buffer` should exist, but just in case...
196
- throw new SentryError ( errMsg ) ;
197
195
}
0 commit comments