Skip to content

Commit f213ba6

Browse files
Don't use the escaped persistence keys when we write to Local Storage (#1281)
1 parent 31c1bf4 commit f213ba6

File tree

2 files changed

+11
-22
lines changed

2 files changed

+11
-22
lines changed

packages/firestore/src/local/shared_client_state.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ export class WebStorageSharedClientState implements SharedClientState {
526526
private readonly sequenceNumberKey: string;
527527
private readonly activeClients: { [key: string]: ClientState } = {};
528528
private readonly storageListener = this.handleWebStorageEvent.bind(this);
529-
private readonly escapedPersistenceKey: string;
530529
private readonly onlineStateKey: string;
531530
private readonly clientStateKeyRe: RegExp;
532531
private readonly mutationBatchKeyRe: RegExp;
@@ -543,7 +542,7 @@ export class WebStorageSharedClientState implements SharedClientState {
543542
constructor(
544543
private readonly queue: AsyncQueue,
545544
private readonly platform: Platform,
546-
persistenceKey: string,
545+
private readonly persistenceKey: string,
547546
private readonly localClientId: ClientId,
548547
initialUser: User
549548
) {
@@ -555,7 +554,7 @@ export class WebStorageSharedClientState implements SharedClientState {
555554
}
556555
// Escape the special characters mentioned here:
557556
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
558-
this.escapedPersistenceKey = persistenceKey.replace(
557+
const escapedPersistenceKey = persistenceKey.replace(
559558
/[.*+?^${}()|[\]\\]/g,
560559
'\\$&'
561560
);
@@ -565,26 +564,20 @@ export class WebStorageSharedClientState implements SharedClientState {
565564
this.localClientStorageKey = this.toWebStorageClientStateKey(
566565
this.localClientId
567566
);
568-
this.sequenceNumberKey = `${SEQUENCE_NUMBER_KEY_PREFIX}_${
569-
this.escapedPersistenceKey
570-
}`;
567+
this.sequenceNumberKey = `${SEQUENCE_NUMBER_KEY_PREFIX}_${persistenceKey}`;
571568
this.activeClients[this.localClientId] = new LocalClientState();
572569

573570
this.clientStateKeyRe = new RegExp(
574-
`^${CLIENT_STATE_KEY_PREFIX}_${this.escapedPersistenceKey}_([^_]*)$`
571+
`^${CLIENT_STATE_KEY_PREFIX}_${escapedPersistenceKey}_([^_]*)$`
575572
);
576573
this.mutationBatchKeyRe = new RegExp(
577-
`^${MUTATION_BATCH_KEY_PREFIX}_${
578-
this.escapedPersistenceKey
579-
}_(\\d+)(?:_(.*))?$`
574+
`^${MUTATION_BATCH_KEY_PREFIX}_${escapedPersistenceKey}_(\\d+)(?:_(.*))?$`
580575
);
581576
this.queryTargetKeyRe = new RegExp(
582-
`^${QUERY_TARGET_KEY_PREFIX}_${this.escapedPersistenceKey}_(\\d+)$`
577+
`^${QUERY_TARGET_KEY_PREFIX}_${escapedPersistenceKey}_(\\d+)$`
583578
);
584579

585-
this.onlineStateKey = `${ONLINE_STATE_KEY_PREFIX}_${
586-
this.escapedPersistenceKey
587-
}`;
580+
this.onlineStateKey = `${ONLINE_STATE_KEY_PREFIX}_${persistenceKey}`;
588581

589582
// Rather than adding the storage observer during start(), we add the
590583
// storage observer during initialization. This ensures that we collect
@@ -926,22 +919,18 @@ export class WebStorageSharedClientState implements SharedClientState {
926919
`Client key cannot contain '_', but was '${clientId}'`
927920
);
928921

929-
return `${CLIENT_STATE_KEY_PREFIX}_${
930-
this.escapedPersistenceKey
931-
}_${clientId}`;
922+
return `${CLIENT_STATE_KEY_PREFIX}_${this.persistenceKey}_${clientId}`;
932923
}
933924

934925
/** Assembles the key for a query state in WebStorage */
935926
private toWebStorageQueryTargetMetadataKey(targetId: TargetId): string {
936-
return `${QUERY_TARGET_KEY_PREFIX}_${
937-
this.escapedPersistenceKey
938-
}_${targetId}`;
927+
return `${QUERY_TARGET_KEY_PREFIX}_${this.persistenceKey}_${targetId}`;
939928
}
940929

941930
/** Assembles the key for a mutation batch in WebStorage */
942931
private toWebStorageMutationBatchKey(batchId: BatchId): string {
943932
let mutationKey = `${MUTATION_BATCH_KEY_PREFIX}_${
944-
this.escapedPersistenceKey
933+
this.persistenceKey
945934
}_${batchId}`;
946935

947936
if (this.currentUser.isAuthenticated()) {

packages/firestore/test/unit/local/persistence_test_helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const INDEXEDDB_TEST_DATABASE_ID = new DatabaseId('test-project');
5656
/** The DatabaseInfo used by most tests that access IndexedDb. */
5757
const INDEXEDDB_TEST_DATABASE_INFO = new DatabaseInfo(
5858
INDEXEDDB_TEST_DATABASE_ID,
59-
'PersistenceTestHelpers',
59+
'[PersistenceTestHelpers]',
6060
'host',
6161
/*ssl=*/ false
6262
);

0 commit comments

Comments
 (0)