mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
encoding: Remove TextEncoder/TextDecoder fallbacks
This commit is contained in:
parent
3940cf22be
commit
daaeea0ff2
@ -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/TextEncoder
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
|
// https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder
|
||||||
/* eslint-disable-next-line @typescript-eslint/naming-convention */
|
// https://nodejs.org/docs/latest-v12.x/api/util.html#util_class_util_textencoder
|
||||||
declare const TextEncoder: any | undefined;
|
// https://nodejs.org/docs/latest-v12.x/api/util.html#util_class_util_textdecoder
|
||||||
/* eslint-disable-next-line @typescript-eslint/naming-convention */
|
// See https://github.com/microsoft/TypeScript/issues/31535
|
||||||
declare const TextDecoder: any | undefined;
|
declare const TextEncoder: any;
|
||||||
|
declare const TextDecoder: any;
|
||||||
function isValidUtf8(data: Uint8Array): boolean {
|
|
||||||
const toStringAndBack = Buffer.from(Buffer.from(data).toString("utf8"), "utf8");
|
|
||||||
return Buffer.compare(Buffer.from(data), toStringAndBack) === 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function toUtf8(str: string): Uint8Array {
|
export function toUtf8(str: string): Uint8Array {
|
||||||
// Browser and future nodejs (https://github.com/nodejs/node/issues/20365)
|
return new TextEncoder().encode(str);
|
||||||
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"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function fromUtf8(data: Uint8Array): string {
|
export function fromUtf8(data: Uint8Array): string {
|
||||||
// Browser and future nodejs (https://github.com/nodejs/node/issues/20365)
|
return new TextDecoder("utf-8", { fatal: true }).decode(data);
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user