keyv 5.6.0 → 6.0.0-alpha.2
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 +15 -15
- package/dist/index.cjs +58 -50
- package/dist/index.d.cts +12 -4
- package/dist/index.d.ts +12 -4
- package/dist/index.js +58 -50
- package/package.json +6 -13
package/README.md
CHANGED
|
@@ -25,12 +25,7 @@ There are a few existing modules similar to Keyv, however Keyv is different beca
|
|
|
25
25
|
- Connection errors are passed through (db failures won't kill your app)
|
|
26
26
|
- Supports the current active LTS version of Node.js or higher
|
|
27
27
|
|
|
28
|
-
# Bun Support
|
|
29
|
-
|
|
30
|
-
We make a best effort to support [Bun](https://bun.sh/) as a runtime. Our default and primary target is Node.js, but we run tests against Bun to ensure compatibility. If you encounter any issues while using Keyv with Bun, please report them at our [GitHub issues](https://github.com/jaredwray/keyv/issues).
|
|
31
|
-
|
|
32
28
|
# Table of Contents
|
|
33
|
-
- [Bun Support](#bun-support)
|
|
34
29
|
- [Usage](#usage)
|
|
35
30
|
- [Type-safe Usage](#type-safe-usage)
|
|
36
31
|
- [Using Storage Adapters](#using-storage-adapters)
|
|
@@ -63,6 +58,7 @@ We make a best effort to support [Bun](https://bun.sh/) as a runtime. Our defaul
|
|
|
63
58
|
- [.deleteMany(keys)](#deletemanykeys)
|
|
64
59
|
- [.clear()](#clear)
|
|
65
60
|
- [.iterator()](#iterator)
|
|
61
|
+
- [Bun Support](#bun-support)
|
|
66
62
|
- [How to Contribute](#how-to-contribute)
|
|
67
63
|
- [License](#license)
|
|
68
64
|
|
|
@@ -318,15 +314,15 @@ The official storage adapters are covered by [over 150 integration tests](https:
|
|
|
318
314
|
|
|
319
315
|
Database | Adapter | Native TTL
|
|
320
316
|
---|---|---
|
|
321
|
-
Redis | [@keyv/redis](https://github.com/jaredwray/keyv/tree/master/
|
|
322
|
-
Valkey | [@keyv/valkey](https://github.com/jaredwray/keyv/tree/master/
|
|
323
|
-
MongoDB | [@keyv/mongo](https://github.com/jaredwray/keyv/tree/master/
|
|
324
|
-
SQLite | [@keyv/sqlite](https://github.com/jaredwray/keyv/tree/master/
|
|
325
|
-
PostgreSQL | [@keyv/postgres](https://github.com/jaredwray/keyv/tree/master/
|
|
326
|
-
MySQL | [@keyv/mysql](https://github.com/jaredwray/keyv/tree/master/
|
|
327
|
-
Etcd | [@keyv/etcd](https://github.com/jaredwray/keyv/tree/master/
|
|
328
|
-
Memcache | [@keyv/memcache](https://github.com/jaredwray/keyv/tree/master/
|
|
329
|
-
DynamoDB | [@keyv/dynamo](https://github.com/jaredwray/keyv/tree/master/
|
|
317
|
+
Redis | [@keyv/redis](https://github.com/jaredwray/keyv/tree/master/storage/redis) | Yes
|
|
318
|
+
Valkey | [@keyv/valkey](https://github.com/jaredwray/keyv/tree/master/storage/valkey) | Yes
|
|
319
|
+
MongoDB | [@keyv/mongo](https://github.com/jaredwray/keyv/tree/master/storage/mongo) | Yes
|
|
320
|
+
SQLite | [@keyv/sqlite](https://github.com/jaredwray/keyv/tree/master/storage/sqlite) | No
|
|
321
|
+
PostgreSQL | [@keyv/postgres](https://github.com/jaredwray/keyv/tree/master/storage/postgres) | No
|
|
322
|
+
MySQL | [@keyv/mysql](https://github.com/jaredwray/keyv/tree/master/storage/mysql) | No
|
|
323
|
+
Etcd | [@keyv/etcd](https://github.com/jaredwray/keyv/tree/master/storage/etcd) | Yes
|
|
324
|
+
Memcache | [@keyv/memcache](https://github.com/jaredwray/keyv/tree/master/storage/memcache) | Yes
|
|
325
|
+
DynamoDB | [@keyv/dynamo](https://github.com/jaredwray/keyv/tree/master/storage/dynamo) | Yes
|
|
330
326
|
|
|
331
327
|
# Third-party Storage Adapters
|
|
332
328
|
|
|
@@ -415,7 +411,7 @@ await keyv.delete('user:1');
|
|
|
415
411
|
await keyv.clear();
|
|
416
412
|
```
|
|
417
413
|
|
|
418
|
-
For more details about BigMap, see the [@keyv/bigmap documentation](https://github.com/jaredwray/keyv/tree/main/
|
|
414
|
+
For more details about BigMap, see the [@keyv/bigmap documentation](https://github.com/jaredwray/keyv/tree/main/storage/bigmap).
|
|
419
415
|
|
|
420
416
|
# Compression
|
|
421
417
|
|
|
@@ -815,6 +811,10 @@ keyv.stats.enabled = true; // Enable stats tracking
|
|
|
815
811
|
keyv.stats.enabled = false; // Disable stats tracking
|
|
816
812
|
```
|
|
817
813
|
|
|
814
|
+
# Bun Support
|
|
815
|
+
|
|
816
|
+
We make a best effort to support [Bun](https://bun.sh/) as a runtime. Our default and primary target is Node.js, but we run tests against Bun to ensure compatibility. If you encounter any issues while using Keyv with Bun, please report them at our [GitHub issues](https://github.com/jaredwray/keyv/issues).
|
|
817
|
+
|
|
818
818
|
# How to Contribute
|
|
819
819
|
|
|
820
820
|
We welcome contributions to Keyv! 🎉 Here are some guides to get you started with contributing:
|
package/dist/index.cjs
CHANGED
|
@@ -239,7 +239,6 @@ var iterableAdapters = [
|
|
|
239
239
|
"etcd"
|
|
240
240
|
];
|
|
241
241
|
var Keyv = class extends event_manager_default {
|
|
242
|
-
opts;
|
|
243
242
|
iterator;
|
|
244
243
|
hooks = new hooks_manager_default();
|
|
245
244
|
stats = new stats_manager_default(false);
|
|
@@ -261,6 +260,7 @@ var Keyv = class extends event_manager_default {
|
|
|
261
260
|
_compression;
|
|
262
261
|
_useKeyPrefix = true;
|
|
263
262
|
_throwOnErrors = false;
|
|
263
|
+
_emitErrors = true;
|
|
264
264
|
/**
|
|
265
265
|
* Keyv Constructor
|
|
266
266
|
* @param {KeyvStoreAdapter | KeyvOptions} store
|
|
@@ -270,29 +270,24 @@ var Keyv = class extends event_manager_default {
|
|
|
270
270
|
super();
|
|
271
271
|
options ??= {};
|
|
272
272
|
store ??= {};
|
|
273
|
-
|
|
273
|
+
const mergedOptions = {
|
|
274
274
|
namespace: "keyv",
|
|
275
275
|
serialize: import_serialize.defaultSerialize,
|
|
276
276
|
deserialize: import_serialize.defaultDeserialize,
|
|
277
277
|
emitErrors: true,
|
|
278
|
-
// @ts-expect-error - Map is not a KeyvStoreAdapter
|
|
279
|
-
store: /* @__PURE__ */ new Map(),
|
|
280
278
|
...options
|
|
281
279
|
};
|
|
282
280
|
if (store && store.get) {
|
|
283
|
-
|
|
281
|
+
mergedOptions.store = store;
|
|
284
282
|
} else {
|
|
285
|
-
|
|
286
|
-
...this.opts,
|
|
287
|
-
...store
|
|
288
|
-
};
|
|
283
|
+
Object.assign(mergedOptions, store);
|
|
289
284
|
}
|
|
290
|
-
this._store =
|
|
291
|
-
this._compression =
|
|
292
|
-
this._serialize =
|
|
293
|
-
this._deserialize =
|
|
294
|
-
if (
|
|
295
|
-
this._namespace =
|
|
285
|
+
this._store = mergedOptions.store ?? /* @__PURE__ */ new Map();
|
|
286
|
+
this._compression = mergedOptions.compression;
|
|
287
|
+
this._serialize = mergedOptions.serialize;
|
|
288
|
+
this._deserialize = mergedOptions.deserialize;
|
|
289
|
+
if (mergedOptions.namespace) {
|
|
290
|
+
this._namespace = mergedOptions.namespace;
|
|
296
291
|
}
|
|
297
292
|
if (this._store) {
|
|
298
293
|
if (!this._isValidStorageAdapter(this._store)) {
|
|
@@ -302,7 +297,10 @@ var Keyv = class extends event_manager_default {
|
|
|
302
297
|
this._store.on("error", (error) => this.emit("error", error));
|
|
303
298
|
}
|
|
304
299
|
this._store.namespace = this._namespace;
|
|
305
|
-
if (
|
|
300
|
+
if (
|
|
301
|
+
// biome-ignore lint/suspicious/noExplicitAny: need to check Map iterator
|
|
302
|
+
typeof this._store[Symbol.iterator] === "function" && this._store instanceof Map
|
|
303
|
+
) {
|
|
306
304
|
this.iterator = this.generateIterator(
|
|
307
305
|
this._store
|
|
308
306
|
);
|
|
@@ -313,17 +311,20 @@ var Keyv = class extends event_manager_default {
|
|
|
313
311
|
);
|
|
314
312
|
}
|
|
315
313
|
}
|
|
316
|
-
if (
|
|
317
|
-
this.stats.enabled =
|
|
314
|
+
if (mergedOptions.stats) {
|
|
315
|
+
this.stats.enabled = mergedOptions.stats;
|
|
316
|
+
}
|
|
317
|
+
if (mergedOptions.ttl) {
|
|
318
|
+
this._ttl = mergedOptions.ttl;
|
|
318
319
|
}
|
|
319
|
-
if (
|
|
320
|
-
this.
|
|
320
|
+
if (mergedOptions.useKeyPrefix !== void 0) {
|
|
321
|
+
this._useKeyPrefix = mergedOptions.useKeyPrefix;
|
|
321
322
|
}
|
|
322
|
-
if (
|
|
323
|
-
this.
|
|
323
|
+
if (mergedOptions.emitErrors !== void 0) {
|
|
324
|
+
this._emitErrors = mergedOptions.emitErrors;
|
|
324
325
|
}
|
|
325
|
-
if (
|
|
326
|
-
this._throwOnErrors =
|
|
326
|
+
if (mergedOptions.throwOnErrors !== void 0) {
|
|
327
|
+
this._throwOnErrors = mergedOptions.throwOnErrors;
|
|
327
328
|
}
|
|
328
329
|
}
|
|
329
330
|
/**
|
|
@@ -341,7 +342,6 @@ var Keyv = class extends event_manager_default {
|
|
|
341
342
|
set store(store) {
|
|
342
343
|
if (this._isValidStorageAdapter(store)) {
|
|
343
344
|
this._store = store;
|
|
344
|
-
this.opts.store = store;
|
|
345
345
|
if (typeof store.on === "function") {
|
|
346
346
|
store.on("error", (error) => this.emit("error", error));
|
|
347
347
|
}
|
|
@@ -386,11 +386,7 @@ var Keyv = class extends event_manager_default {
|
|
|
386
386
|
*/
|
|
387
387
|
set namespace(namespace) {
|
|
388
388
|
this._namespace = namespace;
|
|
389
|
-
this.opts.namespace = namespace;
|
|
390
389
|
this._store.namespace = namespace;
|
|
391
|
-
if (this.opts.store) {
|
|
392
|
-
this.opts.store.namespace = namespace;
|
|
393
|
-
}
|
|
394
390
|
}
|
|
395
391
|
/**
|
|
396
392
|
* Get the current TTL.
|
|
@@ -404,7 +400,6 @@ var Keyv = class extends event_manager_default {
|
|
|
404
400
|
* @param {number} ttl The TTL to set in milliseconds.
|
|
405
401
|
*/
|
|
406
402
|
set ttl(ttl) {
|
|
407
|
-
this.opts.ttl = ttl;
|
|
408
403
|
this._ttl = ttl;
|
|
409
404
|
}
|
|
410
405
|
/**
|
|
@@ -419,7 +414,6 @@ var Keyv = class extends event_manager_default {
|
|
|
419
414
|
* @param {Serialize} serialize The serialize function to set.
|
|
420
415
|
*/
|
|
421
416
|
set serialize(serialize) {
|
|
422
|
-
this.opts.serialize = serialize;
|
|
423
417
|
this._serialize = serialize;
|
|
424
418
|
}
|
|
425
419
|
/**
|
|
@@ -434,7 +428,6 @@ var Keyv = class extends event_manager_default {
|
|
|
434
428
|
* @param {Deserialize} deserialize The deserialize function to set.
|
|
435
429
|
*/
|
|
436
430
|
set deserialize(deserialize) {
|
|
437
|
-
this.opts.deserialize = deserialize;
|
|
438
431
|
this._deserialize = deserialize;
|
|
439
432
|
}
|
|
440
433
|
/**
|
|
@@ -451,7 +444,6 @@ var Keyv = class extends event_manager_default {
|
|
|
451
444
|
*/
|
|
452
445
|
set useKeyPrefix(value) {
|
|
453
446
|
this._useKeyPrefix = value;
|
|
454
|
-
this.opts.useKeyPrefix = value;
|
|
455
447
|
}
|
|
456
448
|
/**
|
|
457
449
|
* Get the current throwErrors value. This will enable or disable throwing errors on methods in addition to emitting them.
|
|
@@ -466,7 +458,21 @@ var Keyv = class extends event_manager_default {
|
|
|
466
458
|
*/
|
|
467
459
|
set throwOnErrors(value) {
|
|
468
460
|
this._throwOnErrors = value;
|
|
469
|
-
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
464
|
+
* @return {boolean} The current emitErrors value.
|
|
465
|
+
* @default true
|
|
466
|
+
*/
|
|
467
|
+
get emitErrors() {
|
|
468
|
+
return this._emitErrors;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
472
|
+
* @param {boolean} value The emitErrors value to set.
|
|
473
|
+
*/
|
|
474
|
+
set emitErrors(value) {
|
|
475
|
+
this._emitErrors = value;
|
|
470
476
|
}
|
|
471
477
|
generateIterator(iterator) {
|
|
472
478
|
const function_ = async function* () {
|
|
@@ -522,7 +528,7 @@ var Keyv = class extends event_manager_default {
|
|
|
522
528
|
}
|
|
523
529
|
// eslint-disable-next-line @stylistic/max-len
|
|
524
530
|
async get(key, options) {
|
|
525
|
-
const
|
|
531
|
+
const store = this._store;
|
|
526
532
|
const isArray = Array.isArray(key);
|
|
527
533
|
const keyPrefixed = isArray ? this._getKeyPrefixArray(key) : this._getKeyPrefix(key);
|
|
528
534
|
const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;
|
|
@@ -541,7 +547,7 @@ var Keyv = class extends event_manager_default {
|
|
|
541
547
|
throw error;
|
|
542
548
|
}
|
|
543
549
|
}
|
|
544
|
-
const deserializedData = typeof rawData === "string" || this.
|
|
550
|
+
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
|
|
545
551
|
if (deserializedData === void 0 || deserializedData === null) {
|
|
546
552
|
this.hooks.trigger("postGet" /* POST_GET */, {
|
|
547
553
|
key: keyPrefixed,
|
|
@@ -567,14 +573,14 @@ var Keyv = class extends event_manager_default {
|
|
|
567
573
|
return options?.raw ? deserializedData : deserializedData.value;
|
|
568
574
|
}
|
|
569
575
|
async getMany(keys, options) {
|
|
570
|
-
const
|
|
576
|
+
const store = this._store;
|
|
571
577
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
572
578
|
const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;
|
|
573
579
|
this.hooks.trigger("preGetMany" /* PRE_GET_MANY */, { keys: keyPrefixed });
|
|
574
580
|
if (store.getMany === void 0) {
|
|
575
581
|
const promises = keyPrefixed.map(async (key) => {
|
|
576
582
|
const rawData2 = await store.get(key);
|
|
577
|
-
const deserializedRow = typeof rawData2 === "string" || this.
|
|
583
|
+
const deserializedRow = typeof rawData2 === "string" || this._compression ? await this.deserializeData(rawData2) : rawData2;
|
|
578
584
|
if (deserializedRow === void 0 || deserializedRow === null) {
|
|
579
585
|
return void 0;
|
|
580
586
|
}
|
|
@@ -630,7 +636,7 @@ var Keyv = class extends event_manager_default {
|
|
|
630
636
|
* @returns {Promise<StoredDataRaw<Value> | undefined>} will return a StoredDataRaw<Value> or undefined if the key does not exist or is expired.
|
|
631
637
|
*/
|
|
632
638
|
async getRaw(key) {
|
|
633
|
-
const
|
|
639
|
+
const store = this._store;
|
|
634
640
|
const keyPrefixed = this._getKeyPrefix(key);
|
|
635
641
|
this.hooks.trigger("preGetRaw" /* PRE_GET_RAW */, { key: keyPrefixed });
|
|
636
642
|
const rawData = await store.get(keyPrefixed);
|
|
@@ -642,7 +648,7 @@ var Keyv = class extends event_manager_default {
|
|
|
642
648
|
this.stats.miss();
|
|
643
649
|
return void 0;
|
|
644
650
|
}
|
|
645
|
-
const deserializedData = typeof rawData === "string" || this.
|
|
651
|
+
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
|
|
646
652
|
if (deserializedData !== void 0 && deserializedData.expires !== void 0 && deserializedData.expires !== null && // biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
647
653
|
deserializedData.expires < Date.now()) {
|
|
648
654
|
this.hooks.trigger("postGetRaw" /* POST_GET_RAW */, {
|
|
@@ -666,7 +672,7 @@ var Keyv = class extends event_manager_default {
|
|
|
666
672
|
* @returns {Promise<Array<StoredDataRaw<Value>>>} will return an array of StoredDataRaw<Value> or undefined if the key does not exist or is expired.
|
|
667
673
|
*/
|
|
668
674
|
async getManyRaw(keys) {
|
|
669
|
-
const
|
|
675
|
+
const store = this._store;
|
|
670
676
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
671
677
|
if (keys.length === 0) {
|
|
672
678
|
const result2 = Array.from({ length: keys.length }).fill(
|
|
@@ -736,7 +742,7 @@ var Keyv = class extends event_manager_default {
|
|
|
736
742
|
if (data.ttl === 0) {
|
|
737
743
|
data.ttl = void 0;
|
|
738
744
|
}
|
|
739
|
-
const
|
|
745
|
+
const store = this._store;
|
|
740
746
|
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : void 0;
|
|
741
747
|
if (typeof data.value === "symbol") {
|
|
742
748
|
this.emit("error", "symbol cannot be serialized");
|
|
@@ -799,7 +805,9 @@ var Keyv = class extends event_manager_default {
|
|
|
799
805
|
return { key: keyPrefixed, value: serializedValue, ttl };
|
|
800
806
|
})
|
|
801
807
|
);
|
|
802
|
-
results = await this._store.setMany(
|
|
808
|
+
results = await this._store.setMany(
|
|
809
|
+
serializedEntries
|
|
810
|
+
);
|
|
803
811
|
}
|
|
804
812
|
} catch (error) {
|
|
805
813
|
this.emit("error", error);
|
|
@@ -816,7 +824,7 @@ var Keyv = class extends event_manager_default {
|
|
|
816
824
|
* @returns {boolean} will return true if item or items are deleted. false if there is an error
|
|
817
825
|
*/
|
|
818
826
|
async delete(key) {
|
|
819
|
-
const
|
|
827
|
+
const store = this._store;
|
|
820
828
|
if (Array.isArray(key)) {
|
|
821
829
|
return this.deleteMany(key);
|
|
822
830
|
}
|
|
@@ -849,7 +857,7 @@ var Keyv = class extends event_manager_default {
|
|
|
849
857
|
*/
|
|
850
858
|
async deleteMany(keys) {
|
|
851
859
|
try {
|
|
852
|
-
const
|
|
860
|
+
const store = this._store;
|
|
853
861
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
854
862
|
this.hooks.trigger("preDelete" /* PRE_DELETE */, { key: keyPrefixed });
|
|
855
863
|
if (store.deleteMany !== void 0) {
|
|
@@ -877,7 +885,7 @@ var Keyv = class extends event_manager_default {
|
|
|
877
885
|
*/
|
|
878
886
|
async clear() {
|
|
879
887
|
this.emit("clear");
|
|
880
|
-
const
|
|
888
|
+
const store = this._store;
|
|
881
889
|
try {
|
|
882
890
|
await store.clear();
|
|
883
891
|
} catch (error) {
|
|
@@ -892,7 +900,7 @@ var Keyv = class extends event_manager_default {
|
|
|
892
900
|
return this.hasMany(key);
|
|
893
901
|
}
|
|
894
902
|
const keyPrefixed = this._getKeyPrefix(key);
|
|
895
|
-
const
|
|
903
|
+
const store = this._store;
|
|
896
904
|
if (store.has !== void 0 && !(store instanceof Map)) {
|
|
897
905
|
return store.has(keyPrefixed);
|
|
898
906
|
}
|
|
@@ -924,7 +932,7 @@ var Keyv = class extends event_manager_default {
|
|
|
924
932
|
*/
|
|
925
933
|
async hasMany(keys) {
|
|
926
934
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
927
|
-
const
|
|
935
|
+
const store = this._store;
|
|
928
936
|
if (store.hasMany !== void 0) {
|
|
929
937
|
return store.hasMany(keyPrefixed);
|
|
930
938
|
}
|
|
@@ -939,7 +947,7 @@ var Keyv = class extends event_manager_default {
|
|
|
939
947
|
* @returns {Promise<void>}
|
|
940
948
|
*/
|
|
941
949
|
async disconnect() {
|
|
942
|
-
const
|
|
950
|
+
const store = this._store;
|
|
943
951
|
this.emit("disconnect");
|
|
944
952
|
if (typeof store.disconnect === "function") {
|
|
945
953
|
return store.disconnect();
|
|
@@ -947,7 +955,7 @@ var Keyv = class extends event_manager_default {
|
|
|
947
955
|
}
|
|
948
956
|
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
949
957
|
emit(event, ...arguments_) {
|
|
950
|
-
if (event === "error" && !this.
|
|
958
|
+
if (event === "error" && !this._emitErrors) {
|
|
951
959
|
return;
|
|
952
960
|
}
|
|
953
961
|
super.emit(event, ...arguments_);
|
package/dist/index.d.cts
CHANGED
|
@@ -158,12 +158,8 @@ type KeyvOptions = {
|
|
|
158
158
|
*/
|
|
159
159
|
throwOnErrors?: boolean;
|
|
160
160
|
};
|
|
161
|
-
type KeyvOptions_ = Omit<KeyvOptions, "store"> & {
|
|
162
|
-
store: KeyvStoreAdapter | (Map<any, any> & KeyvStoreAdapter);
|
|
163
|
-
};
|
|
164
161
|
type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;
|
|
165
162
|
declare class Keyv<GenericValue = any> extends EventManager {
|
|
166
|
-
opts: KeyvOptions_;
|
|
167
163
|
iterator?: IteratorFunction;
|
|
168
164
|
hooks: HooksManager;
|
|
169
165
|
stats: StatsManager;
|
|
@@ -184,6 +180,7 @@ declare class Keyv<GenericValue = any> extends EventManager {
|
|
|
184
180
|
private _compression;
|
|
185
181
|
private _useKeyPrefix;
|
|
186
182
|
private _throwOnErrors;
|
|
183
|
+
private _emitErrors;
|
|
187
184
|
/**
|
|
188
185
|
* Keyv Constructor
|
|
189
186
|
* @param {KeyvStoreAdapter | KeyvOptions | Map<any, any>} store to be provided or just the options
|
|
@@ -275,6 +272,17 @@ declare class Keyv<GenericValue = any> extends EventManager {
|
|
|
275
272
|
* @param {boolean} value The throwOnErrors value to set.
|
|
276
273
|
*/
|
|
277
274
|
set throwOnErrors(value: boolean);
|
|
275
|
+
/**
|
|
276
|
+
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
277
|
+
* @return {boolean} The current emitErrors value.
|
|
278
|
+
* @default true
|
|
279
|
+
*/
|
|
280
|
+
get emitErrors(): boolean;
|
|
281
|
+
/**
|
|
282
|
+
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
283
|
+
* @param {boolean} value The emitErrors value to set.
|
|
284
|
+
*/
|
|
285
|
+
set emitErrors(value: boolean);
|
|
278
286
|
generateIterator(iterator: IteratorFunction): IteratorFunction;
|
|
279
287
|
_checkIterableAdapter(): boolean;
|
|
280
288
|
_getKeyPrefix(key: string): string;
|
package/dist/index.d.ts
CHANGED
|
@@ -158,12 +158,8 @@ type KeyvOptions = {
|
|
|
158
158
|
*/
|
|
159
159
|
throwOnErrors?: boolean;
|
|
160
160
|
};
|
|
161
|
-
type KeyvOptions_ = Omit<KeyvOptions, "store"> & {
|
|
162
|
-
store: KeyvStoreAdapter | (Map<any, any> & KeyvStoreAdapter);
|
|
163
|
-
};
|
|
164
161
|
type IteratorFunction = (argument: any) => AsyncGenerator<any, void>;
|
|
165
162
|
declare class Keyv<GenericValue = any> extends EventManager {
|
|
166
|
-
opts: KeyvOptions_;
|
|
167
163
|
iterator?: IteratorFunction;
|
|
168
164
|
hooks: HooksManager;
|
|
169
165
|
stats: StatsManager;
|
|
@@ -184,6 +180,7 @@ declare class Keyv<GenericValue = any> extends EventManager {
|
|
|
184
180
|
private _compression;
|
|
185
181
|
private _useKeyPrefix;
|
|
186
182
|
private _throwOnErrors;
|
|
183
|
+
private _emitErrors;
|
|
187
184
|
/**
|
|
188
185
|
* Keyv Constructor
|
|
189
186
|
* @param {KeyvStoreAdapter | KeyvOptions | Map<any, any>} store to be provided or just the options
|
|
@@ -275,6 +272,17 @@ declare class Keyv<GenericValue = any> extends EventManager {
|
|
|
275
272
|
* @param {boolean} value The throwOnErrors value to set.
|
|
276
273
|
*/
|
|
277
274
|
set throwOnErrors(value: boolean);
|
|
275
|
+
/**
|
|
276
|
+
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
277
|
+
* @return {boolean} The current emitErrors value.
|
|
278
|
+
* @default true
|
|
279
|
+
*/
|
|
280
|
+
get emitErrors(): boolean;
|
|
281
|
+
/**
|
|
282
|
+
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
283
|
+
* @param {boolean} value The emitErrors value to set.
|
|
284
|
+
*/
|
|
285
|
+
set emitErrors(value: boolean);
|
|
278
286
|
generateIterator(iterator: IteratorFunction): IteratorFunction;
|
|
279
287
|
_checkIterableAdapter(): boolean;
|
|
280
288
|
_getKeyPrefix(key: string): string;
|
package/dist/index.js
CHANGED
|
@@ -213,7 +213,6 @@ var iterableAdapters = [
|
|
|
213
213
|
"etcd"
|
|
214
214
|
];
|
|
215
215
|
var Keyv = class extends event_manager_default {
|
|
216
|
-
opts;
|
|
217
216
|
iterator;
|
|
218
217
|
hooks = new hooks_manager_default();
|
|
219
218
|
stats = new stats_manager_default(false);
|
|
@@ -235,6 +234,7 @@ var Keyv = class extends event_manager_default {
|
|
|
235
234
|
_compression;
|
|
236
235
|
_useKeyPrefix = true;
|
|
237
236
|
_throwOnErrors = false;
|
|
237
|
+
_emitErrors = true;
|
|
238
238
|
/**
|
|
239
239
|
* Keyv Constructor
|
|
240
240
|
* @param {KeyvStoreAdapter | KeyvOptions} store
|
|
@@ -244,29 +244,24 @@ var Keyv = class extends event_manager_default {
|
|
|
244
244
|
super();
|
|
245
245
|
options ??= {};
|
|
246
246
|
store ??= {};
|
|
247
|
-
|
|
247
|
+
const mergedOptions = {
|
|
248
248
|
namespace: "keyv",
|
|
249
249
|
serialize: defaultSerialize,
|
|
250
250
|
deserialize: defaultDeserialize,
|
|
251
251
|
emitErrors: true,
|
|
252
|
-
// @ts-expect-error - Map is not a KeyvStoreAdapter
|
|
253
|
-
store: /* @__PURE__ */ new Map(),
|
|
254
252
|
...options
|
|
255
253
|
};
|
|
256
254
|
if (store && store.get) {
|
|
257
|
-
|
|
255
|
+
mergedOptions.store = store;
|
|
258
256
|
} else {
|
|
259
|
-
|
|
260
|
-
...this.opts,
|
|
261
|
-
...store
|
|
262
|
-
};
|
|
257
|
+
Object.assign(mergedOptions, store);
|
|
263
258
|
}
|
|
264
|
-
this._store =
|
|
265
|
-
this._compression =
|
|
266
|
-
this._serialize =
|
|
267
|
-
this._deserialize =
|
|
268
|
-
if (
|
|
269
|
-
this._namespace =
|
|
259
|
+
this._store = mergedOptions.store ?? /* @__PURE__ */ new Map();
|
|
260
|
+
this._compression = mergedOptions.compression;
|
|
261
|
+
this._serialize = mergedOptions.serialize;
|
|
262
|
+
this._deserialize = mergedOptions.deserialize;
|
|
263
|
+
if (mergedOptions.namespace) {
|
|
264
|
+
this._namespace = mergedOptions.namespace;
|
|
270
265
|
}
|
|
271
266
|
if (this._store) {
|
|
272
267
|
if (!this._isValidStorageAdapter(this._store)) {
|
|
@@ -276,7 +271,10 @@ var Keyv = class extends event_manager_default {
|
|
|
276
271
|
this._store.on("error", (error) => this.emit("error", error));
|
|
277
272
|
}
|
|
278
273
|
this._store.namespace = this._namespace;
|
|
279
|
-
if (
|
|
274
|
+
if (
|
|
275
|
+
// biome-ignore lint/suspicious/noExplicitAny: need to check Map iterator
|
|
276
|
+
typeof this._store[Symbol.iterator] === "function" && this._store instanceof Map
|
|
277
|
+
) {
|
|
280
278
|
this.iterator = this.generateIterator(
|
|
281
279
|
this._store
|
|
282
280
|
);
|
|
@@ -287,17 +285,20 @@ var Keyv = class extends event_manager_default {
|
|
|
287
285
|
);
|
|
288
286
|
}
|
|
289
287
|
}
|
|
290
|
-
if (
|
|
291
|
-
this.stats.enabled =
|
|
288
|
+
if (mergedOptions.stats) {
|
|
289
|
+
this.stats.enabled = mergedOptions.stats;
|
|
290
|
+
}
|
|
291
|
+
if (mergedOptions.ttl) {
|
|
292
|
+
this._ttl = mergedOptions.ttl;
|
|
292
293
|
}
|
|
293
|
-
if (
|
|
294
|
-
this.
|
|
294
|
+
if (mergedOptions.useKeyPrefix !== void 0) {
|
|
295
|
+
this._useKeyPrefix = mergedOptions.useKeyPrefix;
|
|
295
296
|
}
|
|
296
|
-
if (
|
|
297
|
-
this.
|
|
297
|
+
if (mergedOptions.emitErrors !== void 0) {
|
|
298
|
+
this._emitErrors = mergedOptions.emitErrors;
|
|
298
299
|
}
|
|
299
|
-
if (
|
|
300
|
-
this._throwOnErrors =
|
|
300
|
+
if (mergedOptions.throwOnErrors !== void 0) {
|
|
301
|
+
this._throwOnErrors = mergedOptions.throwOnErrors;
|
|
301
302
|
}
|
|
302
303
|
}
|
|
303
304
|
/**
|
|
@@ -315,7 +316,6 @@ var Keyv = class extends event_manager_default {
|
|
|
315
316
|
set store(store) {
|
|
316
317
|
if (this._isValidStorageAdapter(store)) {
|
|
317
318
|
this._store = store;
|
|
318
|
-
this.opts.store = store;
|
|
319
319
|
if (typeof store.on === "function") {
|
|
320
320
|
store.on("error", (error) => this.emit("error", error));
|
|
321
321
|
}
|
|
@@ -360,11 +360,7 @@ var Keyv = class extends event_manager_default {
|
|
|
360
360
|
*/
|
|
361
361
|
set namespace(namespace) {
|
|
362
362
|
this._namespace = namespace;
|
|
363
|
-
this.opts.namespace = namespace;
|
|
364
363
|
this._store.namespace = namespace;
|
|
365
|
-
if (this.opts.store) {
|
|
366
|
-
this.opts.store.namespace = namespace;
|
|
367
|
-
}
|
|
368
364
|
}
|
|
369
365
|
/**
|
|
370
366
|
* Get the current TTL.
|
|
@@ -378,7 +374,6 @@ var Keyv = class extends event_manager_default {
|
|
|
378
374
|
* @param {number} ttl The TTL to set in milliseconds.
|
|
379
375
|
*/
|
|
380
376
|
set ttl(ttl) {
|
|
381
|
-
this.opts.ttl = ttl;
|
|
382
377
|
this._ttl = ttl;
|
|
383
378
|
}
|
|
384
379
|
/**
|
|
@@ -393,7 +388,6 @@ var Keyv = class extends event_manager_default {
|
|
|
393
388
|
* @param {Serialize} serialize The serialize function to set.
|
|
394
389
|
*/
|
|
395
390
|
set serialize(serialize) {
|
|
396
|
-
this.opts.serialize = serialize;
|
|
397
391
|
this._serialize = serialize;
|
|
398
392
|
}
|
|
399
393
|
/**
|
|
@@ -408,7 +402,6 @@ var Keyv = class extends event_manager_default {
|
|
|
408
402
|
* @param {Deserialize} deserialize The deserialize function to set.
|
|
409
403
|
*/
|
|
410
404
|
set deserialize(deserialize) {
|
|
411
|
-
this.opts.deserialize = deserialize;
|
|
412
405
|
this._deserialize = deserialize;
|
|
413
406
|
}
|
|
414
407
|
/**
|
|
@@ -425,7 +418,6 @@ var Keyv = class extends event_manager_default {
|
|
|
425
418
|
*/
|
|
426
419
|
set useKeyPrefix(value) {
|
|
427
420
|
this._useKeyPrefix = value;
|
|
428
|
-
this.opts.useKeyPrefix = value;
|
|
429
421
|
}
|
|
430
422
|
/**
|
|
431
423
|
* Get the current throwErrors value. This will enable or disable throwing errors on methods in addition to emitting them.
|
|
@@ -440,7 +432,21 @@ var Keyv = class extends event_manager_default {
|
|
|
440
432
|
*/
|
|
441
433
|
set throwOnErrors(value) {
|
|
442
434
|
this._throwOnErrors = value;
|
|
443
|
-
|
|
435
|
+
}
|
|
436
|
+
/**
|
|
437
|
+
* Get the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
438
|
+
* @return {boolean} The current emitErrors value.
|
|
439
|
+
* @default true
|
|
440
|
+
*/
|
|
441
|
+
get emitErrors() {
|
|
442
|
+
return this._emitErrors;
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Set the current emitErrors value. This will enable or disable emitting errors on methods.
|
|
446
|
+
* @param {boolean} value The emitErrors value to set.
|
|
447
|
+
*/
|
|
448
|
+
set emitErrors(value) {
|
|
449
|
+
this._emitErrors = value;
|
|
444
450
|
}
|
|
445
451
|
generateIterator(iterator) {
|
|
446
452
|
const function_ = async function* () {
|
|
@@ -496,7 +502,7 @@ var Keyv = class extends event_manager_default {
|
|
|
496
502
|
}
|
|
497
503
|
// eslint-disable-next-line @stylistic/max-len
|
|
498
504
|
async get(key, options) {
|
|
499
|
-
const
|
|
505
|
+
const store = this._store;
|
|
500
506
|
const isArray = Array.isArray(key);
|
|
501
507
|
const keyPrefixed = isArray ? this._getKeyPrefixArray(key) : this._getKeyPrefix(key);
|
|
502
508
|
const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;
|
|
@@ -515,7 +521,7 @@ var Keyv = class extends event_manager_default {
|
|
|
515
521
|
throw error;
|
|
516
522
|
}
|
|
517
523
|
}
|
|
518
|
-
const deserializedData = typeof rawData === "string" || this.
|
|
524
|
+
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
|
|
519
525
|
if (deserializedData === void 0 || deserializedData === null) {
|
|
520
526
|
this.hooks.trigger("postGet" /* POST_GET */, {
|
|
521
527
|
key: keyPrefixed,
|
|
@@ -541,14 +547,14 @@ var Keyv = class extends event_manager_default {
|
|
|
541
547
|
return options?.raw ? deserializedData : deserializedData.value;
|
|
542
548
|
}
|
|
543
549
|
async getMany(keys, options) {
|
|
544
|
-
const
|
|
550
|
+
const store = this._store;
|
|
545
551
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
546
552
|
const isDataExpired = (data) => typeof data.expires === "number" && Date.now() > data.expires;
|
|
547
553
|
this.hooks.trigger("preGetMany" /* PRE_GET_MANY */, { keys: keyPrefixed });
|
|
548
554
|
if (store.getMany === void 0) {
|
|
549
555
|
const promises = keyPrefixed.map(async (key) => {
|
|
550
556
|
const rawData2 = await store.get(key);
|
|
551
|
-
const deserializedRow = typeof rawData2 === "string" || this.
|
|
557
|
+
const deserializedRow = typeof rawData2 === "string" || this._compression ? await this.deserializeData(rawData2) : rawData2;
|
|
552
558
|
if (deserializedRow === void 0 || deserializedRow === null) {
|
|
553
559
|
return void 0;
|
|
554
560
|
}
|
|
@@ -604,7 +610,7 @@ var Keyv = class extends event_manager_default {
|
|
|
604
610
|
* @returns {Promise<StoredDataRaw<Value> | undefined>} will return a StoredDataRaw<Value> or undefined if the key does not exist or is expired.
|
|
605
611
|
*/
|
|
606
612
|
async getRaw(key) {
|
|
607
|
-
const
|
|
613
|
+
const store = this._store;
|
|
608
614
|
const keyPrefixed = this._getKeyPrefix(key);
|
|
609
615
|
this.hooks.trigger("preGetRaw" /* PRE_GET_RAW */, { key: keyPrefixed });
|
|
610
616
|
const rawData = await store.get(keyPrefixed);
|
|
@@ -616,7 +622,7 @@ var Keyv = class extends event_manager_default {
|
|
|
616
622
|
this.stats.miss();
|
|
617
623
|
return void 0;
|
|
618
624
|
}
|
|
619
|
-
const deserializedData = typeof rawData === "string" || this.
|
|
625
|
+
const deserializedData = typeof rawData === "string" || this._compression ? await this.deserializeData(rawData) : rawData;
|
|
620
626
|
if (deserializedData !== void 0 && deserializedData.expires !== void 0 && deserializedData.expires !== null && // biome-ignore lint/style/noNonNullAssertion: need to fix
|
|
621
627
|
deserializedData.expires < Date.now()) {
|
|
622
628
|
this.hooks.trigger("postGetRaw" /* POST_GET_RAW */, {
|
|
@@ -640,7 +646,7 @@ var Keyv = class extends event_manager_default {
|
|
|
640
646
|
* @returns {Promise<Array<StoredDataRaw<Value>>>} will return an array of StoredDataRaw<Value> or undefined if the key does not exist or is expired.
|
|
641
647
|
*/
|
|
642
648
|
async getManyRaw(keys) {
|
|
643
|
-
const
|
|
649
|
+
const store = this._store;
|
|
644
650
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
645
651
|
if (keys.length === 0) {
|
|
646
652
|
const result2 = Array.from({ length: keys.length }).fill(
|
|
@@ -710,7 +716,7 @@ var Keyv = class extends event_manager_default {
|
|
|
710
716
|
if (data.ttl === 0) {
|
|
711
717
|
data.ttl = void 0;
|
|
712
718
|
}
|
|
713
|
-
const
|
|
719
|
+
const store = this._store;
|
|
714
720
|
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : void 0;
|
|
715
721
|
if (typeof data.value === "symbol") {
|
|
716
722
|
this.emit("error", "symbol cannot be serialized");
|
|
@@ -773,7 +779,9 @@ var Keyv = class extends event_manager_default {
|
|
|
773
779
|
return { key: keyPrefixed, value: serializedValue, ttl };
|
|
774
780
|
})
|
|
775
781
|
);
|
|
776
|
-
results = await this._store.setMany(
|
|
782
|
+
results = await this._store.setMany(
|
|
783
|
+
serializedEntries
|
|
784
|
+
);
|
|
777
785
|
}
|
|
778
786
|
} catch (error) {
|
|
779
787
|
this.emit("error", error);
|
|
@@ -790,7 +798,7 @@ var Keyv = class extends event_manager_default {
|
|
|
790
798
|
* @returns {boolean} will return true if item or items are deleted. false if there is an error
|
|
791
799
|
*/
|
|
792
800
|
async delete(key) {
|
|
793
|
-
const
|
|
801
|
+
const store = this._store;
|
|
794
802
|
if (Array.isArray(key)) {
|
|
795
803
|
return this.deleteMany(key);
|
|
796
804
|
}
|
|
@@ -823,7 +831,7 @@ var Keyv = class extends event_manager_default {
|
|
|
823
831
|
*/
|
|
824
832
|
async deleteMany(keys) {
|
|
825
833
|
try {
|
|
826
|
-
const
|
|
834
|
+
const store = this._store;
|
|
827
835
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
828
836
|
this.hooks.trigger("preDelete" /* PRE_DELETE */, { key: keyPrefixed });
|
|
829
837
|
if (store.deleteMany !== void 0) {
|
|
@@ -851,7 +859,7 @@ var Keyv = class extends event_manager_default {
|
|
|
851
859
|
*/
|
|
852
860
|
async clear() {
|
|
853
861
|
this.emit("clear");
|
|
854
|
-
const
|
|
862
|
+
const store = this._store;
|
|
855
863
|
try {
|
|
856
864
|
await store.clear();
|
|
857
865
|
} catch (error) {
|
|
@@ -866,7 +874,7 @@ var Keyv = class extends event_manager_default {
|
|
|
866
874
|
return this.hasMany(key);
|
|
867
875
|
}
|
|
868
876
|
const keyPrefixed = this._getKeyPrefix(key);
|
|
869
|
-
const
|
|
877
|
+
const store = this._store;
|
|
870
878
|
if (store.has !== void 0 && !(store instanceof Map)) {
|
|
871
879
|
return store.has(keyPrefixed);
|
|
872
880
|
}
|
|
@@ -898,7 +906,7 @@ var Keyv = class extends event_manager_default {
|
|
|
898
906
|
*/
|
|
899
907
|
async hasMany(keys) {
|
|
900
908
|
const keyPrefixed = this._getKeyPrefixArray(keys);
|
|
901
|
-
const
|
|
909
|
+
const store = this._store;
|
|
902
910
|
if (store.hasMany !== void 0) {
|
|
903
911
|
return store.hasMany(keyPrefixed);
|
|
904
912
|
}
|
|
@@ -913,7 +921,7 @@ var Keyv = class extends event_manager_default {
|
|
|
913
921
|
* @returns {Promise<void>}
|
|
914
922
|
*/
|
|
915
923
|
async disconnect() {
|
|
916
|
-
const
|
|
924
|
+
const store = this._store;
|
|
917
925
|
this.emit("disconnect");
|
|
918
926
|
if (typeof store.disconnect === "function") {
|
|
919
927
|
return store.disconnect();
|
|
@@ -921,7 +929,7 @@ var Keyv = class extends event_manager_default {
|
|
|
921
929
|
}
|
|
922
930
|
// biome-ignore lint/suspicious/noExplicitAny: type format
|
|
923
931
|
emit(event, ...arguments_) {
|
|
924
|
-
if (event === "error" && !this.
|
|
932
|
+
if (event === "error" && !this._emitErrors) {
|
|
925
933
|
return;
|
|
926
934
|
}
|
|
927
935
|
super.emit(event, ...arguments_);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keyv",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0-alpha.2",
|
|
4
4
|
"description": "Simple key-value storage with support for multiple backends",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -50,25 +50,18 @@
|
|
|
50
50
|
},
|
|
51
51
|
"homepage": "https://github.com/jaredwray/keyv",
|
|
52
52
|
"dependencies": {
|
|
53
|
-
"@keyv/serialize": "^
|
|
53
|
+
"@keyv/serialize": "^6.0.0-alpha.2"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
+
"@biomejs/biome": "^2.3.13",
|
|
57
|
+
"@faker-js/faker": "^10.2.0",
|
|
58
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
56
59
|
"lru.min": "^1.1.1",
|
|
57
60
|
"quick-lru": "^7.0.0",
|
|
58
|
-
"@biomejs/biome": "^2.3.8",
|
|
59
|
-
"@faker-js/faker": "^10.1.0",
|
|
60
|
-
"@vitest/coverage-v8": "^4.0.14",
|
|
61
61
|
"rimraf": "^6.1.2",
|
|
62
62
|
"timekeeper": "^2.3.1",
|
|
63
63
|
"tsd": "^0.33.0",
|
|
64
|
-
"vitest": "^4.0.
|
|
65
|
-
"@keyv/compress-gzip": "^2.0.3",
|
|
66
|
-
"@keyv/sqlite": "^4.0.6",
|
|
67
|
-
"@keyv/test-suite": "^2.1.2",
|
|
68
|
-
"@keyv/mongo": "^3.1.0",
|
|
69
|
-
"@keyv/compress-brotli": "^2.0.5",
|
|
70
|
-
"@keyv/compress-lz4": "^1.0.1",
|
|
71
|
-
"@keyv/memcache": "^2.0.2"
|
|
64
|
+
"vitest": "^4.0.18"
|
|
72
65
|
},
|
|
73
66
|
"tsd": {
|
|
74
67
|
"directory": "test"
|