[njs] Added TypeScript description for crypto module.
Dmitry Volyntsev
xeioex at nginx.com
Tue Oct 20 15:50:27 UTC 2020
details: https://hg.nginx.org/njs/rev/11da5c960017
branches:
changeset: 1545:11da5c960017
user: Jakub Jirutka <jakub at jirutka.cz>
date: Fri Oct 09 17:28:58 2020 +0200
description:
Added TypeScript description for crypto module.
diffstat:
src/ts/crypto.d.ts | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
test/ts/test.ts | 14 +++++++++
2 files changed, 90 insertions(+), 0 deletions(-)
diffs (114 lines):
diff -r e7fb6e22acb7 -r 11da5c960017 src/ts/crypto.d.ts
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ts/crypto.d.ts Fri Oct 09 17:28:58 2020 +0200
@@ -0,0 +1,76 @@
+/// <reference path="njs_core.d.ts" />
+
+declare module "crypto" {
+
+ export type Algorithm = "md5" | "sha1" | "sha256";
+
+ export type DigestEncoding = Exclude<BufferEncoding, "utf8">;
+
+ export interface Hash {
+ /**
+ * Updates the hash content with the given `data` and returns self.
+ */
+ update(data: NjsStringLike | Buffer | DataView | TypedArray): Hash;
+
+ /**
+ * Calculates the digest of all of the data passed using `hash.update()`.
+ *
+ * @example
+ * import cr from 'crypto'
+ * cr.createHash('sha1').update('A').update('B').digest('base64url') // => 'BtlFlCqiamG-GMPiK_GbvKjdK10'
+ *
+ * @param encoding The encoding of the return value. If not provided, a `Buffer` object
+ * (or a byte string before version 0.4.4) is returned.
+ * @return A calculated digest.
+ */
+ digest(): Buffer;
+ digest(encoding: DigestEncoding): string;
+ }
+
+ export interface Hmac {
+ /**
+ * Updates the HMAC content with the given `data` and returns self.
+ */
+ update(data: NjsStringLike | Buffer | DataView | TypedArray): Hmac;
+
+ /**
+ * Calculates the HMAC digest of all of the data passed using `hmac.update()`.
+ *
+ * @example
+ * import cr from 'crypto'
+ * cr.createHmac('sha1', 'secret.key').update('AB').digest('base64url') // => 'Oglm93xn23_MkiaEq_e9u8zk374'
+ *
+ * @param encoding The encoding of the return value. If not provided, a `Buffer` object
+ * (or a byte string before version 0.4.4) is returned.
+ * @return The calculated HMAC digest.
+ */
+ digest(): Buffer;
+ digest(encoding: DigestEncoding): string;
+ }
+
+ interface Crypto {
+ /**
+ * Creates and returns a `Hash` object that can be used to generate hash digests using
+ * the given `algorithm`.
+ *
+ * @param algorithm `'md5'`, `'sha1'`, or `'sha256'`
+ * @returns A `Hash` object.
+ */
+ createHash(algorithm: Algorithm): Hash;
+
+ /**
+ * Creates and returns an HMAC object that uses the given `algorithm` and secret `key`.
+ *
+ * @param algorithm `'md5'`, `'sha1'`, or `'sha256'`
+ * @param key The secret key.
+ * @returns An `HMAC` object.
+ */
+ createHmac(algorithm: Algorithm, key: NjsStringLike): Hmac;
+ }
+
+ const crypto: Crypto;
+
+ // It's exported like this because njs doesn't support named imports.
+ // TODO: Replace NjsFS with individual named exports as soon as njs supports named imports.
+ export default crypto;
+}
diff -r e7fb6e22acb7 -r 11da5c960017 test/ts/test.ts
--- a/test/ts/test.ts Fri Oct 09 17:05:47 2020 +0200
+++ b/test/ts/test.ts Fri Oct 09 17:28:58 2020 +0200
@@ -1,9 +1,11 @@
/// <reference path="../../build/ts/ngx_http_js_module.d.ts" />
/// <reference path="../../build/ts/fs.d.ts" />
/// <reference path="../../build/ts/querystring.d.ts" />
+/// <reference path="../../build/ts/crypto.d.ts" />
import fs from 'fs';
import qs from 'querystring';
+import crypto from 'crypto';
function http_module(r: NginxHTTPRequest) {
var bs: NjsByteString;
@@ -81,6 +83,18 @@ function qs_module(str: NjsByteString) {
s = qs.stringify(o);
}
+function crypto_module(str: NjsByteString) {
+ var h;
+ var b:Buffer;
+ var s:string;
+
+ h = crypto.createHash("sha1");
+ h = h.update(str).update(Buffer.from([0]));
+ b = h.digest();
+
+ s = crypto.createHash("sha256").digest("hex");
+}
+
function buffer(b: Buffer) {
var s:string;
More information about the nginx-devel
mailing list