diff --git a/packages/encoding/src/utf8.ts b/packages/encoding/src/utf8.ts index a23b0091..79380bba 100644 --- a/packages/encoding/src/utf8.ts +++ b/packages/encoding/src/utf8.ts @@ -1,38 +1,17 @@ -// Global symbols in some environments +/* eslint-disable @typescript-eslint/naming-convention */ +// Global symbols in both browsers and Node.js since v11 // https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder // https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder -/* eslint-disable-next-line @typescript-eslint/naming-convention */ -declare const TextEncoder: any | undefined; -/* eslint-disable-next-line @typescript-eslint/naming-convention */ -declare const TextDecoder: any | undefined; - -function isValidUtf8(data: Uint8Array): boolean { - const toStringAndBack = Buffer.from(Buffer.from(data).toString("utf8"), "utf8"); - return Buffer.compare(Buffer.from(data), toStringAndBack) === 0; -} +// https://nodejs.org/docs/latest-v12.x/api/util.html#util_class_util_textencoder +// https://nodejs.org/docs/latest-v12.x/api/util.html#util_class_util_textdecoder +// See https://github.com/microsoft/TypeScript/issues/31535 +declare const TextEncoder: any; +declare const TextDecoder: any; export function toUtf8(str: string): Uint8Array { - // Browser and future nodejs (https://github.com/nodejs/node/issues/20365) - if (typeof TextEncoder !== "undefined") { - return new TextEncoder().encode(str); - } - - // Use Buffer hack instead of nodejs util.TextEncoder to ensure - // webpack does not bundle the util module for browsers. - return new Uint8Array(Buffer.from(str, "utf8")); + return new TextEncoder().encode(str); } export function fromUtf8(data: Uint8Array): string { - // Browser and future nodejs (https://github.com/nodejs/node/issues/20365) - if (typeof TextDecoder !== "undefined") { - return new TextDecoder("utf-8", { fatal: true }).decode(data); - } - - // Use Buffer hack instead of nodejs util.TextDecoder to ensure - // webpack does not bundle the util module for browsers. - // Buffer.toString has no fatal option - if (!isValidUtf8(data)) { - throw new Error("Invalid UTF8 data"); - } - return Buffer.from(data).toString("utf8"); + return new TextDecoder("utf-8", { fatal: true }).decode(data); }