]> git.kaiwu.me - njs.git/commitdiff
Types: added ts types for "zlib" module.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 27 Apr 2023 02:38:23 +0000 (19:38 -0700)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 27 Apr 2023 02:38:23 +0000 (19:38 -0700)
test/ts/test.ts
ts/index.d.ts
ts/njs_modules/zlib.d.ts [new file with mode: 0644]

index cb2ef734470b9aec7aadf1bb2aea86d5a05bd00b..bddb64a9ad20db87d1b86eeb72d1c43c79f0da66 100644 (file)
@@ -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;
index bacac1eabd53bf6e70b22b8658b59fcdab778237..b40b32c210bc9ca1a68c3c118aae85abb5f4110b 100644 (file)
@@ -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 --git a/ts/njs_modules/zlib.d.ts b/ts/njs_modules/zlib.d.ts
new file mode 100644 (file)
index 0000000..c44c5f2
--- /dev/null
@@ -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;
+}