@aws-amplify/core 5.8.2 → 6.0.1-console-preview.f63250a.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/lib/Amplify.d.ts +18 -17
- package/lib/Amplify.js +9 -0
- package/lib/AwsClients/CognitoIdentity/base.d.ts +3 -10
- package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
- package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
- package/lib/AwsClients/CognitoIdentity/getId.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/base.d.ts +2 -10
- package/lib/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/getInAppMessages.js +1 -1
- package/lib/AwsClients/Pinpoint/putEvents.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/putEvents.js +6 -0
- package/lib/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
- package/lib/AwsClients/Pinpoint/updateEndpoint.js +2 -2
- package/lib/Cache/AsyncStorageCache.d.ts +154 -0
- package/lib/Cache/AsyncStorageCache.js +744 -0
- package/lib/Cache/BrowserStorageCache.d.ts +166 -0
- package/lib/Cache/BrowserStorageCache.js +469 -0
- package/lib/Cache/InMemoryCache.d.ts +129 -0
- package/lib/Cache/InMemoryCache.js +349 -0
- package/lib/Cache/StorageCache.d.ts +39 -0
- package/lib/Cache/StorageCache.js +121 -0
- package/lib/Cache/Utils/CacheList.d.ts +89 -0
- package/lib/Cache/Utils/CacheList.js +196 -0
- package/lib/Cache/Utils/CacheUtils.d.ts +25 -0
- package/lib/Cache/Utils/CacheUtils.js +88 -0
- package/lib/Cache/Utils/index.d.ts +2 -0
- package/lib/Cache/Utils/index.js +16 -0
- package/lib/Cache/reactnative.d.ts +3 -0
- package/lib/Cache/reactnative.js +8 -0
- package/lib/Cache/types/Cache.d.ts +55 -0
- package/lib/Cache/types/Cache.js +4 -0
- package/lib/Cache/types/index.d.ts +1 -0
- package/lib/Cache/types/index.js +19 -0
- package/lib/ClientDevice/android.js +3 -2
- package/lib/ClientDevice/ios.d.ts +2 -2
- package/lib/ClientDevice/ios.js +3 -2
- package/lib/ClientDevice/reactnative.js +3 -2
- package/lib/Credentials.d.ts +1 -1
- package/lib/Credentials.js +2 -1
- package/lib/Errors.d.ts +19 -0
- package/lib/Errors.js +50 -1
- package/lib/Hub.js +1 -0
- package/lib/I18n/I18n.d.ts +29 -9
- package/lib/I18n/I18n.js +30 -5
- package/lib/I18n/index.d.ts +18 -5
- package/lib/I18n/index.js +35 -5
- package/lib/I18n/types.d.ts +4 -1
- package/lib/I18n/types.js +3 -0
- package/lib/JS.d.ts +6 -6
- package/lib/Logger/ConsoleLogger.d.ts +11 -11
- package/lib/OAuthHelper/GoogleOAuth.js +2 -1
- package/lib/Platform/detectFramework.js +2 -1
- package/lib/Platform/detection/Next.d.ts +1 -1
- package/lib/Platform/detection/React.js +1 -0
- package/lib/Platform/version.d.ts +1 -1
- package/lib/Platform/version.js +1 -1
- package/lib/Providers/AWSCloudWatchProvider.d.ts +2 -2
- package/lib/Providers/AWSCloudWatchProvider.js +35 -12
- package/lib/RNComponents/index.d.ts +1 -1
- package/lib/RNComponents/reactnative.js +3 -2
- package/lib/ServiceWorker/ServiceWorker.d.ts +4 -4
- package/lib/ServiceWorker/ServiceWorker.js +37 -11
- package/lib/Signer.d.ts +14 -1
- package/lib/StorageHelper/cookieStorage.d.ts +13 -0
- package/lib/StorageHelper/cookieStorage.js +167 -0
- package/lib/StorageHelper/inMemoryStorage.d.ts +10 -0
- package/lib/StorageHelper/inMemoryStorage.js +82 -0
- package/lib/StorageHelper/index.d.ts +7 -3
- package/lib/StorageHelper/index.js +9 -1
- package/lib/StorageHelper/localStorage.d.ts +32 -0
- package/lib/StorageHelper/localStorage.js +114 -0
- package/lib/StorageHelper/reactnative.d.ts +71 -1
- package/lib/StorageHelper/reactnative.js +154 -10
- package/lib/StorageHelper/sessionStorage.d.ts +32 -0
- package/lib/StorageHelper/sessionStorage.js +114 -0
- package/lib/UniversalStorage/index.d.ts +2 -2
- package/lib/Util/BackgroundProcessManager.d.ts +2 -2
- package/lib/Util/BackgroundProcessManager.js +1 -0
- package/lib/Util/DateUtils.d.ts +12 -1
- package/lib/Util/Reachability.js +7 -5
- package/lib/Util/Reachability.native.d.ts +3 -2
- package/lib/Util/StringUtils.js +3 -10
- package/lib/Util/errors/AssertError.d.ts +2 -0
- package/lib/Util/errors/AssertError.js +11 -0
- package/lib/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
- package/lib/clients/middleware/retry/defaultRetryDecider.js +17 -13
- package/lib/clients/middleware/retry/isClockSkewError.js +1 -1
- package/lib/clients/middleware/retry/middleware.d.ts +1 -1
- package/lib/clients/middleware/retry/middleware.js +13 -13
- package/lib/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
- package/lib/clients/serde/responseInfo.js +1 -1
- package/lib/clients/types/aws.d.ts +4 -0
- package/lib/clients/types/index.d.ts +1 -1
- package/lib/constants.d.ts +7 -0
- package/lib/constants.js +9 -1
- package/lib/index.d.ts +18 -4
- package/lib/index.js +32 -5
- package/lib/parseAWSExports.d.ts +1 -1
- package/lib/singleton/Auth/index.d.ts +30 -0
- package/lib/singleton/Auth/index.js +133 -0
- package/lib/singleton/Auth/types.d.ts +95 -0
- package/lib/singleton/Auth/types.js +6 -0
- package/lib/singleton/Auth/utils/index.d.ts +5 -0
- package/lib/singleton/Auth/utils/index.js +52 -0
- package/lib/singleton/Storage/types.d.ts +14 -0
- package/lib/singleton/Storage/types.js +4 -0
- package/lib/singleton/index.d.ts +46 -0
- package/lib/singleton/index.js +147 -0
- package/lib/singleton/types.d.ts +21 -0
- package/lib/singleton/types.js +4 -0
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/types/types.d.ts +31 -1
- package/lib/types/types.js +2 -0
- package/lib-esm/Amplify.d.ts +18 -17
- package/lib-esm/Amplify.js +9 -0
- package/lib-esm/AwsClients/CognitoIdentity/base.d.ts +3 -10
- package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
- package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
- package/lib-esm/AwsClients/CognitoIdentity/getId.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/base.d.ts +2 -10
- package/lib-esm/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/getInAppMessages.js +1 -1
- package/lib-esm/AwsClients/Pinpoint/putEvents.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/putEvents.js +6 -0
- package/lib-esm/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
- package/lib-esm/AwsClients/Pinpoint/updateEndpoint.js +2 -2
- package/lib-esm/Cache/AsyncStorageCache.d.ts +154 -0
- package/lib-esm/Cache/AsyncStorageCache.js +737 -0
- package/lib-esm/Cache/BrowserStorageCache.d.ts +166 -0
- package/lib-esm/Cache/BrowserStorageCache.js +466 -0
- package/lib-esm/Cache/InMemoryCache.d.ts +129 -0
- package/lib-esm/Cache/InMemoryCache.js +346 -0
- package/lib-esm/Cache/StorageCache.d.ts +39 -0
- package/lib-esm/Cache/StorageCache.js +118 -0
- package/lib-esm/Cache/Utils/CacheList.d.ts +89 -0
- package/lib-esm/Cache/Utils/CacheList.js +194 -0
- package/lib-esm/Cache/Utils/CacheUtils.d.ts +25 -0
- package/lib-esm/Cache/Utils/CacheUtils.js +81 -0
- package/lib-esm/Cache/Utils/index.d.ts +2 -0
- package/lib-esm/Cache/Utils/index.js +4 -0
- package/lib-esm/Cache/reactnative.d.ts +3 -0
- package/lib-esm/Cache/reactnative.js +6 -0
- package/lib-esm/Cache/types/Cache.d.ts +55 -0
- package/lib-esm/Cache/types/Cache.js +3 -0
- package/lib-esm/Cache/types/index.d.ts +1 -0
- package/lib-esm/Cache/types/index.js +3 -0
- package/lib-esm/ClientDevice/android.js +1 -0
- package/lib-esm/ClientDevice/ios.d.ts +2 -2
- package/lib-esm/ClientDevice/ios.js +1 -0
- package/lib-esm/ClientDevice/reactnative.js +1 -0
- package/lib-esm/Credentials.d.ts +1 -1
- package/lib-esm/Credentials.js +2 -1
- package/lib-esm/Errors.d.ts +19 -0
- package/lib-esm/Errors.js +49 -0
- package/lib-esm/Hub.js +1 -0
- package/lib-esm/I18n/I18n.d.ts +29 -9
- package/lib-esm/I18n/I18n.js +30 -5
- package/lib-esm/I18n/index.d.ts +18 -5
- package/lib-esm/I18n/index.js +35 -5
- package/lib-esm/I18n/types.d.ts +4 -1
- package/lib-esm/I18n/types.js +3 -0
- package/lib-esm/JS.d.ts +6 -6
- package/lib-esm/Logger/ConsoleLogger.d.ts +11 -11
- package/lib-esm/OAuthHelper/GoogleOAuth.js +2 -1
- package/lib-esm/Platform/detectFramework.js +2 -1
- package/lib-esm/Platform/detection/Next.d.ts +1 -1
- package/lib-esm/Platform/detection/React.js +1 -0
- package/lib-esm/Platform/version.d.ts +1 -1
- package/lib-esm/Platform/version.js +1 -1
- package/lib-esm/Providers/AWSCloudWatchProvider.d.ts +2 -2
- package/lib-esm/Providers/AWSCloudWatchProvider.js +33 -10
- package/lib-esm/RNComponents/index.d.ts +1 -1
- package/lib-esm/RNComponents/reactnative.js +1 -0
- package/lib-esm/ServiceWorker/ServiceWorker.d.ts +4 -4
- package/lib-esm/ServiceWorker/ServiceWorker.js +37 -11
- package/lib-esm/Signer.d.ts +14 -1
- package/lib-esm/StorageHelper/cookieStorage.d.ts +13 -0
- package/lib-esm/StorageHelper/cookieStorage.js +141 -0
- package/lib-esm/StorageHelper/inMemoryStorage.d.ts +10 -0
- package/lib-esm/StorageHelper/inMemoryStorage.js +79 -0
- package/lib-esm/StorageHelper/index.d.ts +7 -3
- package/lib-esm/StorageHelper/index.js +4 -0
- package/lib-esm/StorageHelper/localStorage.d.ts +32 -0
- package/lib-esm/StorageHelper/localStorage.js +111 -0
- package/lib-esm/StorageHelper/reactnative.d.ts +71 -1
- package/lib-esm/StorageHelper/reactnative.js +153 -9
- package/lib-esm/StorageHelper/sessionStorage.d.ts +32 -0
- package/lib-esm/StorageHelper/sessionStorage.js +111 -0
- package/lib-esm/UniversalStorage/index.d.ts +2 -2
- package/lib-esm/Util/BackgroundProcessManager.d.ts +2 -2
- package/lib-esm/Util/BackgroundProcessManager.js +1 -0
- package/lib-esm/Util/DateUtils.d.ts +12 -1
- package/lib-esm/Util/Reachability.js +5 -3
- package/lib-esm/Util/Reachability.native.d.ts +3 -2
- package/lib-esm/Util/StringUtils.js +3 -10
- package/lib-esm/Util/errors/AssertError.d.ts +2 -0
- package/lib-esm/Util/errors/AssertError.js +7 -0
- package/lib-esm/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
- package/lib-esm/clients/middleware/retry/defaultRetryDecider.js +17 -13
- package/lib-esm/clients/middleware/retry/isClockSkewError.js +1 -1
- package/lib-esm/clients/middleware/retry/middleware.d.ts +1 -1
- package/lib-esm/clients/middleware/retry/middleware.js +13 -13
- package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
- package/lib-esm/clients/serde/responseInfo.js +1 -1
- package/lib-esm/clients/types/aws.d.ts +4 -0
- package/lib-esm/clients/types/index.d.ts +1 -1
- package/lib-esm/constants.d.ts +7 -0
- package/lib-esm/constants.js +8 -0
- package/lib-esm/index.d.ts +18 -4
- package/lib-esm/index.js +15 -3
- package/lib-esm/parseAWSExports.d.ts +1 -1
- package/lib-esm/singleton/Auth/index.d.ts +30 -0
- package/lib-esm/singleton/Auth/index.js +129 -0
- package/lib-esm/singleton/Auth/types.d.ts +95 -0
- package/lib-esm/singleton/Auth/types.js +5 -0
- package/lib-esm/singleton/Auth/utils/index.d.ts +5 -0
- package/lib-esm/singleton/Auth/utils/index.js +45 -0
- package/lib-esm/singleton/Storage/types.d.ts +14 -0
- package/lib-esm/singleton/Storage/types.js +3 -0
- package/lib-esm/singleton/index.d.ts +46 -0
- package/lib-esm/singleton/index.js +143 -0
- package/lib-esm/singleton/types.d.ts +21 -0
- package/lib-esm/singleton/types.js +3 -0
- package/lib-esm/tsconfig.tsbuildinfo +1 -1
- package/lib-esm/types/types.d.ts +31 -1
- package/lib-esm/types/types.js +2 -0
- package/package.json +38 -15
- package/src/Amplify.ts +9 -3
- package/src/AwsClients/CognitoIdentity/base.ts +2 -1
- package/src/AwsClients/CognitoIdentity/getCredentialsForIdentity.ts +13 -6
- package/src/AwsClients/Pinpoint/base.ts +6 -2
- package/src/AwsClients/Pinpoint/getInAppMessages.ts +1 -1
- package/src/AwsClients/Pinpoint/putEvents.ts +6 -0
- package/src/AwsClients/Pinpoint/updateEndpoint.ts +2 -2
- package/src/Cache/AsyncStorageCache.ts +500 -0
- package/src/Cache/BrowserStorageCache.ts +517 -0
- package/src/Cache/CHANGELOG.md +1227 -0
- package/src/Cache/InMemoryCache.ts +354 -0
- package/src/Cache/StorageCache.ts +157 -0
- package/src/Cache/Utils/CacheList.ts +198 -0
- package/src/Cache/Utils/CacheUtils.ts +92 -0
- package/src/Cache/Utils/index.ts +11 -0
- package/src/Cache/reactnative.ts +9 -0
- package/src/Cache/types/Cache.ts +75 -0
- package/src/Cache/types/index.ts +4 -0
- package/src/ClientDevice/android.ts +1 -1
- package/src/ClientDevice/ios.ts +2 -2
- package/src/ClientDevice/reactnative.ts +1 -1
- package/src/Credentials.ts +19 -17
- package/src/Errors.ts +41 -0
- package/src/Hub.ts +1 -0
- package/src/I18n/I18n.ts +40 -12
- package/src/I18n/index.ts +45 -12
- package/src/I18n/types.ts +3 -0
- package/src/JS.ts +24 -17
- package/src/Logger/ConsoleLogger.ts +10 -10
- package/src/OAuthHelper/FacebookOAuth.ts +2 -2
- package/src/OAuthHelper/GoogleOAuth.ts +9 -6
- package/src/Platform/detectFramework.ts +1 -1
- package/src/Platform/detection/React.ts +5 -3
- package/src/Platform/version.ts +1 -1
- package/src/Providers/AWSCloudWatchProvider.ts +39 -14
- package/src/RNComponents/index.ts +1 -1
- package/src/RNComponents/reactnative.ts +1 -1
- package/src/ServiceWorker/ServiceWorker.ts +58 -18
- package/src/Signer.ts +22 -3
- package/src/StorageHelper/cookieStorage.ts +99 -0
- package/src/StorageHelper/inMemoryStorage.ts +32 -0
- package/src/StorageHelper/index.ts +6 -1
- package/src/StorageHelper/localStorage.ts +60 -0
- package/src/StorageHelper/reactnative.ts +94 -14
- package/src/StorageHelper/sessionStorage.ts +60 -0
- package/src/Util/BackgroundProcessManager.ts +33 -9
- package/src/Util/DateUtils.ts +14 -5
- package/src/Util/Reachability.native.ts +3 -2
- package/src/Util/Reachability.ts +9 -7
- package/src/Util/Retry.ts +2 -1
- package/src/Util/StringUtils.ts +3 -10
- package/src/Util/errors/AssertError.ts +11 -0
- package/src/clients/handlers/fetch.ts +2 -2
- package/src/clients/middleware/retry/defaultRetryDecider.ts +12 -6
- package/src/clients/middleware/retry/isClockSkewError.ts +1 -1
- package/src/clients/middleware/retry/middleware.ts +9 -9
- package/src/clients/middleware/signing/middleware.ts +1 -1
- package/src/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.ts +1 -1
- package/src/clients/serde/responseInfo.ts +2 -1
- package/src/clients/types/aws.ts +10 -0
- package/src/clients/types/index.ts +1 -0
- package/src/constants.ts +16 -0
- package/src/global.d.ts +11 -0
- package/src/index.ts +66 -4
- package/src/parseAWSExports.ts +1 -1
- package/src/singleton/Auth/index.ts +113 -0
- package/src/singleton/Auth/types.ts +129 -0
- package/src/singleton/Auth/utils/index.ts +68 -0
- package/src/singleton/Storage/types.ts +21 -0
- package/src/singleton/index.ts +131 -0
- package/src/singleton/types.ts +48 -0
- package/src/types/types.ts +38 -1
|
@@ -0,0 +1,500 @@
|
|
|
1
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
5
|
+
import { ConsoleLogger as Logger } from '../Logger';
|
|
6
|
+
import { StorageCache } from './StorageCache';
|
|
7
|
+
import { defaultConfig, getCurrTime } from './Utils';
|
|
8
|
+
import { CacheConfig, CacheItem, CacheItemOptions, ICache } from './types';
|
|
9
|
+
import { STORAGE_CACHE_EXCEPTION } from '../constants';
|
|
10
|
+
import { asserts } from '../Util/errors/AssertError';
|
|
11
|
+
import { getCurrSizeKey } from './Utils/CacheUtils';
|
|
12
|
+
|
|
13
|
+
const logger = new Logger('AsyncStorageCache');
|
|
14
|
+
/*
|
|
15
|
+
* Customized cache which based on the AsyncStorage with LRU implemented
|
|
16
|
+
*/
|
|
17
|
+
export class AsyncStorageCache extends StorageCache implements ICache {
|
|
18
|
+
/**
|
|
19
|
+
* initialize the cache
|
|
20
|
+
*
|
|
21
|
+
* @param {Object} config - the configuration of the cache
|
|
22
|
+
*/
|
|
23
|
+
constructor(config?: CacheConfig) {
|
|
24
|
+
super(config);
|
|
25
|
+
|
|
26
|
+
this.getItem = this.getItem.bind(this);
|
|
27
|
+
this.setItem = this.setItem.bind(this);
|
|
28
|
+
this.removeItem = this.removeItem.bind(this);
|
|
29
|
+
|
|
30
|
+
logger.debug('Using AsyncStorageCache');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* decrease current size of the cache
|
|
35
|
+
* @private
|
|
36
|
+
* @param amount - the amount of the cache size which needs to be decreased
|
|
37
|
+
*/
|
|
38
|
+
async _decreaseCurSizeInBytes(amount: number) {
|
|
39
|
+
const curSize = await this.getCacheCurSize();
|
|
40
|
+
await AsyncStorage.setItem(
|
|
41
|
+
getCurrSizeKey(this.cacheConfig.keyPrefix),
|
|
42
|
+
(curSize - amount).toString()
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* increase current size of the cache
|
|
48
|
+
* @private
|
|
49
|
+
* @param amount - the amount of the cache szie which need to be increased
|
|
50
|
+
*/
|
|
51
|
+
async _increaseCurSizeInBytes(amount: number) {
|
|
52
|
+
const curSize = await this.getCacheCurSize();
|
|
53
|
+
await AsyncStorage.setItem(
|
|
54
|
+
getCurrSizeKey(this.cacheConfig.keyPrefix),
|
|
55
|
+
(curSize + amount).toString()
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* update the visited time if item has been visited
|
|
61
|
+
* @private
|
|
62
|
+
* @param item - the item which need to be refreshed
|
|
63
|
+
* @param prefixedKey - the key of the item
|
|
64
|
+
*
|
|
65
|
+
* @return the refreshed item
|
|
66
|
+
*/
|
|
67
|
+
async _refreshItem(item: CacheItem, prefixedKey: string) {
|
|
68
|
+
item.visitedTime = getCurrTime();
|
|
69
|
+
await AsyncStorage.setItem(prefixedKey, JSON.stringify(item));
|
|
70
|
+
return item;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* check wether item is expired
|
|
75
|
+
* @private
|
|
76
|
+
* @param key - the key of the item
|
|
77
|
+
*
|
|
78
|
+
* @return true if the item is expired.
|
|
79
|
+
*/
|
|
80
|
+
async _isExpired(key: string) {
|
|
81
|
+
const text = await AsyncStorage.getItem(key);
|
|
82
|
+
asserts(text !== null, {
|
|
83
|
+
name: STORAGE_CACHE_EXCEPTION,
|
|
84
|
+
message: `Item not found in the storage by the key: ${key}.`,
|
|
85
|
+
});
|
|
86
|
+
const item = JSON.parse(text);
|
|
87
|
+
if (getCurrTime() >= item.expires) {
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* delete item from cache
|
|
95
|
+
* @private
|
|
96
|
+
* @param prefixedKey - the key of the item
|
|
97
|
+
* @param size - optional, the byte size of the item
|
|
98
|
+
*/
|
|
99
|
+
async _removeItem(prefixedKey: string, size?: number) {
|
|
100
|
+
const config = await AsyncStorage.getItem(prefixedKey);
|
|
101
|
+
asserts(!!config, {
|
|
102
|
+
name: STORAGE_CACHE_EXCEPTION,
|
|
103
|
+
message: `Item not found in the storage by the key: ${prefixedKey}.`,
|
|
104
|
+
});
|
|
105
|
+
const itemSize = size ?? JSON.parse(config).byteSize;
|
|
106
|
+
// first try to update the current size of the cache
|
|
107
|
+
await this._decreaseCurSizeInBytes(itemSize);
|
|
108
|
+
|
|
109
|
+
// try to remove the item from cache
|
|
110
|
+
try {
|
|
111
|
+
await AsyncStorage.removeItem(prefixedKey);
|
|
112
|
+
} catch (removeItemError) {
|
|
113
|
+
// if some error happened, we need to rollback the current size
|
|
114
|
+
await this._increaseCurSizeInBytes(itemSize);
|
|
115
|
+
logger.error(`Failed to remove item: ${removeItemError}`);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* put item into cache
|
|
121
|
+
* @private
|
|
122
|
+
* @param prefixedKey - the key of the item
|
|
123
|
+
* @param itemData - the value of the item
|
|
124
|
+
* @param itemSizeInBytes - the byte size of the item
|
|
125
|
+
*/
|
|
126
|
+
async _setItem(prefixedKey: string, item: any) {
|
|
127
|
+
// first try to update the current size of the cache.
|
|
128
|
+
await this._increaseCurSizeInBytes(item.byteSize);
|
|
129
|
+
|
|
130
|
+
// try to add the item into cache
|
|
131
|
+
try {
|
|
132
|
+
await AsyncStorage.setItem(prefixedKey, JSON.stringify(item));
|
|
133
|
+
} catch (setItemErr) {
|
|
134
|
+
// if some error happened, we need to rollback the current size
|
|
135
|
+
await this._decreaseCurSizeInBytes(item.byteSize);
|
|
136
|
+
logger.error(`Failed to set item ${setItemErr}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* total space needed when poping out items
|
|
142
|
+
* @private
|
|
143
|
+
* @param itemSize
|
|
144
|
+
*
|
|
145
|
+
* @return total space needed
|
|
146
|
+
*/
|
|
147
|
+
async _sizeToPop(itemSize: number) {
|
|
148
|
+
const spaceItemNeed =
|
|
149
|
+
(await this.getCacheCurSize()) +
|
|
150
|
+
itemSize -
|
|
151
|
+
this.cacheConfig.capacityInBytes;
|
|
152
|
+
const cacheThresholdSpace =
|
|
153
|
+
(1 - this.cacheConfig.warningThreshold) *
|
|
154
|
+
this.cacheConfig.capacityInBytes;
|
|
155
|
+
return spaceItemNeed > cacheThresholdSpace
|
|
156
|
+
? spaceItemNeed
|
|
157
|
+
: cacheThresholdSpace;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* see whether cache is full
|
|
162
|
+
* @private
|
|
163
|
+
* @param itemSize
|
|
164
|
+
*
|
|
165
|
+
* @return true if cache is full
|
|
166
|
+
*/
|
|
167
|
+
async _isCacheFull(itemSize: number) {
|
|
168
|
+
return (
|
|
169
|
+
itemSize + (await this.getCacheCurSize()) >
|
|
170
|
+
this.cacheConfig.capacityInBytes
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* scan the storage and find out all the keys owned by this cache
|
|
176
|
+
* also clean the expired keys while scanning
|
|
177
|
+
* @private
|
|
178
|
+
* @return array of keys
|
|
179
|
+
*/
|
|
180
|
+
async _findValidKeys() {
|
|
181
|
+
const keys: string[] = [];
|
|
182
|
+
let keyInCache: Readonly<string[]> = [];
|
|
183
|
+
|
|
184
|
+
keyInCache = await AsyncStorage.getAllKeys();
|
|
185
|
+
|
|
186
|
+
for (let i = 0; i < keyInCache.length; i += 1) {
|
|
187
|
+
const key = keyInCache[i];
|
|
188
|
+
if (
|
|
189
|
+
key.indexOf(this.cacheConfig.keyPrefix) === 0 &&
|
|
190
|
+
key !== getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
191
|
+
) {
|
|
192
|
+
if (await this._isExpired(key)) {
|
|
193
|
+
await this._removeItem(key);
|
|
194
|
+
} else {
|
|
195
|
+
keys.push(key);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return keys;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* get all the items we have, sort them by their priority,
|
|
204
|
+
* if priority is same, sort them by their last visited time
|
|
205
|
+
* pop out items from the low priority (5 is the lowest)
|
|
206
|
+
* @private
|
|
207
|
+
* @param keys - all the keys in this cache
|
|
208
|
+
* @param sizeToPop - the total size of the items which needed to be poped out
|
|
209
|
+
*/
|
|
210
|
+
async _popOutItems(keys: string[], sizeToPop: number) {
|
|
211
|
+
const items: any[] = [];
|
|
212
|
+
let remainedSize = sizeToPop;
|
|
213
|
+
for (let i = 0; i < keys.length; i += 1) {
|
|
214
|
+
const val = await AsyncStorage.getItem(keys[i]);
|
|
215
|
+
if (val != null) {
|
|
216
|
+
const item = JSON.parse(val);
|
|
217
|
+
items.push(item);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// first compare priority
|
|
222
|
+
// then compare visited time
|
|
223
|
+
items.sort((a, b) => {
|
|
224
|
+
if (a.priority > b.priority) {
|
|
225
|
+
return -1;
|
|
226
|
+
} else if (a.priority < b.priority) {
|
|
227
|
+
return 1;
|
|
228
|
+
} else {
|
|
229
|
+
if (a.visitedTime < b.visitedTime) {
|
|
230
|
+
return -1;
|
|
231
|
+
} else return 1;
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
for (let i = 0; i < items.length; i += 1) {
|
|
236
|
+
// pop out items until we have enough room for new item
|
|
237
|
+
await this._removeItem(items[i].key, items[i].byteSize);
|
|
238
|
+
remainedSize -= items[i].byteSize;
|
|
239
|
+
if (remainedSize <= 0) {
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Set item into cache. You can put number, string, boolean or object.
|
|
247
|
+
* The cache will first check whether has the same key.
|
|
248
|
+
* If it has, it will delete the old item and then put the new item in
|
|
249
|
+
* The cache will pop out items if it is full
|
|
250
|
+
* You can specify the cache item options. The cache will abort and output a warning:
|
|
251
|
+
* If the key is invalid
|
|
252
|
+
* If the size of the item exceeds itemMaxSize.
|
|
253
|
+
* If the value is undefined
|
|
254
|
+
* If incorrect cache item configuration
|
|
255
|
+
* If error happened with browser storage
|
|
256
|
+
*
|
|
257
|
+
* @param {String} key - the key of the item
|
|
258
|
+
* @param {Object} value - the value of the item
|
|
259
|
+
* @param {Object} [options] - optional, the specified meta-data
|
|
260
|
+
* @return {Promise}
|
|
261
|
+
*/
|
|
262
|
+
async setItem(key: string, value: any, options: Record<string, any>) {
|
|
263
|
+
logger.debug(
|
|
264
|
+
`Set item: key is ${key}, value is ${value} with options: ${options}`
|
|
265
|
+
);
|
|
266
|
+
const prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
267
|
+
// invalid keys
|
|
268
|
+
if (
|
|
269
|
+
prefixedKey === this.cacheConfig.keyPrefix ||
|
|
270
|
+
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
271
|
+
) {
|
|
272
|
+
logger.warn(`Invalid key: should not be empty or 'CurSize'`);
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (typeof value === 'undefined') {
|
|
277
|
+
logger.warn(`The value of item should not be undefined!`);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const cacheItemOptions = {
|
|
282
|
+
priority:
|
|
283
|
+
options && options.priority !== undefined
|
|
284
|
+
? options.priority
|
|
285
|
+
: this.cacheConfig.defaultPriority,
|
|
286
|
+
expires:
|
|
287
|
+
options && options.expires !== undefined
|
|
288
|
+
? options.expires
|
|
289
|
+
: this.cacheConfig.defaultTTL + getCurrTime(),
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
|
|
293
|
+
logger.warn(
|
|
294
|
+
`Invalid parameter: priority due to out or range. It should be within 1 and 5.`
|
|
295
|
+
);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
|
|
300
|
+
|
|
301
|
+
// check wether this item is too big;
|
|
302
|
+
if (item.byteSize > this.cacheConfig.itemMaxSize) {
|
|
303
|
+
logger.warn(
|
|
304
|
+
`Item with key: ${key} you are trying to put into is too big!`
|
|
305
|
+
);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
// first look into the storage, if it exists, delete it.
|
|
311
|
+
const val = await AsyncStorage.getItem(prefixedKey);
|
|
312
|
+
if (val) {
|
|
313
|
+
await this._removeItem(prefixedKey, JSON.parse(val).byteSize);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
// check whether the cache is full
|
|
317
|
+
if (await this._isCacheFull(item.byteSize)) {
|
|
318
|
+
const validKeys = await this._findValidKeys();
|
|
319
|
+
if (await this._isCacheFull(item.byteSize)) {
|
|
320
|
+
const sizeToPop = await this._sizeToPop(item.byteSize);
|
|
321
|
+
await this._popOutItems(validKeys, sizeToPop);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// put item in the cache
|
|
326
|
+
await this._setItem(prefixedKey, item);
|
|
327
|
+
} catch (e) {
|
|
328
|
+
logger.warn(`setItem failed! ${e}`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Get item from cache. It will return null if item doesn’t exist or it has been expired.
|
|
334
|
+
* If you specified callback function in the options,
|
|
335
|
+
* then the function will be executed if no such item in the cache
|
|
336
|
+
* and finally put the return value into cache.
|
|
337
|
+
* Please make sure the callback function will return the value you want to put into the cache.
|
|
338
|
+
* The cache will abort output a warning:
|
|
339
|
+
* If the key is invalid
|
|
340
|
+
* If error happened with AsyncStorage
|
|
341
|
+
*
|
|
342
|
+
* @param {String} key - the key of the item
|
|
343
|
+
* @param {Object} [options] - the options of callback function
|
|
344
|
+
* @return {Promise} - return a promise resolves to be the value of the item
|
|
345
|
+
*/
|
|
346
|
+
async getItem(key: string, options: CacheItemOptions) {
|
|
347
|
+
logger.debug(`Get item: key is ${key} with options ${options}`);
|
|
348
|
+
let ret = null;
|
|
349
|
+
const prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
350
|
+
|
|
351
|
+
if (
|
|
352
|
+
prefixedKey === this.cacheConfig.keyPrefix ||
|
|
353
|
+
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
354
|
+
) {
|
|
355
|
+
logger.warn(`Invalid key: should not be empty or 'CurSize'`);
|
|
356
|
+
return null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
try {
|
|
360
|
+
ret = await AsyncStorage.getItem(prefixedKey);
|
|
361
|
+
if (ret != null) {
|
|
362
|
+
if (await this._isExpired(prefixedKey)) {
|
|
363
|
+
// if expired, remove that item and return null
|
|
364
|
+
await this._removeItem(prefixedKey, JSON.parse(ret).byteSize);
|
|
365
|
+
} else {
|
|
366
|
+
// if not expired, great, return the value and refresh it
|
|
367
|
+
let item = JSON.parse(ret);
|
|
368
|
+
item = await this._refreshItem(item, prefixedKey);
|
|
369
|
+
return item.data;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if (options && options.callback !== undefined) {
|
|
374
|
+
const val = options.callback();
|
|
375
|
+
if (val !== null) {
|
|
376
|
+
this.setItem(key, val, options);
|
|
377
|
+
}
|
|
378
|
+
return val;
|
|
379
|
+
}
|
|
380
|
+
return null;
|
|
381
|
+
} catch (e) {
|
|
382
|
+
logger.warn(`getItem failed! ${e}`);
|
|
383
|
+
return null;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* remove item from the cache
|
|
389
|
+
* The cache will abort output a warning:
|
|
390
|
+
* If error happened with AsyncStorage
|
|
391
|
+
* @param {String} key - the key of the item
|
|
392
|
+
* @return {Promise}
|
|
393
|
+
*/
|
|
394
|
+
async removeItem(key: string) {
|
|
395
|
+
logger.debug(`Remove item: key is ${key}`);
|
|
396
|
+
const prefixedKey = this.cacheConfig.keyPrefix + key;
|
|
397
|
+
|
|
398
|
+
if (
|
|
399
|
+
prefixedKey === this.cacheConfig.keyPrefix ||
|
|
400
|
+
prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
401
|
+
) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
try {
|
|
406
|
+
const val = await AsyncStorage.getItem(prefixedKey);
|
|
407
|
+
if (val) {
|
|
408
|
+
await this._removeItem(prefixedKey, JSON.parse(val).byteSize);
|
|
409
|
+
}
|
|
410
|
+
} catch (e) {
|
|
411
|
+
logger.warn(`removeItem failed! ${e}`);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* clear the entire cache
|
|
417
|
+
* The cache will abort output a warning:
|
|
418
|
+
* If error happened with AsyncStorage
|
|
419
|
+
* @return {Promise}
|
|
420
|
+
*/
|
|
421
|
+
async clear() {
|
|
422
|
+
logger.debug(`Clear Cache`);
|
|
423
|
+
try {
|
|
424
|
+
const keys = await AsyncStorage.getAllKeys();
|
|
425
|
+
|
|
426
|
+
const keysToRemove: string[] = [];
|
|
427
|
+
for (let i = 0; i < keys.length; i += 1) {
|
|
428
|
+
if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0) {
|
|
429
|
+
keysToRemove.push(keys[i]);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
// can be improved
|
|
434
|
+
for (let i = 0; i < keysToRemove.length; i += 1) {
|
|
435
|
+
await AsyncStorage.removeItem(keysToRemove[i]);
|
|
436
|
+
}
|
|
437
|
+
} catch (e) {
|
|
438
|
+
logger.warn(`clear failed! ${e}`);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* return the current size of the cache
|
|
444
|
+
* @return {Promise}
|
|
445
|
+
*/
|
|
446
|
+
async getCacheCurSize() {
|
|
447
|
+
let ret = await AsyncStorage.getItem(
|
|
448
|
+
getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
449
|
+
);
|
|
450
|
+
if (!ret) {
|
|
451
|
+
await AsyncStorage.setItem(
|
|
452
|
+
getCurrSizeKey(this.cacheConfig.keyPrefix),
|
|
453
|
+
'0'
|
|
454
|
+
);
|
|
455
|
+
ret = '0';
|
|
456
|
+
}
|
|
457
|
+
return Number(ret);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Return all the keys in the cache.
|
|
462
|
+
* Will return an empty array if error happend.
|
|
463
|
+
* @return {Promise}
|
|
464
|
+
*/
|
|
465
|
+
async getAllKeys() {
|
|
466
|
+
try {
|
|
467
|
+
const keys = await AsyncStorage.getAllKeys();
|
|
468
|
+
|
|
469
|
+
const retKeys: string[] = [];
|
|
470
|
+
for (let i = 0; i < keys.length; i += 1) {
|
|
471
|
+
if (
|
|
472
|
+
keys[i].indexOf(this.cacheConfig.keyPrefix) === 0 &&
|
|
473
|
+
keys[i] !== getCurrSizeKey(this.cacheConfig.keyPrefix)
|
|
474
|
+
) {
|
|
475
|
+
retKeys.push(keys[i].substring(this.cacheConfig.keyPrefix.length));
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
return retKeys;
|
|
479
|
+
} catch (e) {
|
|
480
|
+
logger.warn(`getALlkeys failed! ${e}`);
|
|
481
|
+
return [];
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Return a new instance of cache with customized configuration.
|
|
487
|
+
* @param {Object} config - the customized configuration
|
|
488
|
+
* @return {Object} - the new instance of Cache
|
|
489
|
+
*/
|
|
490
|
+
createInstance(config: CacheConfig): ICache {
|
|
491
|
+
if (config.keyPrefix === defaultConfig.keyPrefix) {
|
|
492
|
+
logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
|
|
493
|
+
config.keyPrefix = getCurrTime.toString();
|
|
494
|
+
}
|
|
495
|
+
return new AsyncStorageCache(config);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const instance: ICache = new AsyncStorageCache();
|
|
500
|
+
export { AsyncStorage, instance as Cache };
|