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