[njs] Types: added ts types for "zlib" module.

Dmitry Volyntsev xeioex at nginx.com
Thu Apr 27 04:06:07 UTC 2023


details:   https://hg.nginx.org/njs/rev/677fc88d8d6d
branches:  
changeset: 2092:677fc88d8d6d
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Apr 26 19:38:23 2023 -0700
description:
Types: added ts types for "zlib" module.

diffstat:

 test/ts/test.ts          |    9 +++
 ts/index.d.ts            |    1 +
 ts/njs_modules/zlib.d.ts |  127 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 137 insertions(+), 0 deletions(-)

diffs (165 lines):

diff -r 5f18ec3b9e53 -r 677fc88d8d6d test/ts/test.ts
--- a/test/ts/test.ts	Wed Apr 26 19:38:21 2023 -0700
+++ b/test/ts/test.ts	Wed Apr 26 19:38:23 2023 -0700
@@ -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 -r 5f18ec3b9e53 -r 677fc88d8d6d ts/index.d.ts
--- a/ts/index.d.ts	Wed Apr 26 19:38:21 2023 -0700
+++ b/ts/index.d.ts	Wed Apr 26 19:38:23 2023 -0700
@@ -4,3 +4,4 @@
 /// <reference path="njs_modules/fs.d.ts" />
 /// <reference path="njs_modules/xml.d.ts" />
 /// <reference path="njs_modules/querystring.d.ts" />
+/// <reference path="njs_modules/zlib.d.ts" />
diff -r 5f18ec3b9e53 -r 677fc88d8d6d ts/njs_modules/zlib.d.ts
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ts/njs_modules/zlib.d.ts	Wed Apr 26 19:38:23 2023 -0700
@@ -0,0 +1,127 @@
+/// <reference path="../njs_core.d.ts" />
+
+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;
+}


More information about the nginx-devel mailing list