encoding: Remove TextEncoder/TextDecoder fallbacks

This commit is contained in:
willclarktech 2021-06-09 16:10:49 +02:00
parent 3940cf22be
commit daaeea0ff2
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7

View File

@ -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);
}