keyv 5.2.1 → 5.2.3

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.
package/README.md CHANGED
@@ -144,7 +144,15 @@ await cache.get('foo'); // 'cache'
144
144
 
145
145
  # Events
146
146
 
147
- Keyv is a custom `EventEmitter` and will emit an `'error'` event if there is an error. In addition it will emit a `clear` and `disconnect` event when the corresponding methods are called.
147
+ Keyv is a custom `EventEmitter` and will emit an `'error'` event if there is an error.
148
+ If there is no listener for the `'error'` event, an uncaught exception will be thrown.
149
+ To disable the `'error'` event, pass `emitErrors: false` in the constructor options.
150
+
151
+ ```js
152
+ const keyv = new Keyv({ emitErrors: false });
153
+ ```
154
+
155
+ In addition it will emit `clear` and `disconnect` events when the corresponding methods are called.
148
156
 
149
157
  ```js
150
158
  const keyv = new Keyv();
@@ -277,41 +285,7 @@ The following are third-party storage adapters compatible with Keyv:
277
285
  - [keyv-azuretable](https://github.com/howlowck/keyv-azuretable) - Azure Table Storage/API adapter for Keyv
278
286
  - [keyv-arango](https://github.com/TimMikeladze/keyv-arango) - ArangoDB storage adapter for Keyv
279
287
  - [keyv-momento](https://github.com/momentohq/node-keyv-adaptor/) - Momento storage adapter for Keyv
280
-
281
- # Add Cache Support to your Module
282
-
283
- Keyv is designed to be easily embedded into other modules to add cache support. The recommended pattern is to expose a `cache` option in your modules options which is passed through to Keyv. Caching will work in memory by default and users have the option to also install a Keyv storage adapter and pass in a connection string, or any other storage that implements the `Map` API.
284
-
285
- You should also set a namespace for your module so you can safely call `.clear()` without clearing unrelated app data.
286
-
287
- Inside your module:
288
-
289
- ```js
290
- class AwesomeModule {
291
- constructor(opts) {
292
- this.cache = new Keyv({
293
- uri: typeof opts.cache === 'string' && opts.cache,
294
- store: typeof opts.cache !== 'string' && opts.cache,
295
- namespace: 'awesome-module'
296
- });
297
- }
298
- }
299
- ```
300
-
301
- Now it can be consumed like this:
302
-
303
- ```js
304
- import AwesomeModule from 'awesome-module';
305
-
306
- // Caches stuff in memory by default
307
- const awesomeModule = new AwesomeModule();
308
-
309
- // After npm install --save keyv-redis
310
- const awesomeModule = new AwesomeModule({ cache: 'redis://localhost' });
311
-
312
- // Some third-party module that implements the Map API
313
- const awesomeModule = new AwesomeModule({ cache: some3rdPartyStore });
314
- ```
288
+ - [@resolid/keyv-sqlite](https://github.com/huijiewei/keyv-sqlite) - A new SQLite storage adapter for Keyv
315
289
 
316
290
  # Compression
317
291
 
package/dist/index.cjs CHANGED
@@ -62,7 +62,7 @@ var EventManager = class {
62
62
  off(event, listener) {
63
63
  const listeners = this._eventListeners.get(event) ?? [];
64
64
  const index = listeners.indexOf(listener);
65
- if (index > -1) {
65
+ if (index !== -1) {
66
66
  listeners.splice(index, 1);
67
67
  }
68
68
  if (listeners.length === 0) {
@@ -231,7 +231,9 @@ var iterableAdapters = [
231
231
  "postgres",
232
232
  "mysql",
233
233
  "mongo",
234
- "redis"
234
+ "redis",
235
+ "valkey",
236
+ "etcd"
235
237
  ];
236
238
  var Keyv = class extends event_manager_default {
237
239
  opts;
@@ -291,7 +293,7 @@ var Keyv = class extends event_manager_default {
291
293
  if (!this._isValidStorageAdapter(this._store)) {
292
294
  throw new Error("Invalid storage adapter");
293
295
  }
294
- if (typeof this._store.on === "function" && this.opts.emitErrors) {
296
+ if (typeof this._store.on === "function") {
295
297
  this._store.on("error", (error) => this.emit("error", error));
296
298
  }
297
299
  this._store.namespace = this._namespace;
@@ -325,7 +327,7 @@ var Keyv = class extends event_manager_default {
325
327
  if (this._isValidStorageAdapter(store)) {
326
328
  this._store = store;
327
329
  this.opts.store = store;
328
- if (typeof store.on === "function" && this.opts.emitErrors) {
330
+ if (typeof store.on === "function") {
329
331
  store.on("error", (error) => this.emit("error", error));
330
332
  }
331
333
  if (this._namespace) {
@@ -572,10 +574,19 @@ var Keyv = class extends event_manager_default {
572
574
  }
573
575
  const formattedValue = { value, expires };
574
576
  const serializedValue = await this.serializeData(formattedValue);
575
- await store.set(keyPrefixed, serializedValue, ttl);
577
+ let result = true;
578
+ try {
579
+ const value2 = await store.set(keyPrefixed, serializedValue, ttl);
580
+ if (typeof value2 === "boolean") {
581
+ result = value2;
582
+ }
583
+ } catch (error) {
584
+ result = false;
585
+ this.emit("error", error);
586
+ }
576
587
  this.hooks.trigger("postSet" /* POST_SET */, { key: keyPrefixed, value: serializedValue, ttl });
577
588
  this.stats.set();
578
- return true;
589
+ return result;
579
590
  }
580
591
  /**
581
592
  * Delete an Entry
@@ -598,7 +609,16 @@ var Keyv = class extends event_manager_default {
598
609
  }
599
610
  const keyPrefixed = this._getKeyPrefix(key);
600
611
  this.hooks.trigger("preDelete" /* PRE_DELETE */, { key: keyPrefixed });
601
- const result = await store.delete(keyPrefixed);
612
+ let result = true;
613
+ try {
614
+ const value = await store.delete(keyPrefixed);
615
+ if (typeof value === "boolean") {
616
+ result = value;
617
+ }
618
+ } catch (error) {
619
+ result = false;
620
+ this.emit("error", error);
621
+ }
602
622
  this.hooks.trigger("postDelete" /* POST_DELETE */, { key: keyPrefixed, value: result });
603
623
  this.stats.delete();
604
624
  return result;
@@ -610,7 +630,11 @@ var Keyv = class extends event_manager_default {
610
630
  async clear() {
611
631
  this.emit("clear");
612
632
  const { store } = this.opts;
613
- await store.clear();
633
+ try {
634
+ await store.clear();
635
+ } catch (error) {
636
+ this.emit("error", error);
637
+ }
614
638
  }
615
639
  /**
616
640
  * Has a key
@@ -623,7 +647,12 @@ var Keyv = class extends event_manager_default {
623
647
  if (store.has !== void 0 && !(store instanceof Map)) {
624
648
  return store.has(keyPrefixed);
625
649
  }
626
- const rawData = await store.get(keyPrefixed);
650
+ let rawData;
651
+ try {
652
+ rawData = await store.get(keyPrefixed);
653
+ } catch (error) {
654
+ this.emit("error", error);
655
+ }
627
656
  if (rawData) {
628
657
  const data = await this.deserializeData(rawData);
629
658
  if (data) {
@@ -646,6 +675,12 @@ var Keyv = class extends event_manager_default {
646
675
  return store.disconnect();
647
676
  }
648
677
  }
678
+ emit(event, ...arguments_) {
679
+ if (event === "error" && !this.opts.emitErrors) {
680
+ return;
681
+ }
682
+ super.emit(event, ...arguments_);
683
+ }
649
684
  async serializeData(data) {
650
685
  if (!this._serialize) {
651
686
  return data;
package/dist/index.d.cts CHANGED
@@ -260,6 +260,7 @@ declare class Keyv<GenericValue = any> extends EventManager {
260
260
  * @returns {Promise<void>}
261
261
  */
262
262
  disconnect(): Promise<void>;
263
+ emit(event: string, ...arguments_: any[]): void;
263
264
  serializeData<T>(data: DeserializedData<T>): Promise<string | DeserializedData<T>>;
264
265
  deserializeData<T>(data: string | DeserializedData<T>): Promise<DeserializedData<T> | undefined>;
265
266
  }
package/dist/index.d.ts CHANGED
@@ -260,6 +260,7 @@ declare class Keyv<GenericValue = any> extends EventManager {
260
260
  * @returns {Promise<void>}
261
261
  */
262
262
  disconnect(): Promise<void>;
263
+ emit(event: string, ...arguments_: any[]): void;
263
264
  serializeData<T>(data: DeserializedData<T>): Promise<string | DeserializedData<T>>;
264
265
  deserializeData<T>(data: string | DeserializedData<T>): Promise<DeserializedData<T> | undefined>;
265
266
  }
package/dist/index.js CHANGED
@@ -36,7 +36,7 @@ var EventManager = class {
36
36
  off(event, listener) {
37
37
  const listeners = this._eventListeners.get(event) ?? [];
38
38
  const index = listeners.indexOf(listener);
39
- if (index > -1) {
39
+ if (index !== -1) {
40
40
  listeners.splice(index, 1);
41
41
  }
42
42
  if (listeners.length === 0) {
@@ -205,7 +205,9 @@ var iterableAdapters = [
205
205
  "postgres",
206
206
  "mysql",
207
207
  "mongo",
208
- "redis"
208
+ "redis",
209
+ "valkey",
210
+ "etcd"
209
211
  ];
210
212
  var Keyv = class extends event_manager_default {
211
213
  opts;
@@ -265,7 +267,7 @@ var Keyv = class extends event_manager_default {
265
267
  if (!this._isValidStorageAdapter(this._store)) {
266
268
  throw new Error("Invalid storage adapter");
267
269
  }
268
- if (typeof this._store.on === "function" && this.opts.emitErrors) {
270
+ if (typeof this._store.on === "function") {
269
271
  this._store.on("error", (error) => this.emit("error", error));
270
272
  }
271
273
  this._store.namespace = this._namespace;
@@ -299,7 +301,7 @@ var Keyv = class extends event_manager_default {
299
301
  if (this._isValidStorageAdapter(store)) {
300
302
  this._store = store;
301
303
  this.opts.store = store;
302
- if (typeof store.on === "function" && this.opts.emitErrors) {
304
+ if (typeof store.on === "function") {
303
305
  store.on("error", (error) => this.emit("error", error));
304
306
  }
305
307
  if (this._namespace) {
@@ -546,10 +548,19 @@ var Keyv = class extends event_manager_default {
546
548
  }
547
549
  const formattedValue = { value, expires };
548
550
  const serializedValue = await this.serializeData(formattedValue);
549
- await store.set(keyPrefixed, serializedValue, ttl);
551
+ let result = true;
552
+ try {
553
+ const value2 = await store.set(keyPrefixed, serializedValue, ttl);
554
+ if (typeof value2 === "boolean") {
555
+ result = value2;
556
+ }
557
+ } catch (error) {
558
+ result = false;
559
+ this.emit("error", error);
560
+ }
550
561
  this.hooks.trigger("postSet" /* POST_SET */, { key: keyPrefixed, value: serializedValue, ttl });
551
562
  this.stats.set();
552
- return true;
563
+ return result;
553
564
  }
554
565
  /**
555
566
  * Delete an Entry
@@ -572,7 +583,16 @@ var Keyv = class extends event_manager_default {
572
583
  }
573
584
  const keyPrefixed = this._getKeyPrefix(key);
574
585
  this.hooks.trigger("preDelete" /* PRE_DELETE */, { key: keyPrefixed });
575
- const result = await store.delete(keyPrefixed);
586
+ let result = true;
587
+ try {
588
+ const value = await store.delete(keyPrefixed);
589
+ if (typeof value === "boolean") {
590
+ result = value;
591
+ }
592
+ } catch (error) {
593
+ result = false;
594
+ this.emit("error", error);
595
+ }
576
596
  this.hooks.trigger("postDelete" /* POST_DELETE */, { key: keyPrefixed, value: result });
577
597
  this.stats.delete();
578
598
  return result;
@@ -584,7 +604,11 @@ var Keyv = class extends event_manager_default {
584
604
  async clear() {
585
605
  this.emit("clear");
586
606
  const { store } = this.opts;
587
- await store.clear();
607
+ try {
608
+ await store.clear();
609
+ } catch (error) {
610
+ this.emit("error", error);
611
+ }
588
612
  }
589
613
  /**
590
614
  * Has a key
@@ -597,7 +621,12 @@ var Keyv = class extends event_manager_default {
597
621
  if (store.has !== void 0 && !(store instanceof Map)) {
598
622
  return store.has(keyPrefixed);
599
623
  }
600
- const rawData = await store.get(keyPrefixed);
624
+ let rawData;
625
+ try {
626
+ rawData = await store.get(keyPrefixed);
627
+ } catch (error) {
628
+ this.emit("error", error);
629
+ }
601
630
  if (rawData) {
602
631
  const data = await this.deserializeData(rawData);
603
632
  if (data) {
@@ -620,6 +649,12 @@ var Keyv = class extends event_manager_default {
620
649
  return store.disconnect();
621
650
  }
622
651
  }
652
+ emit(event, ...arguments_) {
653
+ if (event === "error" && !this.opts.emitErrors) {
654
+ return;
655
+ }
656
+ super.emit(event, ...arguments_);
657
+ }
623
658
  async serializeData(data) {
624
659
  if (!this._serialize) {
625
660
  return data;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyv",
3
- "version": "5.2.1",
3
+ "version": "5.2.3",
4
4
  "description": "Simple key-value storage with support for multiple backends",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -69,19 +69,19 @@
69
69
  },
70
70
  "homepage": "https://github.com/jaredwray/keyv",
71
71
  "dependencies": {
72
- "@keyv/serialize": "*"
72
+ "@keyv/serialize": "^1.0.2"
73
73
  },
74
74
  "devDependencies": {
75
- "@keyv/compress-brotli": "*",
76
- "@keyv/compress-gzip": "*",
77
- "@keyv/memcache": "*",
78
- "@keyv/mongo": "*",
79
- "@keyv/sqlite": "*",
80
- "@keyv/test-suite": "*",
81
75
  "rimraf": "^6.0.1",
82
76
  "timekeeper": "^2.3.1",
83
77
  "tsd": "^0.31.2",
84
- "xo": "^0.59.3"
78
+ "xo": "^0.60.0",
79
+ "@keyv/compress-brotli": "^2.0.2",
80
+ "@keyv/compress-gzip": "^2.0.2",
81
+ "@keyv/memcache": "^2.0.1",
82
+ "@keyv/mongo": "^3.0.1",
83
+ "@keyv/sqlite": "^4.0.1",
84
+ "@keyv/test-suite": "^2.0.3"
85
85
  },
86
86
  "tsd": {
87
87
  "directory": "test"