@@ -40,6 +40,7 @@ import {
4040} from '../remote/serializer' ;
4141import { debugAssert } from '../util/assert' ;
4242import { SizedBundleElement } from '../util/bundle_reader' ;
43+ import { Code , FirestoreError } from '../util/error' ;
4344
4445import {
4546 BundleConverter ,
@@ -53,7 +54,7 @@ import { SnapshotVersion } from './snapshot_version';
5354 * Helper to convert objects from bundles to model objects in the SDK.
5455 */
5556export class BundleConverterImpl implements BundleConverter {
56- constructor ( private readonly serializer : JsonProtoSerializer ) { }
57+ constructor ( private readonly serializer : JsonProtoSerializer ) { }
5758
5859 toDocumentKey ( name : string ) : DocumentKey {
5960 return fromName ( this . serializer , name ) ;
@@ -78,19 +79,26 @@ export class BundleConverterImpl implements BundleConverter {
7879 }
7980
8081 toDocumentSnapshotData (
81- docMetadata : BundledDocumentMetadata ,
82- bundledDoc : BundledDocument
83- ) : {
84- documentKey : DocumentKey ;
85- mutableDoc : MutableDocument ;
86- } {
82+ bundleElements : SizedBundleElement [ ] ) : {
83+ documentKey : DocumentKey ;
84+ mutableDoc : MutableDocument ;
85+ } {
86+ const metadata = bundleElements [ 0 ] ?. payload ?. documentMetadata ! ;
87+ const document = bundleElements [ 1 ] ?. payload ?. document ! ;
88+ let error : string | undefined = undefined ;
89+ if ( ! metadata || ! document ) {
90+ error = 'DocumentSnapshot bundle data requires both document metadata and document data' ;
91+ } else if ( metadata . name !== document . name ) {
92+ error = 'DocumentSnapshot metadata is not related to the document in the bundle.' ;
93+ }
94+ if ( error ) {
95+ throw new FirestoreError ( Code . INVALID_ARGUMENT , error ) ;
96+ }
8797 const bundleConverter = new BundleConverterImpl ( this . serializer ) ;
88- const documentKey = bundleConverter . toDocumentKey ( docMetadata . name ! ) ;
89- const mutableDoc = bundleConverter . toMutableDocument ( bundledDoc ) ;
90- mutableDoc . setReadTime (
91- bundleConverter . toSnapshotVersion ( docMetadata . readTime ! )
92- ) ;
93-
98+ const documentKey = bundleConverter . toDocumentKey ( metadata . name ! ) ;
99+ const mutableDoc = bundleConverter . toMutableDocument ( {
100+ metadata, document
101+ } ) ;
94102 return {
95103 documentKey,
96104 mutableDoc
@@ -155,8 +163,8 @@ export class BundleLoader {
155163 } else if ( element . payload . document ) {
156164 debugAssert (
157165 this . documents . length > 0 &&
158- this . documents [ this . documents . length - 1 ] . metadata . name ===
159- element . payload . document . name ,
166+ this . documents [ this . documents . length - 1 ] . metadata . name ===
167+ element . payload . document . name ,
160168 'The document being added does not match the stored metadata.'
161169 ) ;
162170 this . documents [ this . documents . length - 1 ] . document =
@@ -200,7 +208,7 @@ export class BundleLoader {
200208 async complete ( ) : Promise < BundleLoadResult > {
201209 debugAssert (
202210 this . documents [ this . documents . length - 1 ] ?. metadata . exists !== true ||
203- ! ! this . documents [ this . documents . length - 1 ] . document ,
211+ ! ! this . documents [ this . documents . length - 1 ] . document ,
204212 'Bundled documents end with a document metadata element instead of a document.'
205213 ) ;
206214 debugAssert ( ! ! this . bundleMetadata . id , 'Bundle ID must be set.' ) ;
0 commit comments