]> git.kaiwu.me - njs.git/commitdiff
Types: extending data types for methods with NjsStringLike args.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 26 Nov 2020 11:10:59 +0000 (11:10 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 26 Nov 2020 11:10:59 +0000 (11:10 +0000)
test/ts/test.ts
ts/ngx_http_js_module.d.ts
ts/ngx_stream_js_module.d.ts
ts/njs_core.d.ts
ts/njs_modules/crypto.d.ts
ts/njs_modules/fs.d.ts

index 30d92dab63a432b8d33242da197d8b4690ec6841..9a079a0cc7f775ae5668033f4d64737670c1c4dd 100644 (file)
@@ -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) {
index 933640342cd87c7e96c96b4b343b5c1d891608d4..92833d8af30b3228effc54b0e229eb2ed48a90a3 100644 (file)
@@ -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;
 }
index 46aa76dd6bdcbbfa66558a42b5d2fa038ccf34eb..3e01b91db0170241450284b01a03705eb9e5d05c 100644 (file)
@@ -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;
 }
index 8783b3bee04342c7153a1df417a9e6096ef32ffb..ae6d07c5b547b16ad0486ba96c7196bef07f3bfb 100644 (file)
@@ -584,6 +584,7 @@ declare class Buffer extends Uint8Array {
     writeFloatLE(value: number, offset?: number): number;
 }
 
+type NjsStringOrBuffer = NjsStringLike | Buffer | DataView | TypedArray;
 
 // Global objects
 
index 345a2d1a757c853d12b6b53c7d0764095f2abd40..ea554f34767b4138574fdd249f722777d2193375 100644 (file)
@@ -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;
index cc24f243a62e1eaddf1f64b6fec3e2c91062893b..a7a4589a609a1b724fb6e409fe6845c276725b6f 100644 (file)
@@ -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;