@aws-amplify/cache 4.0.58-unstable.2 → 4.0.58

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.
@@ -134,42 +134,31 @@ var __extends = undefined && undefined.__extends || function () {
134
134
  if (b.hasOwnProperty(p)) d[p] = b[p];
135
135
  }
136
136
  };
137
-
138
137
  return _extendStatics(d, b);
139
138
  };
140
-
141
139
  return function (d, b) {
142
140
  _extendStatics(d, b);
143
-
144
141
  function __() {
145
142
  this.constructor = d;
146
143
  }
147
-
148
144
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
149
145
  };
150
146
  }();
151
147
 
152
148
 
153
149
 
154
-
155
150
  var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__["ConsoleLogger"]('Cache');
156
151
  /**
157
152
  * Customized storage based on the SessionStorage or LocalStorage with LRU implemented
158
153
  */
159
-
160
- var BrowserStorageCacheClass =
161
- /** @class */
162
- function (_super) {
154
+ var BrowserStorageCacheClass = /** @class */function (_super) {
163
155
  __extends(BrowserStorageCacheClass, _super);
164
156
  /**
165
157
  * initialize the cache
166
158
  * @param config - the configuration of the cache
167
159
  */
168
-
169
-
170
160
  function BrowserStorageCacheClass(config) {
171
161
  var _this = this;
172
-
173
162
  var cacheConfig = config ? Object.assign({}, _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"], config) : _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"];
174
163
  _this = _super.call(this, cacheConfig) || this;
175
164
  _this.config.storage = cacheConfig.storage;
@@ -184,8 +173,6 @@ function (_super) {
184
173
  * @private
185
174
  * @param amount - the amount of the cache size which needs to be decreased
186
175
  */
187
-
188
-
189
176
  BrowserStorageCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
190
177
  var curSize = this.getCacheCurSize();
191
178
  this.config.storage.setItem(this.cacheCurSizeKey, (curSize - amount).toString());
@@ -196,8 +183,6 @@ function (_super) {
196
183
  * @private
197
184
  * @param amount - the amount of the cache szie which need to be increased
198
185
  */
199
-
200
-
201
186
  BrowserStorageCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
202
187
  var curSize = this.getCacheCurSize();
203
188
  this.config.storage.setItem(this.cacheCurSizeKey, (curSize + amount).toString());
@@ -211,8 +196,6 @@ function (_super) {
211
196
  *
212
197
  * @return the refreshed item
213
198
  */
214
-
215
-
216
199
  BrowserStorageCacheClass.prototype._refreshItem = function (item, prefixedKey) {
217
200
  item.visitedTime = Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])();
218
201
  this.config.storage.setItem(prefixedKey, JSON.stringify(item));
@@ -226,16 +209,12 @@ function (_super) {
226
209
  *
227
210
  * @return true if the item is expired.
228
211
  */
229
-
230
-
231
212
  BrowserStorageCacheClass.prototype._isExpired = function (key) {
232
213
  var text = this.config.storage.getItem(key);
233
214
  var item = JSON.parse(text);
234
-
235
215
  if (Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])() >= item.expires) {
236
216
  return true;
237
217
  }
238
-
239
218
  return false;
240
219
  };
241
220
  /**
@@ -245,14 +224,10 @@ function (_super) {
245
224
  * @param prefixedKey - the key of the item
246
225
  * @param size - optional, the byte size of the item
247
226
  */
248
-
249
-
250
227
  BrowserStorageCacheClass.prototype._removeItem = function (prefixedKey, size) {
251
228
  var itemSize = size ? size : JSON.parse(this.config.storage.getItem(prefixedKey)).byteSize;
252
-
253
- this._decreaseCurSizeInBytes(itemSize); // remove the cache item
254
-
255
-
229
+ this._decreaseCurSizeInBytes(itemSize);
230
+ // remove the cache item
256
231
  this.config.storage.removeItem(prefixedKey);
257
232
  };
258
233
  /**
@@ -263,18 +238,14 @@ function (_super) {
263
238
  * @param itemData - the value of the item
264
239
  * @param itemSizeInBytes - the byte size of the item
265
240
  */
266
-
267
-
268
241
  BrowserStorageCacheClass.prototype._setItem = function (prefixedKey, item) {
269
242
  // update the cache size
270
243
  this._increaseCurSizeInBytes(item.byteSize);
271
-
272
244
  try {
273
245
  this.config.storage.setItem(prefixedKey, JSON.stringify(item));
274
246
  } catch (setItemErr) {
275
247
  // if failed, we need to rollback the cache size
276
248
  this._decreaseCurSizeInBytes(item.byteSize);
277
-
278
249
  logger.error("Failed to set item " + setItemErr);
279
250
  }
280
251
  };
@@ -286,8 +257,6 @@ function (_super) {
286
257
  *
287
258
  * @return total space needed
288
259
  */
289
-
290
-
291
260
  BrowserStorageCacheClass.prototype._sizeToPop = function (itemSize) {
292
261
  var spaceItemNeed = this.getCacheCurSize() + itemSize - this.config.capacityInBytes;
293
262
  var cacheThresholdSpace = (1 - this.config.warningThreshold) * this.config.capacityInBytes;
@@ -301,8 +270,6 @@ function (_super) {
301
270
  *
302
271
  * @return true if cache is full
303
272
  */
304
-
305
-
306
273
  BrowserStorageCacheClass.prototype._isCacheFull = function (itemSize) {
307
274
  return itemSize + this.getCacheCurSize() > this.config.capacityInBytes;
308
275
  };
@@ -314,20 +281,16 @@ function (_super) {
314
281
  *
315
282
  * @return array of keys
316
283
  */
317
-
318
-
319
284
  BrowserStorageCacheClass.prototype._findValidKeys = function () {
320
285
  var keys = [];
321
- var keyInCache = []; // get all keys in Storage
322
-
286
+ var keyInCache = [];
287
+ // get all keys in Storage
323
288
  for (var i = 0; i < this.config.storage.length; i += 1) {
324
289
  keyInCache.push(this.config.storage.key(i));
325
- } // find those items which belong to our cache and also clean those expired items
326
-
327
-
290
+ }
291
+ // find those items which belong to our cache and also clean those expired items
328
292
  for (var i = 0; i < keyInCache.length; i += 1) {
329
293
  var key = keyInCache[i];
330
-
331
294
  if (key.indexOf(this.config.keyPrefix) === 0 && key !== this.cacheCurSizeKey) {
332
295
  if (this._isExpired(key)) {
333
296
  this._removeItem(key);
@@ -336,7 +299,6 @@ function (_super) {
336
299
  }
337
300
  }
338
301
  }
339
-
340
302
  return keys;
341
303
  };
342
304
  /**
@@ -348,23 +310,19 @@ function (_super) {
348
310
  * @param keys - all the keys in this cache
349
311
  * @param sizeToPop - the total size of the items which needed to be poped out
350
312
  */
351
-
352
-
353
313
  BrowserStorageCacheClass.prototype._popOutItems = function (keys, sizeToPop) {
354
314
  var items = [];
355
- var remainedSize = sizeToPop; // get the items from Storage
356
-
315
+ var remainedSize = sizeToPop;
316
+ // get the items from Storage
357
317
  for (var i = 0; i < keys.length; i += 1) {
358
318
  var val = this.config.storage.getItem(keys[i]);
359
-
360
319
  if (val != null) {
361
320
  var item = JSON.parse(val);
362
321
  items.push(item);
363
322
  }
364
- } // first compare priority
323
+ }
324
+ // first compare priority
365
325
  // then compare visited time
366
-
367
-
368
326
  items.sort(function (a, b) {
369
327
  if (a.priority > b.priority) {
370
328
  return -1;
@@ -376,13 +334,10 @@ function (_super) {
376
334
  } else return 1;
377
335
  }
378
336
  });
379
-
380
337
  for (var i = 0; i < items.length; i += 1) {
381
338
  // pop out items until we have enough room for new item
382
339
  this._removeItem(items[i].key, items[i].byteSize);
383
-
384
340
  remainedSize -= items[i].byteSize;
385
-
386
341
  if (remainedSize <= 0) {
387
342
  return;
388
343
  }
@@ -404,61 +359,49 @@ function (_super) {
404
359
  * @param value - the value of the item
405
360
  * @param {Object} [options] - optional, the specified meta-data
406
361
  */
407
-
408
-
409
362
  BrowserStorageCacheClass.prototype.setItem = function (key, value, options) {
410
363
  logger.log("Set item: key is " + key + ", value is " + value + " with options: " + options);
411
- var prefixedKey = this.config.keyPrefix + key; // invalid keys
412
-
364
+ var prefixedKey = this.config.keyPrefix + key;
365
+ // invalid keys
413
366
  if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
414
367
  logger.warn("Invalid key: should not be empty or 'CurSize'");
415
368
  return;
416
369
  }
417
-
418
370
  if (typeof value === 'undefined') {
419
371
  logger.warn("The value of item should not be undefined!");
420
372
  return;
421
373
  }
422
-
423
374
  var cacheItemOptions = {
424
375
  priority: options && options.priority !== undefined ? options.priority : this.config.defaultPriority,
425
376
  expires: options && options.expires !== undefined ? options.expires : this.config.defaultTTL + Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])()
426
377
  };
427
-
428
378
  if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
429
379
  logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
430
380
  return;
431
381
  }
432
-
433
- var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions); // check wether this item is too big;
434
-
382
+ var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
383
+ // check wether this item is too big;
435
384
  if (item.byteSize > this.config.itemMaxSize) {
436
385
  logger.warn("Item with key: " + key + " you are trying to put into is too big!");
437
386
  return;
438
387
  }
439
-
440
388
  try {
441
389
  // first look into the storage, if it exists, delete it.
442
390
  var val = this.config.storage.getItem(prefixedKey);
443
-
444
391
  if (val) {
445
392
  this._removeItem(prefixedKey, JSON.parse(val).byteSize);
446
- } // check whether the cache is full
447
-
448
-
393
+ }
394
+ // check whether the cache is full
449
395
  if (this._isCacheFull(item.byteSize)) {
450
- var validKeys = this._findValidKeys(); // check again and then pop out items
451
-
452
-
396
+ var validKeys = this._findValidKeys();
397
+ // check again and then pop out items
453
398
  if (this._isCacheFull(item.byteSize)) {
454
399
  var sizeToPop = this._sizeToPop(item.byteSize);
455
-
456
400
  this._popOutItems(validKeys, sizeToPop);
457
401
  }
458
- } // put item in the cache
402
+ }
403
+ // put item in the cache
459
404
  // may failed due to storage full
460
-
461
-
462
405
  this._setItem(prefixedKey, item);
463
406
  } catch (e) {
464
407
  logger.warn("setItem failed! " + e);
@@ -479,26 +422,20 @@ function (_super) {
479
422
  *
480
423
  * @return - return the value of the item
481
424
  */
482
-
483
-
484
425
  BrowserStorageCacheClass.prototype.getItem = function (key, options) {
485
426
  logger.log("Get item: key is " + key + " with options " + options);
486
427
  var ret = null;
487
428
  var prefixedKey = this.config.keyPrefix + key;
488
-
489
429
  if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
490
430
  logger.warn("Invalid key: should not be empty or 'CurSize'");
491
431
  return null;
492
432
  }
493
-
494
433
  try {
495
434
  ret = this.config.storage.getItem(prefixedKey);
496
-
497
435
  if (ret != null) {
498
436
  if (this._isExpired(prefixedKey)) {
499
437
  // if expired, remove that item and return null
500
438
  this._removeItem(prefixedKey, JSON.parse(ret).byteSize);
501
-
502
439
  ret = null;
503
440
  } else {
504
441
  // if not expired, great, return the value and refresh it
@@ -507,17 +444,13 @@ function (_super) {
507
444
  return item.data;
508
445
  }
509
446
  }
510
-
511
447
  if (options && options.callback !== undefined) {
512
448
  var val = options.callback();
513
-
514
449
  if (val !== null) {
515
450
  this.setItem(key, val, options);
516
451
  }
517
-
518
452
  return val;
519
453
  }
520
-
521
454
  return null;
522
455
  } catch (e) {
523
456
  logger.warn("getItem failed! " + e);
@@ -530,19 +463,14 @@ function (_super) {
530
463
  * If error happened with browser storage
531
464
  * @param key - the key of the item
532
465
  */
533
-
534
-
535
466
  BrowserStorageCacheClass.prototype.removeItem = function (key) {
536
467
  logger.log("Remove item: key is " + key);
537
468
  var prefixedKey = this.config.keyPrefix + key;
538
-
539
469
  if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
540
470
  return;
541
471
  }
542
-
543
472
  try {
544
473
  var val = this.config.storage.getItem(prefixedKey);
545
-
546
474
  if (val) {
547
475
  this._removeItem(prefixedKey, JSON.parse(val).byteSize);
548
476
  }
@@ -555,20 +483,15 @@ function (_super) {
555
483
  * The cache will abort output a warning:
556
484
  * If error happened with browser storage
557
485
  */
558
-
559
-
560
486
  BrowserStorageCacheClass.prototype.clear = function () {
561
487
  logger.log("Clear Cache");
562
488
  var keysToRemove = [];
563
-
564
489
  for (var i = 0; i < this.config.storage.length; i += 1) {
565
490
  var key = this.config.storage.key(i);
566
-
567
491
  if (key.indexOf(this.config.keyPrefix) === 0) {
568
492
  keysToRemove.push(key);
569
493
  }
570
494
  }
571
-
572
495
  try {
573
496
  for (var i = 0; i < keysToRemove.length; i += 1) {
574
497
  this.config.storage.removeItem(keysToRemove[i]);
@@ -582,19 +505,14 @@ function (_super) {
582
505
  *
583
506
  * @return - all keys in the cache
584
507
  */
585
-
586
-
587
508
  BrowserStorageCacheClass.prototype.getAllKeys = function () {
588
509
  var keys = [];
589
-
590
510
  for (var i = 0; i < this.config.storage.length; i += 1) {
591
511
  var key = this.config.storage.key(i);
592
-
593
512
  if (key.indexOf(this.config.keyPrefix) === 0 && key !== this.cacheCurSizeKey) {
594
513
  keys.push(key.substring(this.config.keyPrefix.length));
595
514
  }
596
515
  }
597
-
598
516
  return keys;
599
517
  };
600
518
  /**
@@ -602,16 +520,12 @@ function (_super) {
602
520
  *
603
521
  * @return - current size of the cache
604
522
  */
605
-
606
-
607
523
  BrowserStorageCacheClass.prototype.getCacheCurSize = function () {
608
524
  var ret = this.config.storage.getItem(this.cacheCurSizeKey);
609
-
610
525
  if (!ret) {
611
526
  this.config.storage.setItem(this.cacheCurSizeKey, '0');
612
527
  ret = '0';
613
528
  }
614
-
615
529
  return Number(ret);
616
530
  };
617
531
  /**
@@ -620,26 +534,20 @@ function (_super) {
620
534
  *
621
535
  * @return - new instance of Cache
622
536
  */
623
-
624
-
625
537
  BrowserStorageCacheClass.prototype.createInstance = function (config) {
626
538
  if (!config.keyPrefix || config.keyPrefix === _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].keyPrefix) {
627
539
  logger.error('invalid keyPrefix, setting keyPrefix with timeStamp');
628
540
  config.keyPrefix = _Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"].toString();
629
541
  }
630
-
631
542
  return new BrowserStorageCacheClass(config);
632
543
  };
633
-
634
544
  return BrowserStorageCacheClass;
635
545
  }(_StorageCache__WEBPACK_IMPORTED_MODULE_1__["StorageCache"]);
636
546
 
637
-
638
547
  var BrowserStorageCache = new BrowserStorageCacheClass();
639
548
  /**
640
549
  * @deprecated use named import
641
550
  */
642
-
643
551
  /* harmony default export */ __webpack_exports__["default"] = (BrowserStorageCache);
644
552
 
645
553
  /***/ }),
@@ -682,25 +590,20 @@ var __extends = undefined && undefined.__extends || function () {
682
590
  if (b.hasOwnProperty(p)) d[p] = b[p];
683
591
  }
684
592
  };
685
-
686
593
  return _extendStatics(d, b);
687
594
  };
688
-
689
595
  return function (d, b) {
690
596
  _extendStatics(d, b);
691
-
692
597
  function __() {
693
598
  this.constructor = d;
694
599
  }
695
-
696
600
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
697
601
  };
698
602
  }();
699
-
700
603
  var __values = undefined && undefined.__values || function (o) {
701
604
  var s = typeof Symbol === "function" && Symbol.iterator,
702
- m = s && o[s],
703
- i = 0;
605
+ m = s && o[s],
606
+ i = 0;
704
607
  if (m) return m.call(o);
705
608
  if (o && typeof o.length === "number") return {
706
609
  next: function next() {
@@ -716,7 +619,6 @@ var __values = undefined && undefined.__values || function (o) {
716
619
 
717
620
 
718
621
 
719
-
720
622
  var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__["ConsoleLogger"]('InMemoryCache');
721
623
  /**
722
624
  * Customized in-memory cache with LRU implemented
@@ -726,21 +628,15 @@ var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_2__["ConsoleLogger"]
726
628
  * @member maxPriority - max of the priority
727
629
  * @member cacheSizeLimit - the limit of cache size
728
630
  */
729
-
730
- var InMemoryCacheClass =
731
- /** @class */
732
- function (_super) {
631
+ var InMemoryCacheClass = /** @class */function (_super) {
733
632
  __extends(InMemoryCacheClass, _super);
734
633
  /**
735
634
  * initialize the cache
736
635
  *
737
636
  * @param config - the configuration of the cache
738
637
  */
739
-
740
-
741
638
  function InMemoryCacheClass(config) {
742
639
  var _this = this;
743
-
744
640
  var cacheConfig = config ? Object.assign({}, _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"], config) : _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"];
745
641
  _this = _super.call(this, cacheConfig) || this;
746
642
  logger.debug('now we start!');
@@ -749,12 +645,11 @@ function (_super) {
749
645
  _this.maxPriority = 5;
750
646
  _this.getItem = _this.getItem.bind(_this);
751
647
  _this.setItem = _this.setItem.bind(_this);
752
- _this.removeItem = _this.removeItem.bind(_this); // initialize list for every priority
753
-
648
+ _this.removeItem = _this.removeItem.bind(_this);
649
+ // initialize list for every priority
754
650
  for (var i = 0; i < _this.maxPriority; i += 1) {
755
651
  _this.cacheList[i] = new _Utils__WEBPACK_IMPORTED_MODULE_0__["CacheList"]();
756
652
  }
757
-
758
653
  return _this;
759
654
  }
760
655
  /**
@@ -762,8 +657,6 @@ function (_super) {
762
657
  *
763
658
  * @param amount - the amount of the cache size which needs to be decreased
764
659
  */
765
-
766
-
767
660
  InMemoryCacheClass.prototype._decreaseCurSizeInBytes = function (amount) {
768
661
  this.curSizeInBytes -= amount;
769
662
  };
@@ -772,8 +665,6 @@ function (_super) {
772
665
  *
773
666
  * @param amount - the amount of the cache szie which need to be increased
774
667
  */
775
-
776
-
777
668
  InMemoryCacheClass.prototype._increaseCurSizeInBytes = function (amount) {
778
669
  this.curSizeInBytes += amount;
779
670
  };
@@ -784,16 +675,12 @@ function (_super) {
784
675
  *
785
676
  * @return true if the item is expired.
786
677
  */
787
-
788
-
789
678
  InMemoryCacheClass.prototype._isExpired = function (key) {
790
679
  var text = _Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].getItem(key);
791
680
  var item = JSON.parse(text);
792
-
793
681
  if (Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])() >= item.expires) {
794
682
  return true;
795
683
  }
796
-
797
684
  return false;
798
685
  };
799
686
  /**
@@ -802,15 +689,12 @@ function (_super) {
802
689
  * @param prefixedKey - the key of the item
803
690
  * @param listIdx - indicates which cache list the key belongs to
804
691
  */
805
-
806
-
807
692
  InMemoryCacheClass.prototype._removeItem = function (prefixedKey, listIdx) {
808
693
  // delete the key from the list
809
- this.cacheList[listIdx].removeItem(prefixedKey); // decrease the current size of the cache
810
-
811
- this._decreaseCurSizeInBytes(JSON.parse(_Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].getItem(prefixedKey)).byteSize); // finally remove the item from memory
812
-
813
-
694
+ this.cacheList[listIdx].removeItem(prefixedKey);
695
+ // decrease the current size of the cache
696
+ this._decreaseCurSizeInBytes(JSON.parse(_Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].getItem(prefixedKey)).byteSize);
697
+ // finally remove the item from memory
814
698
  _Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].removeItem(prefixedKey);
815
699
  };
816
700
  /**
@@ -821,15 +705,12 @@ function (_super) {
821
705
  * @param itemSizeInBytes - the byte size of the item
822
706
  * @param listIdx - indicates which cache list the key belongs to
823
707
  */
824
-
825
-
826
708
  InMemoryCacheClass.prototype._setItem = function (prefixedKey, item, listIdx) {
827
709
  // insert the key into the list
828
- this.cacheList[listIdx].insertItem(prefixedKey); // increase the current size of the cache
829
-
830
- this._increaseCurSizeInBytes(item.byteSize); // finally add the item into memory
831
-
832
-
710
+ this.cacheList[listIdx].insertItem(prefixedKey);
711
+ // increase the current size of the cache
712
+ this._increaseCurSizeInBytes(item.byteSize);
713
+ // finally add the item into memory
833
714
  _Utils__WEBPACK_IMPORTED_MODULE_0__["CacheObject"].setItem(prefixedKey, JSON.stringify(item));
834
715
  };
835
716
  /**
@@ -839,8 +720,6 @@ function (_super) {
839
720
  *
840
721
  * @return true if cache is full
841
722
  */
842
-
843
-
844
723
  InMemoryCacheClass.prototype._isCacheFull = function (itemSize) {
845
724
  return this.curSizeInBytes + itemSize > this.config.capacityInBytes;
846
725
  };
@@ -849,17 +728,13 @@ function (_super) {
849
728
  *
850
729
  * @param key
851
730
  */
852
-
853
-
854
731
  InMemoryCacheClass.prototype.containsKey = function (key) {
855
732
  var prefixedKey = this.config.keyPrefix + key;
856
-
857
733
  for (var i = 0; i < this.maxPriority; i += 1) {
858
734
  if (this.cacheList[i].containsKey(prefixedKey)) {
859
735
  return i + 1;
860
736
  }
861
737
  }
862
-
863
738
  return -1;
864
739
  };
865
740
  /**
@@ -881,59 +756,47 @@ function (_super) {
881
756
  * @throws if the item is too big which exceeds the limit of single item size
882
757
  * @throws if the key is invalid
883
758
  */
884
-
885
-
886
759
  InMemoryCacheClass.prototype.setItem = function (key, value, options) {
887
- var prefixedKey = this.config.keyPrefix + key; // invalid keys
888
-
760
+ var prefixedKey = this.config.keyPrefix + key;
761
+ // invalid keys
889
762
  if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
890
763
  logger.warn("Invalid key: should not be empty or 'CurSize'");
891
764
  return;
892
765
  }
893
-
894
766
  if (typeof value === 'undefined') {
895
767
  logger.warn("The value of item should not be undefined!");
896
768
  return;
897
769
  }
898
-
899
770
  var cacheItemOptions = {
900
771
  priority: options && options.priority !== undefined ? options.priority : this.config.defaultPriority,
901
772
  expires: options && options.expires !== undefined ? options.expires : this.config.defaultTTL + Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getCurrTime"])()
902
773
  };
903
-
904
774
  if (cacheItemOptions.priority < 1 || cacheItemOptions.priority > 5) {
905
775
  logger.warn("Invalid parameter: priority due to out or range. It should be within 1 and 5.");
906
776
  return;
907
777
  }
908
-
909
- var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions); // check wether this item is too big;
910
-
778
+ var item = this.fillCacheItem(prefixedKey, value, cacheItemOptions);
779
+ // check wether this item is too big;
911
780
  if (item.byteSize > this.config.itemMaxSize) {
912
781
  logger.warn("Item with key: " + key + " you are trying to put into is too big!");
913
782
  return;
914
- } // if key already in the cache, then delete it.
915
-
916
-
783
+ }
784
+ // if key already in the cache, then delete it.
917
785
  var presentKeyPrio = this.containsKey(key);
918
-
919
786
  if (presentKeyPrio !== -1) {
920
787
  this._removeItem(prefixedKey, presentKeyPrio - 1);
921
- } // pop out items in the cache when cache is full based on LRU
788
+ }
789
+ // pop out items in the cache when cache is full based on LRU
922
790
  // first start from lowest priority cache list
923
-
924
-
925
791
  var cacheListIdx = this.maxPriority - 1;
926
-
927
792
  while (this._isCacheFull(item.byteSize) && cacheListIdx >= 0) {
928
793
  if (!this.cacheList[cacheListIdx].isEmpty()) {
929
794
  var popedItemKey = this.cacheList[cacheListIdx].getLastItem();
930
-
931
795
  this._removeItem(popedItemKey, cacheListIdx);
932
796
  } else {
933
797
  cacheListIdx -= 1;
934
798
  }
935
799
  }
936
-
937
800
  this._setItem(prefixedKey, item, Number(item.priority) - 1);
938
801
  };
939
802
  /**
@@ -948,20 +811,15 @@ function (_super) {
948
811
  * @param key - the key of the item
949
812
  * @param options - the options of callback function
950
813
  */
951
-
952
-
953
814
  InMemoryCacheClass.prototype.getItem = function (key, options) {
954
815
  var ret = null;
955
816
  var prefixedKey = this.config.keyPrefix + key;
956
-
957
817
  if (prefixedKey === this.config.keyPrefix || prefixedKey === this.cacheCurSizeKey) {
958
818
  logger.warn("Invalid key: should not be empty or 'CurSize'");
959
819
  return null;
960
- } // check whether it's in the cachelist
961
-
962
-
820
+ }
821
+ // check whether it's in the cachelist
963
822
  var presentKeyPrio = this.containsKey(key);
964
-
965
823
  if (presentKeyPrio !== -1) {
966
824
  if (this._isExpired(prefixedKey)) {
967
825
  // if expired, remove that item and return null
@@ -974,17 +832,13 @@ function (_super) {
974
832
  return item.data;
975
833
  }
976
834
  }
977
-
978
835
  if (options && options.callback !== undefined) {
979
836
  var val = options.callback();
980
-
981
837
  if (val !== null) {
982
838
  this.setItem(key, val, options);
983
839
  }
984
-
985
840
  return val;
986
841
  }
987
-
988
842
  return null;
989
843
  };
990
844
  /**
@@ -992,13 +846,10 @@ function (_super) {
992
846
  *
993
847
  * @param key - the key of the item
994
848
  */
995
-
996
-
997
849
  InMemoryCacheClass.prototype.removeItem = function (key) {
998
- var prefixedKey = this.config.keyPrefix + key; // check if the key is in the cache
999
-
850
+ var prefixedKey = this.config.keyPrefix + key;
851
+ // check if the key is in the cache
1000
852
  var presentKeyPrio = this.containsKey(key);
1001
-
1002
853
  if (presentKeyPrio !== -1) {
1003
854
  this._removeItem(prefixedKey, presentKeyPrio - 1);
1004
855
  }
@@ -1006,16 +857,12 @@ function (_super) {
1006
857
  /**
1007
858
  * clear the entire cache
1008
859
  */
1009
-
1010
-
1011
860
  InMemoryCacheClass.prototype.clear = function () {
1012
861
  var e_1, _a;
1013
-
1014
862
  for (var i = 0; i < this.maxPriority; i += 1) {
1015
863
  try {
1016
864
  for (var _b = (e_1 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
1017
865
  var key = _c.value;
1018
-
1019
866
  this._removeItem(key, i);
1020
867
  }
1021
868
  } catch (e_1_1) {
@@ -1034,13 +881,9 @@ function (_super) {
1034
881
  /**
1035
882
  * Return all the keys in the cache.
1036
883
  */
1037
-
1038
-
1039
884
  InMemoryCacheClass.prototype.getAllKeys = function () {
1040
885
  var e_2, _a;
1041
-
1042
886
  var keys = [];
1043
-
1044
887
  for (var i = 0; i < this.maxPriority; i += 1) {
1045
888
  try {
1046
889
  for (var _b = (e_2 = void 0, __values(this.cacheList[i].getKeys())), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -1059,7 +902,6 @@ function (_super) {
1059
902
  }
1060
903
  }
1061
904
  }
1062
-
1063
905
  return keys;
1064
906
  };
1065
907
  /**
@@ -1067,8 +909,6 @@ function (_super) {
1067
909
  *
1068
910
  * @return the current size of the cache
1069
911
  */
1070
-
1071
-
1072
912
  InMemoryCacheClass.prototype.getCacheCurSize = function () {
1073
913
  return this.curSizeInBytes;
1074
914
  };
@@ -1076,21 +916,16 @@ function (_super) {
1076
916
  * Return a new instance of cache with customized configuration.
1077
917
  * @param config - the customized configuration
1078
918
  */
1079
-
1080
-
1081
919
  InMemoryCacheClass.prototype.createInstance = function (config) {
1082
920
  return new InMemoryCacheClass(config);
1083
921
  };
1084
-
1085
922
  return InMemoryCacheClass;
1086
923
  }(_StorageCache__WEBPACK_IMPORTED_MODULE_1__["StorageCache"]);
1087
924
 
1088
-
1089
925
  var InMemoryCache = new InMemoryCacheClass();
1090
926
  /**
1091
927
  * @deprecated use named import
1092
928
  */
1093
-
1094
929
  /* harmony default export */ __webpack_exports__["default"] = (InMemoryCache);
1095
930
 
1096
931
  /***/ }),
@@ -1131,17 +966,12 @@ function _typeof(obj) {
1131
966
  */
1132
967
 
1133
968
 
1134
-
1135
-
1136
969
  var logger = new _aws_amplify_core__WEBPACK_IMPORTED_MODULE_1__["ConsoleLogger"]('StorageCache');
1137
970
  /**
1138
971
  * Initialization of the cache
1139
972
  *
1140
973
  */
1141
-
1142
- var StorageCache =
1143
- /** @class */
1144
- function () {
974
+ var StorageCache = /** @class */function () {
1145
975
  /**
1146
976
  * Initialize the cache
1147
977
  * @param config - the configuration of the cache
@@ -1151,51 +981,41 @@ function () {
1151
981
  this.cacheCurSizeKey = this.config.keyPrefix + 'CurSize';
1152
982
  this.checkConfig();
1153
983
  }
1154
-
1155
984
  StorageCache.prototype.getModuleName = function () {
1156
985
  return 'Cache';
1157
986
  };
1158
-
1159
987
  StorageCache.prototype.checkConfig = function () {
1160
988
  // check configuration
1161
989
  if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.capacityInBytes)) {
1162
990
  logger.error('Invalid parameter: capacityInBytes. It should be an Integer. Setting back to default.');
1163
991
  this.config.capacityInBytes = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].capacityInBytes;
1164
992
  }
1165
-
1166
993
  if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.itemMaxSize)) {
1167
994
  logger.error('Invalid parameter: itemMaxSize. It should be an Integer. Setting back to default.');
1168
995
  this.config.itemMaxSize = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].itemMaxSize;
1169
996
  }
1170
-
1171
997
  if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.defaultTTL)) {
1172
998
  logger.error('Invalid parameter: defaultTTL. It should be an Integer. Setting back to default.');
1173
999
  this.config.defaultTTL = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].defaultTTL;
1174
1000
  }
1175
-
1176
1001
  if (!Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["isInteger"])(this.config.defaultPriority)) {
1177
1002
  logger.error('Invalid parameter: defaultPriority. It should be an Integer. Setting back to default.');
1178
1003
  this.config.defaultPriority = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].defaultPriority;
1179
1004
  }
1180
-
1181
1005
  if (this.config.itemMaxSize > this.config.capacityInBytes) {
1182
1006
  logger.error('Invalid parameter: itemMaxSize. It should be smaller than capacityInBytes. Setting back to default.');
1183
1007
  this.config.itemMaxSize = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].itemMaxSize;
1184
1008
  }
1185
-
1186
1009
  if (this.config.defaultPriority > 5 || this.config.defaultPriority < 1) {
1187
1010
  logger.error('Invalid parameter: defaultPriority. It should be between 1 and 5. Setting back to default.');
1188
1011
  this.config.defaultPriority = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].defaultPriority;
1189
1012
  }
1190
-
1191
1013
  if (Number(this.config.warningThreshold) > 1 || Number(this.config.warningThreshold) < 0) {
1192
1014
  logger.error('Invalid parameter: warningThreshold. It should be between 0 and 1. Setting back to default.');
1193
1015
  this.config.warningThreshold = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].warningThreshold;
1194
- } // set 5MB limit
1195
-
1196
-
1016
+ }
1017
+ // set 5MB limit
1197
1018
  var cacheLimit = 5 * 1024 * 1024;
1198
-
1199
1019
  if (this.config.capacityInBytes > cacheLimit) {
1200
1020
  logger.error('Cache Capacity should be less than 5MB. Setting back to default. Setting back to default.');
1201
1021
  this.config.capacityInBytes = _Utils__WEBPACK_IMPORTED_MODULE_0__["defaultConfig"].capacityInBytes;
@@ -1208,8 +1028,6 @@ function () {
1208
1028
  *
1209
1029
  * @return - the item which has the meta-data and the value
1210
1030
  */
1211
-
1212
-
1213
1031
  StorageCache.prototype.fillCacheItem = function (key, value, options) {
1214
1032
  var ret = {
1215
1033
  key: key,
@@ -1221,8 +1039,8 @@ function () {
1221
1039
  type: _typeof(value),
1222
1040
  byteSize: 0
1223
1041
  };
1224
- ret.byteSize = Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getByteLength"])(JSON.stringify(ret)); // for accurate size
1225
-
1042
+ ret.byteSize = Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getByteLength"])(JSON.stringify(ret));
1043
+ // for accurate size
1226
1044
  ret.byteSize = Object(_Utils__WEBPACK_IMPORTED_MODULE_0__["getByteLength"])(JSON.stringify(ret));
1227
1045
  return ret;
1228
1046
  };
@@ -1232,30 +1050,23 @@ function () {
1232
1050
  *
1233
1051
  * @return - the current configuration
1234
1052
  */
1235
-
1236
-
1237
1053
  StorageCache.prototype.configure = function (config) {
1238
1054
  if (!config) {
1239
1055
  return this.config;
1240
1056
  }
1241
-
1242
1057
  if (config.keyPrefix) {
1243
1058
  logger.warn("Don't try to configure keyPrefix!");
1244
1059
  }
1245
-
1246
1060
  this.config = Object.assign({}, this.config, config, config.Cache);
1247
1061
  this.checkConfig();
1248
1062
  return this.config;
1249
1063
  };
1250
-
1251
1064
  return StorageCache;
1252
1065
  }();
1253
1066
 
1254
-
1255
1067
  /**
1256
1068
  * @deprecated use named import
1257
1069
  */
1258
-
1259
1070
  /* harmony default export */ __webpack_exports__["default"] = (StorageCache);
1260
1071
 
1261
1072
  /***/ }),
@@ -1283,8 +1094,8 @@ __webpack_require__.r(__webpack_exports__);
1283
1094
  */
1284
1095
  var __values = undefined && undefined.__values || function (o) {
1285
1096
  var s = typeof Symbol === "function" && Symbol.iterator,
1286
- m = s && o[s],
1287
- i = 0;
1097
+ m = s && o[s],
1098
+ i = 0;
1288
1099
  if (m) return m.call(o);
1289
1100
  if (o && typeof o.length === "number") return {
1290
1101
  next: function next() {
@@ -1297,16 +1108,12 @@ var __values = undefined && undefined.__values || function (o) {
1297
1108
  };
1298
1109
  throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
1299
1110
  };
1300
-
1301
- var DoubleLinkedNode =
1302
- /** @class */
1303
- function () {
1111
+ var DoubleLinkedNode = /** @class */function () {
1304
1112
  function DoubleLinkedNode(keyVal) {
1305
1113
  this.key = keyVal ? keyVal : '';
1306
1114
  this.prevNode = null;
1307
1115
  this.nextNode = null;
1308
1116
  }
1309
-
1310
1117
  return DoubleLinkedNode;
1311
1118
  }();
1312
1119
  /**
@@ -1320,11 +1127,7 @@ function () {
1320
1127
  * @member hashtable - the hashtable which maps cache key to list node
1321
1128
  * @member length - length of the list
1322
1129
  */
1323
-
1324
-
1325
- var CacheList =
1326
- /** @class */
1327
- function () {
1130
+ var CacheList = /** @class */function () {
1328
1131
  /**
1329
1132
  * initialization
1330
1133
  */
@@ -1341,8 +1144,6 @@ function () {
1341
1144
  *
1342
1145
  * @param node
1343
1146
  */
1344
-
1345
-
1346
1147
  CacheList.prototype.insertNodeToHead = function (node) {
1347
1148
  var tmp = this.head.nextNode;
1348
1149
  this.head.nextNode = node;
@@ -1356,8 +1157,6 @@ function () {
1356
1157
  *
1357
1158
  * @param node
1358
1159
  */
1359
-
1360
-
1361
1160
  CacheList.prototype.removeNode = function (node) {
1362
1161
  node.prevNode.nextNode = node.nextNode;
1363
1162
  node.nextNode.prevNode = node.prevNode;
@@ -1368,8 +1167,6 @@ function () {
1368
1167
  /**
1369
1168
  * @return true if list is empty
1370
1169
  */
1371
-
1372
-
1373
1170
  CacheList.prototype.isEmpty = function () {
1374
1171
  return this.length === 0;
1375
1172
  };
@@ -1378,8 +1175,6 @@ function () {
1378
1175
  *
1379
1176
  * @param key - key of the node
1380
1177
  */
1381
-
1382
-
1383
1178
  CacheList.prototype.refresh = function (key) {
1384
1179
  var node = this.hashtable[key];
1385
1180
  this.removeNode(node);
@@ -1390,8 +1185,6 @@ function () {
1390
1185
  *
1391
1186
  * @param key - the key of the node
1392
1187
  */
1393
-
1394
-
1395
1188
  CacheList.prototype.insertItem = function (key) {
1396
1189
  var node = new DoubleLinkedNode(key);
1397
1190
  this.hashtable[key] = node;
@@ -1400,8 +1193,6 @@ function () {
1400
1193
  /**
1401
1194
  * @return the LAST Recently Visited key
1402
1195
  */
1403
-
1404
-
1405
1196
  CacheList.prototype.getLastItem = function () {
1406
1197
  return this.tail.prevNode.key;
1407
1198
  };
@@ -1409,8 +1200,6 @@ function () {
1409
1200
  * remove the cache key from the list and hashtable
1410
1201
  * @param key - the key of the node
1411
1202
  */
1412
-
1413
-
1414
1203
  CacheList.prototype.removeItem = function (key) {
1415
1204
  var removedItem = this.hashtable[key];
1416
1205
  this.removeNode(removedItem);
@@ -1419,8 +1208,6 @@ function () {
1419
1208
  /**
1420
1209
  * @return length of the list
1421
1210
  */
1422
-
1423
-
1424
1211
  CacheList.prototype.getSize = function () {
1425
1212
  return this.length;
1426
1213
  };
@@ -1428,23 +1215,17 @@ function () {
1428
1215
  * @return true if the key is in the hashtable
1429
1216
  * @param key
1430
1217
  */
1431
-
1432
-
1433
1218
  CacheList.prototype.containsKey = function (key) {
1434
1219
  return key in this.hashtable;
1435
1220
  };
1436
1221
  /**
1437
1222
  * clean up the list and hashtable
1438
1223
  */
1439
-
1440
-
1441
1224
  CacheList.prototype.clearList = function () {
1442
1225
  var e_1, _a;
1443
-
1444
1226
  try {
1445
1227
  for (var _b = __values(Object.keys(this.hashtable)), _c = _b.next(); !_c.done; _c = _b.next()) {
1446
1228
  var key = _c.value;
1447
-
1448
1229
  if (this.hashtable.hasOwnProperty(key)) {
1449
1230
  delete this.hashtable[key];
1450
1231
  }
@@ -1460,7 +1241,6 @@ function () {
1460
1241
  if (e_1) throw e_1.error;
1461
1242
  }
1462
1243
  }
1463
-
1464
1244
  this.head.nextNode = this.tail;
1465
1245
  this.tail.prevNode = this.head;
1466
1246
  this.length = 0;
@@ -1468,8 +1248,6 @@ function () {
1468
1248
  /**
1469
1249
  * @return all keys in the hashtable
1470
1250
  */
1471
-
1472
-
1473
1251
  CacheList.prototype.getKeys = function () {
1474
1252
  return Object.keys(this.hashtable);
1475
1253
  };
@@ -1479,8 +1257,6 @@ function () {
1479
1257
  * @param key
1480
1258
  * @return true if key is the head node
1481
1259
  */
1482
-
1483
-
1484
1260
  CacheList.prototype.isHeadNode = function (key) {
1485
1261
  var node = this.hashtable[key];
1486
1262
  return node.prevNode === this.head;
@@ -1491,16 +1267,12 @@ function () {
1491
1267
  * @param key
1492
1268
  * @return true if key is the tail node
1493
1269
  */
1494
-
1495
-
1496
1270
  CacheList.prototype.isTailNode = function (key) {
1497
1271
  var node = this.hashtable[key];
1498
1272
  return node.nextNode === this.tail;
1499
1273
  };
1500
-
1501
1274
  return CacheList;
1502
1275
  }();
1503
-
1504
1276
  /* harmony default export */ __webpack_exports__["default"] = (CacheList);
1505
1277
 
1506
1278
  /***/ }),
@@ -1537,7 +1309,6 @@ __webpack_require__.r(__webpack_exports__);
1537
1309
  /**
1538
1310
  * Default cache config
1539
1311
  */
1540
-
1541
1312
  var defaultConfig = {
1542
1313
  keyPrefix: 'aws-amplify-cache',
1543
1314
  capacityInBytes: 1048576,
@@ -1553,32 +1324,26 @@ var defaultConfig = {
1553
1324
  * return the byte size of the string
1554
1325
  * @param str
1555
1326
  */
1556
-
1557
1327
  function getByteLength(str) {
1558
1328
  var ret = 0;
1559
1329
  ret = str.length;
1560
-
1561
1330
  for (var i = str.length; i >= 0; i -= 1) {
1562
1331
  var charCode = str.charCodeAt(i);
1563
-
1564
1332
  if (charCode > 0x7f && charCode <= 0x7ff) {
1565
1333
  ret += 1;
1566
1334
  } else if (charCode > 0x7ff && charCode <= 0xffff) {
1567
1335
  ret += 2;
1568
- } // trail surrogate
1569
-
1570
-
1336
+ }
1337
+ // trail surrogate
1571
1338
  if (charCode >= 0xdc00 && charCode <= 0xdfff) {
1572
1339
  i -= 1;
1573
1340
  }
1574
1341
  }
1575
-
1576
1342
  return ret;
1577
1343
  }
1578
1344
  /**
1579
1345
  * get current time
1580
1346
  */
1581
-
1582
1347
  function getCurrTime() {
1583
1348
  var currTime = new Date();
1584
1349
  return currTime.getTime();
@@ -1586,51 +1351,37 @@ function getCurrTime() {
1586
1351
  /**
1587
1352
  * check if passed value is an integer
1588
1353
  */
1589
-
1590
1354
  function isInteger(value) {
1591
1355
  if (Number.isInteger) {
1592
1356
  return Number.isInteger(value);
1593
1357
  }
1594
-
1595
1358
  return _isInteger(value);
1596
1359
  }
1597
-
1598
1360
  function _isInteger(value) {
1599
1361
  return typeof value === 'number' && isFinite(value) && Math.floor(value) === value;
1600
1362
  }
1601
1363
  /**
1602
1364
  * provide an object as the in-memory cache
1603
1365
  */
1604
-
1605
-
1606
1366
  var store = {};
1607
-
1608
- var CacheObject =
1609
- /** @class */
1610
- function () {
1367
+ var CacheObject = /** @class */function () {
1611
1368
  function CacheObject() {}
1612
-
1613
1369
  CacheObject.clear = function () {
1614
1370
  store = {};
1615
1371
  };
1616
-
1617
1372
  CacheObject.getItem = function (key) {
1618
1373
  return store[key] || null;
1619
1374
  };
1620
-
1621
1375
  CacheObject.setItem = function (key, value) {
1622
1376
  store[key] = value;
1623
1377
  };
1624
-
1625
1378
  CacheObject.removeItem = function (key) {
1626
1379
  delete store[key];
1627
1380
  };
1628
-
1629
1381
  return CacheObject;
1630
1382
  }();
1631
1383
 
1632
1384
 
1633
-
1634
1385
  /***/ }),
1635
1386
 
1636
1387
  /***/ "./lib-esm/Utils/index.js":
@@ -1709,7 +1460,6 @@ __webpack_require__.r(__webpack_exports__);
1709
1460
  /**
1710
1461
  * @deprecated use named import
1711
1462
  */
1712
-
1713
1463
  /* harmony default export */ __webpack_exports__["default"] = (_BrowserStorageCache__WEBPACK_IMPORTED_MODULE_1__["BrowserStorageCache"]);
1714
1464
  _aws_amplify_core__WEBPACK_IMPORTED_MODULE_0__["Amplify"].register(_BrowserStorageCache__WEBPACK_IMPORTED_MODULE_1__["BrowserStorageCache"]);
1715
1465