Skip to content

Commit

Permalink
address nits and add logger
Browse files Browse the repository at this point in the history
  • Loading branch information
cwomack committed Oct 30, 2024
1 parent 3e3c873 commit 1c75dbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
6 changes: 3 additions & 3 deletions packages/core/__tests__/storage/DefaultStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DefaultStorage } from '../../src/storage/DefaultStorage';
import { InMemoryStorage } from '../../src/storage/InMemoryStorage';

const key = 'k';
const value = 'value';
Expand Down Expand Up @@ -45,14 +46,13 @@ describe('DefaultStorage', () => {
},
});

console.error = jest.fn(); // Mock console.error
jest.mock('../../src/Logger/ConsoleLogger');

// Create a new DefaultStorage instance to trigger the fallback
const fallbackStorage = new DefaultStorage();

// Verify that the storage still works as expected
await fallbackStorage.setItem(key, value);
expect(await fallbackStorage.getItem(key)).toEqual(value);
expect(fallbackStorage instanceof InMemoryStorage).toEqual(true);

// Verify that the error was logged
expect(console.error).toHaveBeenCalledWith(
Expand Down
4 changes: 2 additions & 2 deletions packages/core/__tests__/storage/SessionStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { InMemoryStorage } from '../../src/storage/InMemoryStorage';
import { SessionStorage } from '../../src/storage/SessionStorage';

const key = 'k';
Expand Down Expand Up @@ -53,8 +54,7 @@ describe('SessionStorage', () => {
const fallbackStorage = new SessionStorage();

// Verify that the storage still works as expected
await fallbackStorage.setItem(key, value);
expect(await fallbackStorage.getItem(key)).toEqual(value);
expect(fallbackStorage).toBeInstanceOf(InMemoryStorage);

// Verify that the error was logged
expect(console.error).toHaveBeenCalledWith(
Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/storage/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { ConsoleLogger } from '../Logger';

import { InMemoryStorage } from './InMemoryStorage';

/**
* @internal
* @returns Either a reference to window.localStorage or an in-memory storage as fallback
*/

const logger = new ConsoleLogger('CoreStorageUtils');

export const getLocalStorageWithFallback = (): Storage => {
try {
// Attempt to use localStorage directly
Expand All @@ -30,13 +35,17 @@ export const getSessionStorageWithFallback = (): Storage => {
try {
// Attempt to use sessionStorage directly
if (typeof window !== 'undefined' && window.sessionStorage) {
// Verify we can actually use it by testing access
window.sessionStorage.getItem('test');

return window.sessionStorage;
}

throw new Error('sessionStorage is not defined');
} catch (e) {
// Handle any errors related to sessionStorage access
console.error('SessionStorage access failed:', e);
}
logger.error('SessionStorage access failed:', e);

// Return in-memory storage as a fallback if sessionStorage is not accessible
return new InMemoryStorage();
return new InMemoryStorage();
}
};

0 comments on commit 1c75dbe

Please sign in to comment.