@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.
Files changed (300) hide show
  1. package/README.md +3 -0
  2. package/lib/Amplify.d.ts +18 -17
  3. package/lib/Amplify.js +9 -0
  4. package/lib/AwsClients/CognitoIdentity/base.d.ts +3 -10
  5. package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
  6. package/lib/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
  7. package/lib/AwsClients/CognitoIdentity/getId.d.ts +3 -17
  8. package/lib/AwsClients/Pinpoint/base.d.ts +2 -10
  9. package/lib/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
  10. package/lib/AwsClients/Pinpoint/getInAppMessages.js +1 -1
  11. package/lib/AwsClients/Pinpoint/putEvents.d.ts +3 -17
  12. package/lib/AwsClients/Pinpoint/putEvents.js +6 -0
  13. package/lib/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
  14. package/lib/AwsClients/Pinpoint/updateEndpoint.js +2 -2
  15. package/lib/Cache/AsyncStorageCache.d.ts +154 -0
  16. package/lib/Cache/AsyncStorageCache.js +744 -0
  17. package/lib/Cache/BrowserStorageCache.d.ts +166 -0
  18. package/lib/Cache/BrowserStorageCache.js +469 -0
  19. package/lib/Cache/InMemoryCache.d.ts +129 -0
  20. package/lib/Cache/InMemoryCache.js +349 -0
  21. package/lib/Cache/StorageCache.d.ts +39 -0
  22. package/lib/Cache/StorageCache.js +121 -0
  23. package/lib/Cache/Utils/CacheList.d.ts +89 -0
  24. package/lib/Cache/Utils/CacheList.js +196 -0
  25. package/lib/Cache/Utils/CacheUtils.d.ts +25 -0
  26. package/lib/Cache/Utils/CacheUtils.js +88 -0
  27. package/lib/Cache/Utils/index.d.ts +2 -0
  28. package/lib/Cache/Utils/index.js +16 -0
  29. package/lib/Cache/reactnative.d.ts +3 -0
  30. package/lib/Cache/reactnative.js +8 -0
  31. package/lib/Cache/types/Cache.d.ts +55 -0
  32. package/lib/Cache/types/Cache.js +4 -0
  33. package/lib/Cache/types/index.d.ts +1 -0
  34. package/lib/Cache/types/index.js +19 -0
  35. package/lib/ClientDevice/android.js +3 -2
  36. package/lib/ClientDevice/ios.d.ts +2 -2
  37. package/lib/ClientDevice/ios.js +3 -2
  38. package/lib/ClientDevice/reactnative.js +3 -2
  39. package/lib/Credentials.d.ts +1 -1
  40. package/lib/Credentials.js +2 -1
  41. package/lib/Errors.d.ts +19 -0
  42. package/lib/Errors.js +50 -1
  43. package/lib/Hub.js +1 -0
  44. package/lib/I18n/I18n.d.ts +29 -9
  45. package/lib/I18n/I18n.js +30 -5
  46. package/lib/I18n/index.d.ts +18 -5
  47. package/lib/I18n/index.js +35 -5
  48. package/lib/I18n/types.d.ts +4 -1
  49. package/lib/I18n/types.js +3 -0
  50. package/lib/JS.d.ts +6 -6
  51. package/lib/Logger/ConsoleLogger.d.ts +11 -11
  52. package/lib/OAuthHelper/GoogleOAuth.js +2 -1
  53. package/lib/Platform/detectFramework.js +2 -1
  54. package/lib/Platform/detection/Next.d.ts +1 -1
  55. package/lib/Platform/detection/React.js +1 -0
  56. package/lib/Platform/version.d.ts +1 -1
  57. package/lib/Platform/version.js +1 -1
  58. package/lib/Providers/AWSCloudWatchProvider.d.ts +2 -2
  59. package/lib/Providers/AWSCloudWatchProvider.js +35 -12
  60. package/lib/RNComponents/index.d.ts +1 -1
  61. package/lib/RNComponents/reactnative.js +3 -2
  62. package/lib/ServiceWorker/ServiceWorker.d.ts +4 -4
  63. package/lib/ServiceWorker/ServiceWorker.js +37 -11
  64. package/lib/Signer.d.ts +14 -1
  65. package/lib/StorageHelper/cookieStorage.d.ts +13 -0
  66. package/lib/StorageHelper/cookieStorage.js +167 -0
  67. package/lib/StorageHelper/inMemoryStorage.d.ts +10 -0
  68. package/lib/StorageHelper/inMemoryStorage.js +82 -0
  69. package/lib/StorageHelper/index.d.ts +7 -3
  70. package/lib/StorageHelper/index.js +9 -1
  71. package/lib/StorageHelper/localStorage.d.ts +32 -0
  72. package/lib/StorageHelper/localStorage.js +114 -0
  73. package/lib/StorageHelper/reactnative.d.ts +71 -1
  74. package/lib/StorageHelper/reactnative.js +154 -10
  75. package/lib/StorageHelper/sessionStorage.d.ts +32 -0
  76. package/lib/StorageHelper/sessionStorage.js +114 -0
  77. package/lib/UniversalStorage/index.d.ts +2 -2
  78. package/lib/Util/BackgroundProcessManager.d.ts +2 -2
  79. package/lib/Util/BackgroundProcessManager.js +1 -0
  80. package/lib/Util/DateUtils.d.ts +12 -1
  81. package/lib/Util/Reachability.js +7 -5
  82. package/lib/Util/Reachability.native.d.ts +3 -2
  83. package/lib/Util/StringUtils.js +3 -10
  84. package/lib/Util/errors/AssertError.d.ts +2 -0
  85. package/lib/Util/errors/AssertError.js +11 -0
  86. package/lib/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
  87. package/lib/clients/middleware/retry/defaultRetryDecider.js +17 -13
  88. package/lib/clients/middleware/retry/isClockSkewError.js +1 -1
  89. package/lib/clients/middleware/retry/middleware.d.ts +1 -1
  90. package/lib/clients/middleware/retry/middleware.js +13 -13
  91. package/lib/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
  92. package/lib/clients/serde/responseInfo.js +1 -1
  93. package/lib/clients/types/aws.d.ts +4 -0
  94. package/lib/clients/types/index.d.ts +1 -1
  95. package/lib/constants.d.ts +7 -0
  96. package/lib/constants.js +9 -1
  97. package/lib/index.d.ts +18 -4
  98. package/lib/index.js +32 -5
  99. package/lib/parseAWSExports.d.ts +1 -1
  100. package/lib/singleton/Auth/index.d.ts +30 -0
  101. package/lib/singleton/Auth/index.js +133 -0
  102. package/lib/singleton/Auth/types.d.ts +95 -0
  103. package/lib/singleton/Auth/types.js +6 -0
  104. package/lib/singleton/Auth/utils/index.d.ts +5 -0
  105. package/lib/singleton/Auth/utils/index.js +52 -0
  106. package/lib/singleton/Storage/types.d.ts +14 -0
  107. package/lib/singleton/Storage/types.js +4 -0
  108. package/lib/singleton/index.d.ts +46 -0
  109. package/lib/singleton/index.js +147 -0
  110. package/lib/singleton/types.d.ts +21 -0
  111. package/lib/singleton/types.js +4 -0
  112. package/lib/tsconfig.tsbuildinfo +1 -1
  113. package/lib/types/types.d.ts +31 -1
  114. package/lib/types/types.js +2 -0
  115. package/lib-esm/Amplify.d.ts +18 -17
  116. package/lib-esm/Amplify.js +9 -0
  117. package/lib-esm/AwsClients/CognitoIdentity/base.d.ts +3 -10
  118. package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.d.ts +3 -17
  119. package/lib-esm/AwsClients/CognitoIdentity/getCredentialsForIdentity.js +8 -8
  120. package/lib-esm/AwsClients/CognitoIdentity/getId.d.ts +3 -17
  121. package/lib-esm/AwsClients/Pinpoint/base.d.ts +2 -10
  122. package/lib-esm/AwsClients/Pinpoint/getInAppMessages.d.ts +3 -17
  123. package/lib-esm/AwsClients/Pinpoint/getInAppMessages.js +1 -1
  124. package/lib-esm/AwsClients/Pinpoint/putEvents.d.ts +3 -17
  125. package/lib-esm/AwsClients/Pinpoint/putEvents.js +6 -0
  126. package/lib-esm/AwsClients/Pinpoint/updateEndpoint.d.ts +3 -17
  127. package/lib-esm/AwsClients/Pinpoint/updateEndpoint.js +2 -2
  128. package/lib-esm/Cache/AsyncStorageCache.d.ts +154 -0
  129. package/lib-esm/Cache/AsyncStorageCache.js +737 -0
  130. package/lib-esm/Cache/BrowserStorageCache.d.ts +166 -0
  131. package/lib-esm/Cache/BrowserStorageCache.js +466 -0
  132. package/lib-esm/Cache/InMemoryCache.d.ts +129 -0
  133. package/lib-esm/Cache/InMemoryCache.js +346 -0
  134. package/lib-esm/Cache/StorageCache.d.ts +39 -0
  135. package/lib-esm/Cache/StorageCache.js +118 -0
  136. package/lib-esm/Cache/Utils/CacheList.d.ts +89 -0
  137. package/lib-esm/Cache/Utils/CacheList.js +194 -0
  138. package/lib-esm/Cache/Utils/CacheUtils.d.ts +25 -0
  139. package/lib-esm/Cache/Utils/CacheUtils.js +81 -0
  140. package/lib-esm/Cache/Utils/index.d.ts +2 -0
  141. package/lib-esm/Cache/Utils/index.js +4 -0
  142. package/lib-esm/Cache/reactnative.d.ts +3 -0
  143. package/lib-esm/Cache/reactnative.js +6 -0
  144. package/lib-esm/Cache/types/Cache.d.ts +55 -0
  145. package/lib-esm/Cache/types/Cache.js +3 -0
  146. package/lib-esm/Cache/types/index.d.ts +1 -0
  147. package/lib-esm/Cache/types/index.js +3 -0
  148. package/lib-esm/ClientDevice/android.js +1 -0
  149. package/lib-esm/ClientDevice/ios.d.ts +2 -2
  150. package/lib-esm/ClientDevice/ios.js +1 -0
  151. package/lib-esm/ClientDevice/reactnative.js +1 -0
  152. package/lib-esm/Credentials.d.ts +1 -1
  153. package/lib-esm/Credentials.js +2 -1
  154. package/lib-esm/Errors.d.ts +19 -0
  155. package/lib-esm/Errors.js +49 -0
  156. package/lib-esm/Hub.js +1 -0
  157. package/lib-esm/I18n/I18n.d.ts +29 -9
  158. package/lib-esm/I18n/I18n.js +30 -5
  159. package/lib-esm/I18n/index.d.ts +18 -5
  160. package/lib-esm/I18n/index.js +35 -5
  161. package/lib-esm/I18n/types.d.ts +4 -1
  162. package/lib-esm/I18n/types.js +3 -0
  163. package/lib-esm/JS.d.ts +6 -6
  164. package/lib-esm/Logger/ConsoleLogger.d.ts +11 -11
  165. package/lib-esm/OAuthHelper/GoogleOAuth.js +2 -1
  166. package/lib-esm/Platform/detectFramework.js +2 -1
  167. package/lib-esm/Platform/detection/Next.d.ts +1 -1
  168. package/lib-esm/Platform/detection/React.js +1 -0
  169. package/lib-esm/Platform/version.d.ts +1 -1
  170. package/lib-esm/Platform/version.js +1 -1
  171. package/lib-esm/Providers/AWSCloudWatchProvider.d.ts +2 -2
  172. package/lib-esm/Providers/AWSCloudWatchProvider.js +33 -10
  173. package/lib-esm/RNComponents/index.d.ts +1 -1
  174. package/lib-esm/RNComponents/reactnative.js +1 -0
  175. package/lib-esm/ServiceWorker/ServiceWorker.d.ts +4 -4
  176. package/lib-esm/ServiceWorker/ServiceWorker.js +37 -11
  177. package/lib-esm/Signer.d.ts +14 -1
  178. package/lib-esm/StorageHelper/cookieStorage.d.ts +13 -0
  179. package/lib-esm/StorageHelper/cookieStorage.js +141 -0
  180. package/lib-esm/StorageHelper/inMemoryStorage.d.ts +10 -0
  181. package/lib-esm/StorageHelper/inMemoryStorage.js +79 -0
  182. package/lib-esm/StorageHelper/index.d.ts +7 -3
  183. package/lib-esm/StorageHelper/index.js +4 -0
  184. package/lib-esm/StorageHelper/localStorage.d.ts +32 -0
  185. package/lib-esm/StorageHelper/localStorage.js +111 -0
  186. package/lib-esm/StorageHelper/reactnative.d.ts +71 -1
  187. package/lib-esm/StorageHelper/reactnative.js +153 -9
  188. package/lib-esm/StorageHelper/sessionStorage.d.ts +32 -0
  189. package/lib-esm/StorageHelper/sessionStorage.js +111 -0
  190. package/lib-esm/UniversalStorage/index.d.ts +2 -2
  191. package/lib-esm/Util/BackgroundProcessManager.d.ts +2 -2
  192. package/lib-esm/Util/BackgroundProcessManager.js +1 -0
  193. package/lib-esm/Util/DateUtils.d.ts +12 -1
  194. package/lib-esm/Util/Reachability.js +5 -3
  195. package/lib-esm/Util/Reachability.native.d.ts +3 -2
  196. package/lib-esm/Util/StringUtils.js +3 -10
  197. package/lib-esm/Util/errors/AssertError.d.ts +2 -0
  198. package/lib-esm/Util/errors/AssertError.js +7 -0
  199. package/lib-esm/clients/middleware/retry/defaultRetryDecider.d.ts +1 -1
  200. package/lib-esm/clients/middleware/retry/defaultRetryDecider.js +17 -13
  201. package/lib-esm/clients/middleware/retry/isClockSkewError.js +1 -1
  202. package/lib-esm/clients/middleware/retry/middleware.d.ts +1 -1
  203. package/lib-esm/clients/middleware/retry/middleware.js +13 -13
  204. package/lib-esm/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.js +1 -1
  205. package/lib-esm/clients/serde/responseInfo.js +1 -1
  206. package/lib-esm/clients/types/aws.d.ts +4 -0
  207. package/lib-esm/clients/types/index.d.ts +1 -1
  208. package/lib-esm/constants.d.ts +7 -0
  209. package/lib-esm/constants.js +8 -0
  210. package/lib-esm/index.d.ts +18 -4
  211. package/lib-esm/index.js +15 -3
  212. package/lib-esm/parseAWSExports.d.ts +1 -1
  213. package/lib-esm/singleton/Auth/index.d.ts +30 -0
  214. package/lib-esm/singleton/Auth/index.js +129 -0
  215. package/lib-esm/singleton/Auth/types.d.ts +95 -0
  216. package/lib-esm/singleton/Auth/types.js +5 -0
  217. package/lib-esm/singleton/Auth/utils/index.d.ts +5 -0
  218. package/lib-esm/singleton/Auth/utils/index.js +45 -0
  219. package/lib-esm/singleton/Storage/types.d.ts +14 -0
  220. package/lib-esm/singleton/Storage/types.js +3 -0
  221. package/lib-esm/singleton/index.d.ts +46 -0
  222. package/lib-esm/singleton/index.js +143 -0
  223. package/lib-esm/singleton/types.d.ts +21 -0
  224. package/lib-esm/singleton/types.js +3 -0
  225. package/lib-esm/tsconfig.tsbuildinfo +1 -1
  226. package/lib-esm/types/types.d.ts +31 -1
  227. package/lib-esm/types/types.js +2 -0
  228. package/package.json +38 -15
  229. package/src/Amplify.ts +9 -3
  230. package/src/AwsClients/CognitoIdentity/base.ts +2 -1
  231. package/src/AwsClients/CognitoIdentity/getCredentialsForIdentity.ts +13 -6
  232. package/src/AwsClients/Pinpoint/base.ts +6 -2
  233. package/src/AwsClients/Pinpoint/getInAppMessages.ts +1 -1
  234. package/src/AwsClients/Pinpoint/putEvents.ts +6 -0
  235. package/src/AwsClients/Pinpoint/updateEndpoint.ts +2 -2
  236. package/src/Cache/AsyncStorageCache.ts +500 -0
  237. package/src/Cache/BrowserStorageCache.ts +517 -0
  238. package/src/Cache/CHANGELOG.md +1227 -0
  239. package/src/Cache/InMemoryCache.ts +354 -0
  240. package/src/Cache/StorageCache.ts +157 -0
  241. package/src/Cache/Utils/CacheList.ts +198 -0
  242. package/src/Cache/Utils/CacheUtils.ts +92 -0
  243. package/src/Cache/Utils/index.ts +11 -0
  244. package/src/Cache/reactnative.ts +9 -0
  245. package/src/Cache/types/Cache.ts +75 -0
  246. package/src/Cache/types/index.ts +4 -0
  247. package/src/ClientDevice/android.ts +1 -1
  248. package/src/ClientDevice/ios.ts +2 -2
  249. package/src/ClientDevice/reactnative.ts +1 -1
  250. package/src/Credentials.ts +19 -17
  251. package/src/Errors.ts +41 -0
  252. package/src/Hub.ts +1 -0
  253. package/src/I18n/I18n.ts +40 -12
  254. package/src/I18n/index.ts +45 -12
  255. package/src/I18n/types.ts +3 -0
  256. package/src/JS.ts +24 -17
  257. package/src/Logger/ConsoleLogger.ts +10 -10
  258. package/src/OAuthHelper/FacebookOAuth.ts +2 -2
  259. package/src/OAuthHelper/GoogleOAuth.ts +9 -6
  260. package/src/Platform/detectFramework.ts +1 -1
  261. package/src/Platform/detection/React.ts +5 -3
  262. package/src/Platform/version.ts +1 -1
  263. package/src/Providers/AWSCloudWatchProvider.ts +39 -14
  264. package/src/RNComponents/index.ts +1 -1
  265. package/src/RNComponents/reactnative.ts +1 -1
  266. package/src/ServiceWorker/ServiceWorker.ts +58 -18
  267. package/src/Signer.ts +22 -3
  268. package/src/StorageHelper/cookieStorage.ts +99 -0
  269. package/src/StorageHelper/inMemoryStorage.ts +32 -0
  270. package/src/StorageHelper/index.ts +6 -1
  271. package/src/StorageHelper/localStorage.ts +60 -0
  272. package/src/StorageHelper/reactnative.ts +94 -14
  273. package/src/StorageHelper/sessionStorage.ts +60 -0
  274. package/src/Util/BackgroundProcessManager.ts +33 -9
  275. package/src/Util/DateUtils.ts +14 -5
  276. package/src/Util/Reachability.native.ts +3 -2
  277. package/src/Util/Reachability.ts +9 -7
  278. package/src/Util/Retry.ts +2 -1
  279. package/src/Util/StringUtils.ts +3 -10
  280. package/src/Util/errors/AssertError.ts +11 -0
  281. package/src/clients/handlers/fetch.ts +2 -2
  282. package/src/clients/middleware/retry/defaultRetryDecider.ts +12 -6
  283. package/src/clients/middleware/retry/isClockSkewError.ts +1 -1
  284. package/src/clients/middleware/retry/middleware.ts +9 -9
  285. package/src/clients/middleware/signing/middleware.ts +1 -1
  286. package/src/clients/middleware/signing/signer/signatureV4/utils/dataHashHelpers.ts +1 -1
  287. package/src/clients/serde/responseInfo.ts +2 -1
  288. package/src/clients/types/aws.ts +10 -0
  289. package/src/clients/types/index.ts +1 -0
  290. package/src/constants.ts +16 -0
  291. package/src/global.d.ts +11 -0
  292. package/src/index.ts +66 -4
  293. package/src/parseAWSExports.ts +1 -1
  294. package/src/singleton/Auth/index.ts +113 -0
  295. package/src/singleton/Auth/types.ts +129 -0
  296. package/src/singleton/Auth/utils/index.ts +68 -0
  297. package/src/singleton/Storage/types.ts +21 -0
  298. package/src/singleton/index.ts +131 -0
  299. package/src/singleton/types.ts +48 -0
  300. package/src/types/types.ts +38 -1
@@ -0,0 +1,354 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { CacheList, defaultConfig, getCurrTime, CacheObject } from './Utils';
5
+
6
+ import { StorageCache } from './StorageCache';
7
+ import { ICache, CacheConfig, CacheItem, CacheItemOptions } from './types';
8
+ import { ConsoleLogger as Logger } from '../Logger';
9
+ import { asserts } from '../Util/errors/AssertError';
10
+ import { STORAGE_CACHE_EXCEPTION } from '../constants';
11
+ import { getCurrSizeKey } from './Utils/CacheUtils';
12
+
13
+ const logger = new Logger('InMemoryCache');
14
+
15
+ /**
16
+ * Customized in-memory cache with LRU implemented
17
+ * @member cacheObj - object which store items
18
+ * @member cacheList - list of keys in the cache with LRU
19
+ * @member curSizeInBytes - current size of the cache
20
+ * @member maxPriority - max of the priority
21
+ */
22
+ export class InMemoryCacheClass extends StorageCache implements ICache {
23
+ private cacheList: CacheList[];
24
+ private curSizeInBytes: number;
25
+ private maxPriority: number;
26
+
27
+ /**
28
+ * initialize the cache
29
+ *
30
+ * @param config - the configuration of the cache
31
+ */
32
+ constructor(config?: CacheConfig) {
33
+ super(config);
34
+
35
+ this.cacheList = [];
36
+ this.curSizeInBytes = 0;
37
+ this.maxPriority = 5;
38
+
39
+ this.getItem = this.getItem.bind(this);
40
+ this.setItem = this.setItem.bind(this);
41
+ this.removeItem = this.removeItem.bind(this);
42
+
43
+ // initialize list for every priority
44
+ for (let i = 0; i < this.maxPriority; i += 1) {
45
+ this.cacheList[i] = new CacheList();
46
+ }
47
+ }
48
+
49
+ /**
50
+ * decrease current size of the cache
51
+ *
52
+ * @param amount - the amount of the cache size which needs to be decreased
53
+ */
54
+ private _decreaseCurSizeInBytes(amount: number): void {
55
+ this.curSizeInBytes -= amount;
56
+ }
57
+
58
+ /**
59
+ * increase current size of the cache
60
+ *
61
+ * @param amount - the amount of the cache szie which need to be increased
62
+ */
63
+ private _increaseCurSizeInBytes(amount: number): void {
64
+ this.curSizeInBytes += amount;
65
+ }
66
+
67
+ /**
68
+ * check whether item is expired
69
+ *
70
+ * @param key - the key of the item
71
+ *
72
+ * @return true if the item is expired.
73
+ */
74
+ private _isExpired(key: string): boolean {
75
+ const text: string | null = CacheObject.getItem(key);
76
+
77
+ asserts(text !== null, {
78
+ name: STORAGE_CACHE_EXCEPTION,
79
+ message: 'item from storage is null',
80
+ });
81
+ const item: CacheItem = JSON.parse(text);
82
+ if (getCurrTime() >= item.expires) {
83
+ return true;
84
+ }
85
+ return false;
86
+ }
87
+
88
+ /**
89
+ * delete item from cache
90
+ *
91
+ * @param prefixedKey - the key of the item
92
+ * @param listIdx - indicates which cache list the key belongs to
93
+ */
94
+ private _removeItem(prefixedKey: string, listIdx: number): void {
95
+ // delete the key from the list
96
+ this.cacheList[listIdx].removeItem(prefixedKey);
97
+ // decrease the current size of the cache
98
+ const item = CacheObject.getItem(prefixedKey);
99
+ asserts(item !== null, {
100
+ name: STORAGE_CACHE_EXCEPTION,
101
+ message: 'item from storage is null',
102
+ });
103
+ this._decreaseCurSizeInBytes(JSON.parse(item).byteSize);
104
+ // finally remove the item from memory
105
+ CacheObject.removeItem(prefixedKey);
106
+ }
107
+
108
+ /**
109
+ * put item into cache
110
+ *
111
+ * @param prefixedKey - the key of the item
112
+ * @param itemData - the value of the item
113
+ * @param itemSizeInBytes - the byte size of the item
114
+ * @param listIdx - indicates which cache list the key belongs to
115
+ */
116
+ private _setItem(
117
+ prefixedKey: string,
118
+ item: CacheItem,
119
+ listIdx: number
120
+ ): void {
121
+ // insert the key into the list
122
+ this.cacheList[listIdx].insertItem(prefixedKey);
123
+ // increase the current size of the cache
124
+ this._increaseCurSizeInBytes(item.byteSize);
125
+ // finally add the item into memory
126
+ CacheObject.setItem(prefixedKey, JSON.stringify(item));
127
+ }
128
+
129
+ /**
130
+ * see whether cache is full
131
+ *
132
+ * @param itemSize
133
+ *
134
+ * @return true if cache is full
135
+ */
136
+ private _isCacheFull(itemSize: number): boolean {
137
+ return this.curSizeInBytes + itemSize > this.cacheConfig.capacityInBytes;
138
+ }
139
+
140
+ /**
141
+ * check whether the cache contains the key
142
+ *
143
+ * @param key
144
+ */
145
+ private containsKey(key: string): number {
146
+ const prefixedKey: string = this.cacheConfig.keyPrefix + key;
147
+ for (let i = 0; i < this.maxPriority; i += 1) {
148
+ if (this.cacheList[i].containsKey(prefixedKey)) {
149
+ return i + 1;
150
+ }
151
+ }
152
+ return -1;
153
+ }
154
+
155
+ /**
156
+ * * Set item into cache. You can put number, string, boolean or object.
157
+ * The cache will first check whether has the same key.
158
+ * If it has, it will delete the old item and then put the new item in
159
+ * The cache will pop out items if it is full
160
+ * You can specify the cache item options. The cache will abort and output a warning:
161
+ * If the key is invalid
162
+ * If the size of the item exceeds itemMaxSize.
163
+ * If the value is undefined
164
+ * If incorrect cache item configuration
165
+ * If error happened with browser storage
166
+ *
167
+ * @param key - the key of the item
168
+ * @param value - the value of the item
169
+ * @param options - optional, the specified meta-data
170
+ *
171
+ * @throws if the item is too big which exceeds the limit of single item size
172
+ * @throws if the key is invalid
173
+ */
174
+ public setItem(
175
+ key: string,
176
+ value: object | string | number | boolean,
177
+ options?: CacheItemOptions
178
+ ): void {
179
+ const prefixedKey: string = this.cacheConfig.keyPrefix + key;
180
+ // invalid keys
181
+ if (
182
+ prefixedKey === this.cacheConfig.keyPrefix ||
183
+ prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
184
+ ) {
185
+ logger.warn(`Invalid key: should not be empty or 'CurSize'`);
186
+ return;
187
+ }
188
+
189
+ if (typeof value === 'undefined') {
190
+ logger.warn(`The value of item should not be undefined!`);
191
+ return;
192
+ }
193
+
194
+ const cacheItemOptions = {
195
+ priority:
196
+ options && options.priority !== undefined
197
+ ? options.priority
198
+ : this.cacheConfig.defaultPriority,
199
+ expires:
200
+ options && options.expires !== undefined
201
+ ? options.expires
202
+ : this.cacheConfig.defaultTTL + getCurrTime(),
203
+ };
204
+
205
+ if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
206
+ logger.warn(
207
+ `Invalid parameter: priority due to out or range. It should be within 1 and 5.`
208
+ );
209
+ return;
210
+ }
211
+
212
+ const item: CacheItem = this.fillCacheItem(
213
+ prefixedKey,
214
+ value,
215
+ cacheItemOptions
216
+ );
217
+
218
+ // check wether this item is too big;
219
+ if (item.byteSize > this.cacheConfig.itemMaxSize) {
220
+ logger.warn(
221
+ `Item with key: ${key} you are trying to put into is too big!`
222
+ );
223
+ return;
224
+ }
225
+
226
+ // if key already in the cache, then delete it.
227
+ const presentKeyPrio: number = this.containsKey(key);
228
+ if (presentKeyPrio !== -1) {
229
+ this._removeItem(prefixedKey, presentKeyPrio - 1);
230
+ }
231
+
232
+ // pop out items in the cache when cache is full based on LRU
233
+ // first start from lowest priority cache list
234
+ let cacheListIdx = this.maxPriority - 1;
235
+ while (this._isCacheFull(item.byteSize) && cacheListIdx >= 0) {
236
+ if (!this.cacheList[cacheListIdx].isEmpty()) {
237
+ const popedItemKey = this.cacheList[cacheListIdx].getLastItem();
238
+ this._removeItem(popedItemKey, cacheListIdx);
239
+ } else {
240
+ cacheListIdx -= 1;
241
+ }
242
+ }
243
+
244
+ this._setItem(prefixedKey, item, Number(item.priority) - 1);
245
+ }
246
+
247
+ /**
248
+ * Get item from cache. It will return null if item doesn’t exist or it has been expired.
249
+ * If you specified callback function in the options,
250
+ * then the function will be executed if no such item in the cache
251
+ * and finally put the return value into cache.
252
+ * Please make sure the callback function will return the value you want to put into the cache.
253
+ * The cache will abort output a warning:
254
+ * If the key is invalid
255
+ *
256
+ * @param key - the key of the item
257
+ * @param options - the options of callback function
258
+ */
259
+ public getItem(key: string, options?: CacheItemOptions): any {
260
+ let ret: string | null = null;
261
+ const prefixedKey: string = this.cacheConfig.keyPrefix + key;
262
+
263
+ if (
264
+ prefixedKey === this.cacheConfig.keyPrefix ||
265
+ prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)
266
+ ) {
267
+ logger.warn(`Invalid key: should not be empty or 'CurSize'`);
268
+ return null;
269
+ }
270
+
271
+ // check whether it's in the cachelist
272
+ const presentKeyPrio: number = this.containsKey(key);
273
+ if (presentKeyPrio !== -1) {
274
+ if (this._isExpired(prefixedKey)) {
275
+ // if expired, remove that item and return null
276
+ this._removeItem(prefixedKey, presentKeyPrio - 1);
277
+ } else {
278
+ // if not expired, great, return the value and refresh it
279
+ ret = CacheObject.getItem(prefixedKey) ?? '';
280
+ const item: CacheItem = JSON.parse(ret);
281
+ this.cacheList[item.priority - 1].refresh(prefixedKey);
282
+ return item.data;
283
+ }
284
+ }
285
+
286
+ if (options && options.callback !== undefined) {
287
+ const val: object | string | number | boolean = options.callback();
288
+ if (val !== null) {
289
+ this.setItem(key, val, options);
290
+ }
291
+ return val;
292
+ }
293
+ return null;
294
+ }
295
+
296
+ /**
297
+ * remove item from the cache
298
+ *
299
+ * @param key - the key of the item
300
+ */
301
+ public removeItem(key: string): void {
302
+ const prefixedKey: string = this.cacheConfig.keyPrefix + key;
303
+
304
+ // check if the key is in the cache
305
+ const presentKeyPrio: number = this.containsKey(key);
306
+ if (presentKeyPrio !== -1) {
307
+ this._removeItem(prefixedKey, presentKeyPrio - 1);
308
+ }
309
+ }
310
+
311
+ /**
312
+ * clear the entire cache
313
+ */
314
+ public clear(): void {
315
+ for (let i = 0; i < this.maxPriority; i += 1) {
316
+ for (const key of this.cacheList[i].getKeys()) {
317
+ this._removeItem(key, i);
318
+ }
319
+ }
320
+ }
321
+
322
+ /**
323
+ * Return all the keys in the cache.
324
+ */
325
+ public getAllKeys(): string[] {
326
+ const keys: string[] = [];
327
+ for (let i = 0; i < this.maxPriority; i += 1) {
328
+ for (const key of this.cacheList[i].getKeys()) {
329
+ keys.push(key.substring(this.cacheConfig.keyPrefix.length));
330
+ }
331
+ }
332
+
333
+ return keys;
334
+ }
335
+
336
+ /**
337
+ * return the current size of the cache
338
+ *
339
+ * @return the current size of the cache
340
+ */
341
+ public getCacheCurSize(): number {
342
+ return this.curSizeInBytes;
343
+ }
344
+
345
+ /**
346
+ * Return a new instance of cache with customized configuration.
347
+ * @param config - the customized configuration
348
+ */
349
+ public createInstance(config: CacheConfig): ICache {
350
+ return new InMemoryCacheClass(config);
351
+ }
352
+ }
353
+
354
+ export const InMemoryCache: ICache = new InMemoryCacheClass();
@@ -0,0 +1,157 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { getCurrTime, getByteLength, defaultConfig } from './Utils';
5
+ import { AmplifyV6 } from '../singleton';
6
+ import { CacheConfig, CacheItem, CacheItemOptions } from './types';
7
+ import { ConsoleLogger as Logger } from '../Logger';
8
+
9
+ const logger = new Logger('StorageCache');
10
+
11
+ /**
12
+ * Initialization of the cache
13
+ *
14
+ */
15
+ export class StorageCache {
16
+ // Contains any fields that have been customized for this Cache instance (i.e. without default values)
17
+ private instanceConfig?: CacheConfig;
18
+
19
+ /**
20
+ * Initialize the cache
21
+ *
22
+ * @param config - Custom configuration for this instance.
23
+ */
24
+ constructor(config?: CacheConfig) {
25
+ if (config) {
26
+ // A configuration was specified for this specific instance
27
+ this.instanceConfig = config;
28
+ }
29
+
30
+ this.sanitizeConfig();
31
+ }
32
+
33
+ public getModuleName() {
34
+ return 'Cache';
35
+ }
36
+
37
+ private sanitizeConfig(): void {
38
+ const tempInstanceConfig = this.instanceConfig || ({} as CacheConfig);
39
+
40
+ if (this.cacheConfig.itemMaxSize > this.cacheConfig.capacityInBytes) {
41
+ logger.error(
42
+ 'Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.'
43
+ );
44
+ tempInstanceConfig.itemMaxSize = defaultConfig.itemMaxSize;
45
+ }
46
+
47
+ if (
48
+ this.cacheConfig.defaultPriority > 5 ||
49
+ this.cacheConfig.defaultPriority < 1
50
+ ) {
51
+ logger.error(
52
+ 'Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.'
53
+ );
54
+ tempInstanceConfig.defaultPriority = defaultConfig.defaultPriority;
55
+ }
56
+
57
+ if (
58
+ Number(this.cacheConfig.warningThreshold) > 1 ||
59
+ Number(this.cacheConfig.warningThreshold) < 0
60
+ ) {
61
+ logger.error(
62
+ 'Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.'
63
+ );
64
+ tempInstanceConfig.warningThreshold = defaultConfig.warningThreshold;
65
+ }
66
+
67
+ // Set 5MB limit
68
+ const cacheLimit: number = 5 * 1024 * 1024;
69
+ if (this.cacheConfig.capacityInBytes > cacheLimit) {
70
+ logger.error(
71
+ 'Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.'
72
+ );
73
+ tempInstanceConfig.capacityInBytes = defaultConfig.capacityInBytes;
74
+ }
75
+
76
+ // Apply sanitized values to the instance config
77
+ if (Object.keys(tempInstanceConfig).length > 0) {
78
+ this.instanceConfig = tempInstanceConfig;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * produce a JSON object with meta-data and data value
84
+ * @param value - the value of the item
85
+ * @param options - optional, the specified meta-data
86
+ *
87
+ * @return - the item which has the meta-data and the value
88
+ */
89
+ protected fillCacheItem(
90
+ key: string,
91
+ value: object | number | string | boolean,
92
+ options: CacheItemOptions
93
+ ): CacheItem {
94
+ const ret: CacheItem = {
95
+ key,
96
+ data: value,
97
+ timestamp: getCurrTime(),
98
+ visitedTime: getCurrTime(),
99
+ priority: options.priority ?? 0,
100
+ expires: options.expires ?? 0,
101
+ type: typeof value,
102
+ byteSize: 0,
103
+ };
104
+
105
+ ret.byteSize = getByteLength(JSON.stringify(ret));
106
+
107
+ // for accurate size
108
+ ret.byteSize = getByteLength(JSON.stringify(ret));
109
+ return ret;
110
+ }
111
+
112
+ /**
113
+ * Set custom configuration for the cache instance.
114
+ *
115
+ * @param config - customized configuration (without keyPrefix, which can't be changed)
116
+ *
117
+ * @return - the current configuration
118
+ */
119
+ public configure(config?: Omit<CacheConfig, 'keyPrefix'>): CacheConfig {
120
+ if (config) {
121
+ if ((config as CacheConfig).keyPrefix) {
122
+ logger.warn(
123
+ 'keyPrefix can not be re-configured on an existing Cache instance.'
124
+ );
125
+ }
126
+
127
+ this.instanceConfig = this.instanceConfig
128
+ ? Object.assign({}, this.instanceConfig, config)
129
+ : (config as CacheConfig);
130
+ }
131
+
132
+ this.sanitizeConfig();
133
+
134
+ return this.cacheConfig;
135
+ }
136
+
137
+ /**
138
+ * Returns an appropriate configuration for the Cache instance. Will apply any custom configuration for this
139
+ * instance on top of the global configuration. Default configuration will be applied in all cases.
140
+ *
141
+ * @internal
142
+ */
143
+ protected get cacheConfig(): CacheConfig {
144
+ const globalCacheConfig = AmplifyV6.getConfig().Cache || {};
145
+
146
+ if (this.instanceConfig) {
147
+ return Object.assign(
148
+ {},
149
+ defaultConfig,
150
+ globalCacheConfig,
151
+ this.instanceConfig
152
+ );
153
+ } else {
154
+ return Object.assign({}, defaultConfig, globalCacheConfig);
155
+ }
156
+ }
157
+ }
@@ -0,0 +1,198 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { asserts } from '../../Util/errors/AssertError';
5
+ import { CACHE_LIST_EXCEPTION } from '../../constants';
6
+
7
+ class DoubleLinkedNode {
8
+ key: string;
9
+ prevNode: DoubleLinkedNode | null;
10
+ nextNode: DoubleLinkedNode | null;
11
+
12
+ constructor(keyVal?: string) {
13
+ this.key = keyVal ? keyVal : '';
14
+ this.prevNode = null;
15
+ this.nextNode = null;
16
+ }
17
+ }
18
+
19
+ /**
20
+ * double linked list plus a hash table inside
21
+ * each key in the cache stored as a node in the list
22
+ * recently visited node will be rotated to the head
23
+ * so the Last Recently Visited node will be at the tail
24
+ *
25
+ * @member head - dummy head of the linked list
26
+ * @member tail - dummy tail of the linked list
27
+ * @member hashtable - the hashtable which maps cache key to list node
28
+ * @member length - length of the list
29
+ */
30
+ export default class CacheList {
31
+ private head: DoubleLinkedNode;
32
+ private tail: DoubleLinkedNode;
33
+ private hashtable: Record<string, any>;
34
+ private length: number;
35
+
36
+ /**
37
+ * initialization
38
+ */
39
+ constructor() {
40
+ this.head = new DoubleLinkedNode();
41
+ this.tail = new DoubleLinkedNode();
42
+ this.hashtable = {};
43
+ this.length = 0;
44
+
45
+ this.head.nextNode = this.tail;
46
+ this.tail.prevNode = this.head;
47
+ }
48
+
49
+ /**
50
+ * insert node to the head of the list
51
+ *
52
+ * @param node
53
+ */
54
+ private insertNodeToHead(node: DoubleLinkedNode) {
55
+ const tmp: DoubleLinkedNode | null = this.head.nextNode;
56
+ this.head.nextNode = node;
57
+ node.nextNode = tmp;
58
+ node.prevNode = this.head;
59
+ asserts(tmp !== null, {
60
+ name: CACHE_LIST_EXCEPTION,
61
+ message: 'previous node is null',
62
+ });
63
+ tmp.prevNode = node;
64
+
65
+ this.length = this.length + 1;
66
+ }
67
+
68
+ /**
69
+ * remove node
70
+ *
71
+ * @param node
72
+ */
73
+ private removeNode(node: DoubleLinkedNode): void {
74
+ asserts(node.prevNode !== null, {
75
+ name: CACHE_LIST_EXCEPTION,
76
+ message: 'previous node is null',
77
+ });
78
+ asserts(node.nextNode !== null, {
79
+ name: CACHE_LIST_EXCEPTION,
80
+ message: 'nextNode node is null',
81
+ });
82
+ node.prevNode.nextNode = node.nextNode;
83
+ node.nextNode.prevNode = node.prevNode;
84
+
85
+ node.prevNode = null;
86
+ node.nextNode = null;
87
+
88
+ this.length = this.length - 1;
89
+ }
90
+
91
+ /**
92
+ * @return true if list is empty
93
+ */
94
+ public isEmpty(): boolean {
95
+ return this.length === 0;
96
+ }
97
+
98
+ /**
99
+ * refresh node so it is rotated to the head
100
+ *
101
+ * @param key - key of the node
102
+ */
103
+ public refresh(key: string): void {
104
+ const node: DoubleLinkedNode = this.hashtable[key];
105
+ this.removeNode(node);
106
+ this.insertNodeToHead(node);
107
+ }
108
+
109
+ /**
110
+ * insert new node to the head and add it in the hashtable
111
+ *
112
+ * @param key - the key of the node
113
+ */
114
+ public insertItem(key: string): void {
115
+ const node: DoubleLinkedNode = new DoubleLinkedNode(key);
116
+ this.hashtable[key] = node;
117
+ this.insertNodeToHead(node);
118
+ }
119
+
120
+ /**
121
+ * @return the LAST Recently Visited key
122
+ */
123
+ public getLastItem(): string {
124
+ asserts(this.tail.prevNode !== null, {
125
+ name: CACHE_LIST_EXCEPTION,
126
+ message: 'previous node is null',
127
+ });
128
+ return this.tail.prevNode.key;
129
+ }
130
+
131
+ /**
132
+ * remove the cache key from the list and hashtable
133
+ * @param key - the key of the node
134
+ */
135
+ public removeItem(key: string): void {
136
+ const removedItem: DoubleLinkedNode = this.hashtable[key];
137
+ this.removeNode(removedItem);
138
+ delete this.hashtable[key];
139
+ }
140
+
141
+ /**
142
+ * @return length of the list
143
+ */
144
+ public getSize(): number {
145
+ return this.length;
146
+ }
147
+
148
+ /**
149
+ * @return true if the key is in the hashtable
150
+ * @param key
151
+ */
152
+ public containsKey(key: string): boolean {
153
+ return key in this.hashtable;
154
+ }
155
+
156
+ /**
157
+ * clean up the list and hashtable
158
+ */
159
+ public clearList(): void {
160
+ for (const key of Object.keys(this.hashtable)) {
161
+ if (this.hashtable.hasOwnProperty(key)) {
162
+ delete this.hashtable[key];
163
+ }
164
+ }
165
+ this.head.nextNode = this.tail;
166
+ this.tail.prevNode = this.head;
167
+ this.length = 0;
168
+ }
169
+
170
+ /**
171
+ * @return all keys in the hashtable
172
+ */
173
+ public getKeys(): string[] {
174
+ return Object.keys(this.hashtable);
175
+ }
176
+
177
+ /**
178
+ * mainly for test
179
+ *
180
+ * @param key
181
+ * @return true if key is the head node
182
+ */
183
+ public isHeadNode(key: string): boolean {
184
+ const node = this.hashtable[key];
185
+ return node.prevNode === this.head;
186
+ }
187
+
188
+ /**
189
+ * mainly for test
190
+ *
191
+ * @param key
192
+ * @return true if key is the tail node
193
+ */
194
+ public isTailNode(key: string): boolean {
195
+ const node = this.hashtable[key];
196
+ return node.nextNode === this.tail;
197
+ }
198
+ }