UNPKG

@4players/odin-common

Version:

A collection of commonly used type definitions and utility functions across ODIN web projects

30 lines (29 loc) 1.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromBase64 = fromBase64; exports.fromBase64Url = fromBase64Url; const result_1 = require("./result"); function fromBase64(base64) { try { const binString = atob(base64); /* @ts-ignore: strings are array-like */ const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0)); const decoder = new TextDecoder('utf8'); return (0, result_1.success)(decoder.decode(bytes)); } catch (error) { return (0, result_1.failure)(String(error)); } } function fromBase64Url(base64uri) { try { const binString = atob(base64uri.replace(/-/g, '+').replace(/_/g, '/')); /* @ts-ignore: strings are array-like */ const bytes = Uint8Array.from(binString, (m) => m.codePointAt(0)); const decoder = new TextDecoder('utf8'); return (0, result_1.success)(decoder.decode(bytes)); } catch (error) { return (0, result_1.failure)(String(error)); } }