@nopwdio/sdk-js
Version:
Nopwd JS SDK
21 lines • 770 B
JavaScript
export const bufferTo64Safe = function (buffer) {
const bytes = new Uint8Array(buffer);
return arrayTo64(bytes);
};
export const arrayTo64 = function (bytes) {
let str = "";
for (const charCode of bytes) {
str += String.fromCharCode(charCode);
}
const base64String = btoa(str);
return base64String.replace(/\+/g, "-").replace(/\//g, "_").replace(/=/g, "");
};
export const decodeFromSafe64 = function (base64) {
// Add removed at end '='
base64 += Array(5 - (base64.length % 4)).join("=");
base64 = base64
.replace(/\-/g, "+") // Convert '-' to '+'
.replace(/\_/g, "/"); // Convert '_' to '/'
return new Uint8Array([...atob(base64)].map((c) => c.charCodeAt(0)));
};
//# sourceMappingURL=encoding.js.map