Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
Copyright (c) Microsoft Corporation.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+38
View File
@@ -0,0 +1,38 @@
# Azure Core XML client library for JavaScript
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.
## Getting started
### Requirements
### Currently supported environments
- [LTS versions of Node.js](https://github.com/nodejs/release#release-schedule)
- Latest versions of Safari, Chrome, Edge, and Firefox.
See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) for more details.
### Installation
This package is primarily used in generated code and not meant to be consumed directly by end users.
## Key concepts
XML parsing is mostly delegated to the browser and `xml2js`.
## Examples
Examples can be found in the `samples-dev` folder and can be ran using `rushx execute:samples`.
## Next steps
See `@azure/core-client` for actual usage.
## Troubleshooting
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).
## Contributing
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.
+3
View File
@@ -0,0 +1,3 @@
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from "./xml.common.js";
//# sourceMappingURL=index.d.ts.map
+5
View File
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,iBAAiB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { stringifyXML, parseXML } from \"./xml.js\";\nexport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common.js\";\n"]}
+3
View File
@@ -0,0 +1,3 @@
{
"type": "module"
}
File diff suppressed because one or more lines are too long
+34
View File
@@ -0,0 +1,34 @@
/**
* Default key used to access the XML attributes.
*/
export declare const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export declare const XML_CHARKEY = "_";
/**
* Options to govern behavior of xml parser and builder.
*/
export interface XmlOptions {
/**
* indicates the name of the root element in the resulting XML when building XML.
*/
rootName?: string;
/**
* indicates whether the root element is to be included or not in the output when parsing XML.
*/
includeRoot?: boolean;
/**
* key used to access the XML value content when parsing XML.
*/
xmlCharKey?: string;
/**
* property name for a CDATA section.
*/
cdataPropName?: string;
/**
* XML nodes to exclude from parsing.
*/
stopNodes?: string[];
}
//# sourceMappingURL=xml.common.d.ts.map
+11
View File
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Default key used to access the XML attributes.
*/
export const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export const XML_CHARKEY = "_";
//# sourceMappingURL=xml.common.js.map
+1
View File
@@ -0,0 +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 /**\n * property name for a CDATA section.\n */\n cdataPropName?: string;\n\n /**\n * XML nodes to exclude from parsing.\n */\n stopNodes?: string[];\n}\n"]}
+4
View File
@@ -0,0 +1,4 @@
import type { XmlOptions } from "./xml.common.js";
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>;
export declare function stringifyXML(content: unknown, opts?: XmlOptions): string;
//# sourceMappingURL=xml-browser.d.mts.map
+220
View File
@@ -0,0 +1,220 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
if (!document || !DOMParser || !Node || !XMLSerializer) {
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. `);
}
// Policy to make our code Trusted Types compliant at running time.
// https://github.com/w3c/webappsec-trusted-types
// We are calling DOMParser.parseFromString() to parse XML payload from Azure services.
// The parsed DOM object is not exposed to outside. Scripts are disabled when parsing
// according to the spec. There are no HTML/XSS security concerns on the usage of
// parseFromString() here.
let ttPolicy;
try {
if (typeof self.trustedTypes !== "undefined") {
ttPolicy = self.trustedTypes.createPolicy("@azure/core-xml#xml.browser", {
createHTML: (s) => s,
});
}
}
catch (e) {
console.warn('Could not create trusted types policy "@azure/core-xml#xml.browser"');
}
const doc = document.implementation.createDocument(null, null, null);
const parser = new DOMParser();
export function parseXML(str, opts = {}) {
var _a, _b, _c, _d, _e, _f;
try {
const updatedOptions = {
rootName: (_a = opts.rootName) !== null && _a !== void 0 ? _a : "",
includeRoot: (_b = opts.includeRoot) !== null && _b !== void 0 ? _b : false,
xmlCharKey: (_c = opts.xmlCharKey) !== null && _c !== void 0 ? _c : XML_CHARKEY,
cdataPropName: (_d = opts.cdataPropName) !== null && _d !== void 0 ? _d : "__cdata",
stopNodes: (_e = opts.stopNodes) !== null && _e !== void 0 ? _e : [],
};
const dom = parser.parseFromString(((_f = ttPolicy === null || ttPolicy === void 0 ? void 0 : ttPolicy.createHTML(str)) !== null && _f !== void 0 ? _f : str), "application/xml");
throwIfError(dom);
let obj;
if (updatedOptions.includeRoot) {
obj = domToObject(dom, updatedOptions);
}
else {
obj = domToObject(dom.childNodes[0], updatedOptions);
}
return Promise.resolve(obj);
}
catch (err) {
return Promise.reject(err);
}
}
let errorNS;
function getErrorNamespace() {
var _a, _b;
if (errorNS === undefined) {
try {
const invalidXML = ((_a = ttPolicy === null || ttPolicy === void 0 ? void 0 : ttPolicy.createHTML("INVALID")) !== null && _a !== void 0 ? _a : "INVALID");
errorNS =
(_b = parser.parseFromString(invalidXML, "text/xml").getElementsByTagName("parsererror")[0]
.namespaceURI) !== null && _b !== void 0 ? _b : "";
}
catch (ignored) {
// Most browsers will return a document containing <parsererror>, but IE will throw.
errorNS = "";
}
}
return errorNS;
}
function throwIfError(dom) {
const parserErrors = dom.getElementsByTagName("parsererror");
if (parserErrors.length > 0 && getErrorNamespace()) {
for (let i = 0; i < parserErrors.length; i++) {
if (parserErrors[i].namespaceURI === errorNS) {
throw new Error(parserErrors[i].innerHTML);
}
}
}
}
function isElement(node) {
return !!node.attributes;
}
/**
* Get the Element-typed version of the provided Node if the provided node is an element with
* attributes. If it isn't, then undefined is returned.
*/
function asElementWithAttributes(node) {
return isElement(node) && node.hasAttributes() ? node : undefined;
}
function domToObject(node, options) {
var _a;
let result = {};
const childNodeCount = node.childNodes.length;
const firstChildNode = node.childNodes[0];
const onlyChildTextValue = (firstChildNode &&
childNodeCount === 1 &&
firstChildNode.nodeType === Node.TEXT_NODE &&
firstChildNode.nodeValue) ||
undefined;
const elementWithAttributes = asElementWithAttributes(node);
if (elementWithAttributes) {
result[XML_ATTRKEY] = {};
for (let i = 0; i < elementWithAttributes.attributes.length; i++) {
const attr = elementWithAttributes.attributes[i];
result[XML_ATTRKEY][attr.nodeName] = attr.nodeValue;
}
if (onlyChildTextValue) {
result[options.xmlCharKey] = onlyChildTextValue;
}
}
else if (childNodeCount === 0) {
result = "";
}
else if (onlyChildTextValue) {
result = onlyChildTextValue;
}
if (!onlyChildTextValue) {
for (let i = 0; i < childNodeCount; i++) {
const child = node.childNodes[i];
// Check if CData
if ((child === null || child === void 0 ? void 0 : child.nodeType) === Node.CDATA_SECTION_NODE) {
// Already in the CDATA
result = child.textContent;
}
else if (((_a = child === null || child === void 0 ? void 0 : child.firstChild) === null || _a === void 0 ? void 0 : _a.nodeType) === Node.CDATA_SECTION_NODE) {
// Look if child is CDATA
result[child.nodeName] = child.textContent;
}
else if (child.nodeType !== Node.TEXT_NODE) {
// Ignore leading/trailing whitespace nodes
const childObject = domToObject(child, options);
if (!result[child.nodeName]) {
result[child.nodeName] = childObject;
}
else if (Array.isArray(result[child.nodeName])) {
result[child.nodeName].push(childObject);
}
else {
result[child.nodeName] = [result[child.nodeName], childObject];
}
}
}
}
return result;
}
const serializer = new XMLSerializer();
export function stringifyXML(content, opts = {}) {
var _a, _b, _c, _d, _e;
const updatedOptions = {
rootName: (_a = opts.rootName) !== null && _a !== void 0 ? _a : "root",
includeRoot: (_b = opts.includeRoot) !== null && _b !== void 0 ? _b : false,
xmlCharKey: (_c = opts.xmlCharKey) !== null && _c !== void 0 ? _c : XML_CHARKEY,
cdataPropName: (_d = opts.cdataPropName) !== null && _d !== void 0 ? _d : "__cdata",
stopNodes: (_e = opts.stopNodes) !== null && _e !== void 0 ? _e : [],
};
const dom = buildNode(content, updatedOptions.rootName, updatedOptions)[0];
return ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
serializer.serializeToString(dom).replace(/ xmlns=""/g, ""));
}
function buildAttributes(attrs) {
const result = [];
for (const key of Object.keys(attrs)) {
const attr = doc.createAttribute(key);
attr.value = attrs[key].toString();
result.push(attr);
}
return result;
}
function buildNode(obj, elementName, options) {
var _a;
if (obj === undefined ||
obj === null ||
typeof obj === "string" ||
typeof obj === "number" ||
typeof obj === "boolean") {
const elem = doc.createElement(elementName);
elem.textContent = obj === undefined || obj === null ? "" : obj.toString();
return [elem];
}
else if (Array.isArray(obj)) {
const result = [];
for (const arrayElem of obj) {
for (const child of buildNode(arrayElem, elementName, options)) {
result.push(child);
}
}
return result;
}
else if (typeof obj === "object") {
let elem;
if ((_a = obj[XML_ATTRKEY]) === null || _a === void 0 ? void 0 : _a["xmlns"]) {
elem = doc.createElementNS(obj[XML_ATTRKEY]["xmlns"], elementName);
}
else {
elem = doc.createElement(elementName);
}
for (const key of Object.keys(obj)) {
if (key === XML_ATTRKEY) {
for (const attr of buildAttributes(obj[key])) {
elem.attributes.setNamedItem(attr);
}
}
else if (key === options.xmlCharKey) {
elem.textContent = obj[key].toString();
}
else if (key === options.cdataPropName) {
const cdataElement = doc.createCDATASection(obj[key].toString());
elem.appendChild(cdataElement);
}
else {
for (const child of buildNode(obj[key], key, options)) {
elem.appendChild(child);
}
}
}
return [elem];
}
else {
throw new Error(`Illegal value passed to buildObject: ${obj}`);
}
}
//# sourceMappingURL=xml-browser.mjs.map
+3
View File
@@ -0,0 +1,3 @@
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from "./xml.common.js";
//# sourceMappingURL=index.d.ts.map
+12
View File
@@ -0,0 +1,12 @@
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.XML_CHARKEY = exports.XML_ATTRKEY = exports.parseXML = exports.stringifyXML = void 0;
var xml_js_1 = require("./xml.js");
Object.defineProperty(exports, "stringifyXML", { enumerable: true, get: function () { return xml_js_1.stringifyXML; } });
Object.defineProperty(exports, "parseXML", { enumerable: true, get: function () { return xml_js_1.parseXML; } });
var xml_common_js_1 = require("./xml.common.js");
Object.defineProperty(exports, "XML_ATTRKEY", { enumerable: true, get: function () { return xml_common_js_1.XML_ATTRKEY; } });
Object.defineProperty(exports, "XML_CHARKEY", { enumerable: true, get: function () { return xml_common_js_1.XML_CHARKEY; } });
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC,mCAAkD;AAAzC,sGAAA,YAAY,OAAA;AAAE,kGAAA,QAAQ,OAAA;AAC/B,iDAAuE;AAA9D,4GAAA,WAAW,OAAA;AAAE,4GAAA,WAAW,OAAA","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { stringifyXML, parseXML } from \"./xml.js\";\nexport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common.js\";\n"]}
+3
View File
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}
+11
View File
@@ -0,0 +1,11 @@
// This file is read by tools that parse documentation comments conforming to the TSDoc standard.
// It should be published with your NPM package. It should not be tracked by Git.
{
"tsdocVersion": "0.12",
"toolPackages": [
{
"packageName": "@microsoft/api-extractor",
"packageVersion": "7.50.1"
}
]
}
+34
View File
@@ -0,0 +1,34 @@
/**
* Default key used to access the XML attributes.
*/
export declare const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export declare const XML_CHARKEY = "_";
/**
* Options to govern behavior of xml parser and builder.
*/
export interface XmlOptions {
/**
* indicates the name of the root element in the resulting XML when building XML.
*/
rootName?: string;
/**
* indicates whether the root element is to be included or not in the output when parsing XML.
*/
includeRoot?: boolean;
/**
* key used to access the XML value content when parsing XML.
*/
xmlCharKey?: string;
/**
* property name for a CDATA section.
*/
cdataPropName?: string;
/**
* XML nodes to exclude from parsing.
*/
stopNodes?: string[];
}
//# sourceMappingURL=xml.common.d.ts.map
+14
View File
@@ -0,0 +1,14 @@
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.XML_CHARKEY = exports.XML_ATTRKEY = void 0;
/**
* Default key used to access the XML attributes.
*/
exports.XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
exports.XML_CHARKEY = "_";
//# sourceMappingURL=xml.common.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"xml.common.js","sourceRoot":"","sources":["../../src/xml.common.ts"],"names":[],"mappings":";AAAA,uCAAuC;AACvC,kCAAkC;;;AAElC;;GAEG;AACU,QAAA,WAAW,GAAG,GAAG,CAAC;AAC/B;;GAEG;AACU,QAAA,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 /**\n * property name for a CDATA section.\n */\n cdataPropName?: string;\n\n /**\n * XML nodes to exclude from parsing.\n */\n stopNodes?: string[];\n}\n"]}
+16
View File
@@ -0,0 +1,16 @@
import { type XmlOptions } from "./xml.common.js";
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
export declare function stringifyXML(obj: unknown, opts?: XmlOptions): string;
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>;
//# sourceMappingURL=xml.d.ts.map
+67
View File
@@ -0,0 +1,67 @@
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringifyXML = stringifyXML;
exports.parseXML = parseXML;
const fast_xml_parser_1 = require("fast-xml-parser");
const xml_common_js_1 = require("./xml.common.js");
function getCommonOptions(options) {
var _a;
return {
attributesGroupName: xml_common_js_1.XML_ATTRKEY,
textNodeName: (_a = options.xmlCharKey) !== null && _a !== void 0 ? _a : xml_common_js_1.XML_CHARKEY,
ignoreAttributes: false,
suppressBooleanAttributes: false,
};
}
function getSerializerOptions(options = {}) {
var _a, _b;
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true, suppressEmptyNode: true, indentBy: "", rootNodeName: (_a = options.rootName) !== null && _a !== void 0 ? _a : "root", cdataPropName: (_b = options.cdataPropName) !== null && _b !== void 0 ? _b : "__cdata" });
}
function getParserOptions(options = {}) {
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false, parseTagValue: false, attributeNamePrefix: "", stopNodes: options.stopNodes, processEntities: true, trimValues: false });
}
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
function stringifyXML(obj, opts = {}) {
const parserOptions = getSerializerOptions(opts);
const j2x = new fast_xml_parser_1.XMLBuilder(parserOptions);
const node = { [parserOptions.rootNodeName]: obj };
const xmlData = j2x.build(node);
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
}
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
async function parseXML(str, opts = {}) {
if (!str) {
throw new Error("Document is empty");
}
const validation = fast_xml_parser_1.XMLValidator.validate(str);
if (validation !== true) {
throw validation;
}
const parser = new fast_xml_parser_1.XMLParser(getParserOptions(opts));
const parsedXml = parser.parse(str);
// Remove the <?xml version="..." ?> node.
// This is a change in behavior on fxp v4. Issue #424
if (parsedXml["?xml"]) {
delete parsedXml["?xml"];
}
if (!opts.includeRoot) {
for (const key of Object.keys(parsedXml)) {
const value = parsedXml[key];
return typeof value === "object" ? Object.assign({}, value) : value;
}
}
return parsedXml;
}
//# sourceMappingURL=xml.js.map
File diff suppressed because one or more lines are too long
+3
View File
@@ -0,0 +1,3 @@
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from "./xml.common.js";
//# sourceMappingURL=index.d.ts.map
+5
View File
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,iBAAiB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { stringifyXML, parseXML } from \"./xml.js\";\nexport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common.js\";\n"]}
+3
View File
@@ -0,0 +1,3 @@
{
"type": "module"
}
+34
View File
@@ -0,0 +1,34 @@
/**
* Default key used to access the XML attributes.
*/
export declare const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export declare const XML_CHARKEY = "_";
/**
* Options to govern behavior of xml parser and builder.
*/
export interface XmlOptions {
/**
* indicates the name of the root element in the resulting XML when building XML.
*/
rootName?: string;
/**
* indicates whether the root element is to be included or not in the output when parsing XML.
*/
includeRoot?: boolean;
/**
* key used to access the XML value content when parsing XML.
*/
xmlCharKey?: string;
/**
* property name for a CDATA section.
*/
cdataPropName?: string;
/**
* XML nodes to exclude from parsing.
*/
stopNodes?: string[];
}
//# sourceMappingURL=xml.common.d.ts.map
+11
View File
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Default key used to access the XML attributes.
*/
export const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export const XML_CHARKEY = "_";
//# sourceMappingURL=xml.common.js.map
+1
View File
@@ -0,0 +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 /**\n * property name for a CDATA section.\n */\n cdataPropName?: string;\n\n /**\n * XML nodes to exclude from parsing.\n */\n stopNodes?: string[];\n}\n"]}
+16
View File
@@ -0,0 +1,16 @@
import { type XmlOptions } from "./xml.common.js";
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
export declare function stringifyXML(obj: unknown, opts?: XmlOptions): string;
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>;
//# sourceMappingURL=xml.d.ts.map
+63
View File
@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { XMLBuilder, XMLParser, XMLValidator } from "fast-xml-parser";
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
function getCommonOptions(options) {
var _a;
return {
attributesGroupName: XML_ATTRKEY,
textNodeName: (_a = options.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY,
ignoreAttributes: false,
suppressBooleanAttributes: false,
};
}
function getSerializerOptions(options = {}) {
var _a, _b;
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true, suppressEmptyNode: true, indentBy: "", rootNodeName: (_a = options.rootName) !== null && _a !== void 0 ? _a : "root", cdataPropName: (_b = options.cdataPropName) !== null && _b !== void 0 ? _b : "__cdata" });
}
function getParserOptions(options = {}) {
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false, parseTagValue: false, attributeNamePrefix: "", stopNodes: options.stopNodes, processEntities: true, trimValues: false });
}
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
export function stringifyXML(obj, opts = {}) {
const parserOptions = getSerializerOptions(opts);
const j2x = new XMLBuilder(parserOptions);
const node = { [parserOptions.rootNodeName]: obj };
const xmlData = j2x.build(node);
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
}
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
export async function parseXML(str, opts = {}) {
if (!str) {
throw new Error("Document is empty");
}
const validation = XMLValidator.validate(str);
if (validation !== true) {
throw validation;
}
const parser = new XMLParser(getParserOptions(opts));
const parsedXml = parser.parse(str);
// Remove the <?xml version="..." ?> node.
// This is a change in behavior on fxp v4. Issue #424
if (parsedXml["?xml"]) {
delete parsedXml["?xml"];
}
if (!opts.includeRoot) {
for (const key of Object.keys(parsedXml)) {
const value = parsedXml[key];
return typeof value === "object" ? Object.assign({}, value) : value;
}
}
return parsedXml;
}
//# sourceMappingURL=xml.js.map
File diff suppressed because one or more lines are too long
+3
View File
@@ -0,0 +1,3 @@
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from "./xml.common.js";
//# sourceMappingURL=index.d.ts.map
+5
View File
@@ -0,0 +1,5 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
export { stringifyXML, parseXML } from "./xml.js";
export { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,kCAAkC;AAElC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAClD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAc,MAAM,iBAAiB,CAAC","sourcesContent":["// Copyright (c) Microsoft Corporation.\n// Licensed under the MIT License.\n\nexport { stringifyXML, parseXML } from \"./xml.js\";\nexport { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from \"./xml.common.js\";\n"]}
+3
View File
@@ -0,0 +1,3 @@
{
"type": "module"
}
+34
View File
@@ -0,0 +1,34 @@
/**
* Default key used to access the XML attributes.
*/
export declare const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export declare const XML_CHARKEY = "_";
/**
* Options to govern behavior of xml parser and builder.
*/
export interface XmlOptions {
/**
* indicates the name of the root element in the resulting XML when building XML.
*/
rootName?: string;
/**
* indicates whether the root element is to be included or not in the output when parsing XML.
*/
includeRoot?: boolean;
/**
* key used to access the XML value content when parsing XML.
*/
xmlCharKey?: string;
/**
* property name for a CDATA section.
*/
cdataPropName?: string;
/**
* XML nodes to exclude from parsing.
*/
stopNodes?: string[];
}
//# sourceMappingURL=xml.common.d.ts.map
+11
View File
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/**
* Default key used to access the XML attributes.
*/
export const XML_ATTRKEY = "$";
/**
* Default key used to access the XML value content.
*/
export const XML_CHARKEY = "_";
//# sourceMappingURL=xml.common.js.map
+1
View File
@@ -0,0 +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 /**\n * property name for a CDATA section.\n */\n cdataPropName?: string;\n\n /**\n * XML nodes to exclude from parsing.\n */\n stopNodes?: string[];\n}\n"]}
+16
View File
@@ -0,0 +1,16 @@
import { type XmlOptions } from "./xml.common.js";
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
export declare function stringifyXML(obj: unknown, opts?: XmlOptions): string;
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
export declare function parseXML(str: string, opts?: XmlOptions): Promise<any>;
//# sourceMappingURL=xml.d.ts.map
+63
View File
@@ -0,0 +1,63 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { XMLBuilder, XMLParser, XMLValidator } from "fast-xml-parser";
import { XML_ATTRKEY, XML_CHARKEY } from "./xml.common.js";
function getCommonOptions(options) {
var _a;
return {
attributesGroupName: XML_ATTRKEY,
textNodeName: (_a = options.xmlCharKey) !== null && _a !== void 0 ? _a : XML_CHARKEY,
ignoreAttributes: false,
suppressBooleanAttributes: false,
};
}
function getSerializerOptions(options = {}) {
var _a, _b;
return Object.assign(Object.assign({}, getCommonOptions(options)), { attributeNamePrefix: "@_", format: true, suppressEmptyNode: true, indentBy: "", rootNodeName: (_a = options.rootName) !== null && _a !== void 0 ? _a : "root", cdataPropName: (_b = options.cdataPropName) !== null && _b !== void 0 ? _b : "__cdata" });
}
function getParserOptions(options = {}) {
return Object.assign(Object.assign({}, getCommonOptions(options)), { parseAttributeValue: false, parseTagValue: false, attributeNamePrefix: "", stopNodes: options.stopNodes, processEntities: true, trimValues: false });
}
/**
* Converts given JSON object to XML string
* @param obj - JSON object to be converted into XML string
* @param opts - Options that govern the XML building of given JSON object
* `rootName` indicates the name of the root element in the resulting XML
*/
export function stringifyXML(obj, opts = {}) {
const parserOptions = getSerializerOptions(opts);
const j2x = new XMLBuilder(parserOptions);
const node = { [parserOptions.rootNodeName]: obj };
const xmlData = j2x.build(node);
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
}
/**
* Converts given XML string into JSON
* @param str - String containing the XML content to be parsed into JSON
* @param opts - Options that govern the parsing of given xml string
* `includeRoot` indicates whether the root element is to be included or not in the output
*/
export async function parseXML(str, opts = {}) {
if (!str) {
throw new Error("Document is empty");
}
const validation = XMLValidator.validate(str);
if (validation !== true) {
throw validation;
}
const parser = new XMLParser(getParserOptions(opts));
const parsedXml = parser.parse(str);
// Remove the <?xml version="..." ?> node.
// This is a change in behavior on fxp v4. Issue #424
if (parsedXml["?xml"]) {
delete parsedXml["?xml"];
}
if (!opts.includeRoot) {
for (const key of Object.keys(parsedXml)) {
const value = parsedXml[key];
return typeof value === "object" ? Object.assign({}, value) : value;
}
}
return parsedXml;
}
//# sourceMappingURL=xml.js.map
File diff suppressed because one or more lines are too long
+113
View File
@@ -0,0 +1,113 @@
{
"name": "@azure/core-xml",
"version": "1.4.5",
"description": "Core library for interacting with XML payloads",
"sdk-type": "client",
"type": "module",
"main": "./dist/commonjs/index.js",
"types": "./dist/commonjs/index.d.ts",
"browser": "./dist/browser/index.js",
"react-native": "./dist/react-native/index.js",
"exports": {
"./package.json": "./package.json",
".": {
"browser": {
"types": "./dist/browser/index.d.ts",
"default": "./dist/browser/index.js"
},
"react-native": {
"types": "./dist/react-native/index.d.ts",
"default": "./dist/react-native/index.js"
},
"import": {
"types": "./dist/esm/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"types": "./dist/commonjs/index.d.ts",
"default": "./dist/commonjs/index.js"
}
}
},
"files": [
"dist/",
"!dist/**/*.d.*ts.map",
"README.md",
"LICENSE"
],
"repository": "github:Azure/azure-sdk-for-js",
"keywords": [
"azure",
"cloud"
],
"author": "Microsoft Corporation",
"license": "MIT",
"bugs": {
"url": "https://github.com/Azure/azure-sdk-for-js/issues"
},
"engines": {
"node": ">=18.0.0"
},
"homepage": "https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/core/core-xml/",
"sideEffects": false,
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
"scripts": {
"build": "npm run clean && dev-tool run build-package && dev-tool run extract-api",
"build:samples": "echo Obsolete",
"build:test": "echo skipped. actual commands inlined in browser test scripts",
"check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,json}\"",
"clean": "dev-tool run vendored rimraf --glob dist dist-* temp types *.tgz *.log",
"execute:samples": "dev-tool samples run samples-dev",
"extract-api": "dev-tool run build-package && dev-tool run extract-api",
"format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.{ts,cts,mts}\" \"test/**/*.{ts,cts,mts}\" \"*.{js,json}\"",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"integration-test:browser": "echo skipped",
"integration-test:node": "echo skipped",
"lint": "eslint README.md package.json api-extractor.json src test",
"lint:fix": "eslint README.md package.json api-extractor.json src test --fix --fix-type [problem,suggestion]",
"pack": "npm pack 2>&1",
"test": "npm run clean && dev-tool run build-package && npm run unit-test:node && dev-tool run build-test && npm run unit-test:browser && npm run integration-test",
"test:browser": "npm run clean && npm run unit-test:browser && npm run integration-test:browser",
"test:node": "npm run clean && dev-tool run build-package && npm run unit-test:node && npm run integration-test:node",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "npm run clean && dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --no-test-proxy --browser",
"unit-test:node": "dev-tool run test:vitest --no-test-proxy",
"update-snippets": "dev-tool run update-snippets"
},
"dependencies": {
"fast-xml-parser": "^5.0.7",
"tslib": "^2.8.1"
},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@types/node": "^18.0.0",
"@types/trusted-types": "^2.0.0",
"@vitest/browser": "^3.0.6",
"@vitest/coverage-istanbul": "^3.0.6",
"eslint": "^9.9.0",
"playwright": "^1.50.1",
"typescript": "~5.7.2",
"vitest": "^3.0.6"
},
"//metadata": {
"migrationDate": "2023-03-08T18:36:03.000Z"
},
"tshy": {
"exports": {
"./package.json": "./package.json",
".": "./src/index.ts"
},
"dialects": [
"esm",
"commonjs"
],
"esmDialects": [
"browser",
"react-native"
],
"selfLink": false,
"project": "./tsconfig.src.json"
},
"module": "./dist/esm/index.js"
}