@azure/core-xml 1.0.1-alpha.20210930.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +40 -0
- package/dist/index.js +117 -0
- package/dist/index.js.map +1 -0
- package/dist-esm/src/index.js +5 -0
- package/dist-esm/src/index.js.map +1 -0
- package/dist-esm/src/xml.browser.js +177 -0
- package/dist-esm/src/xml.browser.js.map +1 -0
- package/dist-esm/src/xml.common.js +11 -0
- package/dist-esm/src/xml.common.js.map +1 -0
- package/dist-esm/src/xml.js +98 -0
- package/dist-esm/src/xml.js.map +1 -0
- package/package.json +107 -0
- package/types/3.1/core-xml.d.ts +40 -0
- package/types/latest/core-xml.d.ts +45 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Release History
|
|
2
|
+
|
|
3
|
+
## 1.0.1 (Unreleased)
|
|
4
|
+
|
|
5
|
+
### Features Added
|
|
6
|
+
|
|
7
|
+
### Breaking Changes
|
|
8
|
+
|
|
9
|
+
### Bugs Fixed
|
|
10
|
+
|
|
11
|
+
### Other Changes
|
|
12
|
+
|
|
13
|
+
## 1.0.0 (2021-08-05)
|
|
14
|
+
|
|
15
|
+
GA release of this package.
|
|
16
|
+
|
|
17
|
+
## 1.0.0-beta.1 (2021-02-04)
|
|
18
|
+
|
|
19
|
+
- First release of package. Exported XML helpers for client packages that need XML support, see README.md for details.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Microsoft
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Azure Core XML client library for JavaScript (Experimental)
|
|
2
|
+
|
|
3
|
+
This library is primarily intended to be used in code generated by [AutoRest](https://github.com/Azure/Autorest) and [`autorest.typescript`](https://github.com/Azure/autorest.typescript) for APIs that require parsing XML payloads.
|
|
4
|
+
|
|
5
|
+
## Getting started
|
|
6
|
+
|
|
7
|
+
### Requirements
|
|
8
|
+
|
|
9
|
+
### Currently supported environments
|
|
10
|
+
|
|
11
|
+
- [LTS versions of Node.js](https://nodejs.org/about/releases/)
|
|
12
|
+
- Latest versions of Safari, Chrome, Edge, and Firefox.
|
|
13
|
+
|
|
14
|
+
See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details.
|
|
15
|
+
|
|
16
|
+
### Installation
|
|
17
|
+
|
|
18
|
+
This package is primarily used in generated code and not meant to be consumed directly by end users.
|
|
19
|
+
|
|
20
|
+
## Key concepts
|
|
21
|
+
|
|
22
|
+
XML parsing is mostly delegated to the browser and `xml2js`.
|
|
23
|
+
|
|
24
|
+
## Examples
|
|
25
|
+
|
|
26
|
+
Examples can be found in the `samples` folder.
|
|
27
|
+
|
|
28
|
+
## Next steps
|
|
29
|
+
|
|
30
|
+
See `@azure/core-client` for actual usage.
|
|
31
|
+
|
|
32
|
+
## Troubleshooting
|
|
33
|
+
|
|
34
|
+
If you run into issues while using this library, please feel free to [file an issue](https://github.com/Azure/azure-sdk-for-js/issues/new).
|
|
35
|
+
|
|
36
|
+
## Contributing
|
|
37
|
+
|
|
38
|
+
If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.
|
|
39
|
+
|
|
40
|
+

|
package/dist/index.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var xml2js = require('xml2js');
|
|
6
|
+
|
|
7
|
+
// Copyright (c) Microsoft Corporation.
|
|
8
|
+
// Licensed under the MIT license.
|
|
9
|
+
/**
|
|
10
|
+
* Default key used to access the XML attributes.
|
|
11
|
+
*/
|
|
12
|
+
const XML_ATTRKEY = "$";
|
|
13
|
+
/**
|
|
14
|
+
* Default key used to access the XML value content.
|
|
15
|
+
*/
|
|
16
|
+
const XML_CHARKEY = "_";
|
|
17
|
+
|
|
18
|
+
// Copyright (c) Microsoft Corporation.
|
|
19
|
+
// Note: The reason we re-define all of the xml2js default settings (version 2.0) here is because the default settings object exposed
|
|
20
|
+
// by the xm2js library is mutable. See https://github.com/Leonidas-from-XIV/node-xml2js/issues/536
|
|
21
|
+
// By creating a new copy of the settings each time we instantiate the parser,
|
|
22
|
+
// we are safeguarding against the possibility of the default settings being mutated elsewhere unintentionally.
|
|
23
|
+
const xml2jsDefaultOptionsV2 = {
|
|
24
|
+
explicitCharkey: false,
|
|
25
|
+
trim: false,
|
|
26
|
+
normalize: false,
|
|
27
|
+
normalizeTags: false,
|
|
28
|
+
attrkey: XML_ATTRKEY,
|
|
29
|
+
explicitArray: true,
|
|
30
|
+
ignoreAttrs: false,
|
|
31
|
+
mergeAttrs: false,
|
|
32
|
+
explicitRoot: true,
|
|
33
|
+
validator: undefined,
|
|
34
|
+
xmlns: false,
|
|
35
|
+
explicitChildren: false,
|
|
36
|
+
preserveChildrenOrder: false,
|
|
37
|
+
childkey: "$$",
|
|
38
|
+
charsAsChildren: false,
|
|
39
|
+
includeWhiteChars: false,
|
|
40
|
+
async: false,
|
|
41
|
+
strict: true,
|
|
42
|
+
attrNameProcessors: undefined,
|
|
43
|
+
attrValueProcessors: undefined,
|
|
44
|
+
tagNameProcessors: undefined,
|
|
45
|
+
valueProcessors: undefined,
|
|
46
|
+
rootName: "root",
|
|
47
|
+
xmldec: {
|
|
48
|
+
version: "1.0",
|
|
49
|
+
encoding: "UTF-8",
|
|
50
|
+
standalone: true
|
|
51
|
+
},
|
|
52
|
+
doctype: undefined,
|
|
53
|
+
renderOpts: {
|
|
54
|
+
pretty: true,
|
|
55
|
+
indent: " ",
|
|
56
|
+
newline: "\n"
|
|
57
|
+
},
|
|
58
|
+
headless: false,
|
|
59
|
+
chunkSize: 10000,
|
|
60
|
+
emptyTag: "",
|
|
61
|
+
cdata: false
|
|
62
|
+
};
|
|
63
|
+
// The xml2js settings for general XML parsing operations.
|
|
64
|
+
const xml2jsParserSettings = Object.assign({}, xml2jsDefaultOptionsV2);
|
|
65
|
+
xml2jsParserSettings.explicitArray = false;
|
|
66
|
+
// The xml2js settings for general XML building operations.
|
|
67
|
+
const xml2jsBuilderSettings = Object.assign({}, xml2jsDefaultOptionsV2);
|
|
68
|
+
xml2jsBuilderSettings.explicitArray = false;
|
|
69
|
+
xml2jsBuilderSettings.renderOpts = {
|
|
70
|
+
pretty: false
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* Converts given JSON object to XML string
|
|
74
|
+
* @param obj - JSON object to be converted into XML string
|
|
75
|
+
* @param opts - Options that govern the XML building of given JSON object
|
|
76
|
+
* `rootName` indicates the name of the root element in the resulting XML
|
|
77
|
+
*/
|
|
78
|
+
function stringifyXML(obj, opts = {}) {
|
|
79
|
+
var _a;
|
|
80
|
+
xml2jsBuilderSettings.rootName = opts.rootName;
|
|
81
|
+
xml2jsBuilderSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY;
|
|
82
|
+
const builder = new xml2js.Builder(xml2jsBuilderSettings);
|
|
83
|
+
return builder.buildObject(obj);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Converts given XML string into JSON
|
|
87
|
+
* @param str - String containing the XML content to be parsed into JSON
|
|
88
|
+
* @param opts - Options that govern the parsing of given xml string
|
|
89
|
+
* `includeRoot` indicates whether the root element is to be included or not in the output
|
|
90
|
+
*/
|
|
91
|
+
function parseXML(str, opts = {}) {
|
|
92
|
+
var _a;
|
|
93
|
+
xml2jsParserSettings.explicitRoot = !!opts.includeRoot;
|
|
94
|
+
xml2jsParserSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY;
|
|
95
|
+
const xmlParser = new xml2js.Parser(xml2jsParserSettings);
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
if (!str) {
|
|
98
|
+
reject(new Error("Document is empty"));
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
xmlParser.parseString(str, (err, res) => {
|
|
102
|
+
if (err) {
|
|
103
|
+
reject(err);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
resolve(res);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
exports.XML_ATTRKEY = XML_ATTRKEY;
|
|
114
|
+
exports.XML_CHARKEY = XML_CHARKEY;
|
|
115
|
+
exports.parseXML = parseXML;
|
|
116
|
+
exports.stringifyXML = stringifyXML;
|
|
117
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/xml.common.ts","../src/xml.ts"],"sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Default key used to access the XML attributes.\n */\nexport const XML_ATTRKEY = \"$\";\n/**\n * Default key used to access the XML value content.\n */\nexport const XML_CHARKEY = \"_\";\n\n/**\n * Options to govern behavior of xml parser and builder.\n */\nexport interface XmlOptions {\n /**\n * indicates the name of the root element in the resulting XML when building XML.\n */\n rootName?: string;\n /**\n * indicates whether the root element is to be included or not in the output when parsing XML.\n */\n includeRoot?: boolean;\n /**\n * key used to access the XML value content when parsing XML.\n */\n xmlCharKey?: string;\n}\n","// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport * as xml2js from \"xml2js\";\nimport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common\";\n\n// Note: The reason we re-define all of the xml2js default settings (version 2.0) here is because the default settings object exposed\n// by the xm2js library is mutable. See https://github.com/Leonidas-from-XIV/node-xml2js/issues/536\n// By creating a new copy of the settings each time we instantiate the parser,\n// we are safeguarding against the possibility of the default settings being mutated elsewhere unintentionally.\nconst xml2jsDefaultOptionsV2: xml2js.OptionsV2 = {\n explicitCharkey: false,\n trim: false,\n normalize: false,\n normalizeTags: false,\n attrkey: XML_ATTRKEY,\n explicitArray: true,\n ignoreAttrs: false,\n mergeAttrs: false,\n explicitRoot: true,\n validator: undefined,\n xmlns: false,\n explicitChildren: false,\n preserveChildrenOrder: false,\n childkey: \"$$\",\n charsAsChildren: false,\n includeWhiteChars: false,\n async: false,\n strict: true,\n attrNameProcessors: undefined,\n attrValueProcessors: undefined,\n tagNameProcessors: undefined,\n valueProcessors: undefined,\n rootName: \"root\",\n xmldec: {\n version: \"1.0\",\n encoding: \"UTF-8\",\n standalone: true\n },\n doctype: undefined,\n renderOpts: {\n pretty: true,\n indent: \" \",\n newline: \"\\n\"\n },\n headless: false,\n chunkSize: 10000,\n emptyTag: \"\",\n cdata: false\n};\n\n// The xml2js settings for general XML parsing operations.\nconst xml2jsParserSettings: any = Object.assign({}, xml2jsDefaultOptionsV2);\nxml2jsParserSettings.explicitArray = false;\n\n// The xml2js settings for general XML building operations.\nconst xml2jsBuilderSettings: any = Object.assign({}, xml2jsDefaultOptionsV2);\nxml2jsBuilderSettings.explicitArray = false;\nxml2jsBuilderSettings.renderOpts = {\n pretty: false\n};\n\n/**\n * Converts given JSON object to XML string\n * @param obj - JSON object to be converted into XML string\n * @param opts - Options that govern the XML building of given JSON object\n * `rootName` indicates the name of the root element in the resulting XML\n */\nexport function stringifyXML(obj: unknown, opts: XmlOptions = {}): string {\n xml2jsBuilderSettings.rootName = opts.rootName;\n xml2jsBuilderSettings.charkey = opts.xmlCharKey ?? XML_CHARKEY;\n const builder = new xml2js.Builder(xml2jsBuilderSettings);\n return builder.buildObject(obj);\n}\n\n/**\n * Converts given XML string into JSON\n * @param str - String containing the XML content to be parsed into JSON\n * @param opts - Options that govern the parsing of given xml string\n * `includeRoot` indicates whether the root element is to be included or not in the output\n */\nexport function parseXML(str: string, opts: XmlOptions = {}): Promise<any> {\n xml2jsParserSettings.explicitRoot = !!opts.includeRoot;\n xml2jsParserSettings.charkey = opts.xmlCharKey ?? XML_CHARKEY;\n const xmlParser = new xml2js.Parser(xml2jsParserSettings);\n return new Promise((resolve, reject) => {\n if (!str) {\n reject(new Error(\"Document is empty\"));\n } else {\n xmlParser.parseString(str, (err?: Error, res?: any) => {\n if (err) {\n reject(err);\n } else {\n resolve(res);\n }\n });\n }\n });\n}\n"],"names":["xml2js.Builder","xml2js.Parser"],"mappings":";;;;;;AAAA;AACA;AAEA;;;MAGa,WAAW,GAAG,IAAI;AAC/B;;;MAGa,WAAW,GAAG;;ACV3B;AACA,AAKA;AACA;AACA;AACA;AACA,MAAM,sBAAsB,GAAqB;IAC/C,eAAe,EAAE,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AC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nexport { stringifyXML, parseXML } from \"./xml\";\nexport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common\";\n"]}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
/// <reference lib="dom"/>
|
|
4
|
+
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common";
|
|
5
|
+
if (!document || !DOMParser || !Node || !XMLSerializer) {
|
|
6
|
+
throw new Error(`This library depends on the following DOM objects: ["document", "DOMParser", "Node", "XMLSerializer"] to parse XML, but some of these are undefined. You may provide a polyfill to make these globally available in order to support your environment. For more information, please refer to https://aka.ms/azsdk/js/web-workers. `);
|
|
7
|
+
}
|
|
8
|
+
const doc = document.implementation.createDocument(null, null, null);
|
|
9
|
+
const parser = new DOMParser();
|
|
10
|
+
export function parseXML(str, opts = {}) {
|
|
11
|
+
var _a, _b, _c;
|
|
12
|
+
try {
|
|
13
|
+
const updatedOptions = {
|
|
14
|
+
rootName: (_a = opts.rootName) !== null && _a !== void 0 ? _a : "",
|
|
15
|
+
includeRoot: (_b = opts.includeRoot) !== null && _b !== void 0 ? _b : false,
|
|
16
|
+
xmlCharKey: (_c = opts.xmlCharKey) !== null && _c !== void 0 ? _c : XML_CHARKEY
|
|
17
|
+
};
|
|
18
|
+
const dom = parser.parseFromString(str, "application/xml");
|
|
19
|
+
throwIfError(dom);
|
|
20
|
+
let obj;
|
|
21
|
+
if (updatedOptions.includeRoot) {
|
|
22
|
+
obj = domToObject(dom, updatedOptions);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
obj = domToObject(dom.childNodes[0], updatedOptions);
|
|
26
|
+
}
|
|
27
|
+
return Promise.resolve(obj);
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
return Promise.reject(err);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
let errorNS;
|
|
34
|
+
function getErrorNamespace() {
|
|
35
|
+
var _a;
|
|
36
|
+
if (errorNS === undefined) {
|
|
37
|
+
try {
|
|
38
|
+
errorNS =
|
|
39
|
+
(_a = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0]
|
|
40
|
+
.namespaceURI) !== null && _a !== void 0 ? _a : "";
|
|
41
|
+
}
|
|
42
|
+
catch (ignored) {
|
|
43
|
+
// Most browsers will return a document containing <parsererror>, but IE will throw.
|
|
44
|
+
errorNS = "";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return errorNS;
|
|
48
|
+
}
|
|
49
|
+
function throwIfError(dom) {
|
|
50
|
+
const parserErrors = dom.getElementsByTagName("parsererror");
|
|
51
|
+
if (parserErrors.length > 0 && getErrorNamespace()) {
|
|
52
|
+
for (let i = 0; i < parserErrors.length; i++) {
|
|
53
|
+
if (parserErrors[i].namespaceURI === errorNS) {
|
|
54
|
+
throw new Error(parserErrors[i].innerHTML);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function isElement(node) {
|
|
60
|
+
return !!node.attributes;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Get the Element-typed version of the provided Node if the provided node is an element with
|
|
64
|
+
* attributes. If it isn't, then undefined is returned.
|
|
65
|
+
*/
|
|
66
|
+
function asElementWithAttributes(node) {
|
|
67
|
+
return isElement(node) && node.hasAttributes() ? node : undefined;
|
|
68
|
+
}
|
|
69
|
+
function domToObject(node, options) {
|
|
70
|
+
let result = {};
|
|
71
|
+
const childNodeCount = node.childNodes.length;
|
|
72
|
+
const firstChildNode = node.childNodes[0];
|
|
73
|
+
const onlyChildTextValue = (firstChildNode &&
|
|
74
|
+
childNodeCount === 1 &&
|
|
75
|
+
firstChildNode.nodeType === Node.TEXT_NODE &&
|
|
76
|
+
firstChildNode.nodeValue) ||
|
|
77
|
+
undefined;
|
|
78
|
+
const elementWithAttributes = asElementWithAttributes(node);
|
|
79
|
+
if (elementWithAttributes) {
|
|
80
|
+
result[XML_ATTRKEY] = {};
|
|
81
|
+
for (let i = 0; i < elementWithAttributes.attributes.length; i++) {
|
|
82
|
+
const attr = elementWithAttributes.attributes[i];
|
|
83
|
+
result[XML_ATTRKEY][attr.nodeName] = attr.nodeValue;
|
|
84
|
+
}
|
|
85
|
+
if (onlyChildTextValue) {
|
|
86
|
+
result[options.xmlCharKey] = onlyChildTextValue;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (childNodeCount === 0) {
|
|
90
|
+
result = "";
|
|
91
|
+
}
|
|
92
|
+
else if (onlyChildTextValue) {
|
|
93
|
+
result = onlyChildTextValue;
|
|
94
|
+
}
|
|
95
|
+
if (!onlyChildTextValue) {
|
|
96
|
+
for (let i = 0; i < childNodeCount; i++) {
|
|
97
|
+
const child = node.childNodes[i];
|
|
98
|
+
// Ignore leading/trailing whitespace nodes
|
|
99
|
+
if (child.nodeType !== Node.TEXT_NODE) {
|
|
100
|
+
const childObject = domToObject(child, options);
|
|
101
|
+
if (!result[child.nodeName]) {
|
|
102
|
+
result[child.nodeName] = childObject;
|
|
103
|
+
}
|
|
104
|
+
else if (Array.isArray(result[child.nodeName])) {
|
|
105
|
+
result[child.nodeName].push(childObject);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
result[child.nodeName] = [result[child.nodeName], childObject];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return result;
|
|
114
|
+
}
|
|
115
|
+
const serializer = new XMLSerializer();
|
|
116
|
+
export function stringifyXML(content, opts = {}) {
|
|
117
|
+
var _a, _b, _c;
|
|
118
|
+
const updatedOptions = {
|
|
119
|
+
rootName: (_a = opts.rootName) !== null && _a !== void 0 ? _a : "root",
|
|
120
|
+
includeRoot: (_b = opts.includeRoot) !== null && _b !== void 0 ? _b : false,
|
|
121
|
+
xmlCharKey: (_c = opts.xmlCharKey) !== null && _c !== void 0 ? _c : XML_CHARKEY
|
|
122
|
+
};
|
|
123
|
+
const dom = buildNode(content, updatedOptions.rootName, updatedOptions)[0];
|
|
124
|
+
return ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' + serializer.serializeToString(dom));
|
|
125
|
+
}
|
|
126
|
+
function buildAttributes(attrs) {
|
|
127
|
+
const result = [];
|
|
128
|
+
for (const key of Object.keys(attrs)) {
|
|
129
|
+
const attr = doc.createAttribute(key);
|
|
130
|
+
attr.value = attrs[key].toString();
|
|
131
|
+
result.push(attr);
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
function buildNode(obj, elementName, options) {
|
|
136
|
+
if (obj === undefined ||
|
|
137
|
+
obj === null ||
|
|
138
|
+
typeof obj === "string" ||
|
|
139
|
+
typeof obj === "number" ||
|
|
140
|
+
typeof obj === "boolean") {
|
|
141
|
+
const elem = doc.createElement(elementName);
|
|
142
|
+
elem.textContent = obj === undefined || obj === null ? "" : obj.toString();
|
|
143
|
+
return [elem];
|
|
144
|
+
}
|
|
145
|
+
else if (Array.isArray(obj)) {
|
|
146
|
+
const result = [];
|
|
147
|
+
for (const arrayElem of obj) {
|
|
148
|
+
for (const child of buildNode(arrayElem, elementName, options)) {
|
|
149
|
+
result.push(child);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
else if (typeof obj === "object") {
|
|
155
|
+
const elem = doc.createElement(elementName);
|
|
156
|
+
for (const key of Object.keys(obj)) {
|
|
157
|
+
if (key === XML_ATTRKEY) {
|
|
158
|
+
for (const attr of buildAttributes(obj[key])) {
|
|
159
|
+
elem.attributes.setNamedItem(attr);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
else if (key === options.xmlCharKey) {
|
|
163
|
+
elem.textContent = obj[key].toString();
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
for (const child of buildNode(obj[key], key, options)) {
|
|
167
|
+
elem.appendChild(child);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
return [elem];
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
throw new Error(`Illegal value passed to buildObject: ${obj}`);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
//# sourceMappingURL=xml.browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xml.browser.js","sourceRoot":"","sources":["../../src/xml.browser.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,0BAA0B;AAC1B,OAAO,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,cAAc,CAAC;AAEpE,IAAI,CAAC,QAAQ,IAAI,CAAC,SAAS,IAAI,CAAC,IAAI,IAAI,CAAC,aAAa,EAAE;IACtD,MAAM,IAAI,KAAK,CACb,oUAAoU,CACrU,CAAC;CACH;AAED,MAAM,GAAG,GAAG,QAAQ,CAAC,cAAc,CAAC,cAAc,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAErE,MAAM,MAAM,GAAG,IAAI,SAAS,EAAE,CAAC;AAC/B,MAAM,UAAU,QAAQ,CAAC,GAAW,EAAE,OAAmB,EAAE;;IACzD,IAAI;QACF,MAAM,cAAc,GAAyB;YAC3C,QAAQ,EAAE,MAAA,IAAI,CAAC,QAAQ,mCAAI,EAAE;YAC7B,WAAW,EAAE,MAAA,IAAI,CAAC,WAAW,mCAAI,KAAK;YACtC,UAAU,EAAE,MAAA,IAAI,CAAC,UAAU,mCAAI,WAAW;SAC3C,CAAC;QACF,MAAM,GAAG,GAAG,MAAM,CAAC,eAAe,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAC;QAC3D,YAAY,CAAC,GAAG,CAAC,CAAC;QAElB,IAAI,GAAG,CAAC;QACR,IAAI,cAAc,CAAC,WAAW,EAAE;YAC9B,GAAG,GAAG,WAAW,CAAC,GAAG,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AAG,GAAG,CAAC,aAAa,CAAC,WAAW,CAAC,CAAC;QAC5C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YAClC,IAAI,GAAG,KAAK,WAAW,EAAE;gBACvB,KAAK,MAAM,IAAI,IAAI,eAAe,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE;oBAC5C,IAAI,CAAC,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC;iBACpC;aACF;iBAAM,IAAI,GAAG,KAAK,OAAO,CAAC,UAAU,EAAE;gBACrC,IAAI,CAAC,WAAW,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,CAAC;aACxC;iBAAM;gBACL,KAAK,MAAM,KAAK,IAAI,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE;oBACrD,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;iBACzB;aACF;SACF;QACD,OAAO,CAAC,IAAI,CAAC,CAAC;KACf;SAAM;QACL,MAAM,IAAI,KAAK,CAAC,wCAAwC,GAAG,EAAE,CAAC,CAAC;KAChE;AACH,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/// <reference lib=\"dom\"/>\nimport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common\";\n\nif (!document || !DOMParser || !Node || !XMLSerializer) {\n throw new Error(\n `This library depends on the following DOM objects: [\"document\", \"DOMParser\", \"Node\", \"XMLSerializer\"] to parse XML, but some of these are undefined. You may provide a polyfill to make these globally available in order to support your environment. For more information, please refer to https://aka.ms/azsdk/js/web-workers. `\n );\n}\n\nconst doc = document.implementation.createDocument(null, null, null);\n\nconst parser = new DOMParser();\nexport function parseXML(str: string, opts: XmlOptions = {}): Promise<any> {\n try {\n const updatedOptions: Required<XmlOptions> = {\n rootName: opts.rootName ?? \"\",\n includeRoot: opts.includeRoot ?? false,\n xmlCharKey: opts.xmlCharKey ?? XML_CHARKEY\n };\n const dom = parser.parseFromString(str, \"application/xml\");\n throwIfError(dom);\n\n let obj;\n if (updatedOptions.includeRoot) {\n obj = domToObject(dom, updatedOptions);\n } else {\n obj = domToObject(dom.childNodes[0], updatedOptions);\n }\n\n return Promise.resolve(obj);\n } catch (err) {\n return Promise.reject(err);\n }\n}\n\nlet errorNS: string | undefined;\n\nfunction getErrorNamespace(): string {\n if (errorNS === undefined) {\n try {\n errorNS =\n parser.parseFromString(\"INVALID\", \"text/xml\").getElementsByTagName(\"parsererror\")[0]\n .namespaceURI! ?? \"\";\n } catch (ignored) {\n // Most browsers will return a document containing <parsererror>, but IE will throw.\n errorNS = \"\";\n }\n }\n\n return errorNS;\n}\n\nfunction throwIfError(dom: Document): void {\n const parserErrors = dom.getElementsByTagName(\"parsererror\");\n if (parserErrors.length > 0 && getErrorNamespace()) {\n for (let i = 0; i < parserErrors.length; i++) {\n if (parserErrors[i].namespaceURI === errorNS) {\n throw new Error(parserErrors[i].innerHTML);\n }\n }\n }\n}\n\nfunction isElement(node: Node): node is Element {\n return !!(node as Element).attributes;\n}\n\n/**\n * Get the Element-typed version of the provided Node if the provided node is an element with\n * attributes. If it isn't, then undefined is returned.\n */\nfunction asElementWithAttributes(node: Node): Element | undefined {\n return isElement(node) && node.hasAttributes() ? node : undefined;\n}\n\nfunction domToObject(node: Node, options: Required<XmlOptions>): any {\n let result: any = {};\n\n const childNodeCount: number = node.childNodes.length;\n\n const firstChildNode: Node = node.childNodes[0];\n const onlyChildTextValue: string | undefined =\n (firstChildNode &&\n childNodeCount === 1 &&\n firstChildNode.nodeType === Node.TEXT_NODE &&\n firstChildNode.nodeValue) ||\n undefined;\n\n const elementWithAttributes: Element | undefined = asElementWithAttributes(node);\n if (elementWithAttributes) {\n result[XML_ATTRKEY] = {};\n\n for (let i = 0; i < elementWithAttributes.attributes.length; i++) {\n const attr = elementWithAttributes.attributes[i];\n result[XML_ATTRKEY][attr.nodeName] = attr.nodeValue;\n }\n\n if (onlyChildTextValue) {\n result[options.xmlCharKey] = onlyChildTextValue;\n }\n } else if (childNodeCount === 0) {\n result = \"\";\n } else if (onlyChildTextValue) {\n result = onlyChildTextValue;\n }\n\n if (!onlyChildTextValue) {\n for (let i = 0; i < childNodeCount; i++) {\n const child = node.childNodes[i];\n // Ignore leading/trailing whitespace nodes\n if (child.nodeType !== Node.TEXT_NODE) {\n const childObject: any = domToObject(child, options);\n if (!result[child.nodeName]) {\n result[child.nodeName] = childObject;\n } else if (Array.isArray(result[child.nodeName])) {\n result[child.nodeName].push(childObject);\n } else {\n result[child.nodeName] = [result[child.nodeName], childObject];\n }\n }\n }\n }\n\n return result;\n}\n\nconst serializer = new XMLSerializer();\n\nexport function stringifyXML(content: unknown, opts: XmlOptions = {}): string {\n const updatedOptions: Required<XmlOptions> = {\n rootName: opts.rootName ?? \"root\",\n includeRoot: opts.includeRoot ?? false,\n xmlCharKey: opts.xmlCharKey ?? XML_CHARKEY\n };\n const dom = buildNode(content, updatedOptions.rootName, updatedOptions)[0];\n return (\n '<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>' + serializer.serializeToString(dom)\n );\n}\n\nfunction buildAttributes(attrs: { [key: string]: { toString(): string } }): Attr[] {\n const result = [];\n for (const key of Object.keys(attrs)) {\n const attr = doc.createAttribute(key);\n attr.value = attrs[key].toString();\n result.push(attr);\n }\n return result;\n}\n\nfunction buildNode(obj: any, elementName: string, options: Required<XmlOptions>): Node[] {\n if (\n obj === undefined ||\n obj === null ||\n typeof obj === \"string\" ||\n typeof obj === \"number\" ||\n typeof obj === \"boolean\"\n ) {\n const elem = doc.createElement(elementName);\n elem.textContent = obj === undefined || obj === null ? \"\" : obj.toString();\n return [elem];\n } else if (Array.isArray(obj)) {\n const result = [];\n for (const arrayElem of obj) {\n for (const child of buildNode(arrayElem, elementName, options)) {\n result.push(child);\n }\n }\n return result;\n } else if (typeof obj === \"object\") {\n const elem = doc.createElement(elementName);\n for (const key of Object.keys(obj)) {\n if (key === XML_ATTRKEY) {\n for (const attr of buildAttributes(obj[key])) {\n elem.attributes.setNamedItem(attr);\n }\n } else if (key === options.xmlCharKey) {\n elem.textContent = obj[key].toString();\n } else {\n for (const child of buildNode(obj[key], key, options)) {\n elem.appendChild(child);\n }\n }\n }\n return [elem];\n } else {\n throw new Error(`Illegal value passed to buildObject: ${obj}`);\n }\n}\n"]}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
/**
|
|
4
|
+
* Default key used to access the XML attributes.
|
|
5
|
+
*/
|
|
6
|
+
export const XML_ATTRKEY = "$";
|
|
7
|
+
/**
|
|
8
|
+
* Default key used to access the XML value content.
|
|
9
|
+
*/
|
|
10
|
+
export const XML_CHARKEY = "_";
|
|
11
|
+
//# sourceMappingURL=xml.common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xml.common.js","sourceRoot":"","sources":["../../src/xml.common.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC;AAC/B;;GAEG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,GAAG,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\n/**\n * Default key used to access the XML attributes.\n */\nexport const XML_ATTRKEY = \"$\";\n/**\n * Default key used to access the XML value content.\n */\nexport const XML_CHARKEY = \"_\";\n\n/**\n * Options to govern behavior of xml parser and builder.\n */\nexport interface XmlOptions {\n /**\n * indicates the name of the root element in the resulting XML when building XML.\n */\n rootName?: string;\n /**\n * indicates whether the root element is to be included or not in the output when parsing XML.\n */\n includeRoot?: boolean;\n /**\n * key used to access the XML value content when parsing XML.\n */\n xmlCharKey?: string;\n}\n"]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Copyright (c) Microsoft Corporation.
|
|
2
|
+
// Licensed under the MIT license.
|
|
3
|
+
import * as xml2js from "xml2js";
|
|
4
|
+
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common";
|
|
5
|
+
// Note: The reason we re-define all of the xml2js default settings (version 2.0) here is because the default settings object exposed
|
|
6
|
+
// by the xm2js library is mutable. See https://github.com/Leonidas-from-XIV/node-xml2js/issues/536
|
|
7
|
+
// By creating a new copy of the settings each time we instantiate the parser,
|
|
8
|
+
// we are safeguarding against the possibility of the default settings being mutated elsewhere unintentionally.
|
|
9
|
+
const xml2jsDefaultOptionsV2 = {
|
|
10
|
+
explicitCharkey: false,
|
|
11
|
+
trim: false,
|
|
12
|
+
normalize: false,
|
|
13
|
+
normalizeTags: false,
|
|
14
|
+
attrkey: XML_ATTRKEY,
|
|
15
|
+
explicitArray: true,
|
|
16
|
+
ignoreAttrs: false,
|
|
17
|
+
mergeAttrs: false,
|
|
18
|
+
explicitRoot: true,
|
|
19
|
+
validator: undefined,
|
|
20
|
+
xmlns: false,
|
|
21
|
+
explicitChildren: false,
|
|
22
|
+
preserveChildrenOrder: false,
|
|
23
|
+
childkey: "$$",
|
|
24
|
+
charsAsChildren: false,
|
|
25
|
+
includeWhiteChars: false,
|
|
26
|
+
async: false,
|
|
27
|
+
strict: true,
|
|
28
|
+
attrNameProcessors: undefined,
|
|
29
|
+
attrValueProcessors: undefined,
|
|
30
|
+
tagNameProcessors: undefined,
|
|
31
|
+
valueProcessors: undefined,
|
|
32
|
+
rootName: "root",
|
|
33
|
+
xmldec: {
|
|
34
|
+
version: "1.0",
|
|
35
|
+
encoding: "UTF-8",
|
|
36
|
+
standalone: true
|
|
37
|
+
},
|
|
38
|
+
doctype: undefined,
|
|
39
|
+
renderOpts: {
|
|
40
|
+
pretty: true,
|
|
41
|
+
indent: " ",
|
|
42
|
+
newline: "\n"
|
|
43
|
+
},
|
|
44
|
+
headless: false,
|
|
45
|
+
chunkSize: 10000,
|
|
46
|
+
emptyTag: "",
|
|
47
|
+
cdata: false
|
|
48
|
+
};
|
|
49
|
+
// The xml2js settings for general XML parsing operations.
|
|
50
|
+
const xml2jsParserSettings = Object.assign({}, xml2jsDefaultOptionsV2);
|
|
51
|
+
xml2jsParserSettings.explicitArray = false;
|
|
52
|
+
// The xml2js settings for general XML building operations.
|
|
53
|
+
const xml2jsBuilderSettings = Object.assign({}, xml2jsDefaultOptionsV2);
|
|
54
|
+
xml2jsBuilderSettings.explicitArray = false;
|
|
55
|
+
xml2jsBuilderSettings.renderOpts = {
|
|
56
|
+
pretty: false
|
|
57
|
+
};
|
|
58
|
+
/**
|
|
59
|
+
* Converts given JSON object to XML string
|
|
60
|
+
* @param obj - JSON object to be converted into XML string
|
|
61
|
+
* @param opts - Options that govern the XML building of given JSON object
|
|
62
|
+
* `rootName` indicates the name of the root element in the resulting XML
|
|
63
|
+
*/
|
|
64
|
+
export function stringifyXML(obj, opts = {}) {
|
|
65
|
+
var _a;
|
|
66
|
+
xml2jsBuilderSettings.rootName = opts.rootName;
|
|
67
|
+
xml2jsBuilderSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY;
|
|
68
|
+
const builder = new xml2js.Builder(xml2jsBuilderSettings);
|
|
69
|
+
return builder.buildObject(obj);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Converts given XML string into JSON
|
|
73
|
+
* @param str - String containing the XML content to be parsed into JSON
|
|
74
|
+
* @param opts - Options that govern the parsing of given xml string
|
|
75
|
+
* `includeRoot` indicates whether the root element is to be included or not in the output
|
|
76
|
+
*/
|
|
77
|
+
export function parseXML(str, opts = {}) {
|
|
78
|
+
var _a;
|
|
79
|
+
xml2jsParserSettings.explicitRoot = !!opts.includeRoot;
|
|
80
|
+
xml2jsParserSettings.charkey = (_a = opts.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY;
|
|
81
|
+
const xmlParser = new xml2js.Parser(xml2jsParserSettings);
|
|
82
|
+
return new Promise((resolve, reject) => {
|
|
83
|
+
if (!str) {
|
|
84
|
+
reject(new Error("Document is empty"));
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
xmlParser.parseString(str, (err, res) => {
|
|
88
|
+
if (err) {
|
|
89
|
+
reject(err);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
resolve(res);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=xml.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xml.js","sourceRoot":"","sources":["../../src/xml.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,KAAK,MAAM,MAAM,QAAQ,CAAC;AACjC,OAAO,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,cAAc,CAAC;AAEpE,qIAAqI;AACrI,mGAAmG;AACnG,8EAA8E;AAC9E,+GAA+G;AAC/G,MAAM,sBAAsB,GAAqB;IAC/C,eAAe,EAAE,KAAK;IACtB,IAAI,EAAE,KAAK;IACX,SAAS,EAAE,KAAK;IAChB,aAAa,EAAE,KAAK;IACpB,OAAO,EAAE,WAAW;IACpB,aAAa,EAAE,IAAI;IACnB,WAAW,EAAE,KAAK;IAClB,UAAU,EAAE,KAAK;IACjB,YAAY,EAAE,IAAI;IAClB,SAAS,EAAE,SAAS;IACpB,KAAK,EAAE,KAAK;IACZ,gBAAgB,EAAE,KAAK;IACvB,qBAAqB,EAAE,KAAK;IAC5B,QAAQ,EAAE,IAAI;IACd,eAAe,EAAE,KAAK;IACtB,iBAAiB,EAAE,KAAK;IACxB,KAAK,EAAE,KAAK;IACZ,MAAM,EAAE,IAAI;IACZ,kBAAkB,EAAE,SAAS;IAC7B,mBAAmB,EAAE,SAAS;IAC9B,iBAAiB,EAAE,SAAS;IAC5B,eAAe,EAAE,SAAS;IAC1B,QAAQ,EAAE,MAAM;IAChB,MAAM,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AAC;IAC9D,MAAM,SAAS,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;IAC1D,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACrC,IAAI,CAAC,GAAG,EAAE;YACR,MAAM,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC;SACxC;aAAM;YACL,SAAS,CAAC,WAAW,CAAC,GAAG,EAAE,CAAC,GAAW,EAAE,GAAS,EAAE,EAAE;gBACpD,IAAI,GAAG,EAAE;oBACP,MAAM,CAAC,GAAG,CAAC,CAAC;iBACb;qBAAM;oBACL,OAAO,CAAC,GAAG,CAAC,CAAC;iBACd;YACH,CAAC,CAAC,CAAC;SACJ;IACH,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport * as xml2js from \"xml2js\";\nimport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common\";\n\n// Note: The reason we re-define all of the xml2js default settings (version 2.0) here is because the default settings object exposed\n// by the xm2js library is mutable. See https://github.com/Leonidas-from-XIV/node-xml2js/issues/536\n// By creating a new copy of the settings each time we instantiate the parser,\n// we are safeguarding against the possibility of the default settings being mutated elsewhere unintentionally.\nconst xml2jsDefaultOptionsV2: xml2js.OptionsV2 = {\n explicitCharkey: false,\n trim: false,\n normalize: false,\n normalizeTags: false,\n attrkey: XML_ATTRKEY,\n explicitArray: true,\n ignoreAttrs: false,\n mergeAttrs: false,\n explicitRoot: true,\n validator: undefined,\n xmlns: false,\n explicitChildren: false,\n preserveChildrenOrder: false,\n childkey: \"$$\",\n charsAsChildren: false,\n includeWhiteChars: false,\n async: false,\n strict: true,\n attrNameProcessors: undefined,\n attrValueProcessors: undefined,\n tagNameProcessors: undefined,\n valueProcessors: undefined,\n rootName: \"root\",\n xmldec: {\n version: \"1.0\",\n encoding: \"UTF-8\",\n standalone: true\n },\n doctype: undefined,\n renderOpts: {\n pretty: true,\n indent: \" \",\n newline: \"\\n\"\n },\n headless: false,\n chunkSize: 10000,\n emptyTag: \"\",\n cdata: false\n};\n\n// The xml2js settings for general XML parsing operations.\nconst xml2jsParserSettings: any = Object.assign({}, xml2jsDefaultOptionsV2);\nxml2jsParserSettings.explicitArray = false;\n\n// The xml2js settings for general XML building operations.\nconst xml2jsBuilderSettings: any = Object.assign({}, xml2jsDefaultOptionsV2);\nxml2jsBuilderSettings.explicitArray = false;\nxml2jsBuilderSettings.renderOpts = {\n pretty: false\n};\n\n/**\n * Converts given JSON object to XML string\n * @param obj - JSON object to be converted into XML string\n * @param opts - Options that govern the XML building of given JSON object\n * `rootName` indicates the name of the root element in the resulting XML\n */\nexport function stringifyXML(obj: unknown, opts: XmlOptions = {}): string {\n xml2jsBuilderSettings.rootName = opts.rootName;\n xml2jsBuilderSettings.charkey = opts.xmlCharKey ?? XML_CHARKEY;\n const builder = new xml2js.Builder(xml2jsBuilderSettings);\n return builder.buildObject(obj);\n}\n\n/**\n * Converts given XML string into JSON\n * @param str - String containing the XML content to be parsed into JSON\n * @param opts - Options that govern the parsing of given xml string\n * `includeRoot` indicates whether the root element is to be included or not in the output\n */\nexport function parseXML(str: string, opts: XmlOptions = {}): Promise<any> {\n xml2jsParserSettings.explicitRoot = !!opts.includeRoot;\n xml2jsParserSettings.charkey = opts.xmlCharKey ?? XML_CHARKEY;\n const xmlParser = new xml2js.Parser(xml2jsParserSettings);\n return new Promise((resolve, reject) => {\n if (!str) {\n reject(new Error(\"Document is empty\"));\n } else {\n xmlParser.parseString(str, (err?: Error, res?: any) => {\n if (err) {\n reject(err);\n } else {\n resolve(res);\n }\n });\n }\n });\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@azure/core-xml",
|
|
3
|
+
"version": "1.0.1-alpha.20210930.2",
|
|
4
|
+
"description": "Core library for interacting with XML payloads",
|
|
5
|
+
"sdk-type": "client",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist-esm/src/index.js",
|
|
8
|
+
"browser": {
|
|
9
|
+
"./dist-esm/src/xml.js": "./dist-esm/src/xml.browser.js"
|
|
10
|
+
},
|
|
11
|
+
"types": "types/latest/core-xml.d.ts",
|
|
12
|
+
"typesVersions": {
|
|
13
|
+
"<3.6": {
|
|
14
|
+
"types/latest/*": [
|
|
15
|
+
"types/3.1/*"
|
|
16
|
+
]
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"scripts": {
|
|
20
|
+
"audit": "node ../../../common/scripts/rush-audit.js && rimraf node_modules package-lock.json && npm i --package-lock-only 2>&1 && npm audit",
|
|
21
|
+
"build:samples": "echo Obsolete",
|
|
22
|
+
"build:test": "tsc -p . && rollup -c 2>&1",
|
|
23
|
+
"build:types": "downlevel-dts types/latest/ types/3.1/",
|
|
24
|
+
"build": "npm run clean && tsc -p . && rollup -c 2>&1 && api-extractor run --local && npm run build:types",
|
|
25
|
+
"check-format": "prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"*.{js,json}\"",
|
|
26
|
+
"clean": "rimraf dist dist-* temp types *.tgz *.log",
|
|
27
|
+
"docs": "typedoc --excludePrivate --excludeNotExported --excludeExternals --stripInternal --mode file --out ./dist/docs ./src",
|
|
28
|
+
"execute:samples": "echo skipped",
|
|
29
|
+
"extract-api": "tsc -p . && api-extractor run --local",
|
|
30
|
+
"format": "prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
|
|
31
|
+
"integration-test:browser": "echo skipped",
|
|
32
|
+
"integration-test:node": "echo skipped",
|
|
33
|
+
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
|
|
34
|
+
"lint:fix": "eslint package.json api-extractor.json src test --ext .ts --fix --fix-type [problem,suggestion]",
|
|
35
|
+
"lint": "eslint package.json api-extractor.json src test --ext .ts",
|
|
36
|
+
"pack": "npm pack 2>&1",
|
|
37
|
+
"test:browser": "npm run clean && npm run build:test && npm run unit-test:browser && npm run integration-test:browser",
|
|
38
|
+
"test:node": "npm run clean && tsc -p . && npm run unit-test:node && npm run integration-test:node",
|
|
39
|
+
"test": "npm run clean && tsc -p . && npm run unit-test:node && rollup -c 2>&1 && npm run unit-test:browser && npm run integration-test",
|
|
40
|
+
"unit-test:browser": "karma start --single-run",
|
|
41
|
+
"unit-test:node": "mocha -r esm -r ts-node/register --reporter ../../../common/tools/mocha-multi-reporter.js --timeout 1200000 --full-trace --exclude \"test/**/browser/*.spec.ts\" \"test/**/*.spec.ts\"",
|
|
42
|
+
"unit-test": "npm run unit-test:node && npm run unit-test:browser"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist/",
|
|
46
|
+
"dist-esm/src/",
|
|
47
|
+
"types/latest/core-xml.d.ts",
|
|
48
|
+
"types/3.1/core-xml.d.ts",
|
|
49
|
+
"README.md",
|
|
50
|
+
"LICENSE"
|
|
51
|
+
],
|
|
52
|
+
"repository": "github:Azure/azure-sdk-for-js",
|
|
53
|
+
"keywords": [
|
|
54
|
+
"azure",
|
|
55
|
+
"cloud"
|
|
56
|
+
],
|
|
57
|
+
"author": "Microsoft Corporation",
|
|
58
|
+
"license": "MIT",
|
|
59
|
+
"bugs": {
|
|
60
|
+
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
|
|
61
|
+
},
|
|
62
|
+
"engines": {
|
|
63
|
+
"node": ">=12.0.0"
|
|
64
|
+
},
|
|
65
|
+
"homepage": "https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-xml/",
|
|
66
|
+
"sideEffects": false,
|
|
67
|
+
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
|
|
68
|
+
"dependencies": {
|
|
69
|
+
"tslib": "^2.2.0",
|
|
70
|
+
"xml2js": "^0.4.19"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"@azure/dev-tool": ">=1.0.0-alpha <1.0.0-alphb",
|
|
74
|
+
"@microsoft/api-extractor": "^7.18.11",
|
|
75
|
+
"@types/chai": "^4.1.6",
|
|
76
|
+
"@types/mocha": "^7.0.2",
|
|
77
|
+
"@types/node": "^12.0.0",
|
|
78
|
+
"@types/sinon": "^9.0.4",
|
|
79
|
+
"@types/xml2js": "^0.4.3",
|
|
80
|
+
"@azure/eslint-plugin-azure-sdk": ">=3.0.0-alpha <3.0.0-alphb",
|
|
81
|
+
"chai": "^4.2.0",
|
|
82
|
+
"downlevel-dts": "~0.4.0",
|
|
83
|
+
"cross-env": "^7.0.2",
|
|
84
|
+
"eslint": "^7.15.0",
|
|
85
|
+
"inherits": "^2.0.3",
|
|
86
|
+
"karma": "^6.2.0",
|
|
87
|
+
"karma-chrome-launcher": "^3.0.0",
|
|
88
|
+
"karma-coverage": "^2.0.0",
|
|
89
|
+
"karma-edge-launcher": "^0.4.2",
|
|
90
|
+
"karma-env-preprocessor": "^0.1.1",
|
|
91
|
+
"karma-firefox-launcher": "^1.1.0",
|
|
92
|
+
"karma-ie-launcher": "^1.0.0",
|
|
93
|
+
"karma-junit-reporter": "^2.0.1",
|
|
94
|
+
"karma-mocha": "^2.0.1",
|
|
95
|
+
"karma-mocha-reporter": "^2.2.5",
|
|
96
|
+
"karma-sourcemap-loader": "^0.3.8",
|
|
97
|
+
"mocha": "^7.1.1",
|
|
98
|
+
"mocha-junit-reporter": "^1.18.0",
|
|
99
|
+
"prettier": "^1.16.4",
|
|
100
|
+
"rimraf": "^3.0.0",
|
|
101
|
+
"rollup": "^1.16.3",
|
|
102
|
+
"sinon": "^9.0.2",
|
|
103
|
+
"typescript": "~4.2.0",
|
|
104
|
+
"util": "^0.12.1",
|
|
105
|
+
"typedoc": "0.15.2"
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts given XML string into JSON
|
|
3
|
+
* @param str - String containing the XML content to be parsed into JSON
|
|
4
|
+
* @param opts - Options that govern the parsing of given xml string
|
|
5
|
+
* `includeRoot` indicates whether the root element is to be included or not in the output
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>;
|
|
8
|
+
/**
|
|
9
|
+
* Converts given JSON object to XML string
|
|
10
|
+
* @param obj - JSON object to be converted into XML string
|
|
11
|
+
* @param opts - Options that govern the XML building of given JSON object
|
|
12
|
+
* `rootName` indicates the name of the root element in the resulting XML
|
|
13
|
+
*/
|
|
14
|
+
export declare function stringifyXML(obj: unknown, opts?: XmlOptions): string;
|
|
15
|
+
/**
|
|
16
|
+
* Default key used to access the XML attributes.
|
|
17
|
+
*/
|
|
18
|
+
export declare const XML_ATTRKEY = "$";
|
|
19
|
+
/**
|
|
20
|
+
* Default key used to access the XML value content.
|
|
21
|
+
*/
|
|
22
|
+
export declare const XML_CHARKEY = "_";
|
|
23
|
+
/**
|
|
24
|
+
* Options to govern behavior of xml parser and builder.
|
|
25
|
+
*/
|
|
26
|
+
export declare interface XmlOptions {
|
|
27
|
+
/**
|
|
28
|
+
* indicates the name of the root element in the resulting XML when building XML.
|
|
29
|
+
*/
|
|
30
|
+
rootName?: string;
|
|
31
|
+
/**
|
|
32
|
+
* indicates whether the root element is to be included or not in the output when parsing XML.
|
|
33
|
+
*/
|
|
34
|
+
includeRoot?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* key used to access the XML value content when parsing XML.
|
|
37
|
+
*/
|
|
38
|
+
xmlCharKey?: string;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts given XML string into JSON
|
|
3
|
+
* @param str - String containing the XML content to be parsed into JSON
|
|
4
|
+
* @param opts - Options that govern the parsing of given xml string
|
|
5
|
+
* `includeRoot` indicates whether the root element is to be included or not in the output
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Converts given JSON object to XML string
|
|
11
|
+
* @param obj - JSON object to be converted into XML string
|
|
12
|
+
* @param opts - Options that govern the XML building of given JSON object
|
|
13
|
+
* `rootName` indicates the name of the root element in the resulting XML
|
|
14
|
+
*/
|
|
15
|
+
export declare function stringifyXML(obj: unknown, opts?: XmlOptions): string;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Default key used to access the XML attributes.
|
|
19
|
+
*/
|
|
20
|
+
export declare const XML_ATTRKEY = "$";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Default key used to access the XML value content.
|
|
24
|
+
*/
|
|
25
|
+
export declare const XML_CHARKEY = "_";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Options to govern behavior of xml parser and builder.
|
|
29
|
+
*/
|
|
30
|
+
export declare interface XmlOptions {
|
|
31
|
+
/**
|
|
32
|
+
* indicates the name of the root element in the resulting XML when building XML.
|
|
33
|
+
*/
|
|
34
|
+
rootName?: string;
|
|
35
|
+
/**
|
|
36
|
+
* indicates whether the root element is to be included or not in the output when parsing XML.
|
|
37
|
+
*/
|
|
38
|
+
includeRoot?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* key used to access the XML value content when parsing XML.
|
|
41
|
+
*/
|
|
42
|
+
xmlCharKey?: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export { }
|