@keyv/compress-gzip 1.0.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 ADDED
@@ -0,0 +1,38 @@
1
+ # @keyv/tiered [<img width="100" align="right" src="https://rawgit.com/lukechilds/keyv/master/media/logo.svg" alt="keyv">](https://github.com/lukechilds/keyv)
2
+
3
+ > Gzip compression for Keyv
4
+
5
+ [![build](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml/badge.svg)](https://github.com/jaredwray/keyv/actions/workflows/tests.yaml)
6
+ [![codecov](https://codecov.io/gh/jaredwray/keyv/branch/main/graph/badge.svg?token=bRzR3RyOXZ)](https://codecov.io/gh/jaredwray/keyv)
7
+ [![npm](https://img.shields.io/npm/v/@keyv/compress-gzip.svg)](https://www.npmjs.com/package/@keyv/compress-gzip)
8
+ [![npm](https://img.shields.io/npm/dm/@keyv/compress-gzip)](https://npmjs.com/package/@keyv/compress-gzip)
9
+
10
+ Gzip compression for [Keyv](https://github.com/jaredwray/keyv).
11
+
12
+ ## Install
13
+
14
+ ```shell
15
+ npm install --save keyv @keyv/compress-gzip
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```javascript
21
+ const KeyvGzip = require('@keyv/compress-gzip');
22
+ const Keyv = require('keyv');
23
+
24
+ const keyv = new Keyv({store: new Map(), compression: new KeyvGzip()});
25
+
26
+ ```
27
+
28
+ ## API
29
+
30
+ ### @keyv/compress-gzip(\[options])
31
+
32
+ #### options
33
+
34
+ All options for @keyv/compress-gzip are based on the package [compress-gzip](https://github.com/nodeca/pako#readme)
35
+
36
+ ## License
37
+
38
+ MIT © Jared Wray
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@keyv/compress-gzip",
3
+ "version": "1.0.1",
4
+ "description": "gzip compression for keyv",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "xo && nyc ava",
8
+ "coverage": "nyc report --reporter=text-lcov > coverage.lcov",
9
+ "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov && rm -rf ./test/testdb.sqlite"
10
+ },
11
+ "xo": {
12
+ "rules": {
13
+ "unicorn/prefer-module": 0,
14
+ "unicorn/prefer-node-protocol": 0
15
+ }
16
+ },
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/jaredwray/keyv.git"
20
+ },
21
+ "keywords": [
22
+ "compress",
23
+ "gzip",
24
+ "keyv",
25
+ "storage",
26
+ "adapter",
27
+ "key",
28
+ "value",
29
+ "store",
30
+ "cache",
31
+ "ttl"
32
+ ],
33
+ "author": "Jared Wray <me@jaredwray.com> (http://jaredwray.com)",
34
+ "license": "MIT",
35
+ "bugs": {
36
+ "url": "https://github.com/jaredwray/keyv/issues"
37
+ },
38
+ "homepage": "https://github.com/jaredwray/keyv",
39
+ "dependencies": {
40
+ "json-buffer": "^3.0.1",
41
+ "pako": "^2.0.4"
42
+ },
43
+ "devDependencies": {
44
+ "@types/keyv": "^3.1.4",
45
+ "ava": "^4.2.0",
46
+ "keyv": "*",
47
+ "nyc": "^15.1.0",
48
+ "requirable": "^1.0.5",
49
+ "this": "^1.1.0",
50
+ "ts-node": "^10.8.2",
51
+ "tsd": "^0.20.0",
52
+ "typescript": "^4.6.4",
53
+ "xo": "^0.48.0"
54
+ },
55
+ "tsd": {
56
+ "directory": "test"
57
+ },
58
+ "types": "./src/index.d.ts",
59
+ "engines": {
60
+ "node": ">= 12"
61
+ },
62
+ "files": [
63
+ "src"
64
+ ]
65
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,19 @@
1
+ import {EventEmitter} from 'node:events';
2
+ import {Store, StoredData} from 'keyv';
3
+
4
+ declare class KeyvGzip extends EventEmitter {
5
+ ttlSupport: any;
6
+ opts: any;
7
+ constructor(options?: string | KeyvGzip.Options);
8
+ compress(value: Value);
9
+ decompress(value: Value);
10
+ }
11
+
12
+ declare namespace KeyvGzip {
13
+ interface Options {
14
+ compress: (...args: any[]) => void;
15
+ decompress: (...args: any[]) => void;
16
+ }
17
+ }
18
+
19
+ export = KeyvGzip;
package/src/index.js ADDED
@@ -0,0 +1,30 @@
1
+ const pako = require('pako');
2
+ const JSONB = require('json-buffer');
3
+
4
+ class KeyvGzip {
5
+ constructor(options) {
6
+ this.opts = {
7
+ to: 'string',
8
+ ...options,
9
+ };
10
+
11
+ this.opts.compress = pako.deflate;
12
+ this.opts.decompress = pako.inflate;
13
+ this.opts.serialize = async ({value, expires}) => JSONB.stringify({value: await this.opts.compress(value, this.opts), expires});
14
+ this.opts.deserialize = async data => {
15
+ const {value, expires} = JSONB.parse(data);
16
+ const value_ = await this.opts.decompress(value, this.opts);
17
+ return {value: value_, expires};
18
+ };
19
+ }
20
+
21
+ compress(value, options) {
22
+ return this.opts.compress(value, {...this.opts, ...options});
23
+ }
24
+
25
+ decompress(value, options) {
26
+ return this.opts.decompress(value, {...this.opts, ...options});
27
+ }
28
+ }
29
+
30
+ module.exports = KeyvGzip;