Skip to content

Don't use the escaped persistence keys when we write to Local Storage #1281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 10 additions & 21 deletions packages/firestore/src/local/shared_client_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
Expand All @@ -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,
'\\$&'
);
Expand All @@ -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
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down