@foal/core
Version:
Full-featured Node.js framework, with no complexity
28 lines (27 loc) • 787 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convertBase64urlToBase64 = convertBase64urlToBase64;
function addPaddingToBase64url(str) {
if (str.length % 4 === 1) {
throw new TypeError('The provided base64url-encoded string has an incorrect length.');
}
if (str.length % 4 === 2) {
return str + '==';
}
if (str.length % 4 === 3) {
return str + '=';
}
return str;
}
/**
* Converts a base64url-encoded string into a base64-encoded string.
*
* @export
* @param {string} str The base64url-encoded string.
* @returns {string} The base64-encoded string.
*/
function convertBase64urlToBase64(str) {
return addPaddingToBase64url(str)
.replace(/\-/g, '+')
.replace(/_/g, '/');
}