UNPKG

@aws-amplify/core

Version:
1 lines 2.61 kB
{"version":3,"file":"utils.mjs","sources":["../../../src/storage/utils.ts"],"sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport { ConsoleLogger } from '../Logger';\nimport { InMemoryStorage } from './InMemoryStorage';\n/**\n * @internal\n * @returns Either a reference to window.localStorage or an in-memory storage as fallback\n */\nconst logger = new ConsoleLogger('CoreStorageUtils');\nexport const getLocalStorageWithFallback = () => {\n try {\n // Attempt to use localStorage directly\n if (typeof window !== 'undefined' && window.localStorage) {\n return window.localStorage;\n }\n }\n catch (e) {\n // Handle any errors related to localStorage access\n logger.info('localStorage not found. InMemoryStorage is used as a fallback.');\n }\n // Return in-memory storage as a fallback if localStorage is not accessible\n return new InMemoryStorage();\n};\n/**\n * @internal\n * @returns Either a reference to window.sessionStorage or an in-memory storage as fallback\n */\nexport const getSessionStorageWithFallback = () => {\n try {\n // Attempt to use sessionStorage directly\n if (typeof window !== 'undefined' && window.sessionStorage) {\n // Verify we can actually use it by testing access\n window.sessionStorage.getItem('test');\n return window.sessionStorage;\n }\n throw new Error('sessionStorage is not defined');\n }\n catch (e) {\n // Handle any errors related to sessionStorage access\n logger.info('sessionStorage not found. InMemoryStorage is used as a fallback.');\n return new InMemoryStorage();\n }\n};\n"],"names":[],"mappings":";;;AAAA;AACA;AAGA;AACA;AACA;AACA;AACA,MAAM,MAAM,GAAG,IAAI,aAAa,CAAC,kBAAkB,CAAC,CAAC;AACzC,MAAC,2BAA2B,GAAG,MAAM;AACjD,IAAI,IAAI;AACR;AACA,QAAQ,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,YAAY,EAAE;AAClE,YAAY,OAAO,MAAM,CAAC,YAAY,CAAC;AACvC,SAAS;AACT,KAAK;AACL,IAAI,OAAO,CAAC,EAAE;AACd;AACA,QAAQ,MAAM,CAAC,IAAI,CAAC,gEAAgE,CAAC,CAAC;AACtF,KAAK;AACL;AACA,IAAI,OAAO,IAAI,eAAe,EAAE,CAAC;AACjC,EAAE;AACF;AACA;AACA;AACA;AACY,MAAC,6BAA6B,GAAG,MAAM;AACnD,IAAI,IAAI;AACR;AACA,QAAQ,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,MAAM,CAAC,cAAc,EAAE;AACpE;AACA,YAAY,MAAM,CAAC,cAAc,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;AAClD,YAAY,OAAO,MAAM,CAAC,cAAc,CAAC;AACzC,SAAS;AACT,QAAQ,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;AACzD,KAAK;AACL,IAAI,OAAO,CAAC,EAAE;AACd;AACA,QAAQ,MAAM,CAAC,IAAI,CAAC,kEAAkE,CAAC,CAAC;AACxF,QAAQ,OAAO,IAAI,eAAe,EAAE,CAAC;AACrC,KAAK;AACL;;;;"}