Skip to content

Commit 7a6c4cc

Browse files
author
Christian Llontop
authored
compress-brotli package using typescript (#693)
* refactor for KeyvBrotli to use typescript * add full testing and types * fix common require for brotli module * add build and prepare command for packages * add sourcemap for emitted js files
1 parent 4fcfe06 commit 7a6c4cc

File tree

22 files changed

+158
-89
lines changed

22 files changed

+158
-89
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"packages/*"
1111
],
1212
"scripts": {
13-
"build": "yarn workspace @keyv/test-suite run build",
13+
"build": "yarn workspaces run build",
1414
"test": " yarn build && c8 --reporter=lcov yarn workspaces run test:ci",
1515
"test:services:start": "docker-compose -f ./docker-compose.yaml up -d",
1616
"test:services:stop": "docker-compose -f ./docker-compose.yaml down -v",

packages/compress-brotli/package.json

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
"name": "@keyv/compress-brotli",
33
"version": "1.1.2",
44
"description": "brotli compression for keyv",
5-
"main": "src/index.js",
5+
"main": "dist/index.js",
6+
"types": "dist/index.d.ts",
67
"scripts": {
8+
"build": "tsc",
9+
"prepare": "yarn build",
710
"test": "xo && c8 ava --serial",
811
"test:ci": "xo && ava --serial",
912
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./test/testdb.sqlite"
@@ -21,19 +24,10 @@
2124
"ts"
2225
]
2326
}
24-
]
27+
],
28+
"import/extensions": 0
2529
}
2630
},
27-
"ava": {
28-
"require": [
29-
"requirable",
30-
"ts-node/register"
31-
],
32-
"extensions": [
33-
"js",
34-
"ts"
35-
]
36-
},
3731
"repository": {
3832
"type": "git",
3933
"url": "git+https://github.com/jaredwray/keyv.git"
@@ -75,11 +69,18 @@
7569
"tsd": {
7670
"directory": "test"
7771
},
78-
"types": "./src/index.d.ts",
7972
"engines": {
8073
"node": ">= 12"
8174
},
8275
"files": [
8376
"src"
84-
]
77+
],
78+
"ava": {
79+
"extensions": [
80+
"ts"
81+
],
82+
"require": [
83+
"ts-node/register"
84+
]
85+
}
8586
}

packages/compress-brotli/src/index.d.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/compress-brotli/src/index.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

packages/compress-brotli/src/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type {BrotliOptions, InputType} from 'node:zlib';
2+
import compressBrotli from 'compress-brotli';
3+
import type {Brotli, CompressResult, Options, SerializeResult, Serialize} from './types';
4+
5+
class KeyvBrotli {
6+
private readonly brotli: Brotli;
7+
constructor(options?: Options) {
8+
this.brotli = compressBrotli(options);
9+
}
10+
11+
async compress(value: any, options?: BrotliOptions): CompressResult {
12+
return this.brotli.compress(value, options);
13+
}
14+
15+
async decompress<T>(data: InputType, options?: BrotliOptions): Promise<T> {
16+
return await this.brotli.decompress(data, options) as T;
17+
}
18+
19+
async serialize({value, expires}: Serialize): Promise<SerializeResult> {
20+
const compressValue = await this.compress(value);
21+
// @ts-expect-error - `expires` is not part of the `SerializeResult` type
22+
return this.brotli.serialize({value: compressValue, expires});
23+
}
24+
25+
async deserialize(data: CompressResult): Promise<Serialize> {
26+
// eslint-disable-next-line @typescript-eslint/no-misused-promises
27+
if (!data) {
28+
return data;
29+
}
30+
31+
const {value, expires} = this.brotli.deserialize(data) as Serialize;
32+
return {value: await this.decompress(value), expires};
33+
}
34+
}
35+
36+
export = KeyvBrotli;

packages/compress-brotli/src/types.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import type {BrotliOptions, CompressCallback, InputType} from 'node:zlib';
2+
import type {parse as JSONBparse, stringify as JSONBstringify} from 'json-buffer';
3+
4+
export type CompressResult = Promise<Parameters<CompressCallback>[1]>;
5+
export type DecompressResult = Promise<ReturnType<typeof JSONBparse>>;
6+
7+
export type SerializeResult = ReturnType<typeof JSONBstringify>;
8+
export type DeserializeResult = ReturnType<typeof JSONBparse>;
9+
10+
type BrotliSerialize<T> = (source: InputType) => T;
11+
type BrotliDeserialize<T> = (source: CompressResult) => T;
12+
13+
export type Serialize = {
14+
value: InputType;
15+
expires?: number;
16+
};
17+
18+
export interface Options {
19+
compressOptions?: BrotliOptions;
20+
decompressOptions?: BrotliOptions;
21+
enable?: boolean;
22+
serialize?: any;
23+
deserialize?: any;
24+
iltorb?: any;
25+
}
26+
27+
export interface Brotli {
28+
serialize: BrotliSerialize<SerializeResult>;
29+
deserialize: BrotliDeserialize<DeserializeResult>;
30+
compress: (data: InputType, options?: BrotliOptions) => CompressResult;
31+
decompress: (data: InputType, options?: BrotliOptions) => DecompressResult;
32+
}

packages/compress-brotli/test/test.js renamed to packages/compress-brotli/test/test.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
const {
2-
constants: {
3-
BROTLI_PARAM_MODE,
4-
BROTLI_PARAM_QUALITY,
5-
},
6-
} = require('node:zlib');
7-
const v8 = require('node:v8');
8-
const test = require('ava');
9-
const json = require('json-buffer');
10-
const {keyvCompresstionTests} = require('@keyv/test-suite');
11-
const KeyvBrotli = require('../src/index.js');
1+
import {constants as zlibConstants} from 'node:zlib';
2+
import v8 from 'node:v8';
3+
import test from 'ava';
4+
import json from 'json-buffer';
5+
import {keyvCompresstionTests} from '@keyv/test-suite';
6+
import KeyvBrotli from '../src/index';
7+
import type {DeserializeResult} from '../src/types';
8+
9+
// eslint-disable-next-line @typescript-eslint/naming-convention
10+
const {BROTLI_PARAM_MODE, BROTLI_PARAM_QUALITY} = zlibConstants;
1211

1312
keyvCompresstionTests(test, new KeyvBrotli());
1413

@@ -20,7 +19,7 @@ test('object type compression/decompression', async t => {
2019
c: true,
2120
};
2221
const compressed = await keyv.compress(object);
23-
const decompressed = await keyv.decompress(compressed, {});
22+
const decompressed = await keyv.decompress(compressed);
2423
t.deepEqual(decompressed, object);
2524
});
2625

@@ -30,8 +29,9 @@ test('disable brotli compression', async t => {
3029
};
3130
const keyv = new KeyvBrotli(options);
3231
const compressed = await keyv.compress('whatever');
32+
// @ts-expect-error Testing non-compressed value
3333
t.is(compressed, 'whatever');
34-
const decompressed = await keyv.decompress(compressed);
34+
const decompressed: DeserializeResult = await keyv.decompress(compressed);
3535
t.is(decompressed, 'whatever');
3636
});
3737

@@ -117,7 +117,9 @@ test('decompression using number array with json-buffer', async t => {
117117

118118
test('deserialize with an empty value', async t => {
119119
const keyv = new KeyvBrotli();
120+
// @ts-expect-error - Testing empty value
120121
const deserialized = await keyv.deserialize('');
121122

123+
// @ts-expect-error - empty value
122124
t.is(deserialized, '');
123125
});

packages/compress-brotli/test/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import v8 from 'node:v8';
33
import test from 'ava';
44
import Keyv from 'keyv';
55
import json from 'json-buffer';
6-
import KeyvBrotli from '../src/index.js';
6+
import KeyvBrotli from '../src/index';
77

88
type MyType = {
99
a?: string;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"compilerOptions": {
3+
/* Language and Environment */
4+
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
5+
6+
/* Modules */
7+
"module": "commonjs", /* Specify what module code is generated. */
8+
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
9+
"baseUrl": "./src", /* Specify the base directory to resolve non-relative module names. */
10+
11+
/* Emit */
12+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
13+
"sourceMap": true, /* Create source map files for emitted JavaScript files. */
14+
"outDir": "./dist", /* Specify an output folder for all emitted files. */
15+
16+
/* Interop Constraints */
17+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
18+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
19+
20+
/* Type Checking */
21+
"strict": true, /* Enable all strict type-checking options. */
22+
23+
/* Completeness */
24+
"skipLibCheck": true, /* Skip type checking all .d.ts files. */
25+
"lib": [
26+
"es2016"
27+
]
28+
},
29+
"include": [
30+
"src/**/*"
31+
]
32+
}

packages/compress-gzip/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"scripts": {
8+
"build": "tsc",
9+
"prepare": "yarn build",
810
"test": "xo && yarn run build && c8 ava --serial",
911
"test:ci": "xo && yarn run build && ava --serial",
10-
"prepare": "yarn run build",
11-
"build": "tsc",
1212
"coverage": "nyc report --reporter=text-lcov > coverage.lcov",
1313
"clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov && rm -rf ./test/testdb.sqlite"
1414
},

packages/etcd/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "Etcd storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage"

packages/keyv/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "Simple key-value storage with support for multiple backends",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./test/testdb.sqlite"

packages/memcache/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "Memcache storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage"

packages/mongo/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "MongoDB storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage"

packages/mysql/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "MySQL/MariaDB storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage"

packages/offline/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "Offline storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./test/testdb.sqlite"

packages/postgres/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "PostgreSQL storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage"

packages/redis/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "Redis storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage"

packages/sqlite/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"description": "SQLite storage adapter for Keyv",
55
"main": "src/index.js",
66
"scripts": {
7+
"build": "echo 'No build step required.'",
8+
"prepare": "yarn build",
79
"test": "xo && c8 ava --serial",
810
"test:ci": "xo && ava --serial",
911
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf ./test/testdb.sqlite"

packages/test-suite/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"scripts": {
99
"build": "tsc",
1010
"prepare": "yarn build",
11-
"test": "yarn build && xo && c8 ava --serial",
12-
"test:ci": "yarn build && xo && ava --serial",
11+
"test": "xo && c8 ava --serial",
12+
"test:ci": "xo && ava --serial",
1313
"clean": "rm -rf node_modules && rm -rf ./coverage && rm -rf dist && rm -rf ./test/testdb.sqlite"
1414
},
1515
"xo": {

0 commit comments

Comments
 (0)