Skip to content

Commit

Permalink
support pako (#462)
Browse files Browse the repository at this point in the history
* support pako

* updated readme

* updated version

Co-authored-by: alphmth <>
  • Loading branch information
jaytist authored Sep 1, 2022
1 parent 13363a7 commit e36c2e9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/compress-gzip/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const keyv = new Keyv({store: new Map(), compression: new KeyvGzip()});

#### options

All options for @keyv/compress-gzip are based on the package [compress-gzip](https://github.com/Rebsos/node-gzip#readme)
All options for @keyv/compress-gzip are based on the package [compress-gzip](https://github.com/nodeca/pako#readme)

## License

Expand Down
16 changes: 8 additions & 8 deletions packages/compress-gzip/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{
"name": "@keyv/compress-gzip",
"version": "1.0.0",
"version": "1.0.1",
"description": "gzip compression for keyv",
"main": "src/index.js",
"scripts": {
"test": "xo && nyc ava",
"coverage": "nyc report --reporter=text-lcov > coverage.lcov",
"clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov && rm -rf ./test/testdb.sqlite"
},
"xo": {
"rules": {
"unicorn/prefer-module": 0,
"unicorn/prefer-node-protocol": 0
}
},
"xo": {
"rules": {
"unicorn/prefer-module": 0,
"unicorn/prefer-node-protocol": 0
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/jaredwray/keyv.git"
Expand All @@ -38,7 +38,7 @@
"homepage": "https://github.com/jaredwray/keyv",
"dependencies": {
"json-buffer": "^3.0.1",
"node-gzip": "^1.1.2"
"pako": "^2.0.4"
},
"devDependencies": {
"@types/keyv": "^3.1.4",
Expand Down
17 changes: 9 additions & 8 deletions packages/compress-gzip/src/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
const {gzip, ungzip} = require('node-gzip');
const pako = require('pako');
const JSONB = require('json-buffer');

class KeyvGzip {
constructor(options) {
this.opts = {
to: 'string',
...options,
};

this.opts.compress = gzip;
this.opts.decompress = ungzip;
this.opts.serialize = async ({value, expires}) => JSONB.stringify({value: await gzip(value, this.opts), expires});
this.opts.compress = pako.deflate;
this.opts.decompress = pako.inflate;
this.opts.serialize = async ({value, expires}) => JSONB.stringify({value: await this.opts.compress(value, this.opts), expires});
this.opts.deserialize = async data => {
const {value, expires} = JSONB.parse(data);
const value_ = await ungzip(value, this.opts);
return {value: value_.toString(), expires};
const value_ = await this.opts.decompress(value, this.opts);
return {value: value_, expires};
};
}

compress(value, options) {
return this.opts.compress(value, options);
return this.opts.compress(value, {...this.opts, ...options});
}

decompress(value, options) {
return this.opts.decompress(value, options);
return this.opts.decompress(value, {...this.opts, ...options});
}
}

Expand Down
14 changes: 1 addition & 13 deletions packages/compress-gzip/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test('gzip compression/decompression', async t => {
const compressed = await keyv.compress('whatever');
t.not(compressed, 'whatever');
const decompressed = await keyv.decompress(compressed);
t.is(decompressed.toString(), 'whatever');
t.is(decompressed, 'whatever');
});

// Test serialize compression
Expand All @@ -32,18 +32,6 @@ test('options while compress', async t => {
const compressedWithoutOptions = await keyv.compress('whatever');
t.not(compressed, compressedWithoutOptions);
});
// Test options while decompress
test('options while decompress', async t => {
const keyv = new KeyvGzip();
const compressed = await keyv.compress('whatever', {chunkSize: 32 * 1024});
t.not(compressed, 'whatever');
const compressedWithoutOptions = await keyv.compress('whatever');
t.not(compressed, compressedWithoutOptions);
const decompress = await keyv.decompress(compressed);
const decompressWithoutOptions = await keyv.decompress(compressedWithoutOptions);
t.not(decompress, decompressWithoutOptions);
t.is(decompress.toString(), decompressWithoutOptions.toString());
});
// Test options at class level
test('options at class level', async t => {
const keyv = new KeyvGzip({chunkSize: 32 * 1024});
Expand Down

0 comments on commit e36c2e9

Please sign in to comment.