UNPKG

@4players/odin

Version:

A cross-platform SDK enabling developers to integrate real-time VoIP chat technology into their projects

44 lines (43 loc) 1.28 kB
export function toRaw(value) { return JSON.parse(JSON.stringify(value)); } export function parseJwt(token) { const base64Url = token.split('.')[1]; const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); const payload = decodeURIComponent(atob(base64) .split('') .map(function (c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }) .join('')); return JSON.parse(payload); } /** * Encodes a value or an object with the help of JSON.stringify to an Uint8Array. * * @param value Value or object to encode * @returns The Uint8Array encoded value */ export function valueToUint8Array(value) { if (typeof value === 'undefined' || value === null) { throw new Error('Error when converting to uint8array because the value is 0 or undefined'); } return new TextEncoder().encode(JSON.stringify(value)); } /** * Decodes a Uint8Array. * * @param bytes Byte array to decode * @returns The decoded value or undefined on error */ export function uint8ArrayToValue(bytes) { if (bytes.length === 0) { return {}; } try { return JSON.parse(new TextDecoder().decode(bytes)); } catch (e) { throw new Error('Error when decoding uInt8Array.'); } }