@bantis/local-cipher 2.1.0 → 2.2.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.
@@ -23,7 +23,7 @@ export declare class SecureStorageService {
23
23
  /**
24
24
  * Observable de eventos de storage
25
25
  */
26
- events$: any;
26
+ events$: Observable<StorageEventData>;
27
27
  constructor(config?: SecureStorageConfig);
28
28
  /**
29
29
  * Guarda un valor encriptado
@@ -110,7 +110,7 @@ class EncryptionHelper {
110
110
  // Derivar la clave AES-GCM
111
111
  return crypto.subtle.deriveKey({
112
112
  name: 'PBKDF2',
113
- salt,
113
+ salt: salt,
114
114
  iterations: this.config.iterations,
115
115
  hash: EncryptionHelper.HASH_ALGORITHM,
116
116
  }, keyMaterial, {
@@ -392,7 +392,7 @@ class Logger {
392
392
  return false;
393
393
  return Logger.LOG_LEVELS[level] <= Logger.LOG_LEVELS[this.logLevel];
394
394
  }
395
- formatMessage(level, message, ...args) {
395
+ formatMessage(level, message) {
396
396
  const timestamp = new Date().toISOString();
397
397
  return `[${this.prefix}] [${level.toUpperCase()}] ${timestamp} - ${message}`;
398
398
  }
@@ -545,17 +545,37 @@ class NamespacedStorage {
545
545
  this.storage = storage;
546
546
  this.prefix = `__ns_${namespace}__`;
547
547
  }
548
+ async getIndex() {
549
+ const indexValue = await this.storage.getItem(`${this.prefix}__index__`);
550
+ return indexValue ? JSON.parse(indexValue) : [];
551
+ }
552
+ async saveToIndex(key) {
553
+ const index = await this.getIndex();
554
+ if (!index.includes(key)) {
555
+ index.push(key);
556
+ await this.storage.setItem(`${this.prefix}__index__`, JSON.stringify(index));
557
+ }
558
+ }
559
+ async removeFromIndex(key) {
560
+ const index = await this.getIndex();
561
+ const newIndex = index.filter(k => k !== key);
562
+ if (newIndex.length !== index.length) {
563
+ await this.storage.setItem(`${this.prefix}__index__`, JSON.stringify(newIndex));
564
+ }
565
+ }
548
566
  /**
549
567
  * Set item in this namespace
550
568
  */
551
569
  async setItem(key, value) {
552
- return this.storage.setItem(`${this.prefix}${key}`, value);
570
+ await this.storage.setItem(`${this.prefix}${key}`, value);
571
+ await this.saveToIndex(key);
553
572
  }
554
573
  /**
555
574
  * Set item with expiry in this namespace
556
575
  */
557
576
  async setItemWithExpiry(key, value, options) {
558
- return this.storage.setItemWithExpiry(`${this.prefix}${key}`, value, options);
577
+ await this.storage.setItemWithExpiry(`${this.prefix}${key}`, value, options);
578
+ await this.saveToIndex(key);
559
579
  }
560
580
  /**
561
581
  * Get item from this namespace
@@ -567,7 +587,8 @@ class NamespacedStorage {
567
587
  * Remove item from this namespace
568
588
  */
569
589
  async removeItem(key) {
570
- return this.storage.removeItem(`${this.prefix}${key}`);
590
+ await this.storage.removeItem(`${this.prefix}${key}`);
591
+ await this.removeFromIndex(key);
571
592
  }
572
593
  /**
573
594
  * Check if item exists in this namespace
@@ -579,29 +600,17 @@ class NamespacedStorage {
579
600
  * Clear all items in this namespace
580
601
  */
581
602
  async clearNamespace() {
582
- const keysToRemove = [];
583
- for (let i = 0; i < localStorage.length; i++) {
584
- const key = localStorage.key(i);
585
- if (key && key.includes(this.prefix)) {
586
- keysToRemove.push(key.replace(this.prefix, ''));
587
- }
588
- }
603
+ const keysToRemove = await this.getIndex();
589
604
  for (const key of keysToRemove) {
590
- await this.removeItem(key);
605
+ await this.storage.removeItem(`${this.prefix}${key}`);
591
606
  }
607
+ await this.storage.removeItem(`${this.prefix}__index__`);
592
608
  }
593
609
  /**
594
610
  * Get all keys in this namespace
595
611
  */
596
612
  async keys() {
597
- const keys = [];
598
- for (let i = 0; i < localStorage.length; i++) {
599
- const key = localStorage.key(i);
600
- if (key && key.includes(this.prefix)) {
601
- keys.push(key.replace(this.prefix, ''));
602
- }
603
- }
604
- return keys;
613
+ return this.getIndex();
605
614
  }
606
615
  }
607
616
 
@@ -624,7 +633,6 @@ async function compress(data) {
624
633
  return encoder.encode(data);
625
634
  }
626
635
  try {
627
- const encoder = new TextEncoder();
628
636
  const stream = new Blob([data]).stream();
629
637
  const compressedStream = stream.pipeThrough(new CompressionStream('gzip'));
630
638
  const compressedBlob = await new Response(compressedStream).blob();
@@ -1153,6 +1161,7 @@ class SecureStorage {
1153
1161
  }
1154
1162
  this.removeAllListeners();
1155
1163
  this.logger.info('SecureStorage destroyed');
1164
+ SecureStorage.instance = null;
1156
1165
  }
1157
1166
  }
1158
1167
  SecureStorage.instance = null;
@@ -1474,7 +1483,7 @@ let SecureStorageService = (() => {
1474
1483
  * @returns Observable con el objeto parseado o null
1475
1484
  */
1476
1485
  getObject(key) {
1477
- return this.getItem(key).pipe(map(value => value ? JSON.parse(value) : null), catchError(() => from([null])));
1486
+ return this.getItem(key).pipe(map((value) => value ? JSON.parse(value) : null), catchError(() => from([null])));
1478
1487
  }
1479
1488
  /**
1480
1489
  * Registra un listener para un tipo de evento específico
@@ -1498,7 +1507,7 @@ let SecureStorageService = (() => {
1498
1507
  * @returns Observable con eventos del tipo especificado
1499
1508
  */
1500
1509
  onEvent$(eventType) {
1501
- return this.events$.pipe(map(event => event.type === eventType ? event : null), map(event => event));
1510
+ return this.events$.pipe(map((event) => event.type === eventType ? event : null), map((event) => event));
1502
1511
  }
1503
1512
  };
1504
1513
  __setFunctionName(_classThis, "SecureStorageService");