Skip to content

Commit 27438f1

Browse files
Make Blob a Compat type (#4074)
1 parent d934f74 commit 27438f1

File tree

5 files changed

+16
-27
lines changed

5 files changed

+16
-27
lines changed

packages/firestore/lite/src/api/bytes.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class Bytes {
4040
} catch (e) {
4141
throw new FirestoreError(
4242
Code.INVALID_ARGUMENT,
43-
'Failed to construct Bytes from Base64 string: ' + e
43+
'Failed to construct data from Base64 string: ' + e
4444
);
4545
}
4646
}

packages/firestore/src/api/blob.ts

+11-23
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import { isBase64Available } from '../platform/base64';
1919
import { Code, FirestoreError } from '../util/error';
20-
import { ByteString } from '../util/byte_string';
2120
import { Bytes } from '../../lite/src/api/bytes';
21+
import { Compat } from '../compat/compat';
2222

2323
/** Helper function to assert Uint8Array is available at runtime. */
2424
function assertUint8ArrayAvailable(): void {
@@ -40,42 +40,30 @@ function assertBase64Available(): void {
4040
}
4141
}
4242

43-
/**
44-
* Immutable class holding a blob (binary data).
45-
*
46-
* This class is directly exposed in the public API. It extends the Bytes class
47-
* of the firestore-exp API to support `instanceof Bytes` checks during user
48-
* data conversion.
49-
*
50-
* Note that while you can't hide the constructor in JavaScript code, we are
51-
* using the hack above to make sure no-one outside this module can call it.
52-
*/
53-
export class Blob extends Bytes {
43+
/** Immutable class holding a blob (binary data) */
44+
export class Blob extends Compat<Bytes> {
5445
static fromBase64String(base64: string): Blob {
5546
assertBase64Available();
56-
try {
57-
return new Blob(ByteString.fromBase64String(base64));
58-
} catch (e) {
59-
throw new FirestoreError(
60-
Code.INVALID_ARGUMENT,
61-
'Failed to construct Blob from Base64 string: ' + e
62-
);
63-
}
47+
return new Blob(Bytes.fromBase64String(base64));
6448
}
6549

6650
static fromUint8Array(array: Uint8Array): Blob {
6751
assertUint8ArrayAvailable();
68-
return new Blob(ByteString.fromUint8Array(array));
52+
return new Blob(Bytes.fromUint8Array(array));
6953
}
7054

7155
toBase64(): string {
7256
assertBase64Available();
73-
return super.toBase64();
57+
return this._delegate.toBase64();
7458
}
7559

7660
toUint8Array(): Uint8Array {
7761
assertUint8ArrayAvailable();
78-
return super.toUint8Array();
62+
return this._delegate.toUint8Array();
63+
}
64+
65+
isEqual(other: Blob): boolean {
66+
return this._delegate.isEqual(other._delegate);
7967
}
8068

8169
toString(): string {

packages/firestore/src/api/user_data_writer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { isValidResourceName } from '../remote/serializer';
4646
import { logError } from '../util/log';
4747
import { ByteString } from '../util/byte_string';
4848
import { Blob } from './blob';
49+
import { Bytes } from '../../lite/src/api/bytes';
4950
import { DocumentReference, Firestore } from './database';
5051

5152
export type ServerTimestampBehavior = 'estimate' | 'previous' | 'none';
@@ -174,7 +175,7 @@ export class UserDataWriter extends AbstractUserDataWriter {
174175
}
175176

176177
protected convertBytes(bytes: ByteString): Blob {
177-
return new Blob(bytes);
178+
return new Blob(new Bytes(bytes));
178179
}
179180

180181
protected convertReference(name: string): DocumentReference {

packages/firestore/src/platform/node_lite/serializer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@
1515
* limitations under the License.
1616
*/
1717

18-
export * from '../node/serializer';
18+
export * from '../browser_lite/serializer';

packages/firestore/test/unit/api/blob.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('Blob', () => {
4848

4949
it('Blob throws on invalid Base64 strings', () => {
5050
expect(() => Blob.fromBase64String('not-base64!')).to.throw(
51-
/Failed to construct Blob from Base64 string:/
51+
/Failed to construct data from Base64 string:/
5252
);
5353
});
5454

0 commit comments

Comments
 (0)