barracuda-client-api
Version:
API Client to connect to Barracuda Enterprise Service Bus
54 lines (53 loc) • 1.98 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.iBtoa = void 0;
/* tslint:disable:no-bitwise no-conditional-assignment triple-equals */
const btoa_1 = __importDefault(require("btoa"));
const index_1 = require("../logging/index");
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
const Base64 = {
btoa: (input = "") => {
const str = input;
let output = "";
for (let block = 0, charCode, i = 0, map = chars; str.charAt(i | 0) || (map = "=", i % 1); output += map.charAt(63 & block >> 8 - i % 1 * 8)) {
charCode = str.charCodeAt(i += 3 / 4);
if (charCode > 0xFF) {
throw new Error("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range.");
}
block = block << 8 | charCode;
}
return output;
},
atob: (input = "") => {
const str = input.replace(/=+$/, "");
let output = "";
if (str.length % 4 == 1) {
throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
}
// noinspection JSAssignmentUsedAsCondition
for (let bc = 0, bs = 0, buffer, i = 0; buffer = str.charAt(i++); ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
buffer = chars.indexOf(buffer);
}
return output;
},
};
exports.default = Base64;
/**
* Isomorphic Btoa
* @param {string} gaToken
* @returns {string}
*/
function iBtoa(gaToken) {
try {
return btoa_1.default(gaToken);
}
catch (e) {
index_1.logDebug("error with btoa function from npm", e);
return Base64.btoa(gaToken);
}
}
exports.iBtoa = iBtoa;