keyv 4.4.1 → 4.5.1

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
@@ -187,6 +187,42 @@ const awesomeModule = new AwesomeModule({ cache: 'redis://localhost' });
187
187
  const awesomeModule = new AwesomeModule({ cache: some3rdPartyStore });
188
188
  ```
189
189
 
190
+ ## Compression
191
+
192
+ Keyv supports `gzip` and `brotli` compression. To enable compression, pass the `compress` option to the constructor.
193
+
194
+ ```js
195
+ const KeyvGzip = require('@keyv/compress-gzip');
196
+ const Keyv = require('keyv');
197
+
198
+ const keyvGzip = new KeyvGzip();
199
+ const keyv = new Keyv({ compression: KeyvGzip });
200
+ ```
201
+
202
+ You can also pass a custom compression function to the `compression` option. Following the pattern of the official compression adapters.
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
+
190
226
  ## API
191
227
 
192
228
  ### new Keyv([uri], [options])
@@ -224,6 +260,13 @@ Default: `undefined`
224
260
 
225
261
  Default TTL. Can be overridden by specififying a TTL on `.set()`.
226
262
 
263
+ #### options.compression
264
+
265
+ Type: `@keyv/compress-<compression_package_name>`<br>
266
+ Default: `undefined`
267
+
268
+ Compression package to use. See [Compression](#compression) for more details.
269
+
227
270
  #### options.serialize
228
271
 
229
272
  Type: `Function`<br>
@@ -315,7 +358,6 @@ In this section of the documentation we will cover:
315
358
  This package requires the following dependencies to run:
316
359
 
317
360
  1) [Yarn V1](https://yarnpkg.com/getting-started/install)
318
- 2) [Lerna](https://lerna.js.org/)
319
361
  3) [Docker](https://docs.docker.com/get-docker/)
320
362
 
321
363
  ## Setting up your workspace
@@ -337,7 +379,7 @@ Once the project is installed locally, you are ready to start up its services:
337
379
 
338
380
  ## Available Commands
339
381
 
340
- Once the project is running, you can execute a variety of commands. The root workspace and each subpackage contain a `package.json` file with a `scripts` field listing all the commands that can be executed from that directory. This project also supports native `yarn`, `lerna`, and `docker` commands.
382
+ Once the project is running, you can execute a variety of commands. The root workspace and each subpackage contain a `package.json` file with a `scripts` field listing all the commands that can be executed from that directory. This project also supports native `yarn`, and `docker` commands.
341
383
 
342
384
  Here, we'll cover the primary commands that can be executed from the root directory. Unless otherwise noted, these commands can also be executed from a subpackage. If executed from a subpackage, they will only affect that subpackage, rather than the entire workspace.
343
385
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "keyv",
3
- "version": "4.4.1",
3
+ "version": "4.5.1",
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": {
@@ -32,21 +33,20 @@
32
33
  },
33
34
  "homepage": "https://github.com/jaredwray/keyv",
34
35
  "dependencies": {
35
- "compress-brotli": "^1.3.8",
36
36
  "json-buffer": "3.0.1"
37
37
  },
38
38
  "devDependencies": {
39
39
  "@keyv/test-suite": "*",
40
- "ava": "^4.3.1",
41
- "eslint": "^8.22.0",
42
- "eslint-plugin-promise": "^6.0.0",
40
+ "ava": "^5.0.1",
41
+ "eslint": "^8.26.0",
42
+ "eslint-plugin-promise": "^6.1.1",
43
43
  "nyc": "^15.1.0",
44
44
  "pify": "5.0.0",
45
45
  "this": "^1.1.0",
46
46
  "timekeeper": "^2.2.0",
47
- "tsd": "^0.22.0",
48
- "typescript": "^4.7.4",
49
- "xo": "^0.51.0"
47
+ "tsd": "^0.24.1",
48
+ "typescript": "^4.8.4",
49
+ "xo": "^0.52.4"
50
50
  },
51
51
  "tsd": {
52
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
+ async compress(value: any, options?: any);
93
+ async decompress(value: any, options?: any);
94
+ async serialize(value: any);
95
+ async deserialize(value: any);
89
96
  }
90
97
 
91
98
  interface DeserializedData<Value> {
package/src/index.js CHANGED
@@ -2,7 +2,6 @@
2
2
 
3
3
  const EventEmitter = require('events');
4
4
  const JSONB = require('json-buffer');
5
- const compressBrotli = require('compress-brotli');
6
5
 
7
6
  const loadStore = options => {
8
7
  const adapters = {
@@ -51,13 +50,10 @@ class Keyv extends EventEmitter {
51
50
  this.opts.store = loadStore(adapterOptions);
52
51
  }
53
52
 
54
- if (this.opts.compress) {
55
- const brotli = compressBrotli(this.opts.compress.opts);
56
- this.opts.serialize = async ({value, expires}) => brotli.serialize({value: await brotli.compress(value), expires});
57
- this.opts.deserialize = async data => {
58
- const {value, expires} = brotli.deserialize(data);
59
- return {value: await brotli.decompress(value), expires};
60
- };
53
+ if (this.opts.compression) {
54
+ const compression = this.opts.compression;
55
+ this.opts.serialize = compression.serialize.bind(compression);
56
+ this.opts.deserialize = compression.deserialize.bind(compression);
61
57
  }
62
58
 
63
59
  if (typeof this.opts.store.on === 'function' && emitErrors) {
@@ -122,7 +118,7 @@ class Keyv extends EventEmitter {
122
118
  for (const key of keyPrefixed) {
123
119
  promises.push(Promise.resolve()
124
120
  .then(() => store.get(key))
125
- .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))
126
122
  .then(data => {
127
123
  if (data === undefined || data === null) {
128
124
  return undefined;
@@ -150,7 +146,7 @@ class Keyv extends EventEmitter {
150
146
 
151
147
  return Promise.resolve()
152
148
  .then(() => isArray ? store.getMany(keyPrefixed) : store.get(keyPrefixed))
153
- .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))
154
150
  .then(data => {
155
151
  if (data === undefined || data === null) {
156
152
  return undefined;