From e2f230ae0af18cb285d29129a317e76947ca1325 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Wed, 26 Apr 2023 19:38:23 -0700 Subject: [PATCH] Types: added ts types for "zlib" module. --- test/ts/test.ts | 9 +++ ts/index.d.ts | 1 + ts/njs_modules/zlib.d.ts | 127 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+) create mode 100644 ts/njs_modules/zlib.d.ts diff --git a/test/ts/test.ts b/test/ts/test.ts index cb2ef734..bddb64a9 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -2,6 +2,7 @@ import fs from 'fs'; import qs from 'querystring'; import cr from 'crypto'; import xml from 'xml'; +import zlib from 'zlib'; async function http_module(r: NginxHTTPRequest) { var bs: NjsByteString; @@ -194,6 +195,14 @@ function xml_module(str: NjsByteString) { node.$tags = [node, node]; } +function zlib_module(str: NjsByteString) { + zlib.deflateRawSync(str, {level: zlib.constants.Z_BEST_COMPRESSION, memLevel: 9}); + zlib.deflateSync(str, {strategy: zlib.constants.Z_RLE}); + + zlib.inflateRawSync(str, {windowBits: 14}); + zlib.inflateSync(str, {chunkSize: 2048}); +} + function crypto_module(str: NjsByteString) { var h; var b:Buffer; diff --git a/ts/index.d.ts b/ts/index.d.ts index bacac1ea..b40b32c2 100644 --- a/ts/index.d.ts +++ b/ts/index.d.ts @@ -4,3 +4,4 @@ /// /// /// +/// diff --git a/ts/njs_modules/zlib.d.ts b/ts/njs_modules/zlib.d.ts new file mode 100644 index 00000000..c44c5f2d --- /dev/null +++ b/ts/njs_modules/zlib.d.ts @@ -0,0 +1,127 @@ +/// + +declare module "zlib" { + interface NjsZlibOptions { + /** + * the buffer size for feeding data to and pulling data + * from the zlib routines, defaults to 1024. + */ + chunkSize?: number; + + /** + * The dictionary buffer. + */ + dictionary?: NjsStringOrBuffer; + + /** + * Compression level, from zlib.constants.Z_NO_COMPRESSION to + * zlib.constants.Z_BEST_COMPRESSION. Defaults to + * zlib.constants.Z_DEFAULT_COMPRESSION. + */ + level?: number; + + /** + * Specifies how much memory should be allocated for the internal compression state. + * 1 uses minimum memory but is slow and reduces compression ratio; + * 9 uses maximum memory for optimal speed. + * The default value is 8. + */ + memLevel?: number; + + /** + * The compression strategy, defaults to zlib.constants.Z_DEFAULT_STRATEGY. + */ + strategy?: number; + + /** + * The log2 of window size. + * -15 to -9 for raw data, from 9 to 15 for an ordinary stream. + */ + windowBits?: number; + } + + type NjsZlibConstants = { + /** + * No compression. + */ + Z_NO_COMPRESSION: number; + + /** + * Fastest, produces the least compression. + */ + Z_BEST_SPEED: number; + + /** + * Trade-off between speed and compression. + */ + Z_DEFAULT_COMPRESSION: number; + + /** + * Slowest, produces the most compression. + */ + Z_BEST_COMPRESSION: number; + + /** + * Filtered strategy: for the data produced by a filter or predictor. + */ + Z_FILTERED: number; + + /** + * Huffman-only strategy: only Huffman encoding, no string matching. + */ + Z_HUFFMAN_ONLY: number; + + /** + * Run Length Encoding strategy: limit match distances to one, + * better compression of PNG image data. + */ + Z_RLE: number; + + /** + * Fixed table strategy: prevents the use of dynamic Huffman codes, + * a simpler decoder for special applications. + */ + Z_FIXED: number; + + /** + * Default strategy, suitable for general purpose compression. + */ + Z_DEFAULT_STRATEGY: number; + }; + + interface Zlib { + /** + * Compresses data using deflate, and do not append a zlib header. + * + * @param data - The data to be compressed. + */ + deflateRawSync(data: NjsStringOrBuffer, options?:NjsZlibOptions): Buffer; + + /** + * Compresses data using deflate. + * + * @param data - The data to be compressed. + */ + deflateSync(data: NjsStringOrBuffer, options?:NjsZlibOptions): Buffer; + + /** + * Decompresses a raw deflate stream. + * + * @param data - The data to be decompressed. + */ + inflateRawSync(data: NjsStringOrBuffer, options?:NjsZlibOptions): Buffer; + + /** + * Decompresses a deflate stream. + * + * @param data - The data to be decompressed. + */ + inflateSync(data: NjsStringOrBuffer, options?:NjsZlibOptions): Buffer; + + constants: NjsZlibConstants; + } + + const zlib: Zlib; + + export default zlib; +} -- 2.47.3