@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,129 @@
1
+ import { StorageCache } from './StorageCache';
2
+ import { ICache, CacheConfig, CacheItemOptions } from './types';
3
+ /**
4
+ * Customized in-memory cache with LRU implemented
5
+ * @member cacheObj - object which store items
6
+ * @member cacheList - list of keys in the cache with LRU
7
+ * @member curSizeInBytes - current size of the cache
8
+ * @member maxPriority - max of the priority
9
+ */
10
+ export declare class InMemoryCacheClass extends StorageCache implements ICache {
11
+ private cacheList;
12
+ private curSizeInBytes;
13
+ private maxPriority;
14
+ /**
15
+ * initialize the cache
16
+ *
17
+ * @param config - the configuration of the cache
18
+ */
19
+ constructor(config?: CacheConfig);
20
+ /**
21
+ * decrease current size of the cache
22
+ *
23
+ * @param amount - the amount of the cache size which needs to be decreased
24
+ */
25
+ private _decreaseCurSizeInBytes;
26
+ /**
27
+ * increase current size of the cache
28
+ *
29
+ * @param amount - the amount of the cache szie which need to be increased
30
+ */
31
+ private _increaseCurSizeInBytes;
32
+ /**
33
+ * check whether item is expired
34
+ *
35
+ * @param key - the key of the item
36
+ *
37
+ * @return true if the item is expired.
38
+ */
39
+ private _isExpired;
40
+ /**
41
+ * delete item from cache
42
+ *
43
+ * @param prefixedKey - the key of the item
44
+ * @param listIdx - indicates which cache list the key belongs to
45
+ */
46
+ private _removeItem;
47
+ /**
48
+ * put item into cache
49
+ *
50
+ * @param prefixedKey - the key of the item
51
+ * @param itemData - the value of the item
52
+ * @param itemSizeInBytes - the byte size of the item
53
+ * @param listIdx - indicates which cache list the key belongs to
54
+ */
55
+ private _setItem;
56
+ /**
57
+ * see whether cache is full
58
+ *
59
+ * @param itemSize
60
+ *
61
+ * @return true if cache is full
62
+ */
63
+ private _isCacheFull;
64
+ /**
65
+ * check whether the cache contains the key
66
+ *
67
+ * @param key
68
+ */
69
+ private containsKey;
70
+ /**
71
+ * * Set item into cache. You can put number, string, boolean or object.
72
+ * The cache will first check whether has the same key.
73
+ * If it has, it will delete the old item and then put the new item in
74
+ * The cache will pop out items if it is full
75
+ * You can specify the cache item options. The cache will abort and output a warning:
76
+ * If the key is invalid
77
+ * If the size of the item exceeds itemMaxSize.
78
+ * If the value is undefined
79
+ * If incorrect cache item configuration
80
+ * If error happened with browser storage
81
+ *
82
+ * @param key - the key of the item
83
+ * @param value - the value of the item
84
+ * @param options - optional, the specified meta-data
85
+ *
86
+ * @throws if the item is too big which exceeds the limit of single item size
87
+ * @throws if the key is invalid
88
+ */
89
+ setItem(key: string, value: object | string | number | boolean, options?: CacheItemOptions): void;
90
+ /**
91
+ * Get item from cache. It will return null if item doesn’t exist or it has been expired.
92
+ * If you specified callback function in the options,
93
+ * then the function will be executed if no such item in the cache
94
+ * and finally put the return value into cache.
95
+ * Please make sure the callback function will return the value you want to put into the cache.
96
+ * The cache will abort output a warning:
97
+ * If the key is invalid
98
+ *
99
+ * @param key - the key of the item
100
+ * @param options - the options of callback function
101
+ */
102
+ getItem(key: string, options?: CacheItemOptions): any;
103
+ /**
104
+ * remove item from the cache
105
+ *
106
+ * @param key - the key of the item
107
+ */
108
+ removeItem(key: string): void;
109
+ /**
110
+ * clear the entire cache
111
+ */
112
+ clear(): void;
113
+ /**
114
+ * Return all the keys in the cache.
115
+ */
116
+ getAllKeys(): string[];
117
+ /**
118
+ * return the current size of the cache
119
+ *
120
+ * @return the current size of the cache
121
+ */
122
+ getCacheCurSize(): number;
123
+ /**
124
+ * Return a new instance of cache with customized configuration.
125
+ * @param config - the customized configuration
126
+ */
127
+ createInstance(config: CacheConfig): ICache;
128
+ }
129
+ export declare const InMemoryCache: ICache;
@@ -0,0 +1,346 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ var __extends = (this && this.__extends) || (function () {
4
+ var extendStatics = function (d, b) {
5
+ extendStatics = Object.setPrototypeOf ||
6
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
7
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
8
+ return extendStatics(d, b);
9
+ };
10
+ return function (d, b) {
11
+ if (typeof b !== "function" && b !== null)
12
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
13
+ extendStatics(d, b);
14
+ function __() { this.constructor = d; }
15
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
16
+ };
17
+ })();
18
+ var __values = (this && this.__values) || function(o) {
19
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
20
+ if (m) return m.call(o);
21
+ if (o && typeof o.length === "number") return {
22
+ next: function () {
23
+ if (o && i >= o.length) o = void 0;
24
+ return { value: o && o[i++], done: !o };
25
+ }
26
+ };
27
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
28
+ };
29
+ import { CacheList, getCurrTime, CacheObject } from './Utils';
30
+ import { StorageCache } from './StorageCache';
31
+ import { ConsoleLogger as Logger } from '../Logger';
32
+ import { asserts } from '../Util/errors/AssertError';
33
+ import { STORAGE_CACHE_EXCEPTION } from '../constants';
34
+ import { getCurrSizeKey } from './Utils/CacheUtils';
35
+ var logger = new Logger('InMemoryCache');
36
+ /**
37
+ * Customized in-memory cache with LRU implemented
38
+ * @member cacheObj - object which store items
39
+ * @member cacheList - list of keys in the cache with LRU
40
+ * @member curSizeInBytes - current size of the cache
41
+ * @member maxPriority - max of the priority
42
+ */
43
+ var InMemoryCacheClass = /** @class */ (function (_super) {
44
+ __extends(InMemoryCacheClass, _super);
45
+ /**
46
+ * initialize the cache
47
+ *
48
+ * @param config - the configuration of the cache
49
+ */
50
+ function InMemoryCacheClass(config) {
51
+ var _this = _super.call(this, config) || this;
52
+ _this.cacheList = [];
53
+ _this.curSizeInBytes = 0;
54
+ _this.maxPriority = 5;
55
+ _this.getItem = _this.getItem.bind(_this);
56
+ _this.setItem = _this.setItem.bind(_this);
57
+ _this.removeItem = _this.removeItem.bind(_this);
58
+ // initialize list for every priority
59
+ for (var i = 0; i < _this.maxPriority; i += 1) {
60
+ _this.cacheList[i] = new CacheList();
61
+ }
62
+ return _this;
63
+ }
64
+ /**
65
+ * decrease current size of the cache
66
+ *
67
+ * @param amount - the amount of the cache size which needs to be decreased
68
+ */
69
+ InMemoryCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
70
+ this.curSizeInBytes -= amount;
71
+ };
72
+ /**
73
+ * increase current size of the cache
74
+ *
75
+ * @param amount - the amount of the cache szie which need to be increased
76
+ */
77
+ InMemoryCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
78
+ this.curSizeInBytes += amount;
79
+ };
80
+ /**
81
+ * check whether item is expired
82
+ *
83
+ * @param key - the key of the item
84
+ *
85
+ * @return true if the item is expired.
86
+ */
87
+ InMemoryCacheClass.prototype._isExpired = function (key) {
88
+ var text = CacheObject.getItem(key);
89
+ asserts(text !== null, {
90
+ name: STORAGE_CACHE_EXCEPTION,
91
+ message: 'item from storage is null',
92
+ });
93
+ var item = JSON.parse(text);
94
+ if (getCurrTime() >= item.expires) {
95
+ return true;
96
+ }
97
+ return false;
98
+ };
99
+ /**
100
+ * delete item from cache
101
+ *
102
+ * @param prefixedKey - the key of the item
103
+ * @param listIdx - indicates which cache list the key belongs to
104
+ */
105
+ InMemoryCacheClass.prototype._removeItem = function (prefixedKey, listIdx) {
106
+ // delete the key from the list
107
+ this.cacheList[listIdx].removeItem(prefixedKey);
108
+ // decrease the current size of the cache
109
+ var item = CacheObject.getItem(prefixedKey);
110
+ asserts(item !== null, {
111
+ name: STORAGE_CACHE_EXCEPTION,
112
+ message: 'item from storage is null',
113
+ });
114
+ this._decreaseCurSizeInBytes(JSON.parse(item).byteSize);
115
+ // finally remove the item from memory
116
+ CacheObject.removeItem(prefixedKey);
117
+ };
118
+ /**
119
+ * put item into cache
120
+ *
121
+ * @param prefixedKey - the key of the item
122
+ * @param itemData - the value of the item
123
+ * @param itemSizeInBytes - the byte size of the item
124
+ * @param listIdx - indicates which cache list the key belongs to
125
+ */
126
+ InMemoryCacheClass.prototype._setItem = function (prefixedKey, item, listIdx) {
127
+ // insert the key into the list
128
+ this.cacheList[listIdx].insertItem(prefixedKey);
129
+ // increase the current size of the cache
130
+ this._increaseCurSizeInBytes(item.byteSize);
131
+ // finally add the item into memory
132
+ CacheObject.setItem(prefixedKey, JSON.stringify(item));
133
+ };
134
+ /**
135
+ * see whether cache is full
136
+ *
137
+ * @param itemSize
138
+ *
139
+ * @return true if cache is full
140
+ */
141
+ InMemoryCacheClass.prototype._isCacheFull = function (itemSize) {
142
+ return this.curSizeInBytes + itemSize > this.cacheConfig.capacityInBytes;
143
+ };
144
+ /**
145
+ * check whether the cache contains the key
146
+ *
147
+ * @param key
148
+ */
149
+ InMemoryCacheClass.prototype.containsKey = function (key) {
150
+ var prefixedKey = this.cacheConfig.keyPrefix + key;
151
+ for (var i = 0; i < this.maxPriority; i += 1) {
152
+ if (this.cacheList[i].containsKey(prefixedKey)) {
153
+ return i + 1;
154
+ }
155
+ }
156
+ return -1;
157
+ };
158
+ /**
159
+ * * Set item into cache. You can put number, string, boolean or object.
160
+ * The cache will first check whether has the same key.
161
+ * If it has, it will delete the old item and then put the new item in
162
+ * The cache will pop out items if it is full
163
+ * You can specify the cache item options. The cache will abort and output a warning:
164
+ * If the key is invalid
165
+ * If the size of the item exceeds itemMaxSize.
166
+ * If the value is undefined
167
+ * If incorrect cache item configuration
168
+ * If error happened with browser storage
169
+ *
170
+ * @param key - the key of the item
171
+ * @param value - the value of the item
172
+ * @param options - optional, the specified meta-data
173
+ *
174
+ * @throws if the item is too big which exceeds the limit of single item size
175
+ * @throws if the key is invalid
176
+ */
177
+ InMemoryCacheClass.prototype.setItem = function (key, value, options) {
178
+ var prefixedKey = this.cacheConfig.keyPrefix + key;
179
+ // invalid keys
180
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
181
+ prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
182
+ logger.warn("Invalid key: should not be empty or 'CurSize'");
183
+ return;
184
+ }
185
+ if (typeof value === 'undefined') {
186
+ logger.warn("The value of item should not be undefined!");
187
+ return;
188
+ }
189
+ var cacheItemOptions = {
190
+ priority: options && options.priority !== undefined
191
+ ? options.priority
192
+ : this.cacheConfig.defaultPriority,
193
+ expires: options && options.expires !== undefined
194
+ ? options.expires
195
+ : this.cacheConfig.defaultTTL + getCurrTime(),
196
+ };
197
+ if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
198
+ logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
199
+ return;
200
+ }
201
+ var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
202
+ // check wether this item is too big;
203
+ if (item.byteSize > this.cacheConfig.itemMaxSize) {
204
+ logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
205
+ return;
206
+ }
207
+ // if key already in the cache, then delete it.
208
+ var presentKeyPrio = this.containsKey(key);
209
+ if (presentKeyPrio !== -1) {
210
+ this._removeItem(prefixedKey, presentKeyPrio - 1);
211
+ }
212
+ // pop out items in the cache when cache is full based on LRU
213
+ // first start from lowest priority cache list
214
+ var cacheListIdx = this.maxPriority - 1;
215
+ while (this._isCacheFull(item.byteSize) && cacheListIdx >= 0) {
216
+ if (!this.cacheList[cacheListIdx].isEmpty()) {
217
+ var popedItemKey = this.cacheList[cacheListIdx].getLastItem();
218
+ this._removeItem(popedItemKey, cacheListIdx);
219
+ }
220
+ else {
221
+ cacheListIdx -= 1;
222
+ }
223
+ }
224
+ this._setItem(prefixedKey, item, Number(item.priority) - 1);
225
+ };
226
+ /**
227
+ * Get item from cache. It will return null if item doesn’t exist or it has been expired.
228
+ * If you specified callback function in the options,
229
+ * then the function will be executed if no such item in the cache
230
+ * and finally put the return value into cache.
231
+ * Please make sure the callback function will return the value you want to put into the cache.
232
+ * The cache will abort output a warning:
233
+ * If the key is invalid
234
+ *
235
+ * @param key - the key of the item
236
+ * @param options - the options of callback function
237
+ */
238
+ InMemoryCacheClass.prototype.getItem = function (key, options) {
239
+ var _a;
240
+ var ret = null;
241
+ var prefixedKey = this.cacheConfig.keyPrefix + key;
242
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
243
+ prefixedKey === getCurrSizeKey(this.cacheConfig.keyPrefix)) {
244
+ logger.warn("Invalid key: should not be empty or 'CurSize'");
245
+ return null;
246
+ }
247
+ // check whether it's in the cachelist
248
+ var presentKeyPrio = this.containsKey(key);
249
+ if (presentKeyPrio !== -1) {
250
+ if (this._isExpired(prefixedKey)) {
251
+ // if expired, remove that item and return null
252
+ this._removeItem(prefixedKey, presentKeyPrio - 1);
253
+ }
254
+ else {
255
+ // if not expired, great, return the value and refresh it
256
+ ret = (_a = CacheObject.getItem(prefixedKey)) !== null && _a !== void 0 ? _a : '';
257
+ var item = JSON.parse(ret);
258
+ this.cacheList[item.priority - 1].refresh(prefixedKey);
259
+ return item.data;
260
+ }
261
+ }
262
+ if (options && options.callback !== undefined) {
263
+ var val = options.callback();
264
+ if (val !== null) {
265
+ this.setItem(key, val, options);
266
+ }
267
+ return val;
268
+ }
269
+ return null;
270
+ };
271
+ /**
272
+ * remove item from the cache
273
+ *
274
+ * @param key - the key of the item
275
+ */
276
+ InMemoryCacheClass.prototype.removeItem = function (key) {
277
+ var prefixedKey = this.cacheConfig.keyPrefix + key;
278
+ // check if the key is in the cache
279
+ var presentKeyPrio = this.containsKey(key);
280
+ if (presentKeyPrio !== -1) {
281
+ this._removeItem(prefixedKey, presentKeyPrio - 1);
282
+ }
283
+ };
284
+ /**
285
+ * clear the entire cache
286
+ */
287
+ InMemoryCacheClass.prototype.clear = function () {
288
+ var e_1, _a;
289
+ for (var i = 0; i < this.maxPriority; i += 1) {
290
+ try {
291
+ for (var _b = (e_1 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
292
+ var key = _c.value;
293
+ this._removeItem(key, i);
294
+ }
295
+ }
296
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
297
+ finally {
298
+ try {
299
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
300
+ }
301
+ finally { if (e_1) throw e_1.error; }
302
+ }
303
+ }
304
+ };
305
+ /**
306
+ * Return all the keys in the cache.
307
+ */
308
+ InMemoryCacheClass.prototype.getAllKeys = function () {
309
+ var e_2, _a;
310
+ var keys = [];
311
+ for (var i = 0; i < this.maxPriority; i += 1) {
312
+ try {
313
+ for (var _b = (e_2 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
314
+ var key = _c.value;
315
+ keys.push(key.substring(this.cacheConfig.keyPrefix.length));
316
+ }
317
+ }
318
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
319
+ finally {
320
+ try {
321
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
322
+ }
323
+ finally { if (e_2) throw e_2.error; }
324
+ }
325
+ }
326
+ return keys;
327
+ };
328
+ /**
329
+ * return the current size of the cache
330
+ *
331
+ * @return the current size of the cache
332
+ */
333
+ InMemoryCacheClass.prototype.getCacheCurSize = function () {
334
+ return this.curSizeInBytes;
335
+ };
336
+ /**
337
+ * Return a new instance of cache with customized configuration.
338
+ * @param config - the customized configuration
339
+ */
340
+ InMemoryCacheClass.prototype.createInstance = function (config) {
341
+ return new InMemoryCacheClass(config);
342
+ };
343
+ return InMemoryCacheClass;
344
+ }(StorageCache));
345
+ export { InMemoryCacheClass };
346
+ export var InMemoryCache = new InMemoryCacheClass();
@@ -0,0 +1,39 @@
1
+ import { CacheConfig, CacheItem, CacheItemOptions } from './types';
2
+ /**
3
+ * Initialization of the cache
4
+ *
5
+ */
6
+ export declare class StorageCache {
7
+ private instanceConfig?;
8
+ /**
9
+ * Initialize the cache
10
+ *
11
+ * @param config - Custom configuration for this instance.
12
+ */
13
+ constructor(config?: CacheConfig);
14
+ getModuleName(): string;
15
+ private sanitizeConfig;
16
+ /**
17
+ * produce a JSON object with meta-data and data value
18
+ * @param value - the value of the item
19
+ * @param options - optional, the specified meta-data
20
+ *
21
+ * @return - the item which has the meta-data and the value
22
+ */
23
+ protected fillCacheItem(key: string, value: object | number | string | boolean, options: CacheItemOptions): CacheItem;
24
+ /**
25
+ * Set custom configuration for the cache instance.
26
+ *
27
+ * @param config - customized configuration (without keyPrefix, which can't be changed)
28
+ *
29
+ * @return - the current configuration
30
+ */
31
+ configure(config?: Omit<CacheConfig, 'keyPrefix'>): CacheConfig;
32
+ /**
33
+ * Returns an appropriate configuration for the Cache instance. Will apply any custom configuration for this
34
+ * instance on top of the global configuration. Default configuration will be applied in all cases.
35
+ *
36
+ * @internal
37
+ */
38
+ protected get cacheConfig(): CacheConfig;
39
+ }
@@ -0,0 +1,118 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { getCurrTime, getByteLength, defaultConfig } from './Utils';
4
+ import { AmplifyV6 } from '../singleton';
5
+ import { ConsoleLogger as Logger } from '../Logger';
6
+ var logger = new Logger('StorageCache');
7
+ /**
8
+ * Initialization of the cache
9
+ *
10
+ */
11
+ var StorageCache = /** @class */ (function () {
12
+ /**
13
+ * Initialize the cache
14
+ *
15
+ * @param config - Custom configuration for this instance.
16
+ */
17
+ function StorageCache(config) {
18
+ if (config) {
19
+ // A configuration was specified for this specific instance
20
+ this.instanceConfig = config;
21
+ }
22
+ this.sanitizeConfig();
23
+ }
24
+ StorageCache.prototype.getModuleName = function () {
25
+ return 'Cache';
26
+ };
27
+ StorageCache.prototype.sanitizeConfig = function () {
28
+ var tempInstanceConfig = this.instanceConfig || {};
29
+ if (this.cacheConfig.itemMaxSize > this.cacheConfig.capacityInBytes) {
30
+ logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
31
+ tempInstanceConfig.itemMaxSize = defaultConfig.itemMaxSize;
32
+ }
33
+ if (this.cacheConfig.defaultPriority > 5 ||
34
+ this.cacheConfig.defaultPriority < 1) {
35
+ logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
36
+ tempInstanceConfig.defaultPriority = defaultConfig.defaultPriority;
37
+ }
38
+ if (Number(this.cacheConfig.warningThreshold) > 1 ||
39
+ Number(this.cacheConfig.warningThreshold) < 0) {
40
+ logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
41
+ tempInstanceConfig.warningThreshold = defaultConfig.warningThreshold;
42
+ }
43
+ // Set 5MB limit
44
+ var cacheLimit = 5 * 1024 * 1024;
45
+ if (this.cacheConfig.capacityInBytes > cacheLimit) {
46
+ logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
47
+ tempInstanceConfig.capacityInBytes = defaultConfig.capacityInBytes;
48
+ }
49
+ // Apply sanitized values to the instance config
50
+ if (Object.keys(tempInstanceConfig).length > 0) {
51
+ this.instanceConfig = tempInstanceConfig;
52
+ }
53
+ };
54
+ /**
55
+ * produce a JSON object with meta-data and data value
56
+ * @param value - the value of the item
57
+ * @param options - optional, the specified meta-data
58
+ *
59
+ * @return - the item which has the meta-data and the value
60
+ */
61
+ StorageCache.prototype.fillCacheItem = function (key, value, options) {
62
+ var _a, _b;
63
+ var ret = {
64
+ key: key,
65
+ data: value,
66
+ timestamp: getCurrTime(),
67
+ visitedTime: getCurrTime(),
68
+ priority: (_a = options.priority) !== null && _a !== void 0 ? _a : 0,
69
+ expires: (_b = options.expires) !== null && _b !== void 0 ? _b : 0,
70
+ type: typeof value,
71
+ byteSize: 0,
72
+ };
73
+ ret.byteSize = getByteLength(JSON.stringify(ret));
74
+ // for accurate size
75
+ ret.byteSize = getByteLength(JSON.stringify(ret));
76
+ return ret;
77
+ };
78
+ /**
79
+ * Set custom configuration for the cache instance.
80
+ *
81
+ * @param config - customized configuration (without keyPrefix, which can't be changed)
82
+ *
83
+ * @return - the current configuration
84
+ */
85
+ StorageCache.prototype.configure = function (config) {
86
+ if (config) {
87
+ if (config.keyPrefix) {
88
+ logger.warn('keyPrefix can not be re-configured on an existing Cache instance.');
89
+ }
90
+ this.instanceConfig = this.instanceConfig
91
+ ? Object.assign({}, this.instanceConfig, config)
92
+ : config;
93
+ }
94
+ this.sanitizeConfig();
95
+ return this.cacheConfig;
96
+ };
97
+ Object.defineProperty(StorageCache.prototype, "cacheConfig", {
98
+ /**
99
+ * Returns an appropriate configuration for the Cache instance. Will apply any custom configuration for this
100
+ * instance on top of the global configuration. Default configuration will be applied in all cases.
101
+ *
102
+ * @internal
103
+ */
104
+ get: function () {
105
+ var globalCacheConfig = AmplifyV6.getConfig().Cache || {};
106
+ if (this.instanceConfig) {
107
+ return Object.assign({}, defaultConfig, globalCacheConfig, this.instanceConfig);
108
+ }
109
+ else {
110
+ return Object.assign({}, defaultConfig, globalCacheConfig);
111
+ }
112
+ },
113
+ enumerable: false,
114
+ configurable: true
115
+ });
116
+ return StorageCache;
117
+ }());
118
+ export { StorageCache };