Skip to content

Commit 7c8ef87

Browse files
committed
Put more logic into bundle_impl.
1 parent 5df33ec commit 7c8ef87

File tree

2 files changed

+33
-42
lines changed

2 files changed

+33
-42
lines changed

packages/firestore/src/api/snapshot.ts

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -577,42 +577,25 @@ export class DocumentSnapshot<
577577
if (error) {
578578
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
579579
}
580+
// Parse the bundle data.
580581
const serializer = newSerializer(db._databaseId);
581-
const reader = createBundleReaderSync(bundleString, serializer);
582-
const elements = reader.getElements();
582+
const elements = createBundleReaderSync(bundleString, serializer).getElements();
583583
if (elements.length === 0) {
584-
throw new FirestoreError(
585-
Code.INVALID_ARGUMENT,
586-
'No snapshat data was found in the bundle.'
587-
);
584+
error = 'No snapshat data was found in the bundle.';
588585
}
589586
if (
590587
elements.length !== 2 ||
591588
!elements[0].payload.documentMetadata ||
592589
!elements[1].payload.document
593590
) {
594-
throw new FirestoreError(
595-
Code.INVALID_ARGUMENT,
596-
'DocumentSnapshot bundle data must contain one document metadata and then one document'
597-
);
591+
error = 'DocumentSnapshot bundle data must contain one document metadata and then one document.';
598592
}
599-
const docMetadata = elements[0].payload!.documentMetadata!;
600-
const docData = elements[1].payload.document!;
601-
if (docMetadata.name! !== docData.name) {
602-
throw new FirestoreError(
603-
Code.INVALID_ARGUMENT,
604-
'The document data is not related to the document metadata in the bundle'
605-
);
593+
if (error) {
594+
throw new FirestoreError(Code.INVALID_ARGUMENT, error);
606595
}
596+
// convert bundle data into the types that the DocumentSnapshot constructore requires.
607597
const bundleConverter = new BundleConverterImpl(serializer);
608-
const bundledDoc = {
609-
metadata: docMetadata,
610-
document: docData
611-
};
612-
const documentSnapshotData = bundleConverter.toDocumentSnapshotData(
613-
docMetadata,
614-
bundledDoc
615-
);
598+
const documentSnapshotData = bundleConverter.toDocumentSnapshotData(elements);
616599
const liteUserDataWriter = new LiteUserDataWriter(db);
617600
return new DocumentSnapshot(
618601
db,
@@ -623,7 +606,7 @@ export class DocumentSnapshot<
623606
/* hasPendingWrites= */ false,
624607
/* fromCache= */ false
625608
),
626-
null
609+
/* converter= */ null
627610
);
628611
}
629612
}

packages/firestore/src/core/bundle_impl.ts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
} from '../remote/serializer';
4141
import { debugAssert } from '../util/assert';
4242
import { SizedBundleElement } from '../util/bundle_reader';
43+
import { Code, FirestoreError } from '../util/error';
4344

4445
import {
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
*/
5556
export 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

Comments
 (0)