@@ -43,13 +43,13 @@ import { DocumentKey } from '../model/document_key';
43
43
import { DocumentSet } from '../model/document_set' ;
44
44
import { ResourcePath } from '../model/path' ;
45
45
import { newSerializer } from '../platform/serializer' ;
46
+ import {
47
+ buildQuerySnapshotJsonBundle ,
48
+ buildDocumentSnapshotJsonBundle
49
+ } from '../platform/snapshot_to_json' ;
46
50
import { fromDocument } from '../remote/serializer' ;
47
51
import { debugAssert , fail } from '../util/assert' ;
48
- import {
49
- BundleBuilder ,
50
- DocumentSnapshotBundleData ,
51
- QuerySnapshotBundleData
52
- } from '../util/bundle_builder_impl' ;
52
+
53
53
import { Code , FirestoreError } from '../util/error' ;
54
54
// API extractor fails importing 'property' unless we also explicitly import 'Property'.
55
55
// eslint-disable-next-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-imports-ts
@@ -529,6 +529,13 @@ export class DocumentSnapshot<
529
529
* @returns a JSON representation of this object.
530
530
*/
531
531
toJSON ( ) : object {
532
+ if ( this . metadata . hasPendingWrites ) {
533
+ throw new FirestoreError (
534
+ Code . FAILED_PRECONDITION ,
535
+ 'DocumentSnapshot.toJSON() attempted to serialize a document with pending writes. ' +
536
+ 'Await waitForPendingWrites() before invoking toJSON().'
537
+ ) ;
538
+ }
532
539
const document = this . _document ;
533
540
// eslint-disable-next-line @typescript-eslint/no-explicit-any
534
541
const result : any = { } ;
@@ -544,29 +551,16 @@ export class DocumentSnapshot<
544
551
) {
545
552
return result ;
546
553
}
547
- const builder : BundleBuilder = new BundleBuilder (
548
- this . _firestore ,
549
- AutoId . newId ( )
550
- ) ;
551
554
const documentData = this . _userDataWriter . convertObjectMap (
552
555
document . data . value . mapValue . fields ,
553
556
'previous'
554
557
) ;
555
- if ( this . metadata . hasPendingWrites ) {
556
- throw new FirestoreError (
557
- Code . FAILED_PRECONDITION ,
558
- 'DocumentSnapshot.toJSON() attempted to serialize a document with pending writes. ' +
559
- 'Await waitForPendingWrites() before invoking toJSON().'
560
- ) ;
561
- }
562
- builder . addBundleDocument (
563
- documentToDocumentSnapshotBundleData (
564
- this . ref . path ,
565
- documentData ,
566
- document
567
- )
558
+ result [ 'bundle' ] = buildDocumentSnapshotJsonBundle (
559
+ this . _firestore ,
560
+ document ,
561
+ documentData ,
562
+ this . ref . path
568
563
) ;
569
- result [ 'bundle' ] = builder . build ( ) ;
570
564
return result ;
571
565
}
572
566
}
@@ -611,6 +605,12 @@ export function documentSnapshotFromJSON<
611
605
...args : unknown [ ]
612
606
) : DocumentSnapshot < AppModelType , DbModelType > {
613
607
if ( validateJSON ( json , DocumentSnapshot . _jsonSchema ) ) {
608
+ if ( json . bundle === 'NOT SUPPORTED' ) {
609
+ throw new FirestoreError (
610
+ Code . INVALID_ARGUMENT ,
611
+ 'The provided JSON object was created in a client environment, which is not supported.'
612
+ ) ;
613
+ }
614
614
// Parse the bundle data.
615
615
const serializer = newSerializer ( db . _databaseId ) ;
616
616
const bundleReader = createBundleReaderSync ( json . bundle , serializer ) ;
@@ -831,52 +831,48 @@ export class QuerySnapshot<
831
831
* @returns a JSON representation of this object.
832
832
*/
833
833
toJSON ( ) : object {
834
+ if ( this . metadata . hasPendingWrites ) {
835
+ throw new FirestoreError (
836
+ Code . FAILED_PRECONDITION ,
837
+ 'QuerySnapshot.toJSON() attempted to serialize a document with pending writes. ' +
838
+ 'Await waitForPendingWrites() before invoking toJSON().'
839
+ ) ;
840
+ }
834
841
// eslint-disable-next-line @typescript-eslint/no-explicit-any
835
842
const result : any = { } ;
836
843
result [ 'type' ] = QuerySnapshot . _jsonSchemaVersion ;
837
844
result [ 'bundleSource' ] = 'QuerySnapshot' ;
838
845
result [ 'bundleName' ] = AutoId . newId ( ) ;
839
846
840
- const builder : BundleBuilder = new BundleBuilder (
841
- this . _firestore ,
842
- result [ 'bundleName' ]
843
- ) ;
844
847
const databaseId = this . _firestore . _databaseId . database ;
845
848
const projectId = this . _firestore . _databaseId . projectId ;
846
849
const parent = `projects/${ projectId } /databases/${ databaseId } /documents` ;
847
- const docBundleDataArray : DocumentSnapshotBundleData [ ] = [ ] ;
848
- const docArray = this . docs ;
849
- docArray . forEach ( doc => {
850
+ const documents : Document [ ] = [ ] ;
851
+ const documentData : DocumentData [ ] = [ ] ;
852
+ const paths : string [ ] = [ ] ;
853
+
854
+ this . docs . forEach ( doc => {
850
855
if ( doc . _document === null ) {
851
856
return ;
852
857
}
853
- const documentData = this . _userDataWriter . convertObjectMap (
854
- doc . _document . data . value . mapValue . fields ,
855
- 'previous'
856
- ) ;
857
- if ( this . metadata . hasPendingWrites ) {
858
- throw new FirestoreError (
859
- Code . FAILED_PRECONDITION ,
860
- 'QuerySnapshot.toJSON() attempted to serialize a document with pending writes. ' +
861
- 'Await waitForPendingWrites() before invoking toJSON().'
862
- ) ;
863
- }
864
- docBundleDataArray . push (
865
- documentToDocumentSnapshotBundleData (
866
- doc . ref . path ,
867
- documentData ,
868
- doc . _document
858
+ documents . push ( doc . _document ) ;
859
+ documentData . push (
860
+ this . _userDataWriter . convertObjectMap (
861
+ doc . _document . data . value . mapValue . fields ,
862
+ 'previous'
869
863
)
870
864
) ;
865
+ paths . push ( doc . ref . path ) ;
871
866
} ) ;
872
- const bundleData : QuerySnapshotBundleData = {
873
- name : result [ 'bundleName' ] ,
874
- query : this . query . _query ,
867
+ result [ 'bundle' ] = buildQuerySnapshotJsonBundle (
868
+ this . _firestore ,
869
+ this . query . _query ,
870
+ result [ 'bundleName' ] ,
875
871
parent ,
876
- docBundleDataArray
877
- } ;
878
- builder . addBundleQuery ( bundleData ) ;
879
- result [ 'bundle' ] = builder . build ( ) ;
872
+ paths ,
873
+ documents ,
874
+ documentData
875
+ ) ;
880
876
return result ;
881
877
}
882
878
}
@@ -921,6 +917,12 @@ export function querySnapshotFromJSON<
921
917
...args : unknown [ ]
922
918
) : QuerySnapshot < AppModelType , DbModelType > {
923
919
if ( validateJSON ( json , QuerySnapshot . _jsonSchema ) ) {
920
+ if ( json . bundle === 'NOT SUPPORTED' ) {
921
+ throw new FirestoreError (
922
+ Code . INVALID_ARGUMENT ,
923
+ 'The provided JSON object was created in a client environment, which is not supported.'
924
+ ) ;
925
+ }
924
926
// Parse the bundle data.
925
927
const serializer = newSerializer ( db . _databaseId ) ;
926
928
const bundleReader = createBundleReaderSync ( json . bundle , serializer ) ;
@@ -1123,20 +1125,3 @@ export function snapshotEqual<AppModelType, DbModelType extends DocumentData>(
1123
1125
1124
1126
return false ;
1125
1127
}
1126
-
1127
- // Formats Document data for bundling a DocumentSnapshot.
1128
- function documentToDocumentSnapshotBundleData (
1129
- path : string ,
1130
- documentData : DocumentData ,
1131
- document : Document
1132
- ) : DocumentSnapshotBundleData {
1133
- return {
1134
- documentData,
1135
- documentKey : document . mutableCopy ( ) . key ,
1136
- documentPath : path ,
1137
- documentExists : true ,
1138
- createdTime : document . createTime . toTimestamp ( ) ,
1139
- readTime : document . readTime . toTimestamp ( ) ,
1140
- versionTime : document . version . toTimestamp ( )
1141
- } ;
1142
- }
0 commit comments