@azure/core-xml 1.1.0 → 1.2.0-alpha.20220125.3
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 +7 -0
- package/dist/index.js +14 -7
- package/dist/index.js.map +1 -1
- package/dist-esm/src/xml.js +15 -8
- package/dist-esm/src/xml.js.map +1 -1
- package/package.json +5 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# Release History
|
|
2
2
|
|
|
3
|
+
## 1.2.0 (Unreleased)
|
|
4
|
+
|
|
5
|
+
### Other Changes
|
|
6
|
+
|
|
7
|
+
- Fix root version node handling difference with fxp v3. [#20035](https://github.com/Azure/azure-sdk-for-js/pull/20035)
|
|
8
|
+
- Upgrade to `fast-xml-parser` to v4 [PR# 19898](https://github.com/Azure/azure-sdk-for-js/pull/19898)
|
|
9
|
+
|
|
3
10
|
## 1.1.0 (2022-01-06)
|
|
4
11
|
|
|
5
12
|
### Other Changes
|
package/dist/index.js
CHANGED
|
@@ -19,17 +19,18 @@ const XML_CHARKEY = "_";
|
|
|
19
19
|
function getCommonOptions(options) {
|
|
20
20
|
var _a;
|
|
21
21
|
return {
|
|
22
|
-
|
|
22
|
+
attributesGroupName: XML_ATTRKEY,
|
|
23
23
|
textNodeName: (_a = options.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY,
|
|
24
24
|
ignoreAttributes: false,
|
|
25
|
+
suppressBooleanAttributes: false,
|
|
25
26
|
};
|
|
26
27
|
}
|
|
27
28
|
function getSerializerOptions(options = {}) {
|
|
28
29
|
var _a;
|
|
29
|
-
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true,
|
|
30
|
+
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true, suppressEmptyNode: true, indentBy: "", rootNodeName: (_a = options.rootName) !== null && _a !== void 0 ? _a : "root" });
|
|
30
31
|
}
|
|
31
32
|
function getParserOptions(options = {}) {
|
|
32
|
-
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false,
|
|
33
|
+
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false, parseTagValue: false, attributeNamePrefix: "" });
|
|
33
34
|
}
|
|
34
35
|
/**
|
|
35
36
|
* Converts given JSON object to XML string
|
|
@@ -39,9 +40,9 @@ function getParserOptions(options = {}) {
|
|
|
39
40
|
*/
|
|
40
41
|
function stringifyXML(obj, opts = {}) {
|
|
41
42
|
const parserOptions = getSerializerOptions(opts);
|
|
42
|
-
const j2x = new fastXmlParser.
|
|
43
|
+
const j2x = new fastXmlParser.XMLBuilder(parserOptions);
|
|
43
44
|
const node = { [parserOptions.rootNodeName]: obj };
|
|
44
|
-
const xmlData = j2x.
|
|
45
|
+
const xmlData = j2x.build(node);
|
|
45
46
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
|
|
46
47
|
}
|
|
47
48
|
/**
|
|
@@ -54,11 +55,17 @@ async function parseXML(str, opts = {}) {
|
|
|
54
55
|
if (!str) {
|
|
55
56
|
throw new Error("Document is empty");
|
|
56
57
|
}
|
|
57
|
-
const validation = fastXmlParser.validate(str);
|
|
58
|
+
const validation = fastXmlParser.XMLValidator.validate(str);
|
|
58
59
|
if (validation !== true) {
|
|
59
60
|
throw validation;
|
|
60
61
|
}
|
|
61
|
-
const
|
|
62
|
+
const parser = new fastXmlParser.XMLParser(getParserOptions(opts));
|
|
63
|
+
const parsedXml = parser.parse(unescapeHTML(str));
|
|
64
|
+
// Remove the <?xml version="..." ?> node.
|
|
65
|
+
// This is a change in behavior on fxp v4. Issue #424
|
|
66
|
+
if (parsedXml["?xml"]) {
|
|
67
|
+
delete parsedXml["?xml"];
|
|
68
|
+
}
|
|
62
69
|
if (!opts.includeRoot) {
|
|
63
70
|
for (const key of Object.keys(parsedXml)) {
|
|
64
71
|
const value = parsedXml[key];
|
package/dist/index.js.map
CHANGED
|
@@ -1 +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 {
|
|
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 { XMLBuilder, XMLValidator, XMLParser } from \"fast-xml-parser\";\nimport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common\";\n\nfunction getCommonOptions(options: XmlOptions) {\n return {\n attributesGroupName: XML_ATTRKEY,\n textNodeName: options.xmlCharKey ?? XML_CHARKEY,\n ignoreAttributes: false,\n suppressBooleanAttributes: false,\n };\n}\n\nfunction getSerializerOptions(options: XmlOptions = {}) {\n return {\n ...getCommonOptions(options),\n attributeNamePrefix: \"@_\",\n format: true,\n suppressEmptyNode: true,\n indentBy: \"\",\n rootNodeName: options.rootName ?? \"root\",\n };\n}\n\nfunction getParserOptions(options: XmlOptions = {}) {\n return {\n ...getCommonOptions(options),\n parseAttributeValue: false,\n parseTagValue: false,\n attributeNamePrefix: \"\",\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 const parserOptions = getSerializerOptions(opts);\n const j2x = new XMLBuilder(parserOptions);\n\n const node = { [parserOptions.rootNodeName]: obj };\n\n const xmlData: string = j2x.build(node);\n return `<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>${xmlData}`.replace(/\\n/g, \"\");\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 async function parseXML(str: string, opts: XmlOptions = {}): Promise<any> {\n if (!str) {\n throw new Error(\"Document is empty\");\n }\n\n const validation = XMLValidator.validate(str);\n\n if (validation !== true) {\n throw validation;\n }\n\n const parser = new XMLParser(getParserOptions(opts));\n const parsedXml = parser.parse(unescapeHTML(str));\n\n // Remove the <?xml version=\"...\" ?> node.\n // This is a change in behavior on fxp v4. Issue #424\n if (parsedXml[\"?xml\"]) {\n delete parsedXml[\"?xml\"];\n }\n\n if (!opts.includeRoot) {\n for (const key of Object.keys(parsedXml)) {\n const value = parsedXml[key];\n return typeof value === \"object\" ? { ...value } : value;\n }\n }\n\n return parsedXml;\n}\n\nfunction unescapeHTML(str: string) {\n return str\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/"/g, '\"');\n}\n"],"names":["XMLBuilder","XMLValidator","XMLParser"],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AAI,CAAC,GAAG,EAAE;QACR,MAAM,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC;KACtC;IAED,MAAM,UAAU,GAAGC,0BAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;IAE9C,IAAI,UAAU,KAAK,IAAI,EAAE;QACvB,MAAM,UAAU,CAAC;KAClB;IAED,MAAM,MAAM,GAAG,IAAIC,uBAAS,CAAC,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC;IACrD,MAAM,SAAS,GAAG,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC;;;IAIlD,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE;QACrB,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC;KAC1B;IAED,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;QACrB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;YACxC,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;YAC7B,OAAO,OAAO,KAAK,KAAK,QAAQ,qBAAQ,KAAK,IAAK,KAAK,CAAC;SACzD;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,YAAY,CAAC,GAAW;IAC/B,OAAO,GAAG;SACP,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC;SACtB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;AAC7B,CAAC;;;;;;;"}
|
package/dist-esm/src/xml.js
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
// Copyright (c) Microsoft Corporation.
|
|
2
2
|
// Licensed under the MIT license.
|
|
3
|
-
import {
|
|
3
|
+
import { XMLBuilder, XMLValidator, XMLParser } from "fast-xml-parser";
|
|
4
4
|
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common";
|
|
5
5
|
function getCommonOptions(options) {
|
|
6
6
|
var _a;
|
|
7
7
|
return {
|
|
8
|
-
|
|
8
|
+
attributesGroupName: XML_ATTRKEY,
|
|
9
9
|
textNodeName: (_a = options.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY,
|
|
10
10
|
ignoreAttributes: false,
|
|
11
|
+
suppressBooleanAttributes: false,
|
|
11
12
|
};
|
|
12
13
|
}
|
|
13
14
|
function getSerializerOptions(options = {}) {
|
|
14
15
|
var _a;
|
|
15
|
-
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true,
|
|
16
|
+
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true, suppressEmptyNode: true, indentBy: "", rootNodeName: (_a = options.rootName) !== null && _a !== void 0 ? _a : "root" });
|
|
16
17
|
}
|
|
17
18
|
function getParserOptions(options = {}) {
|
|
18
|
-
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false,
|
|
19
|
+
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false, parseTagValue: false, attributeNamePrefix: "" });
|
|
19
20
|
}
|
|
20
21
|
/**
|
|
21
22
|
* Converts given JSON object to XML string
|
|
@@ -25,9 +26,9 @@ function getParserOptions(options = {}) {
|
|
|
25
26
|
*/
|
|
26
27
|
export function stringifyXML(obj, opts = {}) {
|
|
27
28
|
const parserOptions = getSerializerOptions(opts);
|
|
28
|
-
const j2x = new
|
|
29
|
+
const j2x = new XMLBuilder(parserOptions);
|
|
29
30
|
const node = { [parserOptions.rootNodeName]: obj };
|
|
30
|
-
const xmlData = j2x.
|
|
31
|
+
const xmlData = j2x.build(node);
|
|
31
32
|
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
|
|
32
33
|
}
|
|
33
34
|
/**
|
|
@@ -40,11 +41,17 @@ export async function parseXML(str, opts = {}) {
|
|
|
40
41
|
if (!str) {
|
|
41
42
|
throw new Error("Document is empty");
|
|
42
43
|
}
|
|
43
|
-
const validation = validate(str);
|
|
44
|
+
const validation = XMLValidator.validate(str);
|
|
44
45
|
if (validation !== true) {
|
|
45
46
|
throw validation;
|
|
46
47
|
}
|
|
47
|
-
const
|
|
48
|
+
const parser = new XMLParser(getParserOptions(opts));
|
|
49
|
+
const parsedXml = parser.parse(unescapeHTML(str));
|
|
50
|
+
// Remove the <?xml version="..." ?> node.
|
|
51
|
+
// This is a change in behavior on fxp v4. Issue #424
|
|
52
|
+
if (parsedXml["?xml"]) {
|
|
53
|
+
delete parsedXml["?xml"];
|
|
54
|
+
}
|
|
48
55
|
if (!opts.includeRoot) {
|
|
49
56
|
for (const key of Object.keys(parsedXml)) {
|
|
50
57
|
const value = parsedXml[key];
|
package/dist-esm/src/xml.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"xml.js","sourceRoot":"","sources":["../../src/xml.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"xml.js","sourceRoot":"","sources":["../../src/xml.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AACtE,OAAO,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,cAAc,CAAC;AAEpE,SAAS,gBAAgB,CAAC,OAAmB;;IAC3C,OAAO;QACL,mBAAmB,EAAE,WAAW;QAChC,YAAY,EAAE,MAAA,OAAO,CAAC,UAAU,mCAAI,WAAW;QAC/C,gBAAgB,EAAE,KAAK;QACvB,yBAAyB,EAAE,KAAK;KACjC,CAAC;AACJ,CAAC;AAED,SAAS,oBAAoB,CAAC,UAAsB,EAAE;;IACpD,uCACK,gBAAgB,CAAC,OAAO,CAAC,KAC5B,mBAAmB,EAAE,IAAI,EACzB,MAAM,EAAE,IAAI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IACrD,IAAI,SAAS,CAAC,MAAM,CAAC,EAAE;QACrB,OAAO,SAAS,CAAC,MAAM,CAAC,CAAC;KAC1B;IAED,IAAI,CAAC,IAAI,CAAC,WAAW,EAAE;QACrB,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;YACxC,MAAM,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;YAC7B,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,mBAAM,KAAK,EAAG,CAAC,CAAC,KAAK,CAAC;SACzD;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,YAAY,CAAC,GAAW;IAC/B,OAAO,GAAG;SACP,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC;SACtB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;AAC7B,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT license.\n\nimport { XMLBuilder, XMLValidator, XMLParser } from \"fast-xml-parser\";\nimport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common\";\n\nfunction getCommonOptions(options: XmlOptions) {\n return {\n attributesGroupName: XML_ATTRKEY,\n textNodeName: options.xmlCharKey ?? XML_CHARKEY,\n ignoreAttributes: false,\n suppressBooleanAttributes: false,\n };\n}\n\nfunction getSerializerOptions(options: XmlOptions = {}) {\n return {\n ...getCommonOptions(options),\n attributeNamePrefix: \"@_\",\n format: true,\n suppressEmptyNode: true,\n indentBy: \"\",\n rootNodeName: options.rootName ?? \"root\",\n };\n}\n\nfunction getParserOptions(options: XmlOptions = {}) {\n return {\n ...getCommonOptions(options),\n parseAttributeValue: false,\n parseTagValue: false,\n attributeNamePrefix: \"\",\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 const parserOptions = getSerializerOptions(opts);\n const j2x = new XMLBuilder(parserOptions);\n\n const node = { [parserOptions.rootNodeName]: obj };\n\n const xmlData: string = j2x.build(node);\n return `<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>${xmlData}`.replace(/\\n/g, \"\");\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 async function parseXML(str: string, opts: XmlOptions = {}): Promise<any> {\n if (!str) {\n throw new Error(\"Document is empty\");\n }\n\n const validation = XMLValidator.validate(str);\n\n if (validation !== true) {\n throw validation;\n }\n\n const parser = new XMLParser(getParserOptions(opts));\n const parsedXml = parser.parse(unescapeHTML(str));\n\n // Remove the <?xml version=\"...\" ?> node.\n // This is a change in behavior on fxp v4. Issue #424\n if (parsedXml[\"?xml\"]) {\n delete parsedXml[\"?xml\"];\n }\n\n if (!opts.includeRoot) {\n for (const key of Object.keys(parsedXml)) {\n const value = parsedXml[key];\n return typeof value === \"object\" ? { ...value } : value;\n }\n }\n\n return parsedXml;\n}\n\nfunction unescapeHTML(str: string) {\n return str\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/"/g, '\"');\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azure/core-xml",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0-alpha.20220125.3",
|
|
4
4
|
"description": "Core library for interacting with XML payloads",
|
|
5
5
|
"sdk-type": "client",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -66,19 +66,19 @@
|
|
|
66
66
|
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
|
|
67
67
|
"dependencies": {
|
|
68
68
|
"tslib": "^2.2.0",
|
|
69
|
-
"fast-xml-parser": "^
|
|
69
|
+
"fast-xml-parser": "^4.0.1"
|
|
70
70
|
},
|
|
71
71
|
"devDependencies": {
|
|
72
|
-
"@azure/dev-tool": "
|
|
72
|
+
"@azure/dev-tool": ">=1.0.0-alpha <1.0.0-alphb",
|
|
73
73
|
"@microsoft/api-extractor": "^7.18.11",
|
|
74
74
|
"@types/chai": "^4.1.6",
|
|
75
75
|
"@types/mocha": "^7.0.2",
|
|
76
76
|
"@types/node": "^12.0.0",
|
|
77
77
|
"@types/sinon": "^9.0.4",
|
|
78
78
|
"@types/xml2js": "^0.4.3",
|
|
79
|
-
"@azure/eslint-plugin-azure-sdk": "
|
|
79
|
+
"@azure/eslint-plugin-azure-sdk": ">=3.0.0-alpha <3.0.0-alphb",
|
|
80
80
|
"chai": "^4.2.0",
|
|
81
|
-
"downlevel-dts": "
|
|
81
|
+
"downlevel-dts": "^0.8.0",
|
|
82
82
|
"cross-env": "^7.0.2",
|
|
83
83
|
"eslint": "^7.15.0",
|
|
84
84
|
"inherits": "^2.0.3",
|