[firestore][js] start of blob support

This commit is contained in:
Salakar 2018-05-01 22:20:55 +01:00
parent 19266d4bae
commit ff3b0fd7ea
2 changed files with 139 additions and 0 deletions

View File

@ -0,0 +1,71 @@
import Base64 from './utils/Base64';
type BlobFormat = 'string' | 'array';
export default class Blob {
constructor(data, type: BlobFormat) {
this._data = data;
this._type = type;
}
/**
* Creates a new Blob from the given Base64 string
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#.fromBase64String
* @param base64 string
*/
static fromBase64String(base64: string): Blob {
// TODO convert to Uint8Array?
return new Blob(base64, 'string');
}
/**
* Creates a new Blob from the given Uint8Array.
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#.fromUint8Array
* @param array Array
*/
static fromUint8Array(array: Array): Blob {
return new Blob(array, 'array');
}
/**
* Returns 'true' if this Blob is equal to the provided one.
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#isEqual
* @param {*} blob Blob The Blob to compare against. Value must not be null.
* @returns boolean 'true' if this Blob is equal to the provided one.
*/
isEqual(blob: Blob): boolean {
// TODO comparison checks
console.log(blob);
return true;
}
/**
* Returns the bytes of a Blob as a Base64-encoded string.
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#toBase64
* @returns string The Base64-encoded string created from the Blob object.
*/
toBase64(): string {
if (this._type === 'string') return this._data;
let binary = '';
const len = this._data.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(this._data[i]);
}
return Base64.btoa(binary);
}
/**
* Returns the bytes of a Blob in a new Uint8Array.
* @url https://firebase.google.com/docs/reference/js/firebase.firestore.Blob#toUint8Array
* @returns non-null Uint8Array The Uint8Array created from the Blob object.
*/
toUint8Array(): Uint8Array {
if (this._type === 'array') return this._data;
// TODO conversion
// TODO conversion
// TODO conversion
return new Uint8Array();
}
}

View File

@ -0,0 +1,68 @@
// @flow
/* eslint-disable */
const CHARS =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
export default {
/**
* window.btoa
*/
btoa(input: string = ''): string {
let map;
let i = 0;
let block = 0;
let output = '';
// eslint-disable-next-line
for (
block = 0, i = 0, map = CHARS;
input.charAt(i | 0) || ((map = '='), i % 1);
output += map.charAt(63 & (block >> (8 - (i % 1) * 8)))
) {
const charCode = input.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;
},
/**
* window.atob
*/
atob(input: string = ''): string {
let i = 0;
let bc = 0;
let bs = 0;
let buffer;
let output = '';
const str = input.replace(/=+$/, '');
if (str.length % 4 === 1) {
throw new Error(
"'atob' failed: The string to be decoded is not correctly encoded."
);
}
// eslint-disable-next-line
for (
bc = 0, bs = 0, 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;
},
};