keyv 5.3.1 → 5.3.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 +45 -4
- package/dist/index.cjs +13 -30
- package/dist/index.js +13 -30
- package/package.json +8 -8
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
|
|
@@ -282,7 +263,7 @@ var Keyv = class extends event_manager_default {
|
|
|
282
263
|
...store
|
|
283
264
|
};
|
|
284
265
|
}
|
|
285
|
-
this._store = this.opts.store;
|
|
266
|
+
this._store = this.opts.store ?? /* @__PURE__ */ new Map();
|
|
286
267
|
this._compression = this.opts.compression;
|
|
287
268
|
this._serialize = this.opts.serialize;
|
|
288
269
|
this._deserialize = this.opts.deserialize;
|
|
@@ -568,24 +549,26 @@ var Keyv = class extends event_manager_default {
|
|
|
568
549
|
* @returns {boolean} if it sets then it will return a true. On failure will return false.
|
|
569
550
|
*/
|
|
570
551
|
async set(key, value, ttl) {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
552
|
+
const data = { key, value, ttl };
|
|
553
|
+
this.hooks.trigger("preSet" /* PRE_SET */, data);
|
|
554
|
+
const keyPrefixed = this._getKeyPrefix(data.key);
|
|
555
|
+
if (data.ttl === void 0) {
|
|
556
|
+
data.ttl = this._ttl;
|
|
575
557
|
}
|
|
576
|
-
if (ttl === 0) {
|
|
577
|
-
ttl = void 0;
|
|
558
|
+
if (data.ttl === 0) {
|
|
559
|
+
data.ttl = void 0;
|
|
578
560
|
}
|
|
579
561
|
const { store } = this.opts;
|
|
580
|
-
const expires = typeof ttl === "number" ? Date.now() + ttl : null;
|
|
581
|
-
if (typeof value === "symbol") {
|
|
562
|
+
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : null;
|
|
563
|
+
if (typeof data.value === "symbol") {
|
|
582
564
|
this.emit("error", "symbol cannot be serialized");
|
|
565
|
+
throw new Error("symbol cannot be serialized");
|
|
583
566
|
}
|
|
584
|
-
const formattedValue = { value, expires };
|
|
567
|
+
const formattedValue = { value: data.value, expires };
|
|
585
568
|
const serializedValue = await this.serializeData(formattedValue);
|
|
586
569
|
let result = true;
|
|
587
570
|
try {
|
|
588
|
-
const value2 = await store.set(keyPrefixed, serializedValue, ttl);
|
|
571
|
+
const value2 = await store.set(keyPrefixed, serializedValue, data.ttl);
|
|
589
572
|
if (typeof value2 === "boolean") {
|
|
590
573
|
result = value2;
|
|
591
574
|
}
|
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
|
|
@@ -256,7 +237,7 @@ var Keyv = class extends event_manager_default {
|
|
|
256
237
|
...store
|
|
257
238
|
};
|
|
258
239
|
}
|
|
259
|
-
this._store = this.opts.store;
|
|
240
|
+
this._store = this.opts.store ?? /* @__PURE__ */ new Map();
|
|
260
241
|
this._compression = this.opts.compression;
|
|
261
242
|
this._serialize = this.opts.serialize;
|
|
262
243
|
this._deserialize = this.opts.deserialize;
|
|
@@ -542,24 +523,26 @@ var Keyv = class extends event_manager_default {
|
|
|
542
523
|
* @returns {boolean} if it sets then it will return a true. On failure will return false.
|
|
543
524
|
*/
|
|
544
525
|
async set(key, value, ttl) {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
526
|
+
const data = { key, value, ttl };
|
|
527
|
+
this.hooks.trigger("preSet" /* PRE_SET */, data);
|
|
528
|
+
const keyPrefixed = this._getKeyPrefix(data.key);
|
|
529
|
+
if (data.ttl === void 0) {
|
|
530
|
+
data.ttl = this._ttl;
|
|
549
531
|
}
|
|
550
|
-
if (ttl === 0) {
|
|
551
|
-
ttl = void 0;
|
|
532
|
+
if (data.ttl === 0) {
|
|
533
|
+
data.ttl = void 0;
|
|
552
534
|
}
|
|
553
535
|
const { store } = this.opts;
|
|
554
|
-
const expires = typeof ttl === "number" ? Date.now() + ttl : null;
|
|
555
|
-
if (typeof value === "symbol") {
|
|
536
|
+
const expires = typeof data.ttl === "number" ? Date.now() + data.ttl : null;
|
|
537
|
+
if (typeof data.value === "symbol") {
|
|
556
538
|
this.emit("error", "symbol cannot be serialized");
|
|
539
|
+
throw new Error("symbol cannot be serialized");
|
|
557
540
|
}
|
|
558
|
-
const formattedValue = { value, expires };
|
|
541
|
+
const formattedValue = { value: data.value, expires };
|
|
559
542
|
const serializedValue = await this.serializeData(formattedValue);
|
|
560
543
|
let result = true;
|
|
561
544
|
try {
|
|
562
|
-
const value2 = await store.set(keyPrefixed, serializedValue, ttl);
|
|
545
|
+
const value2 = await store.set(keyPrefixed, serializedValue, data.ttl);
|
|
563
546
|
if (typeof value2 === "boolean") {
|
|
564
547
|
result = value2;
|
|
565
548
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "keyv",
|
|
3
|
-
"version": "5.3.
|
|
3
|
+
"version": "5.3.3",
|
|
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.6.0",
|
|
61
|
+
"@vitest/coverage-v8": "^3.1.1",
|
|
62
62
|
"rimraf": "^6.0.1",
|
|
63
63
|
"timekeeper": "^2.3.1",
|
|
64
64
|
"tsd": "^0.31.2",
|
|
65
|
-
"vitest": "^3.
|
|
65
|
+
"vitest": "^3.1.1",
|
|
66
66
|
"xo": "^0.60.0",
|
|
67
67
|
"@keyv/compress-gzip": "^2.0.2",
|
|
68
|
-
"@keyv/compress-brotli": "^2.0.3",
|
|
69
|
-
"@keyv/mongo": "^3.0.1",
|
|
70
68
|
"@keyv/compress-lz4": "^1.0.0",
|
|
71
|
-
"@keyv/
|
|
69
|
+
"@keyv/compress-brotli": "^2.0.3",
|
|
72
70
|
"@keyv/memcache": "^2.0.1",
|
|
73
|
-
"@keyv/sqlite": "^4.0.
|
|
71
|
+
"@keyv/sqlite": "^4.0.2",
|
|
72
|
+
"@keyv/mongo": "^3.0.1",
|
|
73
|
+
"@keyv/test-suite": "^2.0.6"
|
|
74
74
|
},
|
|
75
75
|
"tsd": {
|
|
76
76
|
"directory": "test"
|