From d5ba267e1e1ca7f00a59c5665b5a0fc95a66e67c Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Wed, 3 Oct 2018 13:13:42 -0700 Subject: [PATCH] Don't use the escaped persistence keys when we write to Local Storage --- .../src/local/shared_client_state.ts | 31 ++++++------------- .../unit/local/persistence_test_helpers.ts | 2 +- 2 files changed, 11 insertions(+), 22 deletions(-) diff --git a/packages/firestore/src/local/shared_client_state.ts b/packages/firestore/src/local/shared_client_state.ts index 3937ec598aa..b794d83e520 100644 --- a/packages/firestore/src/local/shared_client_state.ts +++ b/packages/firestore/src/local/shared_client_state.ts @@ -526,7 +526,6 @@ export class WebStorageSharedClientState implements SharedClientState { private readonly sequenceNumberKey: string; private readonly activeClients: { [key: string]: ClientState } = {}; private readonly storageListener = this.handleWebStorageEvent.bind(this); - private readonly escapedPersistenceKey: string; private readonly onlineStateKey: string; private readonly clientStateKeyRe: RegExp; private readonly mutationBatchKeyRe: RegExp; @@ -543,7 +542,7 @@ export class WebStorageSharedClientState implements SharedClientState { constructor( private readonly queue: AsyncQueue, private readonly platform: Platform, - persistenceKey: string, + private readonly persistenceKey: string, private readonly localClientId: ClientId, initialUser: User ) { @@ -555,7 +554,7 @@ export class WebStorageSharedClientState implements SharedClientState { } // Escape the special characters mentioned here: // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions - this.escapedPersistenceKey = persistenceKey.replace( + const escapedPersistenceKey = persistenceKey.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); @@ -565,26 +564,20 @@ export class WebStorageSharedClientState implements SharedClientState { this.localClientStorageKey = this.toWebStorageClientStateKey( this.localClientId ); - this.sequenceNumberKey = `${SEQUENCE_NUMBER_KEY_PREFIX}_${ - this.escapedPersistenceKey - }`; + this.sequenceNumberKey = `${SEQUENCE_NUMBER_KEY_PREFIX}_${persistenceKey}`; this.activeClients[this.localClientId] = new LocalClientState(); this.clientStateKeyRe = new RegExp( - `^${CLIENT_STATE_KEY_PREFIX}_${this.escapedPersistenceKey}_([^_]*)$` + `^${CLIENT_STATE_KEY_PREFIX}_${escapedPersistenceKey}_([^_]*)$` ); this.mutationBatchKeyRe = new RegExp( - `^${MUTATION_BATCH_KEY_PREFIX}_${ - this.escapedPersistenceKey - }_(\\d+)(?:_(.*))?$` + `^${MUTATION_BATCH_KEY_PREFIX}_${escapedPersistenceKey}_(\\d+)(?:_(.*))?$` ); this.queryTargetKeyRe = new RegExp( - `^${QUERY_TARGET_KEY_PREFIX}_${this.escapedPersistenceKey}_(\\d+)$` + `^${QUERY_TARGET_KEY_PREFIX}_${escapedPersistenceKey}_(\\d+)$` ); - this.onlineStateKey = `${ONLINE_STATE_KEY_PREFIX}_${ - this.escapedPersistenceKey - }`; + this.onlineStateKey = `${ONLINE_STATE_KEY_PREFIX}_${persistenceKey}`; // Rather than adding the storage observer during start(), we add the // storage observer during initialization. This ensures that we collect @@ -926,22 +919,18 @@ export class WebStorageSharedClientState implements SharedClientState { `Client key cannot contain '_', but was '${clientId}'` ); - return `${CLIENT_STATE_KEY_PREFIX}_${ - this.escapedPersistenceKey - }_${clientId}`; + return `${CLIENT_STATE_KEY_PREFIX}_${this.persistenceKey}_${clientId}`; } /** Assembles the key for a query state in WebStorage */ private toWebStorageQueryTargetMetadataKey(targetId: TargetId): string { - return `${QUERY_TARGET_KEY_PREFIX}_${ - this.escapedPersistenceKey - }_${targetId}`; + return `${QUERY_TARGET_KEY_PREFIX}_${this.persistenceKey}_${targetId}`; } /** Assembles the key for a mutation batch in WebStorage */ private toWebStorageMutationBatchKey(batchId: BatchId): string { let mutationKey = `${MUTATION_BATCH_KEY_PREFIX}_${ - this.escapedPersistenceKey + this.persistenceKey }_${batchId}`; if (this.currentUser.isAuthenticated()) { diff --git a/packages/firestore/test/unit/local/persistence_test_helpers.ts b/packages/firestore/test/unit/local/persistence_test_helpers.ts index 6a0eccbb4ac..a52d7105720 100644 --- a/packages/firestore/test/unit/local/persistence_test_helpers.ts +++ b/packages/firestore/test/unit/local/persistence_test_helpers.ts @@ -56,7 +56,7 @@ export const INDEXEDDB_TEST_DATABASE_ID = new DatabaseId('test-project'); /** The DatabaseInfo used by most tests that access IndexedDb. */ const INDEXEDDB_TEST_DATABASE_INFO = new DatabaseInfo( INDEXEDDB_TEST_DATABASE_ID, - 'PersistenceTestHelpers', + '[PersistenceTestHelpers]', 'host', /*ssl=*/ false );