Skip to content

Commit 14636e8

Browse files
committed
add typechecking
1 parent 323a275 commit 14636e8

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

packages/utils/src/string.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export function unicodeToBase64(plaintext: string): string {
125125
// To account for the fact that different platforms use different character encodings natively, our `tracestate`
126126
// spec calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder.
127127
try {
128+
if (typeof plaintext !== 'string') {
129+
throw new Error(`Input must be a string. Received input of type ${typeof plaintext}.`);
130+
}
131+
128132
// browser
129133
if ('btoa' in globalObject) {
130134
// encode using UTF-8
@@ -175,6 +179,10 @@ export function base64ToUnicode(base64String: string): string {
175179
// calls for all jsonified data to be encoded in UTF-8 bytes before being passed to the base64 encoder. So to reverse
176180
// the process, decode from base64 to bytes, then feed those bytes to a UTF-8 decoder.
177181
try {
182+
if (typeof base64String !== 'string') {
183+
throw new Error(`Input must be a string. Received input of type ${typeof base64String}.`);
184+
}
185+
178186
// browser
179187
if ('atob' in globalObject) {
180188
// `atob` returns a string rather than bytes, so we first need to encode using the native encoding (UTF-16)

0 commit comments

Comments
 (0)