Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support pako #462

Merged
merged 3 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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