Skip to content

Usage of deprecated functions in Url64 util methods #5183

@shamblonaut

Description

@shamblonaut

Description

In the Url64 class, the encode and decode methods use the deprecated unescape and escape functions. This was done to support encoding unicode strings (See #2680).

I believe those functions were used according to the recommendations in the MDN docs at the time. But the docs have since been updated with a new approach which recommends using TextEncoder.encode and TextDecoder.decode methods.

Affects users

None

Affects developers

Low

Although the escape and unescape functions are deprecated, as mentioned in #2680, it is unlikely that they are removed because of backward-compatibility of browsers.

Suggested change

Using the new approach suggested in MDN, we can replace the usage of the deprecated functions by encoding the string with TextEncoder, converting the bytes into a binary string, and then passing it to btoa(). The process can be reversed for decoding (get the binary string from atob(), convert to bytes, and then decode using TextDecoder).

Code from MDN docs:

function base64ToBytes(base64) {
  const binString = atob(base64);
  return Uint8Array.from(binString, (m) => m.codePointAt(0));
}

function bytesToBase64(bytes) {
  const binString = Array.from(bytes, (byte) =>
    String.fromCodePoint(byte),
  ).join("");
  return btoa(binString);
}

// Usage
bytesToBase64(new TextEncoder().encode("a Ā 𐀀 文 🦄")); // "YSDEgCDwkICAIOaWhyDwn6aE"
new TextDecoder().decode(base64ToBytes("YSDEgCDwkICAIOaWhyDwn6aE")); // "a Ā 𐀀 文 🦄"

Metadata

Metadata

Assignees

No one assigned

    Labels

    affects: technical debtImproves the state of the codebaseneeds: triageThis issue has not yet been reviewed by a maintainer

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions