Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed outdated encoding/decoding compatibility functions #7108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 2 additions & 47 deletions packages/tutanota-utils/lib/Encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -188,39 +168,14 @@ 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.
*
* @param string The string to convert.
* @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)
}

/**
Expand All @@ -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 {
Expand Down
17 changes: 0 additions & 17 deletions packages/tutanota-utils/test/EncodingTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import o from "@tutao/otest"
import {
_replaceLoneSurrogates,
_stringToUtf8Uint8ArrayLegacy,
_utf8Uint8ArrayToStringLegacy,
base64ExtToBase64,
base64ToBase64Ext,
base64ToBase64Url,
Expand Down Expand Up @@ -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")
})
Expand Down