@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,21 +1,17 @@
1
1
  "use strict";
2
2
  // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
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 __());
4
+ var __assign = (this && this.__assign) || function () {
5
+ __assign = Object.assign || function(t) {
6
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
7
+ s = arguments[i];
8
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
9
+ t[p] = s[p];
10
+ }
11
+ return t;
17
12
  };
18
- })();
13
+ return __assign.apply(this, arguments);
14
+ };
19
15
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
20
16
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
21
17
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -52,117 +48,328 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
52
48
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
53
49
  }
54
50
  };
51
+ var __values = (this && this.__values) || function(o) {
52
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
53
+ if (m) return m.call(o);
54
+ if (o && typeof o.length === "number") return {
55
+ next: function () {
56
+ if (o && i >= o.length) o = void 0;
57
+ return { value: o && o[i++], done: !o };
58
+ }
59
+ };
60
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
61
+ };
55
62
  Object.defineProperty(exports, "__esModule", { value: true });
56
- exports.Cache = exports.AsyncStorage = exports.AsyncStorageCache = void 0;
57
- var react_native_1 = require("@aws-amplify/react-native");
63
+ exports.StorageCacheCommon = void 0;
64
+ var singleton_1 = require("../singleton");
58
65
  var Logger_1 = require("../Logger");
59
- var StorageCache_1 = require("./StorageCache");
60
- var Utils_1 = require("./Utils");
61
- var CacheUtils_1 = require("./Utils/CacheUtils");
62
- var errorHelpers_1 = require("./Utils/errorHelpers");
63
- var logger = new Logger_1.ConsoleLogger('AsyncStorageCache');
64
- var AsyncStorage = (0, react_native_1.loadAsyncStorage)();
65
- exports.AsyncStorage = AsyncStorage;
66
- /*
67
- * Customized cache which based on the AsyncStorage with LRU implemented
66
+ var constants_1 = require("./constants");
67
+ var utils_1 = require("./utils");
68
+ var errorHelpers_1 = require("./utils/errorHelpers");
69
+ var logger = new Logger_1.ConsoleLogger('StorageCache');
70
+ /**
71
+ * Initialization of the cache
72
+ *
68
73
  */
69
- var AsyncStorageCache = /** @class */ (function (_super) {
70
- __extends(AsyncStorageCache, _super);
74
+ var StorageCacheCommon = /** @class */ (function () {
71
75
  /**
72
- * initialize the cache
76
+ * Initialize the cache
73
77
  *
74
- * @param {Object} config - the configuration of the cache
78
+ * @param config - Custom configuration for this instance.
75
79
  */
76
- function AsyncStorageCache(config) {
77
- var _this = _super.call(this, config) || this;
78
- _this.getItem = _this.getItem.bind(_this);
79
- _this.setItem = _this.setItem.bind(_this);
80
- _this.removeItem = _this.removeItem.bind(_this);
81
- logger.debug('Using AsyncStorageCache');
82
- return _this;
80
+ function StorageCacheCommon(_a) {
81
+ var config = _a.config, keyValueStorage = _a.keyValueStorage;
82
+ var _b;
83
+ var globalCacheConfig = (_b = singleton_1.Amplify.getConfig().Cache) !== null && _b !== void 0 ? _b : {};
84
+ this.config = __assign(__assign(__assign({}, constants_1.defaultConfig), globalCacheConfig), config);
85
+ this.keyValueStorage = keyValueStorage;
86
+ this.sanitizeConfig();
83
87
  }
88
+ StorageCacheCommon.prototype.getModuleName = function () {
89
+ return 'Cache';
90
+ };
84
91
  /**
85
- * decrease current size of the cache
86
- * @private
87
- * @param amount - the amount of the cache size which needs to be decreased
92
+ * Set custom configuration for the cache instance.
93
+ *
94
+ * @param config - customized configuration (without keyPrefix, which can't be changed)
95
+ *
96
+ * @return - the current configuration
88
97
  */
89
- AsyncStorageCache.prototype._decreaseCurSizeInBytes = function (amount) {
98
+ StorageCacheCommon.prototype.configure = function (config) {
99
+ if (config) {
100
+ if (config.keyPrefix) {
101
+ logger.warn('keyPrefix can not be re-configured on an existing Cache instance.');
102
+ }
103
+ this.config = __assign(__assign({}, this.config), config);
104
+ }
105
+ this.sanitizeConfig();
106
+ return this.config;
107
+ };
108
+ /**
109
+ * return the current size of the cache
110
+ * @return {Promise}
111
+ */
112
+ StorageCacheCommon.prototype.getCurrentCacheSize = function () {
90
113
  return __awaiter(this, void 0, void 0, function () {
91
- var curSize;
114
+ var size;
92
115
  return __generator(this, function (_a) {
93
116
  switch (_a.label) {
94
- case 0: return [4 /*yield*/, this.getCacheCurSize()];
117
+ case 0: return [4 /*yield*/, this.getStorage().getItem((0, utils_1.getCurrentSizeKey)(this.config.keyPrefix))];
95
118
  case 1:
96
- curSize = _a.sent();
97
- return [4 /*yield*/, AsyncStorage.setItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix), (curSize - amount).toString())];
119
+ size = _a.sent();
120
+ if (!!size) return [3 /*break*/, 3];
121
+ return [4 /*yield*/, this.getStorage().setItem((0, utils_1.getCurrentSizeKey)(this.config.keyPrefix), '0')];
98
122
  case 2:
99
123
  _a.sent();
100
- return [2 /*return*/];
124
+ size = '0';
125
+ _a.label = 3;
126
+ case 3: return [2 /*return*/, Number(size)];
101
127
  }
102
128
  });
103
129
  });
104
130
  };
105
131
  /**
106
- * increase current size of the cache
107
- * @private
108
- * @param amount - the amount of the cache szie which need to be increased
132
+ * Set item into cache. You can put number, string, boolean or object.
133
+ * The cache will first check whether has the same key.
134
+ * If it has, it will delete the old item and then put the new item in
135
+ * The cache will pop out items if it is full
136
+ * You can specify the cache item options. The cache will abort and output a warning:
137
+ * If the key is invalid
138
+ * If the size of the item exceeds itemMaxSize.
139
+ * If the value is undefined
140
+ * If incorrect cache item configuration
141
+ * If error happened with browser storage
142
+ *
143
+ * @param {String} key - the key of the item
144
+ * @param {Object} value - the value of the item
145
+ * @param {Object} [options] - optional, the specified meta-data
146
+ *
147
+ * @return {Promise}
109
148
  */
110
- AsyncStorageCache.prototype._increaseCurSizeInBytes = function (amount) {
149
+ StorageCacheCommon.prototype.setItem = function (key, value, options) {
111
150
  return __awaiter(this, void 0, void 0, function () {
112
- var curSize;
151
+ var cacheItemOptions, prefixedKey, item, val, validKeys, sizeToPop, e_1;
113
152
  return __generator(this, function (_a) {
114
153
  switch (_a.label) {
115
- case 0: return [4 /*yield*/, this.getCacheCurSize()];
154
+ case 0:
155
+ logger.debug("Set item: key is ".concat(key, ", value is ").concat(value, " with options: ").concat(options));
156
+ if (!key || key === constants_1.currentSizeKey) {
157
+ logger.warn("Invalid key: should not be empty or reserved key: '".concat(constants_1.currentSizeKey, "'"));
158
+ return [2 /*return*/];
159
+ }
160
+ if (typeof value === 'undefined') {
161
+ logger.warn("The value of item should not be undefined!");
162
+ return [2 /*return*/];
163
+ }
164
+ cacheItemOptions = {
165
+ priority: (options === null || options === void 0 ? void 0 : options.priority) !== undefined
166
+ ? options.priority
167
+ : this.config.defaultPriority,
168
+ expires: (options === null || options === void 0 ? void 0 : options.expires) !== undefined
169
+ ? options.expires
170
+ : this.config.defaultTTL + (0, utils_1.getCurrentTime)(),
171
+ };
172
+ if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
173
+ logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
174
+ return [2 /*return*/];
175
+ }
176
+ prefixedKey = "".concat(this.config.keyPrefix).concat(key);
177
+ item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
178
+ // check whether this item is too big;
179
+ if (item.byteSize > this.config.itemMaxSize) {
180
+ logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
181
+ return [2 /*return*/];
182
+ }
183
+ _a.label = 1;
116
184
  case 1:
117
- curSize = _a.sent();
118
- return [4 /*yield*/, AsyncStorage.setItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix), (curSize + amount).toString())];
185
+ _a.trys.push([1, 11, , 12]);
186
+ return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
119
187
  case 2:
188
+ val = _a.sent();
189
+ if (!val) return [3 /*break*/, 4];
190
+ return [4 /*yield*/, this.removeCacheItem(prefixedKey, JSON.parse(val).byteSize)];
191
+ case 3:
120
192
  _a.sent();
121
- return [2 /*return*/];
193
+ _a.label = 4;
194
+ case 4: return [4 /*yield*/, this.isCacheFull(item.byteSize)];
195
+ case 5:
196
+ if (!_a.sent()) return [3 /*break*/, 10];
197
+ return [4 /*yield*/, this.clearInvalidAndGetRemainingKeys()];
198
+ case 6:
199
+ validKeys = _a.sent();
200
+ return [4 /*yield*/, this.isCacheFull(item.byteSize)];
201
+ case 7:
202
+ if (!_a.sent()) return [3 /*break*/, 10];
203
+ return [4 /*yield*/, this.sizeToPop(item.byteSize)];
204
+ case 8:
205
+ sizeToPop = _a.sent();
206
+ return [4 /*yield*/, this.popOutItems(validKeys, sizeToPop)];
207
+ case 9:
208
+ _a.sent();
209
+ _a.label = 10;
210
+ case 10:
211
+ // put item in the cache
212
+ return [2 /*return*/, this.setCacheItem(prefixedKey, item)];
213
+ case 11:
214
+ e_1 = _a.sent();
215
+ logger.warn("setItem failed! ".concat(e_1));
216
+ return [3 /*break*/, 12];
217
+ case 12: return [2 /*return*/];
122
218
  }
123
219
  });
124
220
  });
125
221
  };
126
222
  /**
127
- * update the visited time if item has been visited
128
- * @private
129
- * @param item - the item which need to be refreshed
130
- * @param prefixedKey - the key of the item
223
+ * Get item from cache. It will return null if item doesn’t exist or it has been expired.
224
+ * If you specified callback function in the options,
225
+ * then the function will be executed if no such item in the cache
226
+ * and finally put the return value into cache.
227
+ * Please make sure the callback function will return the value you want to put into the cache.
228
+ * The cache will abort output a warning:
229
+ * If the key is invalid
230
+ * If error happened with AsyncStorage
231
+ *
232
+ * @param {String} key - the key of the item
233
+ * @param {Object} [options] - the options of callback function
131
234
  *
132
- * @return the refreshed item
235
+ * @return {Promise} - return a promise resolves to be the value of the item
133
236
  */
134
- AsyncStorageCache.prototype._refreshItem = function (item, prefixedKey) {
237
+ StorageCacheCommon.prototype.getItem = function (key, options) {
135
238
  return __awaiter(this, void 0, void 0, function () {
239
+ var cached, prefixedKey, item, val, e_2;
136
240
  return __generator(this, function (_a) {
137
241
  switch (_a.label) {
138
242
  case 0:
139
- item.visitedTime = (0, Utils_1.getCurrTime)();
140
- return [4 /*yield*/, AsyncStorage.setItem(prefixedKey, JSON.stringify(item))];
243
+ logger.debug("Get item: key is ".concat(key, " with options ").concat(options));
244
+ if (!key || key === constants_1.currentSizeKey) {
245
+ logger.warn("Invalid key: should not be empty or reserved key: '".concat(constants_1.currentSizeKey, "'"));
246
+ return [2 /*return*/, null];
247
+ }
248
+ prefixedKey = "".concat(this.config.keyPrefix).concat(key);
249
+ _a.label = 1;
141
250
  case 1:
251
+ _a.trys.push([1, 11, , 12]);
252
+ return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
253
+ case 2:
254
+ cached = _a.sent();
255
+ if (!(cached != null)) return [3 /*break*/, 7];
256
+ return [4 /*yield*/, this.isExpired(prefixedKey)];
257
+ case 3:
258
+ if (!_a.sent()) return [3 /*break*/, 5];
259
+ // if expired, remove that item and return null
260
+ return [4 /*yield*/, this.removeCacheItem(prefixedKey, JSON.parse(cached).byteSize)];
261
+ case 4:
262
+ // if expired, remove that item and return null
142
263
  _a.sent();
143
- return [2 /*return*/, item];
264
+ return [3 /*break*/, 7];
265
+ case 5: return [4 /*yield*/, this.updateVisitedTime(JSON.parse(cached), prefixedKey)];
266
+ case 6:
267
+ item = _a.sent();
268
+ return [2 /*return*/, item.data];
269
+ case 7:
270
+ if (!(options === null || options === void 0 ? void 0 : options.callback)) return [3 /*break*/, 10];
271
+ val = options.callback();
272
+ if (!(val !== null)) return [3 /*break*/, 9];
273
+ return [4 /*yield*/, this.setItem(key, val, options)];
274
+ case 8:
275
+ _a.sent();
276
+ _a.label = 9;
277
+ case 9: return [2 /*return*/, val];
278
+ case 10: return [2 /*return*/, null];
279
+ case 11:
280
+ e_2 = _a.sent();
281
+ logger.warn("getItem failed! ".concat(e_2));
282
+ return [2 /*return*/, null];
283
+ case 12: return [2 /*return*/];
144
284
  }
145
285
  });
146
286
  });
147
287
  };
148
288
  /**
149
- * check wether item is expired
150
- * @private
289
+ * remove item from the cache
290
+ * The cache will abort output a warning:
291
+ * If error happened with AsyncStorage
292
+ * @param {String} key - the key of the item
293
+ * @return {Promise}
294
+ */
295
+ StorageCacheCommon.prototype.removeItem = function (key) {
296
+ return __awaiter(this, void 0, void 0, function () {
297
+ var prefixedKey, val, e_3;
298
+ return __generator(this, function (_a) {
299
+ switch (_a.label) {
300
+ case 0:
301
+ logger.debug("Remove item: key is ".concat(key));
302
+ if (!key || key === constants_1.currentSizeKey) {
303
+ logger.warn("Invalid key: should not be empty or reserved key: '".concat(constants_1.currentSizeKey, "'"));
304
+ return [2 /*return*/];
305
+ }
306
+ prefixedKey = "".concat(this.config.keyPrefix).concat(key);
307
+ _a.label = 1;
308
+ case 1:
309
+ _a.trys.push([1, 5, , 6]);
310
+ return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
311
+ case 2:
312
+ val = _a.sent();
313
+ if (!val) return [3 /*break*/, 4];
314
+ return [4 /*yield*/, this.removeCacheItem(prefixedKey, JSON.parse(val).byteSize)];
315
+ case 3:
316
+ _a.sent();
317
+ _a.label = 4;
318
+ case 4: return [3 /*break*/, 6];
319
+ case 5:
320
+ e_3 = _a.sent();
321
+ logger.warn("removeItem failed! ".concat(e_3));
322
+ return [3 /*break*/, 6];
323
+ case 6: return [2 /*return*/];
324
+ }
325
+ });
326
+ });
327
+ };
328
+ /**
329
+ * Return all the keys owned by this cache.
330
+ * Will return an empty array if error occurred.
331
+ *
332
+ * @return {Promise}
333
+ */
334
+ StorageCacheCommon.prototype.getAllKeys = function () {
335
+ return __awaiter(this, void 0, void 0, function () {
336
+ var e_4;
337
+ return __generator(this, function (_a) {
338
+ switch (_a.label) {
339
+ case 0:
340
+ _a.trys.push([0, 2, , 3]);
341
+ return [4 /*yield*/, this.getAllCacheKeys()];
342
+ case 1: return [2 /*return*/, _a.sent()];
343
+ case 2:
344
+ e_4 = _a.sent();
345
+ logger.warn("getAllkeys failed! ".concat(e_4));
346
+ return [2 /*return*/, []];
347
+ case 3: return [2 /*return*/];
348
+ }
349
+ });
350
+ });
351
+ };
352
+ StorageCacheCommon.prototype.getStorage = function () {
353
+ return this.keyValueStorage;
354
+ };
355
+ /**
356
+ * check whether item is expired
357
+ *
151
358
  * @param key - the key of the item
152
359
  *
153
360
  * @return true if the item is expired.
154
361
  */
155
- AsyncStorageCache.prototype._isExpired = function (key) {
362
+ StorageCacheCommon.prototype.isExpired = function (key) {
156
363
  return __awaiter(this, void 0, void 0, function () {
157
364
  var text, item;
158
365
  return __generator(this, function (_a) {
159
366
  switch (_a.label) {
160
- case 0: return [4 /*yield*/, AsyncStorage.getItem(key)];
367
+ case 0: return [4 /*yield*/, this.getStorage().getItem(key)];
161
368
  case 1:
162
369
  text = _a.sent();
163
370
  (0, errorHelpers_1.assert)(text !== null, errorHelpers_1.CacheErrorCode.NoCacheItem, "Key: ".concat(key));
164
371
  item = JSON.parse(text);
165
- if ((0, Utils_1.getCurrTime)() >= item.expires) {
372
+ if ((0, utils_1.getCurrentTime)() >= item.expires) {
166
373
  return [2 /*return*/, true];
167
374
  }
168
375
  return [2 /*return*/, false];
@@ -172,36 +379,36 @@ var AsyncStorageCache = /** @class */ (function (_super) {
172
379
  };
173
380
  /**
174
381
  * delete item from cache
175
- * @private
382
+ *
176
383
  * @param prefixedKey - the key of the item
177
384
  * @param size - optional, the byte size of the item
178
385
  */
179
- AsyncStorageCache.prototype._removeItem = function (prefixedKey, size) {
386
+ StorageCacheCommon.prototype.removeCacheItem = function (prefixedKey, size) {
180
387
  return __awaiter(this, void 0, void 0, function () {
181
- var config, itemSize, removeItemError_1;
388
+ var item, itemSize, removeItemError_1;
182
389
  return __generator(this, function (_a) {
183
390
  switch (_a.label) {
184
- case 0: return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
391
+ case 0: return [4 /*yield*/, this.getStorage().getItem(prefixedKey)];
185
392
  case 1:
186
- config = _a.sent();
187
- (0, errorHelpers_1.assert)(!!config, errorHelpers_1.CacheErrorCode.NoCacheItem, "Key: ".concat(prefixedKey));
188
- itemSize = size !== null && size !== void 0 ? size : JSON.parse(config).byteSize;
393
+ item = _a.sent();
394
+ (0, errorHelpers_1.assert)(item !== null, errorHelpers_1.CacheErrorCode.NoCacheItem, "Key: ".concat(prefixedKey));
395
+ itemSize = size !== null && size !== void 0 ? size : JSON.parse(item).byteSize;
189
396
  // first try to update the current size of the cache
190
- return [4 /*yield*/, this._decreaseCurSizeInBytes(itemSize)];
397
+ return [4 /*yield*/, this.decreaseCurrentSizeInBytes(itemSize)];
191
398
  case 2:
192
399
  // first try to update the current size of the cache
193
400
  _a.sent();
194
401
  _a.label = 3;
195
402
  case 3:
196
403
  _a.trys.push([3, 5, , 7]);
197
- return [4 /*yield*/, AsyncStorage.removeItem(prefixedKey)];
404
+ return [4 /*yield*/, this.getStorage().removeItem(prefixedKey)];
198
405
  case 4:
199
406
  _a.sent();
200
407
  return [3 /*break*/, 7];
201
408
  case 5:
202
409
  removeItemError_1 = _a.sent();
203
410
  // if some error happened, we need to rollback the current size
204
- return [4 /*yield*/, this._increaseCurSizeInBytes(itemSize)];
411
+ return [4 /*yield*/, this.increaseCurrentSizeInBytes(itemSize)];
205
412
  case 6:
206
413
  // if some error happened, we need to rollback the current size
207
414
  _a.sent();
@@ -212,35 +419,145 @@ var AsyncStorageCache = /** @class */ (function (_super) {
212
419
  });
213
420
  });
214
421
  };
422
+ /**
423
+ * produce a JSON object with meta-data and data value
424
+ * @param value - the value of the item
425
+ * @param options - optional, the specified meta-data
426
+ *
427
+ * @return - the item which has the meta-data and the value
428
+ */
429
+ StorageCacheCommon.prototype.fillCacheItem = function (key, value, options) {
430
+ var _a, _b;
431
+ var item = {
432
+ key: key,
433
+ data: value,
434
+ timestamp: (0, utils_1.getCurrentTime)(),
435
+ visitedTime: (0, utils_1.getCurrentTime)(),
436
+ priority: (_a = options.priority) !== null && _a !== void 0 ? _a : 0,
437
+ expires: (_b = options.expires) !== null && _b !== void 0 ? _b : 0,
438
+ type: typeof value,
439
+ byteSize: 0,
440
+ };
441
+ // calculate byte size
442
+ item.byteSize = (0, utils_1.getByteLength)(JSON.stringify(item));
443
+ // re-calculate using cache item with updated byteSize property
444
+ item.byteSize = (0, utils_1.getByteLength)(JSON.stringify(item));
445
+ return item;
446
+ };
447
+ StorageCacheCommon.prototype.sanitizeConfig = function () {
448
+ if (this.config.itemMaxSize > this.config.capacityInBytes) {
449
+ logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
450
+ this.config.itemMaxSize = constants_1.defaultConfig.itemMaxSize;
451
+ }
452
+ if (this.config.defaultPriority > 5 || this.config.defaultPriority < 1) {
453
+ logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
454
+ this.config.defaultPriority = constants_1.defaultConfig.defaultPriority;
455
+ }
456
+ if (Number(this.config.warningThreshold) > 1 ||
457
+ Number(this.config.warningThreshold) < 0) {
458
+ logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
459
+ this.config.warningThreshold = constants_1.defaultConfig.warningThreshold;
460
+ }
461
+ // Set 5MB limit
462
+ var cacheLimit = 5 * 1024 * 1024;
463
+ if (this.config.capacityInBytes > cacheLimit) {
464
+ logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
465
+ this.config.capacityInBytes = constants_1.defaultConfig.capacityInBytes;
466
+ }
467
+ };
468
+ /**
469
+ * increase current size of the cache
470
+ *
471
+ * @param amount - the amount of the cache szie which need to be increased
472
+ */
473
+ StorageCacheCommon.prototype.increaseCurrentSizeInBytes = function (amount) {
474
+ return __awaiter(this, void 0, void 0, function () {
475
+ var size;
476
+ return __generator(this, function (_a) {
477
+ switch (_a.label) {
478
+ case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
479
+ case 1:
480
+ size = _a.sent();
481
+ return [4 /*yield*/, this.getStorage().setItem((0, utils_1.getCurrentSizeKey)(this.config.keyPrefix), (size + amount).toString())];
482
+ case 2:
483
+ _a.sent();
484
+ return [2 /*return*/];
485
+ }
486
+ });
487
+ });
488
+ };
489
+ /**
490
+ * decrease current size of the cache
491
+ *
492
+ * @param amount - the amount of the cache size which needs to be decreased
493
+ */
494
+ StorageCacheCommon.prototype.decreaseCurrentSizeInBytes = function (amount) {
495
+ return __awaiter(this, void 0, void 0, function () {
496
+ var size;
497
+ return __generator(this, function (_a) {
498
+ switch (_a.label) {
499
+ case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
500
+ case 1:
501
+ size = _a.sent();
502
+ return [4 /*yield*/, this.getStorage().setItem((0, utils_1.getCurrentSizeKey)(this.config.keyPrefix), (size - amount).toString())];
503
+ case 2:
504
+ _a.sent();
505
+ return [2 /*return*/];
506
+ }
507
+ });
508
+ });
509
+ };
510
+ /**
511
+ * update the visited time if item has been visited
512
+ *
513
+ * @param item - the item which need to be updated
514
+ * @param prefixedKey - the key of the item
515
+ *
516
+ * @return the updated item
517
+ */
518
+ StorageCacheCommon.prototype.updateVisitedTime = function (item, prefixedKey) {
519
+ return __awaiter(this, void 0, void 0, function () {
520
+ return __generator(this, function (_a) {
521
+ switch (_a.label) {
522
+ case 0:
523
+ item.visitedTime = (0, utils_1.getCurrentTime)();
524
+ return [4 /*yield*/, this.getStorage().setItem(prefixedKey, JSON.stringify(item))];
525
+ case 1:
526
+ _a.sent();
527
+ return [2 /*return*/, item];
528
+ }
529
+ });
530
+ });
531
+ };
215
532
  /**
216
533
  * put item into cache
217
- * @private
534
+ *
218
535
  * @param prefixedKey - the key of the item
219
536
  * @param itemData - the value of the item
220
537
  * @param itemSizeInBytes - the byte size of the item
221
538
  */
222
- AsyncStorageCache.prototype._setItem = function (prefixedKey, item) {
539
+ StorageCacheCommon.prototype.setCacheItem = function (prefixedKey, item) {
223
540
  return __awaiter(this, void 0, void 0, function () {
224
541
  var setItemErr_1;
225
542
  return __generator(this, function (_a) {
226
543
  switch (_a.label) {
227
544
  case 0:
228
545
  // first try to update the current size of the cache.
229
- return [4 /*yield*/, this._increaseCurSizeInBytes(item.byteSize)];
546
+ return [4 /*yield*/, this.increaseCurrentSizeInBytes(item.byteSize)];
230
547
  case 1:
231
548
  // first try to update the current size of the cache.
232
549
  _a.sent();
233
550
  _a.label = 2;
234
551
  case 2:
235
552
  _a.trys.push([2, 4, , 6]);
236
- return [4 /*yield*/, AsyncStorage.setItem(prefixedKey, JSON.stringify(item))];
553
+ return [4 /*yield*/, this.getStorage().setItem(prefixedKey, JSON.stringify(item))];
237
554
  case 3:
238
555
  _a.sent();
239
556
  return [3 /*break*/, 6];
240
557
  case 4:
241
558
  setItemErr_1 = _a.sent();
242
559
  // if some error happened, we need to rollback the current size
243
- return [4 /*yield*/, this._decreaseCurSizeInBytes(item.byteSize)];
560
+ return [4 /*yield*/, this.decreaseCurrentSizeInBytes(item.byteSize)];
244
561
  case 5:
245
562
  // if some error happened, we need to rollback the current size
246
563
  _a.sent();
@@ -253,23 +570,21 @@ var AsyncStorageCache = /** @class */ (function (_super) {
253
570
  };
254
571
  /**
255
572
  * total space needed when poping out items
256
- * @private
573
+ *
257
574
  * @param itemSize
258
575
  *
259
576
  * @return total space needed
260
577
  */
261
- AsyncStorageCache.prototype._sizeToPop = function (itemSize) {
578
+ StorageCacheCommon.prototype.sizeToPop = function (itemSize) {
262
579
  return __awaiter(this, void 0, void 0, function () {
263
- var spaceItemNeed, cacheThresholdSpace;
580
+ var cur, spaceItemNeed, cacheThresholdSpace;
264
581
  return __generator(this, function (_a) {
265
582
  switch (_a.label) {
266
- case 0: return [4 /*yield*/, this.getCacheCurSize()];
583
+ case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
267
584
  case 1:
268
- spaceItemNeed = (_a.sent()) +
269
- itemSize -
270
- this.cacheConfig.capacityInBytes;
271
- cacheThresholdSpace = (1 - this.cacheConfig.warningThreshold) *
272
- this.cacheConfig.capacityInBytes;
585
+ cur = _a.sent();
586
+ spaceItemNeed = cur + itemSize - this.config.capacityInBytes;
587
+ cacheThresholdSpace = (1 - this.config.warningThreshold) * this.config.capacityInBytes;
273
588
  return [2 /*return*/, spaceItemNeed > cacheThresholdSpace
274
589
  ? spaceItemNeed
275
590
  : cacheThresholdSpace];
@@ -279,63 +594,20 @@ var AsyncStorageCache = /** @class */ (function (_super) {
279
594
  };
280
595
  /**
281
596
  * see whether cache is full
282
- * @private
597
+ *
283
598
  * @param itemSize
284
599
  *
285
600
  * @return true if cache is full
286
601
  */
287
- AsyncStorageCache.prototype._isCacheFull = function (itemSize) {
602
+ StorageCacheCommon.prototype.isCacheFull = function (itemSize) {
288
603
  return __awaiter(this, void 0, void 0, function () {
289
- var _a;
290
- return __generator(this, function (_b) {
291
- switch (_b.label) {
292
- case 0:
293
- _a = itemSize;
294
- return [4 /*yield*/, this.getCacheCurSize()];
295
- case 1: return [2 /*return*/, (_a + (_b.sent()) >
296
- this.cacheConfig.capacityInBytes)];
297
- }
298
- });
299
- });
300
- };
301
- /**
302
- * scan the storage and find out all the keys owned by this cache
303
- * also clean the expired keys while scanning
304
- * @private
305
- * @return array of keys
306
- */
307
- AsyncStorageCache.prototype._findValidKeys = function () {
308
- return __awaiter(this, void 0, void 0, function () {
309
- var keys, keyInCache, i, key;
604
+ var cur;
310
605
  return __generator(this, function (_a) {
311
606
  switch (_a.label) {
312
- case 0:
313
- keys = [];
314
- keyInCache = [];
315
- return [4 /*yield*/, AsyncStorage.getAllKeys()];
607
+ case 0: return [4 /*yield*/, this.getCurrentCacheSize()];
316
608
  case 1:
317
- keyInCache = _a.sent();
318
- i = 0;
319
- _a.label = 2;
320
- case 2:
321
- if (!(i < keyInCache.length)) return [3 /*break*/, 7];
322
- key = keyInCache[i];
323
- if (!(key.indexOf(this.cacheConfig.keyPrefix) === 0 &&
324
- key !== (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix))) return [3 /*break*/, 6];
325
- return [4 /*yield*/, this._isExpired(key)];
326
- case 3:
327
- if (!_a.sent()) return [3 /*break*/, 5];
328
- return [4 /*yield*/, this._removeItem(key)];
329
- case 4:
330
- _a.sent();
331
- return [3 /*break*/, 6];
332
- case 5:
333
- keys.push(key);
334
- _a.label = 6;
335
- case 6:
336
- i += 1;
337
- return [3 /*break*/, 2];
338
- case 7: return [2 /*return*/, keys];
609
+ cur = _a.sent();
610
+ return [2 /*return*/, itemSize + cur > this.config.capacityInBytes];
339
611
  }
340
612
  });
341
613
  });
@@ -348,7 +620,7 @@ var AsyncStorageCache = /** @class */ (function (_super) {
348
620
  * @param keys - all the keys in this cache
349
621
  * @param sizeToPop - the total size of the items which needed to be poped out
350
622
  */
351
- AsyncStorageCache.prototype._popOutItems = function (keys, sizeToPop) {
623
+ StorageCacheCommon.prototype.popOutItems = function (keys, sizeToPop) {
352
624
  return __awaiter(this, void 0, void 0, function () {
353
625
  var items, remainedSize, i, val, item, i;
354
626
  return __generator(this, function (_a) {
@@ -360,7 +632,7 @@ var AsyncStorageCache = /** @class */ (function (_super) {
360
632
  _a.label = 1;
361
633
  case 1:
362
634
  if (!(i < keys.length)) return [3 /*break*/, 4];
363
- return [4 /*yield*/, AsyncStorage.getItem(keys[i])];
635
+ return [4 /*yield*/, this.getStorage().getItem(keys[i])];
364
636
  case 2:
365
637
  val = _a.sent();
366
638
  if (val != null) {
@@ -394,7 +666,7 @@ var AsyncStorageCache = /** @class */ (function (_super) {
394
666
  case 5:
395
667
  if (!(i < items.length)) return [3 /*break*/, 8];
396
668
  // pop out items until we have enough room for new item
397
- return [4 /*yield*/, this._removeItem(items[i].key, items[i].byteSize)];
669
+ return [4 /*yield*/, this.removeCacheItem(items[i].key, items[i].byteSize)];
398
670
  case 6:
399
671
  // pop out items until we have enough room for new item
400
672
  _a.sent();
@@ -412,324 +684,117 @@ var AsyncStorageCache = /** @class */ (function (_super) {
412
684
  });
413
685
  };
414
686
  /**
415
- * Set item into cache. You can put number, string, boolean or object.
416
- * The cache will first check whether has the same key.
417
- * If it has, it will delete the old item and then put the new item in
418
- * The cache will pop out items if it is full
419
- * You can specify the cache item options. The cache will abort and output a warning:
420
- * If the key is invalid
421
- * If the size of the item exceeds itemMaxSize.
422
- * If the value is undefined
423
- * If incorrect cache item configuration
424
- * If error happened with browser storage
687
+ * Scan the storage and combine the following operations for efficiency
688
+ * 1. Clear out all expired keys owned by this cache, not including the size key.
689
+ * 2. Return the remaining keys.
425
690
  *
426
- * @param {String} key - the key of the item
427
- * @param {Object} value - the value of the item
428
- * @param {Object} [options] - optional, the specified meta-data
429
- * @return {Promise}
691
+ * @return The remaining valid keys
430
692
  */
431
- AsyncStorageCache.prototype.setItem = function (key, value, options) {
693
+ StorageCacheCommon.prototype.clearInvalidAndGetRemainingKeys = function () {
432
694
  return __awaiter(this, void 0, void 0, function () {
433
- var prefixedKey, cacheItemOptions, item, val, validKeys, sizeToPop, e_1;
434
- return __generator(this, function (_a) {
435
- switch (_a.label) {
436
- case 0:
437
- logger.debug("Set item: key is ".concat(key, ", value is ").concat(value, " with options: ").concat(options));
438
- prefixedKey = this.cacheConfig.keyPrefix + key;
439
- // invalid keys
440
- if (prefixedKey === this.cacheConfig.keyPrefix ||
441
- prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
442
- logger.warn("Invalid key: should not be empty or 'CurSize'");
443
- return [2 /*return*/];
444
- }
445
- if (typeof value === 'undefined') {
446
- logger.warn("The value of item should not be undefined!");
447
- return [2 /*return*/];
448
- }
449
- cacheItemOptions = {
450
- priority: options && options.priority !== undefined
451
- ? options.priority
452
- : this.cacheConfig.defaultPriority,
453
- expires: options && options.expires !== undefined
454
- ? options.expires
455
- : this.cacheConfig.defaultTTL + (0, Utils_1.getCurrTime)(),
456
- };
457
- if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
458
- logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
459
- return [2 /*return*/];
460
- }
461
- item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
462
- // check wether this item is too big;
463
- if (item.byteSize > this.cacheConfig.itemMaxSize) {
464
- logger.warn("Item with key: ".concat(key, " you are trying to put into is too big!"));
465
- return [2 /*return*/];
466
- }
467
- _a.label = 1;
468
- case 1:
469
- _a.trys.push([1, 12, , 13]);
470
- return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
471
- case 2:
472
- val = _a.sent();
473
- if (!val) return [3 /*break*/, 4];
474
- return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(val).byteSize)];
475
- case 3:
476
- _a.sent();
477
- _a.label = 4;
478
- case 4: return [4 /*yield*/, this._isCacheFull(item.byteSize)];
479
- case 5:
480
- if (!_a.sent()) return [3 /*break*/, 10];
481
- return [4 /*yield*/, this._findValidKeys()];
482
- case 6:
483
- validKeys = _a.sent();
484
- return [4 /*yield*/, this._isCacheFull(item.byteSize)];
485
- case 7:
486
- if (!_a.sent()) return [3 /*break*/, 10];
487
- return [4 /*yield*/, this._sizeToPop(item.byteSize)];
488
- case 8:
489
- sizeToPop = _a.sent();
490
- return [4 /*yield*/, this._popOutItems(validKeys, sizeToPop)];
491
- case 9:
492
- _a.sent();
493
- _a.label = 10;
494
- case 10:
495
- // put item in the cache
496
- return [4 /*yield*/, this._setItem(prefixedKey, item)];
497
- case 11:
498
- // put item in the cache
499
- _a.sent();
500
- return [3 /*break*/, 13];
501
- case 12:
502
- e_1 = _a.sent();
503
- logger.warn("setItem failed! ".concat(e_1));
504
- return [3 /*break*/, 13];
505
- case 13: return [2 /*return*/];
506
- }
507
- });
508
- });
509
- };
510
- /**
511
- * Get item from cache. It will return null if item doesn’t exist or it has been expired.
512
- * If you specified callback function in the options,
513
- * then the function will be executed if no such item in the cache
514
- * and finally put the return value into cache.
515
- * Please make sure the callback function will return the value you want to put into the cache.
516
- * The cache will abort output a warning:
517
- * If the key is invalid
518
- * If error happened with AsyncStorage
519
- *
520
- * @param {String} key - the key of the item
521
- * @param {Object} [options] - the options of callback function
522
- * @return {Promise} - return a promise resolves to be the value of the item
523
- */
524
- AsyncStorageCache.prototype.getItem = function (key, options) {
525
- return __awaiter(this, void 0, void 0, function () {
526
- var ret, prefixedKey, item, val, e_2;
527
- return __generator(this, function (_a) {
528
- switch (_a.label) {
695
+ var remainingKeys, keys, keys_1, keys_1_1, key, e_5_1;
696
+ var e_5, _a;
697
+ return __generator(this, function (_b) {
698
+ switch (_b.label) {
529
699
  case 0:
530
- logger.debug("Get item: key is ".concat(key, " with options ").concat(options));
531
- ret = null;
532
- prefixedKey = this.cacheConfig.keyPrefix + key;
533
- if (prefixedKey === this.cacheConfig.keyPrefix ||
534
- prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
535
- logger.warn("Invalid key: should not be empty or 'CurSize'");
536
- return [2 /*return*/, null];
537
- }
538
- _a.label = 1;
700
+ remainingKeys = [];
701
+ return [4 /*yield*/, this.getAllCacheKeys({
702
+ omitSizeKey: true,
703
+ })];
539
704
  case 1:
540
- _a.trys.push([1, 8, , 9]);
541
- return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
705
+ keys = _b.sent();
706
+ _b.label = 2;
542
707
  case 2:
543
- ret = _a.sent();
544
- if (!(ret != null)) return [3 /*break*/, 7];
545
- return [4 /*yield*/, this._isExpired(prefixedKey)];
708
+ _b.trys.push([2, 9, 10, 11]);
709
+ keys_1 = __values(keys), keys_1_1 = keys_1.next();
710
+ _b.label = 3;
546
711
  case 3:
547
- if (!_a.sent()) return [3 /*break*/, 5];
548
- // if expired, remove that item and return null
549
- return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(ret).byteSize)];
712
+ if (!!keys_1_1.done) return [3 /*break*/, 8];
713
+ key = keys_1_1.value;
714
+ return [4 /*yield*/, this.isExpired(key)];
550
715
  case 4:
551
- // if expired, remove that item and return null
552
- _a.sent();
553
- return [3 /*break*/, 7];
716
+ if (!_b.sent()) return [3 /*break*/, 6];
717
+ return [4 /*yield*/, this.removeCacheItem(key)];
554
718
  case 5:
555
- item = JSON.parse(ret);
556
- return [4 /*yield*/, this._refreshItem(item, prefixedKey)];
719
+ _b.sent();
720
+ return [3 /*break*/, 7];
557
721
  case 6:
558
- item = _a.sent();
559
- return [2 /*return*/, item.data];
722
+ remainingKeys.push(key);
723
+ _b.label = 7;
560
724
  case 7:
561
- if (options && options.callback !== undefined) {
562
- val = options.callback();
563
- if (val !== null) {
564
- this.setItem(key, val, options);
565
- }
566
- return [2 /*return*/, val];
567
- }
568
- return [2 /*return*/, null];
569
- case 8:
570
- e_2 = _a.sent();
571
- logger.warn("getItem failed! ".concat(e_2));
572
- return [2 /*return*/, null];
573
- case 9: return [2 /*return*/];
574
- }
575
- });
576
- });
577
- };
578
- /**
579
- * remove item from the cache
580
- * The cache will abort output a warning:
581
- * If error happened with AsyncStorage
582
- * @param {String} key - the key of the item
583
- * @return {Promise}
584
- */
585
- AsyncStorageCache.prototype.removeItem = function (key) {
586
- return __awaiter(this, void 0, void 0, function () {
587
- var prefixedKey, val, e_3;
588
- return __generator(this, function (_a) {
589
- switch (_a.label) {
590
- case 0:
591
- logger.debug("Remove item: key is ".concat(key));
592
- prefixedKey = this.cacheConfig.keyPrefix + key;
593
- if (prefixedKey === this.cacheConfig.keyPrefix ||
594
- prefixedKey === (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
595
- return [2 /*return*/];
725
+ keys_1_1 = keys_1.next();
726
+ return [3 /*break*/, 3];
727
+ case 8: return [3 /*break*/, 11];
728
+ case 9:
729
+ e_5_1 = _b.sent();
730
+ e_5 = { error: e_5_1 };
731
+ return [3 /*break*/, 11];
732
+ case 10:
733
+ try {
734
+ if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
596
735
  }
597
- _a.label = 1;
598
- case 1:
599
- _a.trys.push([1, 5, , 6]);
600
- return [4 /*yield*/, AsyncStorage.getItem(prefixedKey)];
601
- case 2:
602
- val = _a.sent();
603
- if (!val) return [3 /*break*/, 4];
604
- return [4 /*yield*/, this._removeItem(prefixedKey, JSON.parse(val).byteSize)];
605
- case 3:
606
- _a.sent();
607
- _a.label = 4;
608
- case 4: return [3 /*break*/, 6];
609
- case 5:
610
- e_3 = _a.sent();
611
- logger.warn("removeItem failed! ".concat(e_3));
612
- return [3 /*break*/, 6];
613
- case 6: return [2 /*return*/];
736
+ finally { if (e_5) throw e_5.error; }
737
+ return [7 /*endfinally*/];
738
+ case 11: return [2 /*return*/, remainingKeys];
614
739
  }
615
740
  });
616
741
  });
617
742
  };
618
743
  /**
619
744
  * clear the entire cache
620
- * The cache will abort output a warning:
621
- * If error happened with AsyncStorage
745
+ * The cache will abort and output a warning if error occurs
622
746
  * @return {Promise}
623
747
  */
624
- AsyncStorageCache.prototype.clear = function () {
748
+ StorageCacheCommon.prototype.clear = function () {
625
749
  return __awaiter(this, void 0, void 0, function () {
626
- var keys, keysToRemove, i, i, e_4;
627
- return __generator(this, function (_a) {
628
- switch (_a.label) {
750
+ var keys, keys_2, keys_2_1, key, e_6_1, e_7;
751
+ var e_6, _a;
752
+ return __generator(this, function (_b) {
753
+ switch (_b.label) {
629
754
  case 0:
630
755
  logger.debug("Clear Cache");
631
- _a.label = 1;
756
+ _b.label = 1;
632
757
  case 1:
633
- _a.trys.push([1, 7, , 8]);
634
- return [4 /*yield*/, AsyncStorage.getAllKeys()];
758
+ _b.trys.push([1, 11, , 12]);
759
+ return [4 /*yield*/, this.getAllKeys()];
635
760
  case 2:
636
- keys = _a.sent();
637
- keysToRemove = [];
638
- for (i = 0; i < keys.length; i += 1) {
639
- if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0) {
640
- keysToRemove.push(keys[i]);
641
- }
642
- }
643
- i = 0;
644
- _a.label = 3;
761
+ keys = _b.sent();
762
+ _b.label = 3;
645
763
  case 3:
646
- if (!(i < keysToRemove.length)) return [3 /*break*/, 6];
647
- return [4 /*yield*/, AsyncStorage.removeItem(keysToRemove[i])];
764
+ _b.trys.push([3, 8, 9, 10]);
765
+ keys_2 = __values(keys), keys_2_1 = keys_2.next();
766
+ _b.label = 4;
648
767
  case 4:
649
- _a.sent();
650
- _a.label = 5;
768
+ if (!!keys_2_1.done) return [3 /*break*/, 7];
769
+ key = keys_2_1.value;
770
+ return [4 /*yield*/, this.getStorage().removeItem(key)];
651
771
  case 5:
652
- i += 1;
653
- return [3 /*break*/, 3];
654
- case 6: return [3 /*break*/, 8];
655
- case 7:
656
- e_4 = _a.sent();
657
- logger.warn("clear failed! ".concat(e_4));
658
- return [3 /*break*/, 8];
659
- case 8: return [2 /*return*/];
660
- }
661
- });
662
- });
663
- };
664
- /**
665
- * return the current size of the cache
666
- * @return {Promise}
667
- */
668
- AsyncStorageCache.prototype.getCacheCurSize = function () {
669
- return __awaiter(this, void 0, void 0, function () {
670
- var ret;
671
- return __generator(this, function (_a) {
672
- switch (_a.label) {
673
- case 0: return [4 /*yield*/, AsyncStorage.getItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix))];
674
- case 1:
675
- ret = _a.sent();
676
- if (!!ret) return [3 /*break*/, 3];
677
- return [4 /*yield*/, AsyncStorage.setItem((0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix), '0')];
678
- case 2:
679
- _a.sent();
680
- ret = '0';
681
- _a.label = 3;
682
- case 3: return [2 /*return*/, Number(ret)];
683
- }
684
- });
685
- });
686
- };
687
- /**
688
- * Return all the keys in the cache.
689
- * Will return an empty array if error happend.
690
- * @return {Promise}
691
- */
692
- AsyncStorageCache.prototype.getAllKeys = function () {
693
- return __awaiter(this, void 0, void 0, function () {
694
- var keys, retKeys, i, e_5;
695
- return __generator(this, function (_a) {
696
- switch (_a.label) {
697
- case 0:
698
- _a.trys.push([0, 2, , 3]);
699
- return [4 /*yield*/, AsyncStorage.getAllKeys()];
700
- case 1:
701
- keys = _a.sent();
702
- retKeys = [];
703
- for (i = 0; i < keys.length; i += 1) {
704
- if (keys[i].indexOf(this.cacheConfig.keyPrefix) === 0 &&
705
- keys[i] !== (0, CacheUtils_1.getCurrSizeKey)(this.cacheConfig.keyPrefix)) {
706
- retKeys.push(keys[i].substring(this.cacheConfig.keyPrefix.length));
707
- }
772
+ _b.sent();
773
+ _b.label = 6;
774
+ case 6:
775
+ keys_2_1 = keys_2.next();
776
+ return [3 /*break*/, 4];
777
+ case 7: return [3 /*break*/, 10];
778
+ case 8:
779
+ e_6_1 = _b.sent();
780
+ e_6 = { error: e_6_1 };
781
+ return [3 /*break*/, 10];
782
+ case 9:
783
+ try {
784
+ if (keys_2_1 && !keys_2_1.done && (_a = keys_2.return)) _a.call(keys_2);
708
785
  }
709
- return [2 /*return*/, retKeys];
710
- case 2:
711
- e_5 = _a.sent();
712
- logger.warn("getALlkeys failed! ".concat(e_5));
713
- return [2 /*return*/, []];
714
- case 3: return [2 /*return*/];
786
+ finally { if (e_6) throw e_6.error; }
787
+ return [7 /*endfinally*/];
788
+ case 10: return [3 /*break*/, 12];
789
+ case 11:
790
+ e_7 = _b.sent();
791
+ logger.warn("clear failed! ".concat(e_7));
792
+ return [3 /*break*/, 12];
793
+ case 12: return [2 /*return*/];
715
794
  }
716
795
  });
717
796
  });
718
797
  };
719
- /**
720
- * Return a new instance of cache with customized configuration.
721
- * @param {Object} config - the customized configuration
722
- * @return {Object} - the new instance of Cache
723
- */
724
- AsyncStorageCache.prototype.createInstance = function (config) {
725
- if (config.keyPrefix === Utils_1.defaultConfig.keyPrefix) {
726
- logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
727
- config.keyPrefix = Utils_1.getCurrTime.toString();
728
- }
729
- return new AsyncStorageCache(config);
730
- };
731
- return AsyncStorageCache;
732
- }(StorageCache_1.StorageCache));
733
- exports.AsyncStorageCache = AsyncStorageCache;
734
- var instance = new AsyncStorageCache();
735
- exports.Cache = instance;
798
+ return StorageCacheCommon;
799
+ }());
800
+ exports.StorageCacheCommon = StorageCacheCommon;