@@ -54,7 +54,7 @@ import { assert, fail } from '../util/assert';
54
54
import { Code , FirestoreError } from '../util/error' ;
55
55
import * as obj from '../util/obj' ;
56
56
import { ByteString } from '../util/byte_string' ;
57
- import * as typeUtils from '../util/types' ;
57
+ import { isNegativeZero , isNullOrUndefined } from '../util/types' ;
58
58
59
59
import {
60
60
ArrayRemoveTransformOperation ,
@@ -99,14 +99,7 @@ const OPERATORS = (() => {
99
99
} ) ( ) ;
100
100
101
101
function assertPresent ( value : unknown , description : string ) : asserts value {
102
- assert ( ! typeUtils . isNullOrUndefined ( value ) , description + ' is missing' ) ;
103
- }
104
-
105
- // This is a supplement to the generated proto interfaces, which fail to account
106
- // for the fact that a timestamp may be encoded as either a string OR this.
107
- interface TimestampProto {
108
- seconds ?: string | number ;
109
- nanos ?: number ;
102
+ assert ( ! isNullOrUndefined ( value ) , description + ' is missing' ) ;
110
103
}
111
104
112
105
export interface SerializerOptions {
@@ -148,46 +141,33 @@ export class JsonProtoSerializer {
148
141
* our generated proto interfaces say Int32Value must be. But GRPC actually
149
142
* expects a { value: <number> } struct.
150
143
*/
151
- private toInt32Value ( val : number | null ) : number | null {
152
- if ( this . options . useProto3Json || typeUtils . isNullOrUndefined ( val ) ) {
144
+ private toInt32Proto ( val : number | null ) : number | { value : number } | null {
145
+ if ( this . options . useProto3Json || isNullOrUndefined ( val ) ) {
153
146
return val ;
154
147
} else {
155
- // ProtobufJS requires that we wrap Int32Values.
156
- // Use any because we need to match generated Proto types.
157
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
158
- return { value : val } as any ;
148
+ return { value : val } ;
159
149
}
160
150
}
161
151
162
152
/**
163
153
* Returns a number (or null) from a google.protobuf.Int32Value proto.
164
- * DO NOT USE THIS FOR ANYTHING ELSE.
165
- * This method cheats. It's typed as accepting "number" because that's what
166
- * our generated proto interfaces say Int32Value must be, but it actually
167
- * accepts { value: number } to match our serialization in toInt32Value().
168
154
*/
169
- private fromInt32Value ( val : number | undefined ) : number | null {
155
+ private fromInt32Proto (
156
+ val : number | { value : number } | undefined
157
+ ) : number | null {
170
158
let result ;
171
159
if ( typeof val === 'object' ) {
172
- // Use any because we need to match generated Proto types.
173
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
174
- result = ( val as any ) . value ;
160
+ result = val . value ;
175
161
} else {
176
- // We accept raw numbers (without the {value: ... } wrapper) for
177
- // compatibility with legacy persisted data.
178
162
result = val ;
179
163
}
180
- return typeUtils . isNullOrUndefined ( result ) ? null : result ;
164
+ return isNullOrUndefined ( result ) ? null : result ;
181
165
}
182
166
183
167
/**
184
168
* Returns a value for a Date that's appropriate to put into a proto.
185
- * DO NOT USE THIS FOR ANYTHING ELSE.
186
- * This method cheats. It's typed as returning "string" because that's what
187
- * our generated proto interfaces say dates must be. But it's easier and safer
188
- * to actually return a Timestamp proto.
189
169
*/
190
- private toTimestamp ( timestamp : Timestamp ) : string {
170
+ private toTimestamp ( timestamp : Timestamp ) : api . Timestamp {
191
171
if ( this . options . useProto3Json ) {
192
172
// Serialize to ISO-8601 date format, but with full nano resolution.
193
173
// Since JS Date has only millis, let's only use it for the seconds and
@@ -208,25 +188,21 @@ export class JsonProtoSerializer {
208
188
}
209
189
}
210
190
211
- private fromTimestamp ( date : string | TimestampProto ) : Timestamp {
191
+ private fromTimestamp ( date : api . Timestamp ) : Timestamp {
212
192
const timestamp = normalizeTimestamp ( date ) ;
213
193
return new Timestamp ( timestamp . seconds , timestamp . nanos ) ;
214
194
}
215
195
216
196
/**
217
197
* Returns a value for bytes that's appropriate to put in a proto.
218
- * DO NOT USE THIS FOR ANYTHING ELSE.
219
- * This method cheats. It's typed as returning "string" because that's what
220
- * our generated proto interfaces say bytes must be. But it should return
221
- * an Uint8Array in Node.
222
198
*
223
199
* Visible for testing.
224
200
*/
225
- toBytes ( bytes : Blob | ByteString ) : string {
201
+ toBytes ( bytes : Blob | ByteString ) : string | Uint8Array {
226
202
if ( this . options . useProto3Json ) {
227
203
return bytes . toBase64 ( ) ;
228
204
} else {
229
- return ( bytes . toUint8Array ( ) as unknown ) as string ;
205
+ return bytes . toUint8Array ( ) ;
230
206
}
231
207
}
232
208
@@ -249,11 +225,11 @@ export class JsonProtoSerializer {
249
225
}
250
226
}
251
227
252
- toVersion ( version : SnapshotVersion ) : string {
228
+ toVersion ( version : SnapshotVersion ) : api . Timestamp {
253
229
return this . toTimestamp ( version . toTimestamp ( ) ) ;
254
230
}
255
231
256
- fromVersion ( version : string ) : SnapshotVersion {
232
+ fromVersion ( version : api . Timestamp ) : SnapshotVersion {
257
233
assert ( ! ! version , "Trying to deserialize version that isn't set" ) ;
258
234
return SnapshotVersion . fromTimestamp ( this . fromTimestamp ( version ) ) ;
259
235
}
@@ -374,7 +350,7 @@ export class JsonProtoSerializer {
374
350
return { doubleValue : 'Infinity' } as { } ;
375
351
} else if ( doubleValue === - Infinity ) {
376
352
return { doubleValue : '-Infinity' } as { } ;
377
- } else if ( typeUtils . isNegativeZero ( doubleValue ) ) {
353
+ } else if ( isNegativeZero ( doubleValue ) ) {
378
354
return { doubleValue : '-0' } as { } ;
379
355
}
380
356
}
@@ -846,7 +822,7 @@ export class JsonProtoSerializer {
846
822
847
823
private fromWriteResult (
848
824
proto : api . WriteResult ,
849
- commitTime : string
825
+ commitTime : api . Timestamp
850
826
) : MutationResult {
851
827
// NOTE: Deletes don't have an updateTime.
852
828
let version = proto . updateTime
@@ -873,7 +849,7 @@ export class JsonProtoSerializer {
873
849
874
850
fromWriteResults (
875
851
protos : api . WriteResult [ ] | undefined ,
876
- commitTime ?: string
852
+ commitTime ?: api . Timestamp
877
853
) : MutationResult [ ] {
878
854
if ( protos && protos . length > 0 ) {
879
855
assert (
@@ -1000,7 +976,7 @@ export class JsonProtoSerializer {
1000
976
result . structuredQuery ! . orderBy = orderBy ;
1001
977
}
1002
978
1003
- const limit = this . toInt32Value ( target . limit ) ;
979
+ const limit = this . toInt32Proto ( target . limit ) ;
1004
980
if ( limit !== null ) {
1005
981
result . structuredQuery ! . limit = limit ;
1006
982
}
@@ -1046,7 +1022,7 @@ export class JsonProtoSerializer {
1046
1022
1047
1023
let limit : number | null = null ;
1048
1024
if ( query . limit ) {
1049
- limit = this . fromInt32Value ( query . limit ) ;
1025
+ limit = this . fromInt32Proto ( query . limit ) ;
1050
1026
}
1051
1027
1052
1028
let startAt : Bound | null = null ;
0 commit comments