]> git.kaiwu.me - njs.git/commitdiff
Types: added ts types for the xml API.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 2 Feb 2023 03:04:39 +0000 (19:04 -0800)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 2 Feb 2023 03:04:39 +0000 (19:04 -0800)
test/ts/package.json
test/ts/test.ts
ts/index.d.ts
ts/njs_modules/xml.d.ts [new file with mode: 0644]
ts/package.json

index 3daeea81ccdb0d6769427974388ef74d218dff6c..f20930172ab6afefcdd925c2fd667c40b00e5da3 100644 (file)
@@ -10,6 +10,6 @@
   "license": "BSD-2-Clause",
   "devDependencies": {
     "njs-types": "file:../../ts",
-    "typescript": "~4.0.3"
+    "typescript": "~4.9.5"
   }
 }
index 1780a21821d1dcf7dd756fdc1b989fa911ac51ab..cc3392935f27e2b4c152128d12587b0a0d1a993b 100644 (file)
@@ -1,6 +1,7 @@
 import fs from 'fs';
 import qs from 'querystring';
 import cr from 'crypto';
+import xml from 'xml';
 
 async function http_module(r: NginxHTTPRequest) {
     var bs: NjsByteString;
@@ -147,6 +148,25 @@ function qs_module(str: NjsByteString) {
     s = qs.stringify(o);
 }
 
+function xml_module(str: NjsByteString) {
+    let doc;
+    let node;
+    let children, selectedChildren;
+
+    doc = xml.parse(str);
+    node = doc.$root;
+
+    node.$ns;
+    children = node.$tags;
+    selectedChildren = node.$tags$xxx;
+
+    node?.xxx?.yyy?.$attr$zzz;
+
+    let buf:Buffer = xml.exclusiveC14n(node);
+    buf = xml.exclusiveC14n(doc, node.xxx, false);
+    buf = xml.exclusiveC14n(node, null, true, "aa bb");
+}
+
 function crypto_module(str: NjsByteString) {
     var h;
     var b:Buffer;
index eec5c3421a1de2b520a35ff01e800742272af0cd..bacac1eabd53bf6e70b22b8658b59fcdab778237 100644 (file)
@@ -2,4 +2,5 @@
 /// <reference path="njs_webcrypto.d.ts" />
 /// <reference path="njs_modules/crypto.d.ts" />
 /// <reference path="njs_modules/fs.d.ts" />
+/// <reference path="njs_modules/xml.d.ts" />
 /// <reference path="njs_modules/querystring.d.ts" />
diff --git a/ts/njs_modules/xml.d.ts b/ts/njs_modules/xml.d.ts
new file mode 100644 (file)
index 0000000..61c707a
--- /dev/null
@@ -0,0 +1,162 @@
+/// <reference path="../njs_core.d.ts" />
+
+declare module "xml" {
+
+    type XMLTagName =
+        | `_${string}`
+        | `a${string}`
+        | `b${string}`
+        | `c${string}`
+        | `d${string}`
+        | `e${string}`
+        | `f${string}`
+        | `g${string}`
+        | `h${string}`
+        | `i${string}`
+        | `j${string}`
+        | `k${string}`
+        | `l${string}`
+        | `m${string}`
+        | `n${string}`
+        | `o${string}`
+        | `p${string}`
+        | `q${string}`
+        | `r${string}`
+        | `s${string}`
+        | `t${string}`
+        | `u${string}`
+        | `v${string}`
+        | `w${string}`
+        | `x${string}`
+        | `y${string}`
+        | `z${string}`
+        | `A${string}`
+        | `B${string}`
+        | `C${string}`
+        | `D${string}`
+        | `E${string}`
+        | `F${string}`
+        | `G${string}`
+        | `H${string}`
+        | `I${string}`
+        | `J${string}`
+        | `K${string}`
+        | `L${string}`
+        | `M${string}`
+        | `N${string}`
+        | `O${string}`
+        | `P${string}`
+        | `Q${string}`
+        | `R${string}`
+        | `S${string}`
+        | `T${string}`
+        | `U${string}`
+        | `V${string}`
+        | `W${string}`
+        | `X${string}`
+        | `Y${string}`
+        | `Z${string}`;
+
+    export interface XMLDoc {
+        /**
+         * The doc's root node.
+         */
+        readonly $root: XMLNode;
+
+        /**
+         * The doc's root by its name or undefined.
+         */
+        readonly [rootTagName: XMLTagName]: XMLNode | undefined;
+    }
+
+    export interface XMLNode {
+        /**
+         * node.$attr$xxx - the node's attribute value of "xxx".
+         */
+        readonly [key: `$attr$${string}`]: string | undefined;
+
+        /**
+         * node.$attrs - an XMLAttr wrapper object for all the attributes
+         * of the node.
+         */
+        readonly $attrs: XMLAttr;
+
+        /**
+         * node.$tag$xxx - the node's first child tag named "xxx".
+         */
+        readonly [key: `$tag$${string}`]: XMLNode | undefined;
+
+        /**
+         * node.$tags$xxx - all children tags named "xxx" of the node.
+         */
+        readonly [key: `$tags$${string}`]: XMLNode[] | undefined;
+
+        /**
+         * node.$name - the name of the node.
+         */
+        readonly $name: string;
+
+        /**
+         * node.$ns - the namespace of the node.
+         */
+        readonly $ns: string;
+
+        /**
+         * node.$parent - the parent node of the current node.
+         */
+        readonly $parent: string;
+
+        /**
+         * node.$text - the content of the node.
+         */
+        readonly $text: string;
+
+        /**
+         * node.$tags - all the node's children tags.
+         */
+        readonly $tags: XMLNode[] | undefined;
+
+        /**
+         * node.xxx is the same as node.$tag$xxx.
+         */
+        readonly [key: XMLTagName]: XMLNode | undefined;
+    }
+
+    export interface XMLAttr {
+        /**
+         * attr.xxx is the attribute value of "xxx".
+         */
+        readonly [key: string]: string | undefined;
+    }
+
+    interface Xml {
+        /**
+         * Parses src buffer for an XML document and returns a wrapper object.
+         *
+         * @param src a string or a buffer with an XML document.
+         * @return A XMLDoc wrapper object representing the parsed XML document.
+         */
+        parse(src: NjsStringLike): XMLDoc;
+
+        /**
+         * Canonicalizes root_node and its children according to
+         * https://www.w3.org/TR/xml-exc-c14n/.
+         *
+         * @param root - XMLDoc or XMLNode.
+         * @param excluding_node - allows to omit from the output a part of the
+         * document corresponding to the excluding_node and its children.
+         * @param withComments - a boolean (false by default). When withComments
+         * is true canonicalization corresponds to
+         * http://www.w3.org/2001/10/xml-exc-c14n#WithComments.
+         * @param prefix_list - an optional string with a space separated namespace
+         * prefixes for namespaces that should also be included into the output.
+         * @return Buffer object containing canonicalized output.
+         */
+        exclusiveC14n(root: XMLDoc | XMLNode, excluding_node?: XMLNode | null | undefined,
+                      withComments?: boolean, prefix_list?: string): Buffer;
+    }
+
+    const xml: Xml;
+
+    export default xml;
+}
index 2c8a8162851d10e9414db24ad2c1cb99637f0e14..cafe063711791680937035507120295e2fff087f 100644 (file)
@@ -26,6 +26,6 @@
   },
   "homepage": "https://nginx.org/en/docs/njs/",
   "devDependencies": {
-    "typescript": "^4.0.3"
+    "typescript": "^4.9.5"
   }
 }