Skip to content

Commit

Permalink
Revert "fix(core): Check for storage initialization errors (#13938)"
Browse files Browse the repository at this point in the history
This reverts commit 0f50917.
  • Loading branch information
yuhengshs committed Nov 13, 2024
1 parent 7459b12 commit f3f8a3f
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 86 deletions.
6 changes: 3 additions & 3 deletions packages/aws-amplify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
"name": "[Analytics] record (Pinpoint)",
"path": "./dist/esm/analytics/index.mjs",
"import": "{ record }",
"limit": "17.5 kB"
"limit": "17.41 kB"
},
{
"name": "[Analytics] record (Kinesis)",
Expand All @@ -317,7 +317,7 @@
"name": "[Analytics] identifyUser (Pinpoint)",
"path": "./dist/esm/analytics/index.mjs",
"import": "{ identifyUser }",
"limit": "15.95 kB"
"limit": "15.91 kB"
},
{
"name": "[Analytics] enable",
Expand Down Expand Up @@ -497,7 +497,7 @@
"name": "[Storage] uploadData (S3)",
"path": "./dist/esm/storage/index.mjs",
"import": "{ uploadData }",
"limit": "20.15 kB"
"limit": "20.08 kB"
}
]
}
22 changes: 0 additions & 22 deletions packages/core/__tests__/storage/DefaultStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { DefaultStorage } from '../../src/storage/DefaultStorage';
import { InMemoryStorage } from '../../src/storage/InMemoryStorage';

const key = 'k';
const value = 'value';
Expand Down Expand Up @@ -36,25 +35,4 @@ describe('DefaultStorage', () => {
await defaultStorage.clear();
expect(defaultStorage.getItem(key)).resolves.toBeNull();
});

it('should fall back to alternative storage when localStorage is not accessible', async () => {
// Mock window.localStorage to throw an error
const originalLocalStorage = window.localStorage;

Object.defineProperty(window, 'localStorage', {
value: undefined,
writable: true,
});

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

// Verify that the storage still works as expected
expect(fallbackStorage.storage instanceof InMemoryStorage).toEqual(true);

// Restore the original localStorage
Object.defineProperty(window, 'localStorage', {
value: originalLocalStorage,
});
});
});
24 changes: 1 addition & 23 deletions packages/core/__tests__/storage/SessionStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { InMemoryStorage } from '../../src/storage/InMemoryStorage';
import { SessionStorage } from '../../src/storage/SessionStorage';

const key = 'k';
const value = 'value';

describe('SessionStorage', () => {
describe('sessionStorage', () => {
let sessionStorage: SessionStorage;

beforeEach(() => {
Expand Down Expand Up @@ -38,25 +37,4 @@ describe('SessionStorage', () => {
await sessionStorage.clear();
expect(await sessionStorage.getItem(key)).toBeNull();
});

it('should fall back to alternative storage when sessionStorage is not accessible', async () => {
// Mock window.sessionStorage to throw an error
const originalSessionStorage = window.sessionStorage;

Object.defineProperty(window, 'sessionStorage', {
value: undefined,
writable: true,
});

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

// Verify that the storage still works as expected
expect(fallbackStorage.storage instanceof InMemoryStorage).toEqual(true);

// Restore the original sessionStorage
Object.defineProperty(window, 'sessionStorage', {
value: originalSessionStorage,
});
});
});
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"name": "Cache (default browser storage)",
"path": "./dist/esm/index.mjs",
"import": "{ Cache }",
"limit": "3.4 kB"
"limit": "3.3 kB"
}
],
"exports": {
Expand Down
45 changes: 8 additions & 37 deletions packages/core/src/storage/utils.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,22 @@
// 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
if (typeof window !== 'undefined' && window.localStorage) {
return window.localStorage;
}
} catch (e) {
// Handle any errors related to localStorage access
logger.error('LocalStorage access failed:', e);
}

// Return in-memory storage as a fallback if localStorage is not accessible
return new InMemoryStorage();
};
export const getLocalStorageWithFallback = (): Storage =>
typeof window !== 'undefined' && window.localStorage
? window.localStorage
: new InMemoryStorage();

/**
* @internal
* @returns Either a reference to window.sessionStorage or an in-memory storage as fallback
*/
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
logger.error('SessionStorage access failed:', e);

return new InMemoryStorage();
}
};
export const getSessionStorageWithFallback = (): Storage =>
typeof window !== 'undefined' && window.sessionStorage
? window.sessionStorage
: new InMemoryStorage();

0 comments on commit f3f8a3f

Please # to comment.