Skip to content

Make Blob a Compat type #4074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/firestore/lite/src/api/bytes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class Bytes {
} catch (e) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Failed to construct Bytes from Base64 string: ' + e
'Failed to construct data from Base64 string: ' + e
);
}
}
Expand Down
30 changes: 7 additions & 23 deletions packages/firestore/src/api/blob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import { isBase64Available } from '../platform/base64';
import { Code, FirestoreError } from '../util/error';
import { ByteString } from '../util/byte_string';
import { Bytes } from '../../lite/src/api/bytes';
import { Compat } from '../compat/compat';

/** Helper function to assert Uint8Array is available at runtime. */
function assertUint8ArrayAvailable(): void {
Expand All @@ -40,42 +40,26 @@ function assertBase64Available(): void {
}
}

/**
* Immutable class holding a blob (binary data).
*
* This class is directly exposed in the public API. It extends the Bytes class
* of the firestore-exp API to support `instanceof Bytes` checks during user
* data conversion.
*
* Note that while you can't hide the constructor in JavaScript code, we are
* using the hack above to make sure no-one outside this module can call it.
*/
export class Blob extends Bytes {
/** Immutable class holding a blob (binary data) */
export class Blob extends Compat<Bytes> {
static fromBase64String(base64: string): Blob {
assertBase64Available();
try {
return new Blob(ByteString.fromBase64String(base64));
} catch (e) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Failed to construct Blob from Base64 string: ' + e
);
}
return new Blob(Bytes.fromBase64String(base64));
}

static fromUint8Array(array: Uint8Array): Blob {
assertUint8ArrayAvailable();
return new Blob(ByteString.fromUint8Array(array));
return new Blob(Bytes.fromUint8Array(array));
}

toBase64(): string {
assertBase64Available();
return super.toBase64();
return this._delegate.toBase64();
}

toUint8Array(): Uint8Array {
assertUint8ArrayAvailable();
return super.toUint8Array();
return this._delegate.toUint8Array();
}

toString(): string {
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/src/api/user_data_writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { isValidResourceName } from '../remote/serializer';
import { logError } from '../util/log';
import { ByteString } from '../util/byte_string';
import { Blob } from './blob';
import { Bytes } from '../../lite/src/api/bytes';
import { DocumentReference, Firestore } from './database';

export type ServerTimestampBehavior = 'estimate' | 'previous' | 'none';
Expand Down Expand Up @@ -174,7 +175,7 @@ export class UserDataWriter extends AbstractUserDataWriter {
}

protected convertBytes(bytes: ByteString): Blob {
return new Blob(bytes);
return new Blob(new Bytes(bytes));
}

protected convertReference(name: string): DocumentReference {
Expand Down