-
Notifications
You must be signed in to change notification settings - Fork 4
HawkStorage abstraction added #164
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export type { HawkStorage } from './storages/hawk-storage'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * Abstract key–value storage contract used by Hawk internals to persist data across sessions. | ||
| */ | ||
| export interface HawkStorage { | ||
| /** | ||
| * Returns the value associated with the given key, or `null` if none exists or on error. | ||
| * | ||
| * @param key - Storage key to look up. | ||
| */ | ||
| getItem(key: string): string | null; | ||
|
|
||
| /** | ||
| * Persists a value under the given key. Must not throw on failure. | ||
| * | ||
| * @param key - Storage key. | ||
| * @param value - Value to store. | ||
| */ | ||
| setItem(key: string, value: string): void; | ||
|
|
||
| /** | ||
| * Removes the entry for the given key. Must not throw on failure. | ||
| * | ||
| * @param key - Storage key to remove. | ||
| */ | ||
| removeItem(key: string): void; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type { HawkStorage } from '@hawk.so/core'; | ||
| import log from '../utils/log.ts'; | ||
|
|
||
| /** | ||
| * {@link HawkStorage} implementation backed by the browser's {@linkcode localStorage}. | ||
| */ | ||
| export class HawkLocalStorage implements HawkStorage { | ||
| /** @inheritDoc */ | ||
| public getItem(key: string): string | null { | ||
| try { | ||
| return localStorage.getItem(key); | ||
| } catch (e) { | ||
| log('HawkLocalStorage: getItem failed', 'error', e); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** @inheritDoc */ | ||
| public setItem(key: string, value: string): void { | ||
| try { | ||
| localStorage.setItem(key, value); | ||
| } catch (e) { | ||
| log('HawkLocalStorage: setItem failed', 'error', e); | ||
| } | ||
| } | ||
|
|
||
| /** @inheritDoc */ | ||
| public removeItem(key: string): void { | ||
| try { | ||
| localStorage.removeItem(key); | ||
| } catch (e) { | ||
| log('HawkLocalStorage: removeItem failed', 'error', e); | ||
| } | ||
| } | ||
| } | ||
75 changes: 75 additions & 0 deletions
75
packages/javascript/tests/storages/hawk-local-storage.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import type { Mock } from 'vitest'; | ||
| import { beforeEach, afterEach, describe, it, expect, vi } from 'vitest'; | ||
| import { HawkLocalStorage } from '../../src/storages/hawk-local-storage'; | ||
|
|
||
| describe('HawkLocalStorage', () => { | ||
| let getItemSpy: Mock<(key: string) => string | null>; | ||
| let setItemSpy: Mock<(key: string, value: string) => void>; | ||
| let removeItemSpy: Mock<(key: string) => void>; | ||
| let storage: HawkLocalStorage; | ||
|
|
||
| beforeEach(() => { | ||
| localStorage.clear(); | ||
| storage = new HawkLocalStorage(); | ||
| getItemSpy = vi.spyOn(Storage.prototype, 'getItem'); | ||
| setItemSpy = vi.spyOn(Storage.prototype, 'setItem'); | ||
| removeItemSpy = vi.spyOn(Storage.prototype, 'removeItem'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| vi.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('should return null when key does not exist', () => { | ||
| expect(storage.getItem('foo')).toBeNull(); | ||
| expect(getItemSpy).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('should return value when key exists in storage', () => { | ||
| localStorage.setItem('foo', 'bar'); | ||
|
|
||
| expect(storage.getItem('foo')).toEqual('bar'); | ||
| expect(getItemSpy).toHaveBeenCalledWith('foo'); | ||
| }); | ||
|
|
||
| it('should persist item via setItem()', () => { | ||
| storage.setItem('foo', 'bar'); | ||
|
|
||
| expect(setItemSpy).toHaveBeenCalledWith('foo', 'bar'); | ||
| expect(localStorage.getItem('foo')).toEqual('bar'); | ||
| }); | ||
|
|
||
| it('should remove item via removeItem()', () => { | ||
| localStorage.setItem('foo', 'bar'); | ||
| storage.removeItem('foo'); | ||
|
|
||
| expect(removeItemSpy).toHaveBeenCalledWith('foo'); | ||
| expect(localStorage.getItem('foo')).toBeNull(); | ||
| }); | ||
|
|
||
| it('should not affect other keys when removing', () => { | ||
| localStorage.setItem('foo', 'bar'); | ||
| storage.removeItem('baz'); | ||
|
|
||
| expect(removeItemSpy).toHaveBeenCalledWith('baz'); | ||
| expect(localStorage.getItem('foo')).toEqual('bar'); | ||
| }); | ||
|
|
||
| it('should return null when getItem throws', () => { | ||
| getItemSpy.mockImplementation(() => { throw new Error('SecurityError'); }); | ||
|
|
||
| expect(storage.getItem('foo')).toBeNull(); | ||
| }); | ||
|
|
||
| it('should not throw when setItem throws', () => { | ||
| setItemSpy.mockImplementation(() => { throw new Error('QuotaExceededError'); }); | ||
|
|
||
| expect(() => storage.setItem('foo', 'bar')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('should not throw when removeItem throws', () => { | ||
| removeItemSpy.mockImplementation(() => { throw new Error('SecurityError'); }); | ||
|
|
||
| expect(() => storage.removeItem('foo')).not.toThrow(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.