From: Dmitry Volyntsev Date: Tue, 9 Mar 2021 13:24:06 +0000 (+0000) Subject: Types: updated TS definitions. X-Git-Tag: 0.5.2~1 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=c91531f90474f62e6f2f537e2f6eeffbf43466d1;p=njs.git Types: updated TS definitions. --- diff --git a/test/ts/test.ts b/test/ts/test.ts index ef24d70f..4ec9db0c 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -93,6 +93,11 @@ function http_module(r: NginxHTTPRequest) { }) .then(body => r.return(200, body)) .catch(e => r.return(501, e.message)) + + // js_body_filter + r.sendBuffer(Buffer.from("xxx"), {last:true}); + r.sendBuffer("xxx", {flush: true}); + r.done(); } function fs_module() { @@ -150,6 +155,7 @@ function timers() { function njs_object() { njs.dump('asdf'); njs.version != process.argv[1]; + njs.on('exit', ()=> {}); } function ngx_object() { diff --git a/ts/ngx_http_js_module.d.ts b/ts/ngx_http_js_module.d.ts index a7586bfe..9ed3b92a 100644 --- a/ts/ngx_http_js_module.d.ts +++ b/ts/ngx_http_js_module.d.ts @@ -262,11 +262,31 @@ interface NginxSubrequestOptions { detached?: boolean } +interface NginxHTTPSendBufferOptions { + /** + * True if data is a last buffer. + */ + last?: boolean + /** + * True if the buffer should have the flush flag. + */ + flush?: boolean +} + interface NginxHTTPRequest { /** * Request arguments object. */ readonly args: NginxHTTPArgs; + /** + * After calling this function, next data chunks will be passed to + * the client without calling js_body_filter. + * + * **Warning:** May be called only from the js_body_filter function. + * + * @since 0.5.2 + */ + done(): void; /** * Writes a string to the error log on the error level of logging. * @param message Message to log. @@ -374,9 +394,22 @@ interface NginxHTTPRequest { */ return(status: number, body?: NjsStringOrBuffer): void; /** - * Sends the HTTP headers to the client. + * Sends a part of the response body to the client. */ send(part: NjsStringOrBuffer): void; + /** + * Adds data to the chain of data chunks to be forwarded to the next body filter. + * The actual forwarding happens later, when the all the data chunks of the current + * chain are processed. + * + * **Warning:** May be called only from the js_body_filter function. + * + * @since 0.5.2 + * @param data Data to send. + * @param options Object used to override nginx buffer flags derived from + * an incoming data chunk buffer. + */ + sendBuffer(data: NjsStringOrBuffer, options?: NginxHTTPSendBufferOptions): void; /** * Sends the HTTP headers to the client. */ diff --git a/ts/ngx_stream_js_module.d.ts b/ts/ngx_stream_js_module.d.ts index 6e9d4049..849ffce0 100644 --- a/ts/ngx_stream_js_module.d.ts +++ b/ts/ngx_stream_js_module.d.ts @@ -97,21 +97,44 @@ interface NginxStreamSendOptions { interface NginxStreamRequest { /** - * Successfully finalizes the phase handler. + * Successfully finalizes the phase handler. An alias to s.done(0). + * + * @since 0.2.4 + * @see done() */ allow(): void; /** - * Finalizes the phase handler and passes control to the next handler. + * Passing control to the next handler of the current phase (if any). + * An alias to s.done(-5). + * + * @since 0.2.4 + * @see done() */ decline(): void; /** * Finalizes the phase handler with the access error code. + * An alias to s.done(403). + * + * @since 0.2.4 + * @see done() */ deny(): void; /** - * Successfully finalizes the current phase handler - * or finalizes it with the specified numeric code. - * @param code Finalization code. + * Sets an exit code for the current phase handler to a code value. + * The actual finalization happens when the js handler is completed and + * all pending events, for example from ngx.fetch() or setTimeout(), + * are processed. + * + * @param code Finalization code, by default is 0. + * Possible code values: + * 0 - successful finalization, passing control to the next phase + * -5 - undecided, passing control to the next handler of the current + * phase (if any) + * 403 - access is forbidden + * @since 0.2.4 + * @see allow() + * @see decline() + * @see deny() */ done(code?: number): void; /** @@ -127,6 +150,7 @@ interface NginxStreamRequest { /** * Unregisters the callback set by on() method. * @param event Event type to unregister. + * @see on() */ off(event: "upload" | "download" | "upstream" | "downstream"): void; /** @@ -138,6 +162,7 @@ interface NginxStreamRequest { * * **Warning:** For string data type bytes invalid in UTF-8 encoding may be * converted into the replacement character. + * @see off() */ on(event: "upload" | "download", callback: (data: NjsByteString, flags: NginxStreamCallbackFlags) => void): void; @@ -148,12 +173,23 @@ interface NginxStreamRequest { */ readonly remoteAddress: NjsByteString; /** - * Sends the data to the client. + * Adds data to the chain of data chunks that will be forwarded in + * the forward direction: in download callback to a client; in upload + * to an upstream server. The actual forwarding happens later, when the all + * the data chunks of the current chain are processed. + * + * @since 0.2.4 * @param data Data to send. * @param options Object used to override nginx buffer flags derived from * an incoming data chunk buffer. + * @see on() */ send(data: NjsStringOrBuffer, options?: NginxStreamSendOptions): void; + /** + * The stream session exit status. It is an alias to the $status variable. + * @since 0.5.2 + */ + readonly status: number; /** * nginx variables as Buffers. * diff --git a/ts/njs_core.d.ts b/ts/njs_core.d.ts index 54172d11..808c08f2 100644 --- a/ts/njs_core.d.ts +++ b/ts/njs_core.d.ts @@ -591,6 +591,11 @@ type NjsStringOrBuffer = NjsStringLike | Buffer | DataView | TypedArray; interface NjsGlobal { readonly version: string; dump(value: any, indent?: number): string; + /** + * Registers a callback for the "exit" event. The callback is called before + * the VM is destroyed. + */ + on(event: "exit", callback: () => void): void; } declare const njs: NjsGlobal;