@aws-amplify/core 5.8.2 → 6.0.1-console-preview.7919075.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 +160 -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 +156 -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 +144 -0
  299. package/src/singleton/types.ts +48 -0
  300. package/src/types/types.ts +38 -1
@@ -0,0 +1,166 @@
1
+ import { StorageCache } from './StorageCache';
2
+ import { ICache, CacheConfig, CacheItemOptions } from './types';
3
+ /**
4
+ * Customized storage based on the SessionStorage or LocalStorage with LRU implemented
5
+ */
6
+ export declare class BrowserStorageCacheClass extends StorageCache implements ICache {
7
+ /**
8
+ * initialize the cache
9
+ * @param config - the configuration of the cache
10
+ */
11
+ constructor(config?: CacheConfig);
12
+ private getStorage;
13
+ /**
14
+ * decrease current size of the cache
15
+ *
16
+ * @private
17
+ * @param amount - the amount of the cache size which needs to be decreased
18
+ */
19
+ private _decreaseCurSizeInBytes;
20
+ /**
21
+ * increase current size of the cache
22
+ *
23
+ * @private
24
+ * @param amount - the amount of the cache szie which need to be increased
25
+ */
26
+ private _increaseCurSizeInBytes;
27
+ /**
28
+ * update the visited time if item has been visited
29
+ *
30
+ * @private
31
+ * @param item - the item which need to be refreshed
32
+ * @param prefixedKey - the key of the item
33
+ *
34
+ * @return the refreshed item
35
+ */
36
+ private _refreshItem;
37
+ /**
38
+ * check wether item is expired
39
+ *
40
+ * @private
41
+ * @param key - the key of the item
42
+ *
43
+ * @return true if the item is expired.
44
+ */
45
+ private _isExpired;
46
+ /**
47
+ * delete item from cache
48
+ *
49
+ * @private
50
+ * @param prefixedKey - the key of the item
51
+ * @param size - optional, the byte size of the item
52
+ */
53
+ private _removeItem;
54
+ /**
55
+ * put item into cache
56
+ *
57
+ * @private
58
+ * @param prefixedKey - the key of the item
59
+ * @param itemData - the value of the item
60
+ * @param itemSizeInBytes - the byte size of the item
61
+ */
62
+ private _setItem;
63
+ /**
64
+ * total space needed when poping out items
65
+ *
66
+ * @private
67
+ * @param itemSize
68
+ *
69
+ * @return total space needed
70
+ */
71
+ private _sizeToPop;
72
+ /**
73
+ * see whether cache is full
74
+ *
75
+ * @private
76
+ * @param itemSize
77
+ *
78
+ * @return true if cache is full
79
+ */
80
+ private _isCacheFull;
81
+ /**
82
+ * scan the storage and find out all the keys owned by this cache
83
+ * also clean the expired keys while scanning
84
+ *
85
+ * @private
86
+ *
87
+ * @return array of keys
88
+ */
89
+ private _findValidKeys;
90
+ /**
91
+ * get all the items we have, sort them by their priority,
92
+ * if priority is same, sort them by their last visited time
93
+ * pop out items from the low priority (5 is the lowest)
94
+ *
95
+ * @private
96
+ * @param keys - all the keys in this cache
97
+ * @param sizeToPop - the total size of the items which needed to be poped out
98
+ */
99
+ private _popOutItems;
100
+ /**
101
+ * Set item into cache. You can put number, string, boolean or object.
102
+ * The cache will first check whether has the same key.
103
+ * If it has, it will delete the old item and then put the new item in
104
+ * The cache will pop out items if it is full
105
+ * You can specify the cache item options. The cache will abort and output a warning:
106
+ * If the key is invalid
107
+ * If the size of the item exceeds itemMaxSize.
108
+ * If the value is undefined
109
+ * If incorrect cache item configuration
110
+ * If error happened with browser storage
111
+ *
112
+ * @param key - the key of the item
113
+ * @param value - the value of the item
114
+ * @param {Object} [options] - optional, the specified meta-data
115
+ */
116
+ setItem(key: string, value: object | number | string | boolean, options?: CacheItemOptions): void;
117
+ /**
118
+ * Get item from cache. It will return null if item doesn’t exist or it has been expired.
119
+ * If you specified callback function in the options,
120
+ * then the function will be executed if no such item in the cache
121
+ * and finally put the return value into cache.
122
+ * Please make sure the callback function will return the value you want to put into the cache.
123
+ * The cache will abort output a warning:
124
+ * If the key is invalid
125
+ * If error happened with browser storage
126
+ *
127
+ * @param key - the key of the item
128
+ * @param {Object} [options] - the options of callback function
129
+ *
130
+ * @return - return the value of the item
131
+ */
132
+ getItem(key: string, options?: CacheItemOptions): any;
133
+ /**
134
+ * remove item from the cache
135
+ * The cache will abort output a warning:
136
+ * If error happened with browser storage
137
+ * @param key - the key of the item
138
+ */
139
+ removeItem(key: string): void;
140
+ /**
141
+ * clear the entire cache
142
+ * The cache will abort output a warning:
143
+ * If error happened with browser storage
144
+ */
145
+ clear(): void;
146
+ /**
147
+ * Return all the keys in the cache.
148
+ *
149
+ * @return - all keys in the cache
150
+ */
151
+ getAllKeys(): string[];
152
+ /**
153
+ * return the current size of the cache
154
+ *
155
+ * @return - current size of the cache
156
+ */
157
+ getCacheCurSize(): number;
158
+ /**
159
+ * Return a new instance of cache with customized configuration.
160
+ * @param config - the customized configuration
161
+ *
162
+ * @return - new instance of Cache
163
+ */
164
+ createInstance(config: CacheConfig): ICache;
165
+ }
166
+ export declare const BrowserStorageCache: ICache;
@@ -0,0 +1,469 @@
1
+ "use strict";
2
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+ // SPDX-License-Identifier: Apache-2.0
4
+ var __extends = (this && this.__extends) || (function () {
5
+ var extendStatics = function (d, b) {
6
+ extendStatics = Object.setPrototypeOf ||
7
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
8
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
9
+ return extendStatics(d, b);
10
+ };
11
+ return function (d, b) {
12
+ if (typeof b !== "function" && b !== null)
13
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
14
+ extendStatics(d, b);
15
+ function __() { this.constructor = d; }
16
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
17
+ };
18
+ })();
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.BrowserStorageCache = exports.BrowserStorageCacheClass = void 0;
21
+ var Logger_1 = require("../Logger");
22
+ var Utils_1 = require("./Utils");
23
+ var StorageCache_1 = require("./StorageCache");
24
+ var AssertError_1 = require("../Util/errors/AssertError");
25
+ var constants_1 = require("../constants");
26
+ var CacheUtils_1 = require("./Utils/CacheUtils");
27
+ var logger = new Logger_1.ConsoleLogger('Cache');
28
+ /**
29
+ * Customized storage based on the SessionStorage or LocalStorage with LRU implemented
30
+ */
31
+ var BrowserStorageCacheClass = /** @class */ (function (_super) {
32
+ __extends(BrowserStorageCacheClass, _super);
33
+ /**
34
+ * initialize the cache
35
+ * @param config - the configuration of the cache
36
+ */
37
+ function BrowserStorageCacheClass(config) {
38
+ var _this = _super.call(this, config) || this;
39
+ (0, AssertError_1.asserts)(!!_this.cacheConfig.storage, {
40
+ name: constants_1.STORAGE_CACHE_EXCEPTION,
41
+ message: 'Storage is not defined in the cache config',
42
+ });
43
+ _this.cacheConfig.storage = _this.cacheConfig.storage;
44
+ _this.getItem = _this.getItem.bind(_this);
45
+ _this.setItem = _this.setItem.bind(_this);
46
+ _this.removeItem = _this.removeItem.bind(_this);
47
+ return _this;
48
+ }
49
+ BrowserStorageCacheClass.prototype.getStorage = function () {
50
+ (0, AssertError_1.asserts)(!!this.cacheConfig.storage, {
51
+ name: constants_1.STORAGE_CACHE_EXCEPTION,
52
+ message: 'Storage is not defined in the cache config',
53
+ });
54
+ return this.cacheConfig.storage;
55
+ };
56
+ /**
57
+ * decrease current size of the cache
58
+ *
59
+ * @private
60
+ * @param amount - the amount of the cache size which needs to be decreased
61
+ */
62
+ BrowserStorageCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
63
+ var curSize = this.getCacheCurSize();
64
+ this.getStorage().setItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix), (curSize - amount).toString());
65
+ };
66
+ /**
67
+ * increase current size of the cache
68
+ *
69
+ * @private
70
+ * @param amount - the amount of the cache szie which need to be increased
71
+ */
72
+ BrowserStorageCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
73
+ var curSize = this.getCacheCurSize();
74
+ this.getStorage().setItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix), (curSize + amount).toString());
75
+ };
76
+ /**
77
+ * update the visited time if item has been visited
78
+ *
79
+ * @private
80
+ * @param item - the item which need to be refreshed
81
+ * @param prefixedKey - the key of the item
82
+ *
83
+ * @return the refreshed item
84
+ */
85
+ BrowserStorageCacheClass.prototype._refreshItem = function (item, prefixedKey) {
86
+ item.visitedTime = (0, Utils_1.getCurrTime)();
87
+ this.getStorage().setItem(prefixedKey, JSON.stringify(item));
88
+ return item;
89
+ };
90
+ /**
91
+ * check wether item is expired
92
+ *
93
+ * @private
94
+ * @param key - the key of the item
95
+ *
96
+ * @return true if the item is expired.
97
+ */
98
+ BrowserStorageCacheClass.prototype._isExpired = function (key) {
99
+ var text = this.getStorage().getItem(key);
100
+ (0, AssertError_1.asserts)(text !== null, {
101
+ name: constants_1.STORAGE_CACHE_EXCEPTION,
102
+ message: "Item not found in the storage by the key: ".concat(key, "."),
103
+ });
104
+ var item = JSON.parse(text);
105
+ if ((0, Utils_1.getCurrTime)() >= item.expires) {
106
+ return true;
107
+ }
108
+ return false;
109
+ };
110
+ /**
111
+ * delete item from cache
112
+ *
113
+ * @private
114
+ * @param prefixedKey - the key of the item
115
+ * @param size - optional, the byte size of the item
116
+ */
117
+ BrowserStorageCacheClass.prototype._removeItem = function (prefixedKey, size) {
118
+ var item = this.getStorage().getItem(prefixedKey);
119
+ (0, AssertError_1.asserts)(item !== null, {
120
+ name: constants_1.STORAGE_CACHE_EXCEPTION,
121
+ message: "Item not found in the storage by the key: ".concat(prefixedKey, "."),
122
+ });
123
+ var itemSize = size !== null && size !== void 0 ? size : JSON.parse(item).byteSize;
124
+ this._decreaseCurSizeInBytes(itemSize);
125
+ // remove the cache item
126
+ this.getStorage().removeItem(prefixedKey);
127
+ };
128
+ /**
129
+ * put item into cache
130
+ *
131
+ * @private
132
+ * @param prefixedKey - the key of the item
133
+ * @param itemData - the value of the item
134
+ * @param itemSizeInBytes - the byte size of the item
135
+ */
136
+ BrowserStorageCacheClass.prototype._setItem = function (prefixedKey, item) {
137
+ // update the cache size
138
+ this._increaseCurSizeInBytes(item.byteSize);
139
+ try {
140
+ this.getStorage().setItem(prefixedKey, JSON.stringify(item));
141
+ }
142
+ catch (setItemErr) {
143
+ // if failed, we need to rollback the cache size
144
+ this._decreaseCurSizeInBytes(item.byteSize);
145
+ logger.error("Failed to set item ".concat(setItemErr));
146
+ }
147
+ };
148
+ /**
149
+ * total space needed when poping out items
150
+ *
151
+ * @private
152
+ * @param itemSize
153
+ *
154
+ * @return total space needed
155
+ */
156
+ BrowserStorageCacheClass.prototype._sizeToPop = function (itemSize) {
157
+ var spaceItemNeed = this.getCacheCurSize() + itemSize - this.cacheConfig.capacityInBytes;
158
+ var cacheThresholdSpace = (1 - this.cacheConfig.warningThreshold) *
159
+ this.cacheConfig.capacityInBytes;
160
+ return spaceItemNeed > cacheThresholdSpace
161
+ ? spaceItemNeed
162
+ : cacheThresholdSpace;
163
+ };
164
+ /**
165
+ * see whether cache is full
166
+ *
167
+ * @private
168
+ * @param itemSize
169
+ *
170
+ * @return true if cache is full
171
+ */
172
+ BrowserStorageCacheClass.prototype._isCacheFull = function (itemSize) {
173
+ return itemSize + this.getCacheCurSize() > this.cacheConfig.capacityInBytes;
174
+ };
175
+ /**
176
+ * scan the storage and find out all the keys owned by this cache
177
+ * also clean the expired keys while scanning
178
+ *
179
+ * @private
180
+ *
181
+ * @return array of keys
182
+ */
183
+ BrowserStorageCacheClass.prototype._findValidKeys = function () {
184
+ var keys = [];
185
+ var keyInCache = [];
186
+ // get all keys in Storage
187
+ for (var i = 0; i < this.getStorage().length; i += 1) {
188
+ var key = this.getStorage().key(i);
189
+ if (key) {
190
+ keyInCache.push(key);
191
+ }
192
+ }
193
+ // find those items which belong to our cache and also clean those expired items
194
+ for (var i = 0; i < keyInCache.length; i += 1) {
195
+ var key = keyInCache[i];
196
+ if (key.indexOf(this.cacheConfig.keyPrefix) === 0 &&
197
+ key !== (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
198
+ if (this._isExpired(key)) {
199
+ this._removeItem(key);
200
+ }
201
+ else {
202
+ keys.push(key);
203
+ }
204
+ }
205
+ }
206
+ return keys;
207
+ };
208
+ /**
209
+ * get all the items we have, sort them by their priority,
210
+ * if priority is same, sort them by their last visited time
211
+ * pop out items from the low priority (5 is the lowest)
212
+ *
213
+ * @private
214
+ * @param keys - all the keys in this cache
215
+ * @param sizeToPop - the total size of the items which needed to be poped out
216
+ */
217
+ BrowserStorageCacheClass.prototype._popOutItems = function (keys, sizeToPop) {
218
+ var items = [];
219
+ var remainedSize = sizeToPop;
220
+ // get the items from Storage
221
+ for (var i = 0; i < keys.length; i += 1) {
222
+ var val = this.getStorage().getItem(keys[i]);
223
+ if (val != null) {
224
+ var item = JSON.parse(val);
225
+ items.push(item);
226
+ }
227
+ }
228
+ // first compare priority
229
+ // then compare visited time
230
+ items.sort(function (a, b) {
231
+ if (a.priority > b.priority) {
232
+ return -1;
233
+ }
234
+ else if (a.priority < b.priority) {
235
+ return 1;
236
+ }
237
+ else {
238
+ if (a.visitedTime < b.visitedTime) {
239
+ return -1;
240
+ }
241
+ else
242
+ return 1;
243
+ }
244
+ });
245
+ for (var i = 0; i < items.length; i += 1) {
246
+ // pop out items until we have enough room for new item
247
+ this._removeItem(items[i].key, items[i].byteSize);
248
+ remainedSize -= items[i].byteSize;
249
+ if (remainedSize <= 0) {
250
+ return;
251
+ }
252
+ }
253
+ };
254
+ /**
255
+ * Set item into cache. You can put number, string, boolean or object.
256
+ * The cache will first check whether has the same key.
257
+ * If it has, it will delete the old item and then put the new item in
258
+ * The cache will pop out items if it is full
259
+ * You can specify the cache item options. The cache will abort and output a warning:
260
+ * If the key is invalid
261
+ * If the size of the item exceeds itemMaxSize.
262
+ * If the value is undefined
263
+ * If incorrect cache item configuration
264
+ * If error happened with browser storage
265
+ *
266
+ * @param key - the key of the item
267
+ * @param value - the value of the item
268
+ * @param {Object} [options] - optional, the specified meta-data
269
+ */
270
+ BrowserStorageCacheClass.prototype.setItem = function (key, value, options) {
271
+ logger.log("Set item: key is ".concat(key, ", value is ").concat(value, " with options: ").concat(options));
272
+ var prefixedKey = this.cacheConfig.keyPrefix + key;
273
+ // invalid keys
274
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
275
+ prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
276
+ logger.warn("Invalid key: should not be empty or 'CurSize'");
277
+ return;
278
+ }
279
+ if (typeof value === 'undefined') {
280
+ logger.warn("The value of item should not be undefined!");
281
+ return;
282
+ }
283
+ var cacheItemOptions = {
284
+ priority: options && options.priority !== undefined
285
+ ? options.priority
286
+ : this.cacheConfig.defaultPriority,
287
+ expires: options && options.expires !== undefined
288
+ ? options.expires
289
+ : this.cacheConfig.defaultTTL + (0, Utils_1.getCurrTime)(),
290
+ };
291
+ if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
292
+ logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
293
+ return;
294
+ }
295
+ var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
296
+ // check wether this item is too big;
297
+ if (item.byteSize > this.cacheConfig.itemMaxSize) {
298
+ logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
299
+ return;
300
+ }
301
+ try {
302
+ // first look into the storage, if it exists, delete it.
303
+ var val = this.getStorage().getItem(prefixedKey);
304
+ if (val) {
305
+ this._removeItem(prefixedKey, JSON.parse(val).byteSize);
306
+ }
307
+ // check whether the cache is full
308
+ if (this._isCacheFull(item.byteSize)) {
309
+ var validKeys = this._findValidKeys();
310
+ // check again and then pop out items
311
+ if (this._isCacheFull(item.byteSize)) {
312
+ var sizeToPop = this._sizeToPop(item.byteSize);
313
+ this._popOutItems(validKeys, sizeToPop);
314
+ }
315
+ }
316
+ // put item in the cache
317
+ // may failed due to storage full
318
+ this._setItem(prefixedKey, item);
319
+ }
320
+ catch (e) {
321
+ logger.warn("setItem failed! ".concat(e));
322
+ }
323
+ };
324
+ /**
325
+ * Get item from cache. It will return null if item doesn’t exist or it has been expired.
326
+ * If you specified callback function in the options,
327
+ * then the function will be executed if no such item in the cache
328
+ * and finally put the return value into cache.
329
+ * Please make sure the callback function will return the value you want to put into the cache.
330
+ * The cache will abort output a warning:
331
+ * If the key is invalid
332
+ * If error happened with browser storage
333
+ *
334
+ * @param key - the key of the item
335
+ * @param {Object} [options] - the options of callback function
336
+ *
337
+ * @return - return the value of the item
338
+ */
339
+ BrowserStorageCacheClass.prototype.getItem = function (key, options) {
340
+ logger.log("Get item: key is ".concat(key, " with options ").concat(options));
341
+ var ret = null;
342
+ var prefixedKey = this.cacheConfig.keyPrefix + key;
343
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
344
+ prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
345
+ logger.warn("Invalid key: should not be empty or 'CurSize'");
346
+ return null;
347
+ }
348
+ try {
349
+ ret = this.getStorage().getItem(prefixedKey);
350
+ if (ret != null) {
351
+ if (this._isExpired(prefixedKey)) {
352
+ // if expired, remove that item and return null
353
+ this._removeItem(prefixedKey, JSON.parse(ret).byteSize);
354
+ ret = null;
355
+ }
356
+ else {
357
+ // if not expired, great, return the value and refresh it
358
+ var item = JSON.parse(ret);
359
+ item = this._refreshItem(item, prefixedKey);
360
+ return item.data;
361
+ }
362
+ }
363
+ if (options && options.callback !== undefined) {
364
+ var val = options.callback();
365
+ if (val !== null) {
366
+ this.setItem(key, val, options);
367
+ }
368
+ return val;
369
+ }
370
+ return null;
371
+ }
372
+ catch (e) {
373
+ logger.warn("getItem failed! ".concat(e));
374
+ return null;
375
+ }
376
+ };
377
+ /**
378
+ * remove item from the cache
379
+ * The cache will abort output a warning:
380
+ * If error happened with browser storage
381
+ * @param key - the key of the item
382
+ */
383
+ BrowserStorageCacheClass.prototype.removeItem = function (key) {
384
+ logger.log("Remove item: key is ".concat(key));
385
+ var prefixedKey = this.cacheConfig.keyPrefix + key;
386
+ if (prefixedKey === this.cacheConfig.keyPrefix ||
387
+ prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
388
+ return;
389
+ }
390
+ try {
391
+ var val = this.getStorage().getItem(prefixedKey);
392
+ if (val) {
393
+ this._removeItem(prefixedKey, JSON.parse(val).byteSize);
394
+ }
395
+ }
396
+ catch (e) {
397
+ logger.warn("removeItem failed! ".concat(e));
398
+ }
399
+ };
400
+ /**
401
+ * clear the entire cache
402
+ * The cache will abort output a warning:
403
+ * If error happened with browser storage
404
+ */
405
+ BrowserStorageCacheClass.prototype.clear = function () {
406
+ logger.log("Clear Cache");
407
+ var keysToRemove = [];
408
+ for (var i = 0; i < this.getStorage().length; i += 1) {
409
+ var key = this.getStorage().key(i);
410
+ if ((key === null || key === void 0 ? void 0 : key.indexOf(this.cacheConfig.keyPrefix)) === 0) {
411
+ keysToRemove.push(key);
412
+ }
413
+ }
414
+ try {
415
+ for (var i = 0; i < keysToRemove.length; i += 1) {
416
+ this.getStorage().removeItem(keysToRemove[i]);
417
+ }
418
+ }
419
+ catch (e) {
420
+ logger.warn("clear failed! ".concat(e));
421
+ }
422
+ };
423
+ /**
424
+ * Return all the keys in the cache.
425
+ *
426
+ * @return - all keys in the cache
427
+ */
428
+ BrowserStorageCacheClass.prototype.getAllKeys = function () {
429
+ var keys = [];
430
+ for (var i = 0; i < this.getStorage().length; i += 1) {
431
+ var key = this.getStorage().key(i);
432
+ if (key &&
433
+ key.indexOf(this.cacheConfig.keyPrefix) === 0 &&
434
+ key !== (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
435
+ keys.push(key.substring(this.cacheConfig.keyPrefix.length));
436
+ }
437
+ }
438
+ return keys;
439
+ };
440
+ /**
441
+ * return the current size of the cache
442
+ *
443
+ * @return - current size of the cache
444
+ */
445
+ BrowserStorageCacheClass.prototype.getCacheCurSize = function () {
446
+ var ret = this.getStorage().getItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix));
447
+ if (!ret) {
448
+ this.getStorage().setItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix), '0');
449
+ ret = '0';
450
+ }
451
+ return Number(ret);
452
+ };
453
+ /**
454
+ * Return a new instance of cache with customized configuration.
455
+ * @param config - the customized configuration
456
+ *
457
+ * @return - new instance of Cache
458
+ */
459
+ BrowserStorageCacheClass.prototype.createInstance = function (config) {
460
+ if (!config.keyPrefix || config.keyPrefix === Utils_1.defaultConfig.keyPrefix) {
461
+ logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
462
+ config.keyPrefix = Utils_1.getCurrTime.toString();
463
+ }
464
+ return new BrowserStorageCacheClass(config);
465
+ };
466
+ return BrowserStorageCacheClass;
467
+ }(StorageCache_1.StorageCache));
468
+ exports.BrowserStorageCacheClass = BrowserStorageCacheClass;
469
+ exports.BrowserStorageCache = new BrowserStorageCacheClass();