keyv 4.5.0 → 4.5.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 CHANGED
@@ -195,12 +195,34 @@ Keyv supports `gzip` and `brotli` compression. To enable compression, pass the `
195
195
  const KeyvGzip = require('@keyv/compress-gzip');
196
196
  const Keyv = require('keyv');
197
197
 
198
- const keyvGzip = new KeyvGzip();;
198
+ const keyvGzip = new KeyvGzip();
199
199
  const keyv = new Keyv({ compression: KeyvGzip });
200
200
  ```
201
201
 
202
202
  You can also pass a custom compression function to the `compression` option. Following the pattern of the official compression adapters.
203
203
 
204
+ ### Want to build your own?
205
+
206
+ Great! Keyv is designed to be easily extended. You can build your own compression adapter by following the pattern of the official compression adapters based on this interface:
207
+
208
+ ```typescript
209
+ interface CompressionAdapter {
210
+ async compress(value: any, options?: any);
211
+ async decompress(value: any, options?: any);
212
+ async serialize(value: any);
213
+ async deserialize(value: any);
214
+ }
215
+ ```
216
+
217
+ In addition to the interface, you can test it with our compression test suite using @keyv/test-suite:
218
+
219
+ ```js
220
+ const {keyvCompresstionTests} = require('@keyv/test-suite');
221
+ const KeyvGzip = require('@keyv/compress-gzip');
222
+
223
+ keyvCompresstionTests(test, new KeyvGzip());
224
+ ```
225
+
204
226
  ## API
205
227
 
206
228
  ### new Keyv([uri], [options])
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyv",
3
- "version": "4.5.0",
3
+ "version": "4.5.2",
4
4
  "description": "Simple key-value storage with support for multiple backends",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -11,7 +11,8 @@
11
11
  "xo": {
12
12
  "rules": {
13
13
  "unicorn/prefer-module": 0,
14
- "unicorn/prefer-node-protocol": 0
14
+ "unicorn/prefer-node-protocol": 0,
15
+ "@typescript-eslint/consistent-type-definitions": 0
15
16
  }
16
17
  },
17
18
  "repository": {
@@ -36,16 +37,16 @@
36
37
  },
37
38
  "devDependencies": {
38
39
  "@keyv/test-suite": "*",
39
- "ava": "^4.3.0",
40
- "eslint": "^8.19.0",
41
- "eslint-plugin-promise": "^6.0.0",
40
+ "ava": "^5.0.1",
41
+ "eslint": "^8.26.0",
42
+ "eslint-plugin-promise": "^6.1.1",
42
43
  "nyc": "^15.1.0",
43
44
  "pify": "5.0.0",
44
45
  "this": "^1.1.0",
45
46
  "timekeeper": "^2.2.0",
46
- "tsd": "^0.22.0",
47
- "typescript": "^4.7.4",
48
- "xo": "^0.51.0"
47
+ "tsd": "^0.24.1",
48
+ "typescript": "^4.8.4",
49
+ "xo": "^0.52.4"
49
50
  },
50
51
  "tsd": {
51
52
  "directory": "test"
package/src/index.d.ts CHANGED
@@ -85,7 +85,14 @@ declare namespace Keyv {
85
85
  /** Specify an adapter to use. e.g `'redis'` or `'mongodb'`. */
86
86
  adapter?: 'redis' | 'mongodb' | 'mongo' | 'sqlite' | 'postgresql' | 'postgres' | 'mysql' | undefined;
87
87
  /** Enable compression option **/
88
- compress?: Record<string, unknown> | undefined;
88
+ compression?: CompressionAdapter | undefined;
89
+ }
90
+
91
+ interface CompressionAdapter {
92
+ compress(value: any, options?: any): Promise<any>;
93
+ decompress(value: any, options?: any): Promise<any>;
94
+ serialize(value: any): Promise<any>;
95
+ deserialize(value: any): Promise<any>;
89
96
  }
90
97
 
91
98
  interface DeserializedData<Value> {
package/src/index.js CHANGED
@@ -52,9 +52,8 @@ class Keyv extends EventEmitter {
52
52
 
53
53
  if (this.opts.compression) {
54
54
  const compression = this.opts.compression;
55
- const {serialize, deserialize} = compression.opts;
56
- this.opts.serialize = serialize;
57
- this.opts.deserialize = deserialize;
55
+ this.opts.serialize = compression.serialize.bind(compression);
56
+ this.opts.deserialize = compression.deserialize.bind(compression);
58
57
  }
59
58
 
60
59
  if (typeof this.opts.store.on === 'function' && emitErrors) {
@@ -119,7 +118,7 @@ class Keyv extends EventEmitter {
119
118
  for (const key of keyPrefixed) {
120
119
  promises.push(Promise.resolve()
121
120
  .then(() => store.get(key))
122
- .then(data => (typeof data === 'string') ? this.opts.deserialize(data) : data)
121
+ .then(data => (typeof data === 'string') ? this.opts.deserialize(data) : (this.opts.compression ? this.opts.deserialize(data) : data))
123
122
  .then(data => {
124
123
  if (data === undefined || data === null) {
125
124
  return undefined;
@@ -147,7 +146,7 @@ class Keyv extends EventEmitter {
147
146
 
148
147
  return Promise.resolve()
149
148
  .then(() => isArray ? store.getMany(keyPrefixed) : store.get(keyPrefixed))
150
- .then(data => (typeof data === 'string') ? this.opts.deserialize(data) : data)
149
+ .then(data => (typeof data === 'string') ? this.opts.deserialize(data) : (this.opts.compression ? this.opts.deserialize(data) : data))
151
150
  .then(data => {
152
151
  if (data === undefined || data === null) {
153
152
  return undefined;