[njs] Types: extending data types for methods with NjsStringLike args.

Dmitry Volyntsev xeioex at nginx.com
Thu Nov 26 11:12:36 UTC 2020


details:   https://hg.nginx.org/njs/rev/5bd78c74777a
branches:  
changeset: 1572:5bd78c74777a
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Nov 26 11:10:59 2020 +0000
description:
Types: extending data types for methods with NjsStringLike args.

diffstat:

 test/ts/test.ts              |   5 ++++-
 ts/ngx_http_js_module.d.ts   |  20 ++++++++++----------
 ts/ngx_stream_js_module.d.ts |   8 ++++----
 ts/njs_core.d.ts             |   1 +
 ts/njs_modules/crypto.d.ts   |   6 +++---
 ts/njs_modules/fs.d.ts       |   8 ++++----
 6 files changed, 26 insertions(+), 22 deletions(-)

diffs (207 lines):

diff -r 434f20c29f4c -r 5bd78c74777a test/ts/test.ts
--- a/test/ts/test.ts	Wed Nov 25 10:47:47 2020 +0000
+++ b/test/ts/test.ts	Thu Nov 26 11:10:59 2020 +0000
@@ -49,6 +49,7 @@ function http_module(r: NginxHTTPRequest
     // r.log
 
     r.log(bs);
+    r.log(Buffer.from("abc"));
     r.log(r.headersOut['Connection'] ?? '');
 
     // r.variables
@@ -61,7 +62,7 @@ function http_module(r: NginxHTTPRequest
     r.subrequest('/p/sub2', {method:'POST'}).then(reply => r.return(reply.status));
     vod = r.subrequest('/p/sub3', reply => r.return(reply.status));
     vod = r.subrequest('/p/sub4', {method:'POST'}, reply => r.return(reply.status));
-    vod = r.subrequest('/p/sub5', {detached:true});
+    vod = r.subrequest(Buffer.from('/p/sub5'), {detached:true});
     // Warning: vod = r.subrequest('/p/sub9', {detached:true}, reply => r.return(reply.status));
     r.subrequest('/p/sub6', 'a=1&b=2').then(reply => r.return(reply.status,
                                         JSON.stringify(JSON.parse(reply.responseBody ?? ''))));
@@ -73,6 +74,8 @@ function fs_module() {
 
     s = fs.readFileSync('/path', 'utf8');
     s = fs.readFileSync(Buffer.from('/path'), {encoding:'hex'});
+
+    fs.writeFileSync('/path', Buffer.from('abc'));
 }
 
 function qs_module(str: NjsByteString) {
diff -r 434f20c29f4c -r 5bd78c74777a ts/ngx_http_js_module.d.ts
--- a/ts/ngx_http_js_module.d.ts	Wed Nov 25 10:47:47 2020 +0000
+++ b/ts/ngx_http_js_module.d.ts	Thu Nov 26 11:10:59 2020 +0000
@@ -263,7 +263,7 @@ interface NginxHTTPRequest {
      * Writes a string to the error log on the error level of logging.
      * @param message Message to log.
      */
-    error(message: NjsStringLike): void;
+    error(message: NjsStringOrBuffer): void;
     /**
      * Finishes sending a response to the client.
      */
@@ -286,12 +286,12 @@ interface NginxHTTPRequest {
      * The actual redirect happens after the handler execution is completed.
      * @param uri Location to redirect to.
      */
-    internalRedirect(uri: NjsStringLike): void;
+    internalRedirect(uri: NjsStringOrBuffer): void;
     /**
      * Writes a string to the error log on the info level of logging.
      * @param message Message to log.
      */
-    log(message: NjsStringLike): void;
+    log(message: NjsStringOrBuffer): void;
     /**
      * HTTP method.
      */
@@ -323,11 +323,11 @@ interface NginxHTTPRequest {
      * @param status Respose status code.
      * @param body Respose body.
      */
-    return(status: number, body?: NjsStringLike): void;
+    return(status: number, body?: NjsStringOrBuffer): void;
     /**
      * Sends the HTTP headers to the client.
      */
-    send(part: NjsStringLike): void;
+    send(part: NjsStringOrBuffer): void;
     /**
      * Sends the HTTP headers to the client.
      */
@@ -346,11 +346,11 @@ interface NginxHTTPRequest {
      * @param options Subrequest options.
      * @param callback Completion callback.
      */
-    subrequest(uri: NjsStringLike, options: NginxSubrequestOptions & { detached: true }): void;
-    subrequest(uri: NjsStringLike, options?: NginxSubrequestOptions | string): Promise<NginxHTTPRequest>;
-    subrequest(uri: NjsStringLike, options: NginxSubrequestOptions & { detached?: false } | string,
+    subrequest(uri: NjsStringOrBuffer, options: NginxSubrequestOptions & { detached: true }): void;
+    subrequest(uri: NjsStringOrBuffer, options?: NginxSubrequestOptions | string): Promise<NginxHTTPRequest>;
+    subrequest(uri: NjsStringOrBuffer, options: NginxSubrequestOptions & { detached?: false } | string,
                callback:(reply:NginxHTTPRequest) => void): void;
-    subrequest(uri: NjsStringLike, callback:(reply:NginxHTTPRequest) => void): void;
+    subrequest(uri: NjsStringOrBuffer, callback:(reply:NginxHTTPRequest) => void): void;
     /**
      * Current URI in request, normalized.
      */
@@ -363,5 +363,5 @@ interface NginxHTTPRequest {
      * Writes a string to the error log on the warn level of logging.
      * @param message Message to log.
      */
-    warn(message: NjsStringLike): void;
+    warn(message: NjsStringOrBuffer): void;
 }
diff -r 434f20c29f4c -r 5bd78c74777a ts/ngx_stream_js_module.d.ts
--- a/ts/ngx_stream_js_module.d.ts	Wed Nov 25 10:47:47 2020 +0000
+++ b/ts/ngx_stream_js_module.d.ts	Thu Nov 26 11:10:59 2020 +0000
@@ -110,12 +110,12 @@ interface NginxStreamRequest {
      * Writes a string to the error log on the error level of logging.
      * @param message Message to log.
      */
-    error(message: NjsStringLike): void;
+    error(message: NjsStringOrBuffer): void;
     /**
      * Writes a string to the error log on the info level of logging.
      * @param message Message to log.
      */
-    log(message: NjsStringLike): void;
+    log(message: NjsStringOrBuffer): void;
     /**
      * Unregisters the callback set by on() method.
      */
@@ -135,7 +135,7 @@ interface NginxStreamRequest {
      * @param options Object used to override nginx buffer flags derived from
      * an incoming data chunk buffer.
      */
-    send(data: NjsStringLike, options?: NginxStreamSendOptions): void;
+    send(data: NjsStringOrBuffer, options?: NginxStreamSendOptions): void;
     /**
      * nginx variables object.
      */
@@ -144,5 +144,5 @@ interface NginxStreamRequest {
      * Writes a string to the error log on the warn level of logging.
      * @param message Message to log.
      */
-    warn(message: NjsStringLike): void;
+    warn(message: NjsStringOrBuffer): void;
 }
diff -r 434f20c29f4c -r 5bd78c74777a ts/njs_core.d.ts
--- a/ts/njs_core.d.ts	Wed Nov 25 10:47:47 2020 +0000
+++ b/ts/njs_core.d.ts	Thu Nov 26 11:10:59 2020 +0000
@@ -584,6 +584,7 @@ declare class Buffer extends Uint8Array 
     writeFloatLE(value: number, offset?: number): number;
 }
 
+type NjsStringOrBuffer = NjsStringLike | Buffer | DataView | TypedArray;
 
 // Global objects
 
diff -r 434f20c29f4c -r 5bd78c74777a ts/njs_modules/crypto.d.ts
--- a/ts/njs_modules/crypto.d.ts	Wed Nov 25 10:47:47 2020 +0000
+++ b/ts/njs_modules/crypto.d.ts	Thu Nov 26 11:10:59 2020 +0000
@@ -10,7 +10,7 @@ declare module "crypto" {
         /**
          * Updates the hash content with the given `data` and returns self.
          */
-        update(data: NjsStringLike | Buffer | DataView | TypedArray): Hash;
+        update(data: NjsStringOrBuffer): Hash;
 
         /**
          * Calculates the digest of all of the data passed using `hash.update()`.
@@ -31,7 +31,7 @@ declare module "crypto" {
         /**
          * Updates the HMAC content with the given `data` and returns self.
          */
-        update(data: NjsStringLike | Buffer | DataView | TypedArray): Hmac;
+        update(data: NjsStringOrBuffer): Hmac;
 
         /**
          * Calculates the HMAC digest of all of the data passed using `hmac.update()`.
@@ -65,7 +65,7 @@ declare module "crypto" {
          * @param key The secret key.
          * @returns An `HMAC` object.
          */
-        createHmac(algorithm: Algorithm, key: NjsStringLike): Hmac;
+        createHmac(algorithm: Algorithm, key: NjsStringOrBuffer): Hmac;
     }
 
     const crypto: Crypto;
diff -r 434f20c29f4c -r 5bd78c74777a ts/njs_modules/fs.d.ts
--- a/ts/njs_modules/fs.d.ts	Wed Nov 25 10:47:47 2020 +0000
+++ b/ts/njs_modules/fs.d.ts	Thu Nov 26 11:10:59 2020 +0000
@@ -124,7 +124,7 @@ declare module "fs" {
          *   If `mode` is not supplied, the default of `0o666` is used.
          *   If `flag` is not supplied, the default of `'a'` is used.
          */
-        appendFile(path: PathLike, data: NjsStringLike | Buffer, options?: WriteFileOptions): Promise<void>;
+        appendFile(path: PathLike, data: NjsStringOrBuffer, options?: WriteFileOptions): Promise<void>;
 
         /**
          * Asynchronously creates a directory at the specified `path`.
@@ -219,7 +219,7 @@ declare module "fs" {
          *   If `mode` is not supplied, the default of `0o666` is used.
          *   If `flag` is not supplied, the default of `'w'` is used.
          */
-        writeFile(path: PathLike, data: NjsStringLike | Buffer, options?: WriteFileOptions): Promise<void>;
+        writeFile(path: PathLike, data: NjsStringOrBuffer, options?: WriteFileOptions): Promise<void>;
     }
 
     interface NjsFS {
@@ -264,7 +264,7 @@ declare module "fs" {
          *   If `mode` is not supplied, the default of `0o666` is used.
          *   If `flag` is not supplied, the default of `'a'` is used.
          */
-        appendFileSync(path: PathLike, data: NjsStringLike | Buffer, options?: WriteFileOptions): void;
+        appendFileSync(path: PathLike, data: NjsStringOrBuffer, options?: WriteFileOptions): void;
 
         /**
          * Synchronously creates a directory at the specified `path`.
@@ -373,7 +373,7 @@ declare module "fs" {
          *   If `mode` is not supplied, the default of `0o666` is used.
          *   If `flag` is not supplied, the default of `'w'` is used.
          */
-        writeFileSync(path: PathLike, data: NjsStringLike | Buffer, options?: WriteFileOptions): void;
+        writeFileSync(path: PathLike, data: NjsStringOrBuffer, options?: WriteFileOptions): void;
     }
 
     const fs: NjsFS;


More information about the nginx-devel mailing list