From 61cee826abab5fee9577a4dc6ec2c00a91c9b838 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Thu, 26 Nov 2020 11:10:59 +0000 Subject: [PATCH] Types: extending data types for methods with NjsStringLike args. --- 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(-) diff --git a/test/ts/test.ts b/test/ts/test.ts index 30d92dab..9a079a0c 100644 --- a/test/ts/test.ts +++ b/test/ts/test.ts @@ -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 --git a/ts/ngx_http_js_module.d.ts b/ts/ngx_http_js_module.d.ts index 93364034..92833d8a 100644 --- a/ts/ngx_http_js_module.d.ts +++ b/ts/ngx_http_js_module.d.ts @@ -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; - subrequest(uri: NjsStringLike, options: NginxSubrequestOptions & { detached?: false } | string, + subrequest(uri: NjsStringOrBuffer, options: NginxSubrequestOptions & { detached: true }): void; + subrequest(uri: NjsStringOrBuffer, options?: NginxSubrequestOptions | string): Promise; + 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 --git a/ts/ngx_stream_js_module.d.ts b/ts/ngx_stream_js_module.d.ts index 46aa76dd..3e01b91d 100644 --- a/ts/ngx_stream_js_module.d.ts +++ b/ts/ngx_stream_js_module.d.ts @@ -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 --git a/ts/njs_core.d.ts b/ts/njs_core.d.ts index 8783b3be..ae6d07c5 100644 --- a/ts/njs_core.d.ts +++ b/ts/njs_core.d.ts @@ -584,6 +584,7 @@ declare class Buffer extends Uint8Array { writeFloatLE(value: number, offset?: number): number; } +type NjsStringOrBuffer = NjsStringLike | Buffer | DataView | TypedArray; // Global objects diff --git a/ts/njs_modules/crypto.d.ts b/ts/njs_modules/crypto.d.ts index 345a2d1a..ea554f34 100644 --- a/ts/njs_modules/crypto.d.ts +++ b/ts/njs_modules/crypto.d.ts @@ -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 --git a/ts/njs_modules/fs.d.ts b/ts/njs_modules/fs.d.ts index cc24f243..a7a4589a 100644 --- a/ts/njs_modules/fs.d.ts +++ b/ts/njs_modules/fs.d.ts @@ -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; + appendFile(path: PathLike, data: NjsStringOrBuffer, options?: WriteFileOptions): Promise; /** * 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; + writeFile(path: PathLike, data: NjsStringOrBuffer, options?: WriteFileOptions): Promise; } 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; -- 2.47.3