rxdb
Version:
A local-first realtime NoSQL Database for JavaScript applications - https://rxdb.info/
48 lines (44 loc) • 1.22 kB
JavaScript
/**
* NO! We cannot just use btoa() and atob()
* because they do not work correctly with binary data.
* @link https://stackoverflow.com/q/30106476/3443137
*/
import { encode, decode } from 'js-base64';
/**
* atob() and btoa() do not work well with non ascii chars,
* so we have to use these helper methods instead.
* @link https://stackoverflow.com/a/30106551/3443137
*/
// Encoding UTF8 -> base64
export function b64EncodeUnicode(str) {
return encode(str);
}
// Decoding base64 -> UTF8
export function b64DecodeUnicode(str) {
return decode(str);
}
/**
* @link https://stackoverflow.com/a/9458996/3443137
*/
export function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
/**
* @link https://stackoverflow.com/a/21797381
*/
export function base64ToArrayBuffer(base64) {
var binary_string = atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
//# sourceMappingURL=utils-base64.js.map