declare const TextEncoder: any | undefined; 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; } export function toUtf8(str: string): Uint8Array { 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 { 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'); }