diff --git a/packages/tutanota-utils/lib/Encoding.ts b/packages/tutanota-utils/lib/Encoding.ts index b63c0d714989..1375cb5dee9b 100644 --- a/packages/tutanota-utils/lib/Encoding.ts +++ b/packages/tutanota-utils/lib/Encoding.ts @@ -129,26 +129,6 @@ export function base64UrlToBase64(base64url: Base64Url): Base64 { throw new Error("Illegal base64 string.") } -// just for edge, as it does not support TextEncoder yet -export function _stringToUtf8Uint8ArrayLegacy(string: string): Uint8Array { - let fixedString - - try { - fixedString = encodeURIComponent(string) - } catch (e) { - fixedString = encodeURIComponent(_replaceLoneSurrogates(string)) // we filter lone surrogates as trigger URIErrors, otherwise (see https://github.com/tutao/tutanota/issues/618) - } - - let utf8 = unescape(fixedString) - let uint8Array = new Uint8Array(utf8.length) - - for (let i = 0; i < utf8.length; i++) { - uint8Array[i] = utf8.charCodeAt(i) - } - - return uint8Array -} - const REPLACEMENT_CHAR = "\uFFFD" export function _replaceLoneSurrogates(s: string | null | undefined): string { @@ -188,19 +168,6 @@ export function _replaceLoneSurrogates(s: string | null | undefined): string { return result.join("") } -const encoder = - typeof TextEncoder == "function" - ? new TextEncoder() - : { - encode: _stringToUtf8Uint8ArrayLegacy, - } -const decoder = - typeof TextDecoder == "function" - ? new TextDecoder() - : { - decode: _utf8Uint8ArrayToStringLegacy, - } - /** * Converts a string to a Uint8Array containing a UTF-8 string data. * @@ -208,19 +175,7 @@ const decoder = * @return The array. */ export function stringToUtf8Uint8Array(string: string): Uint8Array { - return encoder.encode(string) -} - -// just for edge, as it does not support TextDecoder yet -export function _utf8Uint8ArrayToStringLegacy(uint8Array: Uint8Array): string { - let stringArray: string[] = [] - stringArray.length = uint8Array.length - - for (let i = 0; i < uint8Array.length; i++) { - stringArray[i] = String.fromCharCode(uint8Array[i]) - } - - return decodeURIComponent(escape(stringArray.join(""))) + return new TextEncoder().encode(string) } /** @@ -230,7 +185,7 @@ export function _utf8Uint8ArrayToStringLegacy(uint8Array: Uint8Array): string { * @return The string. */ export function utf8Uint8ArrayToString(uint8Array: Uint8Array): string { - return decoder.decode(uint8Array) + return new TextDecoder().decode(uint8Array) } export function hexToUint8Array(hex: Hex): Uint8Array { diff --git a/packages/tutanota-utils/test/EncodingTest.ts b/packages/tutanota-utils/test/EncodingTest.ts index bb8432bbd29e..9a8f1f007014 100644 --- a/packages/tutanota-utils/test/EncodingTest.ts +++ b/packages/tutanota-utils/test/EncodingTest.ts @@ -1,8 +1,6 @@ import o from "@tutao/otest" import { _replaceLoneSurrogates, - _stringToUtf8Uint8ArrayLegacy, - _utf8Uint8ArrayToStringLegacy, base64ExtToBase64, base64ToBase64Ext, base64ToBase64Url, @@ -44,21 +42,6 @@ o.spec("Encoding", function () { o(_replaceLoneSurrogates("a\uDFFFb")).equals("a\uFFFDb") // lone low surrogate }) - o("StringToUint8ArrayAndBackLegacy", () => stringToUint8ArrayAndBack(_stringToUtf8Uint8ArrayLegacy, _utf8Uint8ArrayToStringLegacy)) - - function stringToUint8ArrayAndBack(encoder, decoder) { - o(decoder(encoder("halloTest € à 草"))).equals("halloTest € à 草") - o(decoder(encoder(""))).equals("") - o(decoder(encoder("1"))).equals("1") - o(decoder(encoder("7=/=£±™⅛°™⅜£¤°±⅛™¤°°®↑°°ÆЪ±↑£°±↑Ω£®°±đ]łæ}đ2w034r70uf"))).equals("7=/=£±™⅛°™⅜£¤°±⅛™¤°°®↑°°ÆЪ±↑£°±↑Ω£®°±đ]łæ}đ2w034r70uf") - o(Array.from(encoder("€"))).deepEquals([226, 130, 172]) - o(decoder(encoder("b\uDFFFa"))).equals("b\uFFFDa") // lone low surrogate is replaced with REPLACEMENT CHARACTER - - o(decoder(encoder("b\uD800a"))).equals("b\uFFFDa") // lone high surrogate is replace with REPLACEMENT CHARACTER - - o(decoder(encoder("b\uD800\uDFFFa"))).equals("b\uD800\uDFFFa") // high and low surrogate - } - o("HexToArrayBufferAndBack", function () { o(uint8ArrayToHex(hexToUint8Array("ba9012cb349de910924ed81239d18423"))).equals("ba9012cb349de910924ed81239d18423") })