Skip to content

Add a persistence manager class #2925

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 26 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
f6d83b5
Add persistence layer: index db, in memory, and browser{local, session}
sam-gc Apr 14, 2020
efab6d0
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 14, 2020
b561823
Add indexed_db isAvailable test
sam-gc Apr 15, 2020
67e46a9
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 15, 2020
7cc84c1
Add persistence layer: index db, in memory, and browser{local, session}
sam-gc Apr 14, 2020
c9bbfe8
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 14, 2020
67446ea
[AUTOMATED]: License Headers
sam-gc Apr 14, 2020
afdce7e
Add persistence manager class
sam-gc Apr 16, 2020
ef91d2b
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 16, 2020
0d8659d
[AUTOMATED]: License Headers
sam-gc Apr 16, 2020
a15679c
Formatting
sam-gc Apr 16, 2020
4254fac
Formatting
sam-gc Apr 16, 2020
2711c51
Address review comments
sam-gc Apr 16, 2020
ed0ff67
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 16, 2020
a1cbef8
Add util/assert
sam-gc Apr 16, 2020
f1cfec2
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 16, 2020
eebc885
Update assertType to allow multiple types
sam-gc Apr 17, 2020
1d93a6d
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 17, 2020
a6338ec
UserImpl toPlainObject
sam-gc Apr 17, 2020
ede366c
Move instantiation into persistence manager, add a toPlainObject as well
sam-gc Apr 17, 2020
8ccb75e
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 17, 2020
1fce8ba
Move CONST_ to _CONST
sam-gc Apr 17, 2020
df6c1d0
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 17, 2020
cd255da
PR feedback
sam-gc Apr 17, 2020
ea70a8a
[AUTOMATED]: Prettier Code Styling
sam-gc Apr 17, 2020
2749d1e
Addressing PR feedback
sam-gc Apr 17, 2020
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
28 changes: 12 additions & 16 deletions packages-exp/auth-exp/src/core/persistence/browser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* limitations under the License.
*/

import * as sinon from 'sinon';
import { PersistenceType } from '.';
import { expect } from 'chai';
import { browserLocalPersistence, browserSessionPersistence } from './browser';
import { User } from '../../model/user';
import * as sinon from 'sinon';

import { testUser } from '../../../test/mock_auth';
import { PersistedBlob, PersistenceType } from './';
import { browserLocalPersistence, browserSessionPersistence } from './browser';

describe('core/persistence/browser', () => {
beforeEach(() => {
Expand All @@ -44,16 +44,14 @@ describe('core/persistence/browser', () => {
expect(await persistence.get(key)).to.be.null;
});

it('should call instantiator function if provided', async () => {
it('should return persistedblob from user', async () => {
const key = 'my-super-special-user';
const value = testUser('some-uid');

expect(await persistence.get(key)).to.be.null;
await persistence.set(key, value);
const out = await persistence.get<User>(key, blob =>
testUser(`test-${blob.uid}`)
);
expect(out?.uid).to.eql('test-some-uid');
await persistence.set(key, value.toPlainObject());
const out = await persistence.get<PersistedBlob>(key);
expect(out!['uid']).to.eql(value.uid);
await persistence.remove(key);
expect(await persistence.get(key)).to.be.null;
});
Expand Down Expand Up @@ -89,16 +87,14 @@ describe('core/persistence/browser', () => {
expect(await persistence.get(key)).to.be.null;
});

it('should call instantiator function if provided', async () => {
it('should emit blobified persisted user', async () => {
const key = 'my-super-special-user';
const value = testUser('some-uid');

expect(await persistence.get(key)).to.be.null;
await persistence.set(key, value);
const out = await persistence.get<User>(key, blob =>
testUser(`test-${blob.uid}`)
);
expect(out?.uid).to.eql('test-some-uid');
await persistence.set(key, value.toPlainObject());
const out = await persistence.get<PersistedBlob>(key);
expect(out!['uid']).to.eql(value.uid);
await persistence.remove(key);
expect(await persistence.get(key)).to.be.null;
});
Expand Down
21 changes: 6 additions & 15 deletions packages-exp/auth-exp/src/core/persistence/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,9 @@
* limitations under the License.
*/

import {
Persistence,
PersistenceType,
PersistenceValue,
Instantiator
} from '.';
import { Persistence, PersistenceType, PersistenceValue } from './';

const STORAGE_AVAILABLE_KEY_ = '__sak';
const _STORAGE_AVAILABLE_KEY = '__sak';

class BrowserPersistence implements Persistence {
type: PersistenceType = PersistenceType.LOCAL;
Expand All @@ -34,8 +29,8 @@ class BrowserPersistence implements Persistence {
if (!this.storage) {
return false;
}
this.storage.setItem(STORAGE_AVAILABLE_KEY_, '1');
this.storage.removeItem(STORAGE_AVAILABLE_KEY_);
this.storage.setItem(_STORAGE_AVAILABLE_KEY, '1');
this.storage.removeItem(_STORAGE_AVAILABLE_KEY);
return true;
} catch {
return false;
Expand All @@ -46,13 +41,9 @@ class BrowserPersistence implements Persistence {
this.storage.setItem(key, JSON.stringify(value));
}

async get<T extends PersistenceValue>(
key: string,
instantiator?: Instantiator<T>
): Promise<T | null> {
async get<T extends PersistenceValue>(key: string): Promise<T | null> {
const json = this.storage.getItem(key);
const obj = json ? JSON.parse(json) : null;
return instantiator && obj ? instantiator(obj) : obj;
return json ? JSON.parse(json) : null;
}

async remove(key: string): Promise<void> {
Expand Down
10 changes: 5 additions & 5 deletions packages-exp/auth-exp/src/core/persistence/in_memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
* limitations under the License.
*/

import { inMemoryPersistence as persistence } from './in_memory';
import { PersistenceType } from '.';
import { expect } from 'chai';
import { User } from '../../model/user';

import { testUser } from '../../../test/mock_auth';
import { PersistenceType } from './';
import { inMemoryPersistence as persistence } from './in_memory';

describe('core/persistence/in_memory', () => {
it('should work with persistence type', async () => {
Expand All @@ -38,8 +38,8 @@ describe('core/persistence/in_memory', () => {
const value = testUser('uid');

expect(await persistence.get(key)).to.be.null;
await persistence.set(key, value);
expect(await persistence.get<User>(key)).to.eql(value);
await persistence.set(key, value.toPlainObject());
expect(await persistence.get(key)).to.eql(value.toPlainObject());
expect(await persistence.get('other-key')).to.be.null;
await persistence.remove(key);
expect(await persistence.get(key)).to.be.null;
Expand Down
15 changes: 7 additions & 8 deletions packages-exp/auth-exp/src/core/persistence/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,26 @@
* limitations under the License.
*/

import { User } from '../../model/user';

export enum PersistenceType {
SESSION = 'SESSION',
LOCAL = 'LOCAL',
NONE = 'NONE'
}

export interface PersistedBlob {
[key: string]: unknown;
}

export interface Instantiator<T> {
(blob: { [key: string]: unknown }): T;
(blob: PersistedBlob): T;
}

export type PersistenceValue = PersistenceType | User;
export type PersistenceValue = PersistedBlob | string;

export interface Persistence {
type: PersistenceType;
isAvailable(): Promise<boolean>;
set(key: string, value: PersistenceValue): Promise<void>;
get<T extends PersistenceValue>(
key: string,
instantiator?: Instantiator<T>
): Promise<T | null>;
get<T extends PersistenceValue>(key: string): Promise<T | null>;
remove(key: string): Promise<void>;
}
18 changes: 8 additions & 10 deletions packages-exp/auth-exp/src/core/persistence/indexed_db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
* limitations under the License.
*/

import * as sinon from 'sinon';
import { PersistenceType } from '.';
import { expect } from 'chai';
import { indexedDBLocalPersistence as persistence } from './indexed_db';
import { User } from '../../model/user';
import * as sinon from 'sinon';

import { testUser } from '../../../test/mock_auth';
import { PersistenceType } from './';
import { indexedDBLocalPersistence as persistence } from './indexed_db';

describe('core/persistence/indexed_db', () => {
afterEach(sinon.restore);
Expand All @@ -36,16 +36,14 @@ describe('core/persistence/indexed_db', () => {
expect(await persistence.get(key)).to.be.null;
});

it('should call instantiator function if provided', async () => {
it('should return blobified user value', async () => {
const key = 'my-super-special-user';
const value = testUser('some-uid');

expect(await persistence.get(key)).to.be.null;
await persistence.set(key, value);
const out = await persistence.get<User>(key, blob =>
testUser(`test-${blob.uid}`)
);
expect(out?.uid).to.eql('test-some-uid');
await persistence.set(key, value.toPlainObject());
const out = await persistence.get(key);
expect(out).to.eql(value.toPlainObject());
await persistence.remove(key);
expect(await persistence.get(key)).to.be.null;
});
Expand Down
22 changes: 8 additions & 14 deletions packages-exp/auth-exp/src/core/persistence/indexed_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
*/

import {
PersistedBlob,
Persistence,
PersistenceType,
PersistenceValue,
Instantiator
} from '.';
PersistenceValue
} from './';

const STORAGE_AVAILABLE_KEY_ = '__sak';
const _STORAGE_AVAILABLE_KEY = '__sak';

export const DB_NAME = 'firebaseLocalStorageDb';
const DB_VERSION = 1;
const DB_OBJECTSTORE_NAME = 'firebaseLocalStorage';
const DB_DATA_KEYPATH = 'fbase_key';

type DBValue = { [key: string]: unknown } | string;
type DBValue = PersistedBlob | string;

interface DBObject {
[DB_DATA_KEYPATH]: string;
Expand Down Expand Up @@ -154,8 +154,8 @@ class IndexedDBLocalPersistence implements Persistence {
return false;
}
const db = await openDatabase();
await putObject(db, STORAGE_AVAILABLE_KEY_, '1');
await deleteObject(db, STORAGE_AVAILABLE_KEY_);
await putObject(db, _STORAGE_AVAILABLE_KEY, '1');
await deleteObject(db, _STORAGE_AVAILABLE_KEY);
return true;
} catch {}
return false;
Expand All @@ -166,15 +166,9 @@ class IndexedDBLocalPersistence implements Persistence {
return putObject(db, key, value);
}

async get<T extends PersistenceValue>(
key: string,
instantiator?: Instantiator<T>
): Promise<T | null> {
async get<T extends PersistenceValue>(key: string): Promise<T | null> {
const db = await this.initialize();
const obj = await getObject(db, key);
if (instantiator && obj && typeof obj !== 'string') {
return instantiator(obj);
}
return obj as T;
}

Expand Down
Loading