1
1
/**
2
2
* @license
3
- * Copyright 2017 Google Inc.
3
+ * Copyright 2017 Google LLC
4
4
*
5
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
6
* you may not use this file except in compliance with the License.
@@ -23,11 +23,7 @@ import {
23
23
validateArgType ,
24
24
validateExactNumberOfArgs
25
25
} from '../util/input_validation' ;
26
- import { primitiveComparator } from '../util/misc' ;
27
- import {
28
- binaryStringFromUint8Array ,
29
- uint8ArrayFromBinaryString
30
- } from '../util/byte_string' ;
26
+ import { ByteString } from '../util/byte_string' ;
31
27
32
28
/** Helper function to assert Uint8Array is available at runtime. */
33
29
function assertUint8ArrayAvailable ( ) : void {
@@ -57,24 +53,21 @@ function assertBase64Available(): void {
57
53
* using the hack above to make sure no-one outside this module can call it.
58
54
*/
59
55
export class Blob {
60
- // Prefix with underscore to signal this is a private variable in JS and
61
- // prevent it showing up for autocompletion.
62
- // A binary string is a string with each char as Unicode code point in the
63
- // range of [0, 255], essentially simulating a byte array.
64
- private _binaryString : string ;
56
+ // Prefix with underscore to signal that we consider this not part of the
57
+ // public API and to prevent it from showing up for autocompletion.
58
+ _byteString : ByteString ;
65
59
66
- private constructor ( binaryString : string ) {
60
+ constructor ( byteString : ByteString ) {
67
61
assertBase64Available ( ) ;
68
- this . _binaryString = binaryString ;
62
+ this . _byteString = byteString ;
69
63
}
70
64
71
65
static fromBase64String ( base64 : string ) : Blob {
72
66
validateExactNumberOfArgs ( 'Blob.fromBase64String' , arguments , 1 ) ;
73
67
validateArgType ( 'Blob.fromBase64String' , 'string' , 1 , base64 ) ;
74
68
assertBase64Available ( ) ;
75
69
try {
76
- const binaryString = PlatformSupport . getPlatform ( ) . atob ( base64 ) ;
77
- return new Blob ( binaryString ) ;
70
+ return new Blob ( ByteString . fromBase64String ( base64 ) ) ;
78
71
} catch ( e ) {
79
72
throw new FirestoreError (
80
73
Code . INVALID_ARGUMENT ,
@@ -89,42 +82,27 @@ export class Blob {
89
82
if ( ! ( array instanceof Uint8Array ) ) {
90
83
throw invalidClassError ( 'Blob.fromUint8Array' , 'Uint8Array' , 1 , array ) ;
91
84
}
92
- const binaryString = binaryStringFromUint8Array ( array ) ;
93
- return new Blob ( binaryString ) ;
85
+ return new Blob ( ByteString . fromUint8Array ( array ) ) ;
94
86
}
95
87
96
88
toBase64 ( ) : string {
97
89
validateExactNumberOfArgs ( 'Blob.toBase64' , arguments , 0 ) ;
98
90
assertBase64Available ( ) ;
99
- return PlatformSupport . getPlatform ( ) . btoa ( this . _binaryString ) ;
91
+ return this . _byteString . toBase64 ( ) ;
100
92
}
101
93
102
94
toUint8Array ( ) : Uint8Array {
103
95
validateExactNumberOfArgs ( 'Blob.toUint8Array' , arguments , 0 ) ;
104
96
assertUint8ArrayAvailable ( ) ;
105
- const buffer = uint8ArrayFromBinaryString ( this . _binaryString ) ;
106
- return buffer ;
97
+ return this . _byteString . toUint8Array ( ) ;
107
98
}
108
99
109
100
toString ( ) : string {
110
101
return 'Blob(base64: ' + this . toBase64 ( ) + ')' ;
111
102
}
112
103
113
104
isEqual ( other : Blob ) : boolean {
114
- return this . _binaryString === other . _binaryString ;
115
- }
116
-
117
- _approximateByteSize ( ) : number {
118
- // Assume UTF-16 encoding in memory (see StringValue.approximateByteSize())
119
- return this . _binaryString . length * 2 ;
120
- }
121
-
122
- /**
123
- * Actually private to JS consumers of our API, so this function is prefixed
124
- * with an underscore.
125
- */
126
- _compareTo ( other : Blob ) : number {
127
- return primitiveComparator ( this . _binaryString , other . _binaryString ) ;
105
+ return this . _byteString . isEqual ( other . _byteString ) ;
128
106
}
129
107
}
130
108
0 commit comments