keyv 5.3.2 → 5.3.4
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 +45 -4
- package/dist/index.cjs +27 -32
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +27 -32
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -24,6 +24,46 @@ There are a few existing modules similar to Keyv, however Keyv is different beca
|
|
|
24
24
|
- Connection errors are passed through (db failures won't kill your app)
|
|
25
25
|
- Supports the current active LTS version of Node.js or higher
|
|
26
26
|
|
|
27
|
+
# Table of Contents
|
|
28
|
+
- [Usage](#usage)
|
|
29
|
+
- [Type-safe Usage](#type-safe-usage)
|
|
30
|
+
- [Using Storage Adapters](#using-storage-adapters)
|
|
31
|
+
- [Namespaces](#namespaces)
|
|
32
|
+
- [Events](#events)
|
|
33
|
+
- [Hooks](#hooks)
|
|
34
|
+
- [Custom Serializers](#custom-serializers)
|
|
35
|
+
- [Official Storage Adapters](#official-storage-adapters)
|
|
36
|
+
- [Third-party Storage Adapters](#third-party-storage-adapters)
|
|
37
|
+
- [Compression](#compression)
|
|
38
|
+
- [API](#api)
|
|
39
|
+
- [new Keyv([storage-adapter], [options]) or new Keyv([options])](#new-keyvstorage-adapter-options-or-new-keyvoptions)
|
|
40
|
+
- [.namespace](#namespace)
|
|
41
|
+
- [.ttl](#ttl)
|
|
42
|
+
- [.store](#store)
|
|
43
|
+
- [.serialize](#serialize)
|
|
44
|
+
- [.deserialize](#deserialize)
|
|
45
|
+
- [.compression](#compression)
|
|
46
|
+
- [.useKeyPrefix](#usekeyprefix)
|
|
47
|
+
- [Keyv Instance](#keyv-instance)
|
|
48
|
+
- [.set(key, value, [ttl])](#setkey-value-ttl)
|
|
49
|
+
- [.setMany(entries)](#setmanyentries)
|
|
50
|
+
- [.get(key, [options])](#getkey-options)
|
|
51
|
+
- [.getMany(keys, [options])](#getmanykeys-options)
|
|
52
|
+
- [.delete(key)](#deletekey)
|
|
53
|
+
- [.deleteMany(keys)](#deletemanykeys)
|
|
54
|
+
- [.clear()](#clear)
|
|
55
|
+
- [.iterator()](#iterator)
|
|
56
|
+
- [API - Properties](#api---properties)
|
|
57
|
+
- [.namespace](#namespace-1)
|
|
58
|
+
- [.ttl](#ttl-1)
|
|
59
|
+
- [.store](#store-1)
|
|
60
|
+
- [.serialize](#serialize-1)
|
|
61
|
+
- [.deserialize](#deserialize-1)
|
|
62
|
+
- [.compression](#compression-1)
|
|
63
|
+
- [.useKeyPrefix](#usekeyprefix-1)
|
|
64
|
+
- [How to Contribute](#how-to-contribute)
|
|
65
|
+
- [License](#license)
|
|
66
|
+
|
|
27
67
|
# Usage
|
|
28
68
|
|
|
29
69
|
Install Keyv.
|
|
@@ -189,7 +229,7 @@ import Keyv, { KeyvHooks } from 'keyv';
|
|
|
189
229
|
```js
|
|
190
230
|
//PRE_SET hook
|
|
191
231
|
const keyv = new Keyv();
|
|
192
|
-
keyv.hooks.addHandler(KeyvHooks.PRE_SET, (
|
|
232
|
+
keyv.hooks.addHandler(KeyvHooks.PRE_SET, (data) => console.log(`Setting key ${data.key} to ${data.value}`));
|
|
193
233
|
|
|
194
234
|
//POST_SET hook
|
|
195
235
|
const keyv = new Keyv();
|
|
@@ -200,9 +240,10 @@ In these examples you can also manipulate the value before it is set. For exampl
|
|
|
200
240
|
|
|
201
241
|
```js
|
|
202
242
|
const keyv = new Keyv();
|
|
203
|
-
keyv.hooks.addHandler(KeyvHooks.PRE_SET, (
|
|
204
|
-
console.log(`
|
|
205
|
-
key = `prefix-${key}`;
|
|
243
|
+
keyv.hooks.addHandler(KeyvHooks.PRE_SET, (data) => {
|
|
244
|
+
console.log(`Manipulating key ${data.key} and ${data.value}`);
|
|
245
|
+
data.key = `prefix-${data.key}`;
|
|
246
|
+
data.value = `prefix-${data.value}`;
|
|
206
247
|
});
|
|
207
248
|
```
|
|
208
249
|
|
package/dist/index.cjs
CHANGED
|
@@ -83,14 +83,6 @@ var EventManager = class {
|
|
|
83
83
|
for (const listener of listeners) {
|
|
84
84
|
listener(...arguments_);
|
|
85
85
|
}
|
|
86
|
-
} else if (event === "error") {
|
|
87
|
-
if (arguments_[0] instanceof Error) {
|
|
88
|
-
throw arguments_[0];
|
|
89
|
-
} else {
|
|
90
|
-
const error = new CustomError(arguments_[0]);
|
|
91
|
-
error.context = arguments_[0];
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
86
|
}
|
|
95
87
|
}
|
|
96
88
|
// Get all listeners for a specific event
|
|
@@ -110,17 +102,6 @@ var EventManager = class {
|
|
|
110
102
|
this._maxListeners = n;
|
|
111
103
|
}
|
|
112
104
|
};
|
|
113
|
-
var CustomError = class _CustomError extends Error {
|
|
114
|
-
context;
|
|
115
|
-
constructor(message, context) {
|
|
116
|
-
super(message);
|
|
117
|
-
this.context = context;
|
|
118
|
-
if (Error.captureStackTrace) {
|
|
119
|
-
Error.captureStackTrace(this, _CustomError);
|
|
120
|
-
}
|
|
121
|
-
this.name = this.constructor.name;
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
105
|
var event_manager_default = EventManager;
|
|
125
106
|
|
|
126
107
|
// src/hooks-manager.ts
|
|
@@ -482,6 +463,7 @@ var Keyv = class extends event_manager_default {
|
|
|
482
463
|
_isValidStorageAdapter(store) {
|
|
483
464
|
return store instanceof Map || typeof store.get === "function" && typeof store.set === "function" && typeof store.delete === "function" && typeof store.clear === "function";
|
|
484
465
|
}
|
|
466
|
+
// eslint-disable-next-line @stylistic/max-len
|
|
485
467
|
async get(key, options) {
|
|
486
468
|
const { store } = this.opts;
|
|
487
469
|
const isArray = Array.isArray(key);
|
|
@@ -568,24 +550,24 @@ var Keyv = class extends event_manager_default {
|
|
|
568
550
|
* @returns {boolean} if it sets then it will return a true. On failure will return false.
|
|
569
551
|
*/
|
|
570
552
|
async set(key, value, ttl) {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
ttl = void 0;
|
|
553
|
+
const data = { key, value, ttl };
|
|
554
|
+
this.hooks.trigger("preSet" /* PRE_SET */, data);
|
|
555
|
+
const keyPrefixed = this._getKeyPrefix(data.key);
|
|
556
|
+
data.ttl ??= this._ttl;
|
|
557
|
+
if (data.ttl === 0) {
|
|
558
|
+
data.ttl = void 0;
|
|
578
559
|
}
|
|
579
560
|
const { store } = this.opts;
|
|
580
|
-
const expires = typeof ttl === "number" ? Date.now() + ttl :
|
|
581
|
-
if (typeof value === "symbol") {
|
|
561
|
+
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : void 0;
|
|
562
|
+
if (typeof data.value === "symbol") {
|
|
582
563
|
this.emit("error", "symbol cannot be serialized");
|
|
564
|
+
throw new Error("symbol cannot be serialized");
|
|
583
565
|
}
|
|
584
|
-
const formattedValue = { value, expires };
|
|
566
|
+
const formattedValue = { value: data.value, expires };
|
|
585
567
|
const serializedValue = await this.serializeData(formattedValue);
|
|
586
568
|
let result = true;
|
|
587
569
|
try {
|
|
588
|
-
const value2 = await store.set(keyPrefixed, serializedValue, ttl);
|
|
570
|
+
const value2 = await store.set(keyPrefixed, serializedValue, data.ttl);
|
|
589
571
|
if (typeof value2 === "boolean") {
|
|
590
572
|
result = value2;
|
|
591
573
|
}
|
|
@@ -606,8 +588,21 @@ var Keyv = class extends event_manager_default {
|
|
|
606
588
|
let results = [];
|
|
607
589
|
try {
|
|
608
590
|
if (this._store.setMany !== void 0) {
|
|
609
|
-
|
|
610
|
-
|
|
591
|
+
const serializedEntries = await Promise.all(entries.map(async ({ key, value, ttl }) => {
|
|
592
|
+
ttl ??= this._ttl;
|
|
593
|
+
if (ttl === 0) {
|
|
594
|
+
ttl = void 0;
|
|
595
|
+
}
|
|
596
|
+
const expires = typeof ttl === "number" ? Date.now() + ttl : void 0;
|
|
597
|
+
if (typeof value === "symbol") {
|
|
598
|
+
this.emit("error", "symbol cannot be serialized");
|
|
599
|
+
throw new Error("symbol cannot be serialized");
|
|
600
|
+
}
|
|
601
|
+
const formattedValue = { value, expires };
|
|
602
|
+
const serializedValue = await this.serializeData(formattedValue);
|
|
603
|
+
return { key, value: serializedValue, ttl };
|
|
604
|
+
}));
|
|
605
|
+
results = await this._store.setMany(serializedEntries);
|
|
611
606
|
}
|
|
612
607
|
const promises = [];
|
|
613
608
|
for (const entry of entries) {
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -57,14 +57,6 @@ var EventManager = class {
|
|
|
57
57
|
for (const listener of listeners) {
|
|
58
58
|
listener(...arguments_);
|
|
59
59
|
}
|
|
60
|
-
} else if (event === "error") {
|
|
61
|
-
if (arguments_[0] instanceof Error) {
|
|
62
|
-
throw arguments_[0];
|
|
63
|
-
} else {
|
|
64
|
-
const error = new CustomError(arguments_[0]);
|
|
65
|
-
error.context = arguments_[0];
|
|
66
|
-
throw error;
|
|
67
|
-
}
|
|
68
60
|
}
|
|
69
61
|
}
|
|
70
62
|
// Get all listeners for a specific event
|
|
@@ -84,17 +76,6 @@ var EventManager = class {
|
|
|
84
76
|
this._maxListeners = n;
|
|
85
77
|
}
|
|
86
78
|
};
|
|
87
|
-
var CustomError = class _CustomError extends Error {
|
|
88
|
-
context;
|
|
89
|
-
constructor(message, context) {
|
|
90
|
-
super(message);
|
|
91
|
-
this.context = context;
|
|
92
|
-
if (Error.captureStackTrace) {
|
|
93
|
-
Error.captureStackTrace(this, _CustomError);
|
|
94
|
-
}
|
|
95
|
-
this.name = this.constructor.name;
|
|
96
|
-
}
|
|
97
|
-
};
|
|
98
79
|
var event_manager_default = EventManager;
|
|
99
80
|
|
|
100
81
|
// src/hooks-manager.ts
|
|
@@ -456,6 +437,7 @@ var Keyv = class extends event_manager_default {
|
|
|
456
437
|
_isValidStorageAdapter(store) {
|
|
457
438
|
return store instanceof Map || typeof store.get === "function" && typeof store.set === "function" && typeof store.delete === "function" && typeof store.clear === "function";
|
|
458
439
|
}
|
|
440
|
+
// eslint-disable-next-line @stylistic/max-len
|
|
459
441
|
async get(key, options) {
|
|
460
442
|
const { store } = this.opts;
|
|
461
443
|
const isArray = Array.isArray(key);
|
|
@@ -542,24 +524,24 @@ var Keyv = class extends event_manager_default {
|
|
|
542
524
|
* @returns {boolean} if it sets then it will return a true. On failure will return false.
|
|
543
525
|
*/
|
|
544
526
|
async set(key, value, ttl) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
ttl = void 0;
|
|
527
|
+
const data = { key, value, ttl };
|
|
528
|
+
this.hooks.trigger("preSet" /* PRE_SET */, data);
|
|
529
|
+
const keyPrefixed = this._getKeyPrefix(data.key);
|
|
530
|
+
data.ttl ??= this._ttl;
|
|
531
|
+
if (data.ttl === 0) {
|
|
532
|
+
data.ttl = void 0;
|
|
552
533
|
}
|
|
553
534
|
const { store } = this.opts;
|
|
554
|
-
const expires = typeof ttl === "number" ? Date.now() + ttl :
|
|
555
|
-
if (typeof value === "symbol") {
|
|
535
|
+
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : void 0;
|
|
536
|
+
if (typeof data.value === "symbol") {
|
|
556
537
|
this.emit("error", "symbol cannot be serialized");
|
|
538
|
+
throw new Error("symbol cannot be serialized");
|
|
557
539
|
}
|
|
558
|
-
const formattedValue = { value, expires };
|
|
540
|
+
const formattedValue = { value: data.value, expires };
|
|
559
541
|
const serializedValue = await this.serializeData(formattedValue);
|
|
560
542
|
let result = true;
|
|
561
543
|
try {
|
|
562
|
-
const value2 = await store.set(keyPrefixed, serializedValue, ttl);
|
|
544
|
+
const value2 = await store.set(keyPrefixed, serializedValue, data.ttl);
|
|
563
545
|
if (typeof value2 === "boolean") {
|
|
564
546
|
result = value2;
|
|
565
547
|
}
|
|
@@ -580,8 +562,21 @@ var Keyv = class extends event_manager_default {
|
|
|
580
562
|
let results = [];
|
|
581
563
|
try {
|
|
582
564
|
if (this._store.setMany !== void 0) {
|
|
583
|
-
|
|
584
|
-
|
|
565
|
+
const serializedEntries = await Promise.all(entries.map(async ({ key, value, ttl }) => {
|
|
566
|
+
ttl ??= this._ttl;
|
|
567
|
+
if (ttl === 0) {
|
|
568
|
+
ttl = void 0;
|
|
569
|
+
}
|
|
570
|
+
const expires = typeof ttl === "number" ? Date.now() + ttl : void 0;
|
|
571
|
+
if (typeof value === "symbol") {
|
|
572
|
+
this.emit("error", "symbol cannot be serialized");
|
|
573
|
+
throw new Error("symbol cannot be serialized");
|
|
574
|
+
}
|
|
575
|
+
const formattedValue = { value, expires };
|
|
576
|
+
const serializedValue = await this.serializeData(formattedValue);
|
|
577
|
+
return { key, value: serializedValue, ttl };
|
|
578
|
+
}));
|
|
579
|
+
results = await this._store.setMany(serializedEntries);
|
|
585
580
|
}
|
|
586
581
|
const promises = [];
|
|
587
582
|
for (const entry of entries) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keyv",
|
|
3
|
-
"version": "5.3.
|
|
3
|
+
"version": "5.3.4",
|
|
4
4
|
"description": "Simple key-value storage with support for multiple backends",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -57,20 +57,20 @@
|
|
|
57
57
|
"@keyv/serialize": "^1.0.3"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@faker-js/faker": "^9.
|
|
61
|
-
"@vitest/coverage-v8": "^3.
|
|
60
|
+
"@faker-js/faker": "^9.8.0",
|
|
61
|
+
"@vitest/coverage-v8": "^3.2.3",
|
|
62
62
|
"rimraf": "^6.0.1",
|
|
63
63
|
"timekeeper": "^2.3.1",
|
|
64
|
-
"tsd": "^0.
|
|
65
|
-
"vitest": "^3.
|
|
66
|
-
"xo": "^
|
|
67
|
-
"@keyv/compress-brotli": "^2.0.
|
|
68
|
-
"@keyv/compress-gzip": "^2.0.
|
|
69
|
-
"@keyv/
|
|
64
|
+
"tsd": "^0.32.0",
|
|
65
|
+
"vitest": "^3.2.3",
|
|
66
|
+
"xo": "^1.1.0",
|
|
67
|
+
"@keyv/compress-brotli": "^2.0.4",
|
|
68
|
+
"@keyv/compress-gzip": "^2.0.3",
|
|
69
|
+
"@keyv/sqlite": "^4.0.4",
|
|
70
70
|
"@keyv/memcache": "^2.0.1",
|
|
71
|
-
"@keyv/
|
|
72
|
-
"@keyv/
|
|
73
|
-
"@keyv/
|
|
71
|
+
"@keyv/test-suite": "^2.0.7",
|
|
72
|
+
"@keyv/mongo": "^3.0.2",
|
|
73
|
+
"@keyv/compress-lz4": "^1.0.0"
|
|
74
74
|
},
|
|
75
75
|
"tsd": {
|
|
76
76
|
"directory": "test"
|