From 00e672fe38852e331bfd3d2acd91a39602848d8e Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Mon, 16 Nov 2020 13:15:11 -0700 Subject: [PATCH 1/5] Make Blob a Compat type This is mostly a no-op but it aligns the implementation of Blob with the rest of the API types --- packages/firestore/lite/src/api/bytes.ts | 2 +- packages/firestore/src/api/blob.ts | 30 +++++-------------- .../firestore/src/api/user_data_writer.ts | 3 +- 3 files changed, 10 insertions(+), 25 deletions(-) diff --git a/packages/firestore/lite/src/api/bytes.ts b/packages/firestore/lite/src/api/bytes.ts index 0c8dfe9b206..8e1ce1152c3 100644 --- a/packages/firestore/lite/src/api/bytes.ts +++ b/packages/firestore/lite/src/api/bytes.ts @@ -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 ); } } diff --git a/packages/firestore/src/api/blob.ts b/packages/firestore/src/api/blob.ts index 067b3e350aa..44cb6ac6784 100644 --- a/packages/firestore/src/api/blob.ts +++ b/packages/firestore/src/api/blob.ts @@ -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 { @@ -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 { 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 { diff --git a/packages/firestore/src/api/user_data_writer.ts b/packages/firestore/src/api/user_data_writer.ts index bb97df37b17..150d066689f 100644 --- a/packages/firestore/src/api/user_data_writer.ts +++ b/packages/firestore/src/api/user_data_writer.ts @@ -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'; @@ -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 { From f2efa66d516ea6ee1a11a9d9e2d80ad688a9d813 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 18 Nov 2020 13:26:49 -0700 Subject: [PATCH 2/5] Test fix --- packages/firestore/src/api/blob.ts | 4 ++++ packages/firestore/src/platform/node_lite/serializer.ts | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/firestore/src/api/blob.ts b/packages/firestore/src/api/blob.ts index 44cb6ac6784..eef8399da13 100644 --- a/packages/firestore/src/api/blob.ts +++ b/packages/firestore/src/api/blob.ts @@ -62,6 +62,10 @@ export class Blob extends Compat { return this._delegate.toUint8Array(); } + isEqual(other: Blob) { + return this._delegate.isEqual(other._delegate); + } + toString(): string { return 'Blob(base64: ' + this.toBase64() + ')'; } diff --git a/packages/firestore/src/platform/node_lite/serializer.ts b/packages/firestore/src/platform/node_lite/serializer.ts index 3f2d664b97c..cdf6bf1dfb1 100644 --- a/packages/firestore/src/platform/node_lite/serializer.ts +++ b/packages/firestore/src/platform/node_lite/serializer.ts @@ -15,4 +15,4 @@ * limitations under the License. */ -export * from '../node/serializer'; +export * from '../browser_lite/serializer'; From 3cd5c63fe0c1ed4a09f0f1bd580bbffec613aec1 Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 18 Nov 2020 14:37:20 -0700 Subject: [PATCH 3/5] Lint --- packages/firestore/src/api/blob.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firestore/src/api/blob.ts b/packages/firestore/src/api/blob.ts index eef8399da13..c6039a485a0 100644 --- a/packages/firestore/src/api/blob.ts +++ b/packages/firestore/src/api/blob.ts @@ -62,7 +62,7 @@ export class Blob extends Compat { return this._delegate.toUint8Array(); } - isEqual(other: Blob) { + isEqual(other: Blob) : boolean { return this._delegate.isEqual(other._delegate); } From 0abec27583340f92ceddd563d13ca203d2d6d09d Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 18 Nov 2020 14:37:29 -0700 Subject: [PATCH 4/5] Lint --- packages/firestore/src/api/blob.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firestore/src/api/blob.ts b/packages/firestore/src/api/blob.ts index c6039a485a0..231c37a6bf2 100644 --- a/packages/firestore/src/api/blob.ts +++ b/packages/firestore/src/api/blob.ts @@ -62,7 +62,7 @@ export class Blob extends Compat { return this._delegate.toUint8Array(); } - isEqual(other: Blob) : boolean { + isEqual(other: Blob): boolean { return this._delegate.isEqual(other._delegate); } From af08d0aed8943c1fc0dfbdd5a7b5144a6b40f8da Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 18 Nov 2020 17:07:57 -0700 Subject: [PATCH 5/5] Test fix --- packages/firestore/test/unit/api/blob.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firestore/test/unit/api/blob.test.ts b/packages/firestore/test/unit/api/blob.test.ts index 0f9513bbd34..266a8f7b049 100644 --- a/packages/firestore/test/unit/api/blob.test.ts +++ b/packages/firestore/test/unit/api/blob.test.ts @@ -48,7 +48,7 @@ describe('Blob', () => { it('Blob throws on invalid Base64 strings', () => { expect(() => Blob.fromBase64String('not-base64!')).to.throw( - /Failed to construct Blob from Base64 string:/ + /Failed to construct data from Base64 string:/ ); });