From 08ae840fdca272bacef60812802abd3c9aa570c3 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 27 Nov 2020 13:17:53 +0000 Subject: [PATCH] Types: added description for Buffer properties. --- 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(-) diff --git a/test/ts/test.ts b/test/ts/test.ts index 42a6ae8a..4aae7812 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -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 --git a/ts/ngx_http_js_module.d.ts b/ts/ngx_http_js_module.d.ts index 239fa528..a7586bfe 100644 --- a/ts/ngx_http_js_module.d.ts +++ b/ts/ngx_http_js_module.d.ts @@ -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 --git a/ts/ngx_stream_js_module.d.ts b/ts/ngx_stream_js_module.d.ts index 3fedc32f..6e9d4049 100644 --- a/ts/ngx_stream_js_module.d.ts +++ b/ts/ngx_stream_js_module.d.ts @@ -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; /** -- 2.47.3