[njs] Types: added description for Buffer properties.

Dmitry Volyntsev xeioex at nginx.com
Mon Nov 30 18:13:38 UTC 2020


details:   https://hg.nginx.org/njs/rev/5a6d8e16591b
branches:  
changeset: 1577:5a6d8e16591b
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Nov 27 13:17:53 2020 +0000
description:
Types: added description for Buffer properties.

diffstat:

 test/ts/test.ts              |  20 ++++++++++++-
 ts/ngx_http_js_module.d.ts   |  61 +++++++++++++++++++++++++++++++++++++++++++-
 ts/ngx_stream_js_module.d.ts |  34 ++++++++++++++++++++++--
 3 files changed, 109 insertions(+), 6 deletions(-)

diffs (198 lines):

diff -r a8cb5f47bfea -r 5a6d8e16591b test/ts/test.ts
--- a/test/ts/test.ts	Fri Nov 27 12:28:44 2020 +0000
+++ b/test/ts/test.ts	Fri Nov 27 13:17:53 2020 +0000
@@ -44,8 +44,6 @@ function http_module(r: NginxHTTPRequest
     r.headersOut['Set-Cookie'] = ['aaa', 'bbb'];
     r.headersOut['Foo'] = ['aaa', 'bbb'];
 
-    r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"] ?? ''));
-
     // r.log
 
     r.log(bs);
@@ -57,7 +55,11 @@ function http_module(r: NginxHTTPRequest
     r.variables.a == 'a';
     r.variables.cookie_a = 'b';
 
+    // r.rawVariables
+    r.rawVariables.a?.equals(Buffer.from([1]));
+
     // r.subrequest
+    r.subrequest('/uri', reply => r.return(200, reply.headersOut["Location"] ?? ''));
     r.subrequest('/p/sub1').then(reply => r.return(reply.status));
     r.subrequest('/p/sub2', {method:'POST'}).then(reply => r.return(reply.status));
     vod = r.subrequest('/p/sub3', reply => r.return(reply.status));
@@ -66,6 +68,20 @@ function http_module(r: NginxHTTPRequest
     // 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 ?? ''))));
+
+    // r.requestText
+    r.requestText == 'a';
+    r.requestText?.startsWith('a');
+
+    // r.requestBuffer
+    r.requestBuffer?.equals(Buffer.from([1]));
+
+    // r.responseText
+    r.responseText == 'a';
+    r.responseText?.startsWith('a');
+
+    // r.responseBuffer
+    r.responseBuffer?.equals(Buffer.from([1]));
 }
 
 function fs_module() {
diff -r a8cb5f47bfea -r 5a6d8e16591b ts/ngx_http_js_module.d.ts
--- a/ts/ngx_http_js_module.d.ts	Fri Nov 27 12:28:44 2020 +0000
+++ b/ts/ngx_http_js_module.d.ts	Fri Nov 27 13:17:53 2020 +0000
@@ -233,6 +233,13 @@ interface NginxVariables {
     [prop: string]: NjsStringLike | undefined;
 }
 
+/**
+ * @since 0.5.0
+ */
+type NginxRawVariables = {
+    [K in keyof NginxVariables]: Buffer | undefined;
+};
+
 interface NginxSubrequestOptions {
     /**
      * Arguments string, by default an empty string is used.
@@ -310,11 +317,52 @@ interface NginxHTTPRequest {
      * To ensure that the client request body is in memory, its size should be
      * limited by client_max_body_size, and a sufficient buffer size should be set
      * using client_body_buffer_size. The property is available only in the js_content directive.
+     *
+     * @since 0.5.0
+     */
+    readonly requestBuffer?: Buffer;
+    /**
+     * The same as `requestBuffer`, but returns a string.
+     *
+     * **Warning:** It may convert bytes invalid in UTF-8 encoding into the replacement character.
+     *
+     * @see requestBuffer
+     * @since 0.5.0
+     */
+    readonly requestText?: NjsByteString;
+    /**
+     * The same as `requestBuffer`, but returns a string.
+     *
+     * **Warning:** It may convert bytes invalid in UTF-8 encoding into the replacement character.
+     *
+     * @see requestBuffer
+     * @see requestText
+     * @deprecated Use `requestText` or `requestBuffer` instead.
      */
     readonly requestBody?: NjsByteString;
     /**
      * Subrequest response body. The size of response body is limited by
      * the subrequest_output_buffer_size directive.
+     *
+     * @since 0.5.0
+     */
+    readonly responseBuffer?: Buffer;
+    /**
+     * The same as `responseBuffer`, but returns a string.
+     *
+     * **Warning:** It may convert bytes invalid in UTF-8 encoding into the replacement character.
+     *
+     * @see responseBuffer
+     */
+    readonly responseText?: NjsByteString;
+    /**
+     * The same as `responseBuffer`, but returns a string.
+     *
+     * **Warning:** It may convert bytes invalid in UTF-8 encoding into the replacement character.
+     *
+     * @see responseBuffer
+     * @see responseText
+     * @deprecated Use `responseText` or `responseBuffer` instead.
      */
     readonly responseBody?: NjsByteString;
     /**
@@ -357,7 +405,18 @@ interface NginxHTTPRequest {
      */
     readonly uri: NjsByteString;
     /**
-     * nginx variables object.
+     * nginx variables as Buffers.
+     *
+     * @since 0.5.0
+     * @see variables
+     */
+    readonly rawVariables: NginxRawVariables;
+    /**
+     * nginx variables as strings.
+     *
+     * **Warning:** Bytes invalid in UTF-8 encoding may be converted into the replacement character.
+     *
+     * @see rawVariables
      */
     readonly variables: NginxVariables;
     /**
diff -r a8cb5f47bfea -r 5a6d8e16591b ts/ngx_stream_js_module.d.ts
--- a/ts/ngx_stream_js_module.d.ts	Fri Nov 27 12:28:44 2020 +0000
+++ b/ts/ngx_stream_js_module.d.ts	Fri Nov 27 13:17:53 2020 +0000
@@ -70,6 +70,13 @@ interface NginxStreamVariables {
     [prop: string]: NjsByteString | undefined;
 }
 
+/**
+ * @since 0.5.0
+ */
+type NginxStreamRawVariables = {
+    [K in keyof NginxStreamVariables]: Buffer | undefined;
+};
+
 interface NginxStreamCallbackFlags {
     /**
      * True if data is a last buffer.
@@ -119,13 +126,23 @@ interface NginxStreamRequest {
     log(message: NjsStringOrBuffer): void;
     /**
      * Unregisters the callback set by on() method.
+     * @param event Event type to unregister.
      */
-    off(event: "upload" | "download"): void;
+    off(event: "upload" | "download" | "upstream" | "downstream"): void;
     /**
      * Registers a callback for the specified event.
+     * @param event Event type to register. The callback data value type
+     * depends on the event type. For "upload" | "download" the data type is string.
+     * For "upstream" | "downstream" the data type is Buffer.
+     * String and buffer events cannot be mixed for a single session.
+     *
+     * **Warning:** For string data type bytes invalid in UTF-8 encoding may be
+     * converted into the replacement character.
      */
     on(event: "upload" | "download",
-       callback:(data:NjsByteString,  flags: NginxStreamCallbackFlags) => void): void;
+       callback: (data: NjsByteString, flags: NginxStreamCallbackFlags) => void): void;
+    on(event: "upstream" | "downstream",
+       callback: (data: Buffer, flags: NginxStreamCallbackFlags) => void): void;
     /**
      * Client address.
      */
@@ -138,7 +155,18 @@ interface NginxStreamRequest {
      */
     send(data: NjsStringOrBuffer, options?: NginxStreamSendOptions): void;
     /**
-     * nginx variables object.
+     * nginx variables as Buffers.
+     *
+     * @since 0.5.0
+     * @see variables
+     */
+    readonly rawVariables: NginxStreamRawVariables;
+    /**
+     * nginx variables as strings.
+     *
+     * **Warning:** Bytes invalid in UTF-8 encoding may be converted into the replacement character.
+     *
+     * @see rawVariables
      */
     readonly variables: NginxStreamVariables;
     /**


More information about the nginx-devel mailing list