@onlyflows/servicenow-mcp 2.3.0-dev.3 → 2.3.0-dev.5
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 +165 -0
- package/README.md +177 -29
- package/dist/atf-policy.d.ts +1 -1
- package/dist/atf-policy.d.ts.map +1 -1
- package/dist/atf-policy.js +2 -0
- package/dist/atf-policy.js.map +1 -1
- package/dist/config.d.ts +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/credential-tables.d.ts +32 -0
- package/dist/credential-tables.d.ts.map +1 -0
- package/dist/credential-tables.js +58 -0
- package/dist/credential-tables.js.map +1 -0
- package/dist/encoded-query-policy.d.ts.map +1 -1
- package/dist/encoded-query-policy.js +8 -1
- package/dist/encoded-query-policy.js.map +1 -1
- package/dist/execution-context.d.ts +14 -0
- package/dist/execution-context.d.ts.map +1 -1
- package/dist/execution-context.js +8 -0
- package/dist/execution-context.js.map +1 -1
- package/dist/field-policy.d.ts +34 -1
- package/dist/field-policy.d.ts.map +1 -1
- package/dist/field-policy.js +308 -25
- package/dist/field-policy.js.map +1 -1
- package/dist/hidden-values.d.ts +225 -0
- package/dist/hidden-values.d.ts.map +1 -0
- package/dist/hidden-values.js +748 -0
- package/dist/hidden-values.js.map +1 -0
- package/dist/http-entrypoint.d.ts.map +1 -1
- package/dist/http-entrypoint.js +7 -0
- package/dist/http-entrypoint.js.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/metadata-cache.d.ts +38 -0
- package/dist/metadata-cache.d.ts.map +1 -1
- package/dist/metadata-cache.js +76 -3
- package/dist/metadata-cache.js.map +1 -1
- package/dist/policy-card.d.ts +16 -1
- package/dist/policy-card.d.ts.map +1 -1
- package/dist/policy-card.js +13 -2
- package/dist/policy-card.js.map +1 -1
- package/dist/profile-admin.d.ts +18 -0
- package/dist/profile-admin.d.ts.map +1 -1
- package/dist/profile-admin.js +26 -1
- package/dist/profile-admin.js.map +1 -1
- package/dist/profile-manager.d.ts +13 -1
- package/dist/profile-manager.d.ts.map +1 -1
- package/dist/profile-manager.js +23 -1
- package/dist/profile-manager.js.map +1 -1
- package/dist/setup.d.ts +4 -0
- package/dist/setup.d.ts.map +1 -1
- package/dist/setup.js +150 -66
- package/dist/setup.js.map +1 -1
- package/dist/structured-query.d.ts.map +1 -1
- package/dist/structured-query.js +6 -1
- package/dist/structured-query.js.map +1 -1
- package/dist/table-policy.d.ts +34 -1
- package/dist/table-policy.d.ts.map +1 -1
- package/dist/table-policy.js +95 -10
- package/dist/table-policy.js.map +1 -1
- package/dist/tool-error.d.ts +8 -1
- package/dist/tool-error.d.ts.map +1 -1
- package/dist/tool-error.js +23 -0
- package/dist/tool-error.js.map +1 -1
- package/dist/tool-table-access.d.ts.map +1 -1
- package/dist/tool-table-access.js +25 -6
- package/dist/tool-table-access.js.map +1 -1
- package/dist/tools/aggregate.js +2 -2
- package/dist/tools/aggregate.js.map +1 -1
- package/dist/tools/all-fields-cap.d.ts.map +1 -1
- package/dist/tools/all-fields-cap.js +29 -7
- package/dist/tools/all-fields-cap.js.map +1 -1
- package/dist/tools/atf-author-module.d.ts +1 -7
- package/dist/tools/atf-author-module.d.ts.map +1 -1
- package/dist/tools/atf-author-module.js +16 -7
- package/dist/tools/atf-author-module.js.map +1 -1
- package/dist/tools/atf-contracts.d.ts +69 -4
- package/dist/tools/atf-contracts.d.ts.map +1 -1
- package/dist/tools/atf-contracts.js +32 -2
- package/dist/tools/atf-contracts.js.map +1 -1
- package/dist/tools/atf-renderers.js +2 -1
- package/dist/tools/atf-renderers.js.map +1 -1
- package/dist/tools/atf-results-module.d.ts.map +1 -1
- package/dist/tools/atf-results-module.js +5 -3
- package/dist/tools/atf-results-module.js.map +1 -1
- package/dist/tools/atf-run-module.js +3 -2
- package/dist/tools/atf-run-module.js.map +1 -1
- package/dist/tools/atf-shared.d.ts +6 -0
- package/dist/tools/atf-shared.d.ts.map +1 -1
- package/dist/tools/atf-shared.js +49 -10
- package/dist/tools/atf-shared.js.map +1 -1
- package/dist/tools/atf-step-catalog.d.ts +13 -8
- package/dist/tools/atf-step-catalog.d.ts.map +1 -1
- package/dist/tools/atf-step-catalog.js +169 -5
- package/dist/tools/atf-step-catalog.js.map +1 -1
- package/dist/tools/batch.d.ts.map +1 -1
- package/dist/tools/batch.js +4 -2
- package/dist/tools/batch.js.map +1 -1
- package/dist/tools/flow-view-contracts.d.ts +57 -1
- package/dist/tools/flow-view-contracts.d.ts.map +1 -1
- package/dist/tools/flow-view-contracts.js +34 -2
- package/dist/tools/flow-view-contracts.js.map +1 -1
- package/dist/tools/flow-view-graph.d.ts +14 -0
- package/dist/tools/flow-view-graph.d.ts.map +1 -1
- package/dist/tools/flow-view-graph.js +43 -1
- package/dist/tools/flow-view-graph.js.map +1 -1
- package/dist/tools/flow-view-module.js +1 -1
- package/dist/tools/flow-view-module.js.map +1 -1
- package/dist/tools/flow-view-policy.d.ts +3 -3
- package/dist/tools/flow-view-policy.d.ts.map +1 -1
- package/dist/tools/flow-view-policy.js +5 -4
- package/dist/tools/flow-view-policy.js.map +1 -1
- package/dist/tools/flow-view-renderers.d.ts.map +1 -1
- package/dist/tools/flow-view-renderers.js +9 -21
- package/dist/tools/flow-view-renderers.js.map +1 -1
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +80 -13
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/platform-view-contracts.d.ts +189 -0
- package/dist/tools/platform-view-contracts.d.ts.map +1 -0
- package/dist/tools/platform-view-contracts.js +353 -0
- package/dist/tools/platform-view-contracts.js.map +1 -0
- package/dist/tools/platform-view-evaluate.d.ts +11 -0
- package/dist/tools/platform-view-evaluate.d.ts.map +1 -0
- package/dist/tools/platform-view-evaluate.js +19 -0
- package/dist/tools/platform-view-evaluate.js.map +1 -0
- package/dist/tools/platform-view-module.d.ts +28 -0
- package/dist/tools/platform-view-module.d.ts.map +1 -0
- package/dist/tools/platform-view-module.js +111 -0
- package/dist/tools/platform-view-module.js.map +1 -0
- package/dist/tools/platform-view-scan.d.ts +57 -0
- package/dist/tools/platform-view-scan.d.ts.map +1 -0
- package/dist/tools/platform-view-scan.js +375 -0
- package/dist/tools/platform-view-scan.js.map +1 -0
- package/dist/tools/platform-view-upgrades.d.ts +152 -0
- package/dist/tools/platform-view-upgrades.d.ts.map +1 -0
- package/dist/tools/platform-view-upgrades.js +545 -0
- package/dist/tools/platform-view-upgrades.js.map +1 -0
- package/dist/tools/profile.d.ts.map +1 -1
- package/dist/tools/profile.js +1 -0
- package/dist/tools/profile.js.map +1 -1
- package/dist/tools/query-module.js +1 -1
- package/dist/tools/query-module.js.map +1 -1
- package/dist/tools/record-write-shared.d.ts.map +1 -1
- package/dist/tools/record-write-shared.js +4 -3
- package/dist/tools/record-write-shared.js.map +1 -1
- package/dist/tools/result-envelope.d.ts +116 -36
- package/dist/tools/result-envelope.d.ts.map +1 -1
- package/dist/tools/result-envelope.js +38 -3
- package/dist/tools/result-envelope.js.map +1 -1
- package/dist/tools/syslog-cursor.d.ts +61 -0
- package/dist/tools/syslog-cursor.d.ts.map +1 -0
- package/dist/tools/syslog-cursor.js +146 -0
- package/dist/tools/syslog-cursor.js.map +1 -0
- package/dist/tools/syslog.d.ts +2 -0
- package/dist/tools/syslog.d.ts.map +1 -1
- package/dist/tools/syslog.js +126 -2
- package/dist/tools/syslog.js.map +1 -1
- package/dist/update-set-eval/secret-patterns.d.ts +75 -0
- package/dist/update-set-eval/secret-patterns.d.ts.map +1 -0
- package/dist/update-set-eval/secret-patterns.js +570 -0
- package/dist/update-set-eval/secret-patterns.js.map +1 -0
- package/dist/update-set-eval/xml-reader.d.ts +117 -0
- package/dist/update-set-eval/xml-reader.d.ts.map +1 -0
- package/dist/update-set-eval/xml-reader.js +561 -0
- package/dist/update-set-eval/xml-reader.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/CLIENT-SETUP.md +8 -6
- package/docs/FLOW-VIEW.md +14 -5
- package/docs/PRODUCTION-SECURITY.md +63 -0
- package/docs/PROFILE-CREDENTIALS.md +10 -4
- package/package.json +1 -1
- package/server.json +2 -2
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A minimal, bounded XML reader for `sys_update_xml.payload`.
|
|
3
|
+
*
|
|
4
|
+
* Payloads come from this instance and, for a remote set, from another one, so
|
|
5
|
+
* the reader treats every byte as hostile:
|
|
6
|
+
* - iterative, never recursive, with caps on depth, element count,
|
|
7
|
+
* attributes per element, name length and total bytes;
|
|
8
|
+
* - no DTD of any kind: DOCTYPE, ENTITY and every other `<!` declaration are
|
|
9
|
+
* rejected, so nothing can declare an entity or point at an external one;
|
|
10
|
+
* - only the five predefined entities and valid numeric character
|
|
11
|
+
* references are decoded; any other `&name;` is rejected;
|
|
12
|
+
* - processing instructions are rejected, except one leading
|
|
13
|
+
* `<?xml version=… encoding=… standalone=…?>` declaration at offset 0
|
|
14
|
+
* (after an optional byte-order mark), which every payload has;
|
|
15
|
+
* - an XInclude namespace binding is rejected, although nothing here would
|
|
16
|
+
* act on it;
|
|
17
|
+
* - comments are skipped and kept out of the tree;
|
|
18
|
+
* - adjacent text and CDATA sections, including those a comment or a CDATA
|
|
19
|
+
* boundary splits (`]]]]><![CDATA[>`), become one text node, so a value
|
|
20
|
+
* a caller masks node by node cannot be split across nodes.
|
|
21
|
+
* It never throws on input. A failure is a fixed code plus a fixed message;
|
|
22
|
+
* neither carries any payload text.
|
|
23
|
+
*
|
|
24
|
+
* No dependency: the npm XML parsers either resolve entities or are far larger
|
|
25
|
+
* than this format needs.
|
|
26
|
+
*/
|
|
27
|
+
/** Default caps. Callers may lower any of them; `maxBytes` may also be raised, to `XML_READER_MAX_BYTES`. */
|
|
28
|
+
export declare const XML_READER_LIMITS: Readonly<{
|
|
29
|
+
maxDepth: 64;
|
|
30
|
+
maxElements: 20000;
|
|
31
|
+
maxAttributesPerElement: 64;
|
|
32
|
+
/** UTF-8 bytes. The evaluator's per-payload cap. */
|
|
33
|
+
maxBytes: 262144;
|
|
34
|
+
maxNameLength: 256;
|
|
35
|
+
}>;
|
|
36
|
+
/** The ceiling for a caller's `maxBytes`. */
|
|
37
|
+
export declare const XML_READER_MAX_BYTES = 1048576;
|
|
38
|
+
export interface XmlReaderOptions {
|
|
39
|
+
readonly maxDepth?: number;
|
|
40
|
+
readonly maxElements?: number;
|
|
41
|
+
readonly maxAttributesPerElement?: number;
|
|
42
|
+
readonly maxBytes?: number;
|
|
43
|
+
}
|
|
44
|
+
export interface XmlDeclaration {
|
|
45
|
+
readonly version: string;
|
|
46
|
+
readonly encoding: string | null;
|
|
47
|
+
readonly standalone: "yes" | "no" | null;
|
|
48
|
+
}
|
|
49
|
+
export interface XmlText {
|
|
50
|
+
readonly kind: "text";
|
|
51
|
+
/** Decoded text. */
|
|
52
|
+
readonly value: string;
|
|
53
|
+
/** True when any part of the text came from a CDATA section. */
|
|
54
|
+
readonly cdata: boolean;
|
|
55
|
+
}
|
|
56
|
+
export interface XmlElement {
|
|
57
|
+
readonly kind: "element";
|
|
58
|
+
readonly name: string;
|
|
59
|
+
/** Decoded attribute values, in document order. */
|
|
60
|
+
readonly attributes: ReadonlyMap<string, string>;
|
|
61
|
+
readonly children: readonly XmlNode[];
|
|
62
|
+
}
|
|
63
|
+
export type XmlNode = XmlElement | XmlText;
|
|
64
|
+
export interface XmlDocument {
|
|
65
|
+
readonly declaration: XmlDeclaration | null;
|
|
66
|
+
readonly root: XmlElement;
|
|
67
|
+
readonly elementCount: number;
|
|
68
|
+
readonly maxDepth: number;
|
|
69
|
+
/** UTF-8 length of the input. */
|
|
70
|
+
readonly bytes: number;
|
|
71
|
+
}
|
|
72
|
+
export declare const XML_READ_ERROR_CODES: readonly ["not_text", "too_large", "no_root", "invalid_declaration", "processing_instruction", "doctype", "entity_declaration", "markup_declaration", "external_reference", "invalid_entity_reference", "invalid_character_reference", "malformed_tag", "invalid_name", "name_too_long", "invalid_attribute", "duplicate_attribute", "mismatched_tag", "unclosed_element", "unclosed_comment", "unclosed_cdata", "content_outside_root", "multiple_roots", "depth_limit", "element_limit", "attribute_limit"];
|
|
73
|
+
export type XmlReadErrorCode = (typeof XML_READ_ERROR_CODES)[number];
|
|
74
|
+
/** One fixed sentence per code. None interpolates anything. */
|
|
75
|
+
export declare const XML_READ_ERROR_MESSAGES: Readonly<Record<XmlReadErrorCode, string>>;
|
|
76
|
+
export interface XmlReadFailure {
|
|
77
|
+
readonly ok: false;
|
|
78
|
+
readonly code: XmlReadErrorCode;
|
|
79
|
+
readonly message: string;
|
|
80
|
+
}
|
|
81
|
+
export type XmlReadResult = {
|
|
82
|
+
readonly ok: true;
|
|
83
|
+
readonly document: XmlDocument;
|
|
84
|
+
} | XmlReadFailure;
|
|
85
|
+
/**
|
|
86
|
+
* Reads a payload. Never throws on input; a bad option value (a programming
|
|
87
|
+
* error) throws a `RangeError` with a fixed message.
|
|
88
|
+
*/
|
|
89
|
+
export declare function readXml(text: string, options?: XmlReaderOptions): XmlReadResult;
|
|
90
|
+
export declare function getAttribute(element: XmlElement, name: string): string | undefined;
|
|
91
|
+
/** Direct child elements, optionally only those with `name`. */
|
|
92
|
+
export declare function childElements(element: XmlElement, name?: string): XmlElement[];
|
|
93
|
+
export declare function firstChildElement(element: XmlElement, name: string): XmlElement | undefined;
|
|
94
|
+
/** The element's own text: its direct text and CDATA children, joined. Descendants are not included. */
|
|
95
|
+
export declare function textContent(element: XmlElement): string;
|
|
96
|
+
/**
|
|
97
|
+
* Visits every element in document order, iteratively. `ancestors` runs from
|
|
98
|
+
* the root to the element's parent and is only valid during the call. Return
|
|
99
|
+
* `"skip"` to leave an element's children unvisited.
|
|
100
|
+
*/
|
|
101
|
+
export declare function walkElements(root: XmlElement, visit: (element: XmlElement, ancestors: readonly XmlElement[]) => void | "skip"): void;
|
|
102
|
+
export interface XmlSerializeOptions {
|
|
103
|
+
/** Emit `<?xml version="1.0" encoding="UTF-8"?>` first. Default false. */
|
|
104
|
+
readonly declaration?: boolean;
|
|
105
|
+
/** Rewrites each text node before it is escaped; `ancestors` ends with `parent`. */
|
|
106
|
+
readonly text?: (value: string, parent: XmlElement, ancestors: readonly XmlElement[]) => string;
|
|
107
|
+
/** Rewrites each attribute value before it is escaped; `ancestors` ends with `element`. */
|
|
108
|
+
readonly attribute?: (name: string, value: string, element: XmlElement, ancestors: readonly XmlElement[]) => string;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Writes a tree back out, iteratively. CDATA comes out as escaped text, so
|
|
112
|
+
* nothing in a value can close a section early. The hooks let a caller mask
|
|
113
|
+
* text and attribute values on the way out (the payload view's masking)
|
|
114
|
+
* without copying the tree.
|
|
115
|
+
*/
|
|
116
|
+
export declare function serializeXml(root: XmlElement, options?: XmlSerializeOptions): string;
|
|
117
|
+
//# sourceMappingURL=xml-reader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"xml-reader.d.ts","sourceRoot":"","sources":["../../src/update-set-eval/xml-reader.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* A minimal, bounded XML reader for `sys_update_xml.payload`.
|
|
4
|
+
*
|
|
5
|
+
* Payloads come from this instance and, for a remote set, from another one, so
|
|
6
|
+
* the reader treats every byte as hostile:
|
|
7
|
+
* - iterative, never recursive, with caps on depth, element count,
|
|
8
|
+
* attributes per element, name length and total bytes;
|
|
9
|
+
* - no DTD of any kind: DOCTYPE, ENTITY and every other `<!` declaration are
|
|
10
|
+
* rejected, so nothing can declare an entity or point at an external one;
|
|
11
|
+
* - only the five predefined entities and valid numeric character
|
|
12
|
+
* references are decoded; any other `&name;` is rejected;
|
|
13
|
+
* - processing instructions are rejected, except one leading
|
|
14
|
+
* `<?xml version=… encoding=… standalone=…?>` declaration at offset 0
|
|
15
|
+
* (after an optional byte-order mark), which every payload has;
|
|
16
|
+
* - an XInclude namespace binding is rejected, although nothing here would
|
|
17
|
+
* act on it;
|
|
18
|
+
* - comments are skipped and kept out of the tree;
|
|
19
|
+
* - adjacent text and CDATA sections, including those a comment or a CDATA
|
|
20
|
+
* boundary splits (`]]]]><![CDATA[>`), become one text node, so a value
|
|
21
|
+
* a caller masks node by node cannot be split across nodes.
|
|
22
|
+
* It never throws on input. A failure is a fixed code plus a fixed message;
|
|
23
|
+
* neither carries any payload text.
|
|
24
|
+
*
|
|
25
|
+
* No dependency: the npm XML parsers either resolve entities or are far larger
|
|
26
|
+
* than this format needs.
|
|
27
|
+
*/
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.XML_READ_ERROR_MESSAGES = exports.XML_READ_ERROR_CODES = exports.XML_READER_MAX_BYTES = exports.XML_READER_LIMITS = void 0;
|
|
30
|
+
exports.readXml = readXml;
|
|
31
|
+
exports.getAttribute = getAttribute;
|
|
32
|
+
exports.childElements = childElements;
|
|
33
|
+
exports.firstChildElement = firstChildElement;
|
|
34
|
+
exports.textContent = textContent;
|
|
35
|
+
exports.walkElements = walkElements;
|
|
36
|
+
exports.serializeXml = serializeXml;
|
|
37
|
+
/** Default caps. Callers may lower any of them; `maxBytes` may also be raised, to `XML_READER_MAX_BYTES`. */
|
|
38
|
+
exports.XML_READER_LIMITS = Object.freeze({
|
|
39
|
+
maxDepth: 64,
|
|
40
|
+
maxElements: 20_000,
|
|
41
|
+
maxAttributesPerElement: 64,
|
|
42
|
+
/** UTF-8 bytes. The evaluator's per-payload cap. */
|
|
43
|
+
maxBytes: 262_144,
|
|
44
|
+
maxNameLength: 256,
|
|
45
|
+
});
|
|
46
|
+
/** The ceiling for a caller's `maxBytes`. */
|
|
47
|
+
exports.XML_READER_MAX_BYTES = 1_048_576;
|
|
48
|
+
exports.XML_READ_ERROR_CODES = Object.freeze([
|
|
49
|
+
"not_text",
|
|
50
|
+
"too_large",
|
|
51
|
+
"no_root",
|
|
52
|
+
"invalid_declaration",
|
|
53
|
+
"processing_instruction",
|
|
54
|
+
"doctype",
|
|
55
|
+
"entity_declaration",
|
|
56
|
+
"markup_declaration",
|
|
57
|
+
"external_reference",
|
|
58
|
+
"invalid_entity_reference",
|
|
59
|
+
"invalid_character_reference",
|
|
60
|
+
"malformed_tag",
|
|
61
|
+
"invalid_name",
|
|
62
|
+
"name_too_long",
|
|
63
|
+
"invalid_attribute",
|
|
64
|
+
"duplicate_attribute",
|
|
65
|
+
"mismatched_tag",
|
|
66
|
+
"unclosed_element",
|
|
67
|
+
"unclosed_comment",
|
|
68
|
+
"unclosed_cdata",
|
|
69
|
+
"content_outside_root",
|
|
70
|
+
"multiple_roots",
|
|
71
|
+
"depth_limit",
|
|
72
|
+
"element_limit",
|
|
73
|
+
"attribute_limit",
|
|
74
|
+
]);
|
|
75
|
+
/** One fixed sentence per code. None interpolates anything. */
|
|
76
|
+
exports.XML_READ_ERROR_MESSAGES = Object.freeze({
|
|
77
|
+
not_text: "The payload is not text.",
|
|
78
|
+
too_large: "The payload is larger than the XML reader accepts.",
|
|
79
|
+
no_root: "The payload has no root element.",
|
|
80
|
+
invalid_declaration: "The payload's XML declaration is not one the reader accepts.",
|
|
81
|
+
processing_instruction: "The payload has a processing instruction other than the leading XML declaration.",
|
|
82
|
+
doctype: "The payload has a DOCTYPE declaration, which the reader does not accept.",
|
|
83
|
+
entity_declaration: "The payload has an ENTITY declaration, which the reader does not accept.",
|
|
84
|
+
markup_declaration: "The payload has a markup declaration, which the reader does not accept.",
|
|
85
|
+
external_reference: "The payload refers to external content, which the reader does not accept.",
|
|
86
|
+
invalid_entity_reference: "The payload has an entity reference other than the five predefined ones.",
|
|
87
|
+
invalid_character_reference: "The payload has a character reference that is not a valid XML character.",
|
|
88
|
+
malformed_tag: "The payload has a malformed tag.",
|
|
89
|
+
invalid_name: "The payload has an element or attribute name the reader does not accept.",
|
|
90
|
+
name_too_long: "The payload has an element or attribute name longer than the reader accepts.",
|
|
91
|
+
invalid_attribute: "The payload has a malformed attribute.",
|
|
92
|
+
duplicate_attribute: "The payload has an element with the same attribute twice.",
|
|
93
|
+
mismatched_tag: "The payload has an end tag that does not match its start tag.",
|
|
94
|
+
unclosed_element: "The payload ends before every element is closed.",
|
|
95
|
+
unclosed_comment: "The payload has a comment that is not closed.",
|
|
96
|
+
unclosed_cdata: "The payload has a CDATA section that is not closed.",
|
|
97
|
+
content_outside_root: "The payload has content outside its root element.",
|
|
98
|
+
multiple_roots: "The payload has more than one root element.",
|
|
99
|
+
depth_limit: "The payload nests elements deeper than the reader accepts.",
|
|
100
|
+
element_limit: "The payload has more elements than the reader accepts.",
|
|
101
|
+
attribute_limit: "The payload has an element with more attributes than the reader accepts.",
|
|
102
|
+
});
|
|
103
|
+
const XINCLUDE_NAMESPACES = new Set([
|
|
104
|
+
"http://www.w3.org/2001/XInclude",
|
|
105
|
+
"http://www.w3.org/2003/XInclude",
|
|
106
|
+
]);
|
|
107
|
+
const BOM = 0xfeff;
|
|
108
|
+
function resolveLimits(options) {
|
|
109
|
+
const pick = (key, ceiling) => {
|
|
110
|
+
const value = options?.[key];
|
|
111
|
+
if (value === undefined)
|
|
112
|
+
return exports.XML_READER_LIMITS[key];
|
|
113
|
+
if (!Number.isInteger(value) || value < 1 || value > ceiling) {
|
|
114
|
+
throw new RangeError(`XML reader option ${key} is out of range.`);
|
|
115
|
+
}
|
|
116
|
+
return value;
|
|
117
|
+
};
|
|
118
|
+
return {
|
|
119
|
+
maxDepth: pick("maxDepth", exports.XML_READER_LIMITS.maxDepth),
|
|
120
|
+
maxElements: pick("maxElements", exports.XML_READER_LIMITS.maxElements),
|
|
121
|
+
maxAttributesPerElement: pick("maxAttributesPerElement", exports.XML_READER_LIMITS.maxAttributesPerElement),
|
|
122
|
+
maxBytes: pick("maxBytes", exports.XML_READER_MAX_BYTES),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
/** Thrown inside the reader only; `readXml` turns it into a result. */
|
|
126
|
+
class Reject {
|
|
127
|
+
code;
|
|
128
|
+
constructor(code) {
|
|
129
|
+
this.code = code;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function fail(code) {
|
|
133
|
+
return { ok: false, code, message: exports.XML_READ_ERROR_MESSAGES[code] };
|
|
134
|
+
}
|
|
135
|
+
const isSpace = (c) => c === 32 || c === 9 || c === 10 || c === 13;
|
|
136
|
+
const isNameStart = (c) => (c >= 65 && c <= 90) || (c >= 97 && c <= 122) || c === 95 || c === 58; // A-Z a-z _ :
|
|
137
|
+
const isNameChar = (c) => isNameStart(c) || (c >= 48 && c <= 57) || c === 45 || c === 46; // 0-9 - .
|
|
138
|
+
/** The XML 1.0 `Char` production. */
|
|
139
|
+
function isXmlChar(code) {
|
|
140
|
+
return (code === 0x9 ||
|
|
141
|
+
code === 0xa ||
|
|
142
|
+
code === 0xd ||
|
|
143
|
+
(code >= 0x20 && code <= 0xd7ff) ||
|
|
144
|
+
(code >= 0xe000 && code <= 0xfffd) ||
|
|
145
|
+
(code >= 0x10000 && code <= 0x10ffff));
|
|
146
|
+
}
|
|
147
|
+
const PREDEFINED = Object.freeze({
|
|
148
|
+
lt: "<",
|
|
149
|
+
gt: ">",
|
|
150
|
+
amp: "&",
|
|
151
|
+
quot: '"',
|
|
152
|
+
apos: "'",
|
|
153
|
+
});
|
|
154
|
+
/**
|
|
155
|
+
* Decodes references in `text[from, to)`. Every search stays inside the
|
|
156
|
+
* segment and each reference is bounded (at most ten characters between `&`
|
|
157
|
+
* and `;`), so the work is linear in the segment.
|
|
158
|
+
*/
|
|
159
|
+
function decode(text, from, to, normalizeWhitespace) {
|
|
160
|
+
const segment = text.slice(from, to);
|
|
161
|
+
const raw = (value) => (normalizeWhitespace ? value.replace(/[\t\n\r]/gu, " ") : value);
|
|
162
|
+
let amp = segment.indexOf("&");
|
|
163
|
+
if (amp < 0)
|
|
164
|
+
return raw(segment);
|
|
165
|
+
const parts = [];
|
|
166
|
+
let cursor = 0;
|
|
167
|
+
while (amp >= 0) {
|
|
168
|
+
parts.push(raw(segment.slice(cursor, amp)));
|
|
169
|
+
const semi = segment.indexOf(";", amp + 1);
|
|
170
|
+
if (semi < 0 || semi - amp > 11)
|
|
171
|
+
throw new Reject("invalid_entity_reference");
|
|
172
|
+
const body = segment.slice(amp + 1, semi);
|
|
173
|
+
if (body.charCodeAt(0) === 35) {
|
|
174
|
+
// #
|
|
175
|
+
const hex = body.charCodeAt(1) === 120; // x
|
|
176
|
+
const digits = body.slice(hex ? 2 : 1);
|
|
177
|
+
const valid = hex ? /^[0-9A-Fa-f]{1,6}$/u.test(digits) : /^[0-9]{1,7}$/u.test(digits);
|
|
178
|
+
const code = valid ? Number.parseInt(digits, hex ? 16 : 10) : -1;
|
|
179
|
+
if (!isXmlChar(code))
|
|
180
|
+
throw new Reject("invalid_character_reference");
|
|
181
|
+
parts.push(String.fromCodePoint(code));
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
const value = Object.hasOwn(PREDEFINED, body) ? PREDEFINED[body] : undefined;
|
|
185
|
+
if (value === undefined)
|
|
186
|
+
throw new Reject("invalid_entity_reference");
|
|
187
|
+
parts.push(value);
|
|
188
|
+
}
|
|
189
|
+
cursor = semi + 1;
|
|
190
|
+
amp = segment.indexOf("&", cursor);
|
|
191
|
+
}
|
|
192
|
+
parts.push(raw(segment.slice(cursor)));
|
|
193
|
+
return parts.join("");
|
|
194
|
+
}
|
|
195
|
+
/** Ends the element's pending text run as one text node. */
|
|
196
|
+
function flushText(open) {
|
|
197
|
+
if (open.text.length === 0)
|
|
198
|
+
return;
|
|
199
|
+
const value = open.text.length === 1 ? open.text[0] : open.text.join("");
|
|
200
|
+
open.children.push({ kind: "text", value, cdata: open.cdata });
|
|
201
|
+
open.text.length = 0;
|
|
202
|
+
open.cdata = false;
|
|
203
|
+
}
|
|
204
|
+
/** Reads the `<?xml …?>` declaration at `at`; returns it and the offset after `?>`. */
|
|
205
|
+
function readDeclaration(text, at) {
|
|
206
|
+
const close = text.indexOf("?>", at);
|
|
207
|
+
if (close < 0 || close - at > 256)
|
|
208
|
+
throw new Reject("invalid_declaration");
|
|
209
|
+
const body = text.slice(at + 5, close);
|
|
210
|
+
// version, then optional encoding, then optional standalone; quotes must match.
|
|
211
|
+
const match = /^[ \t\r\n]+version[ \t\r\n]*=[ \t\r\n]*(?:"(1\.[0-9]{1,3})"|'(1\.[0-9]{1,3})')(?:[ \t\r\n]+encoding[ \t\r\n]*=[ \t\r\n]*(?:"([A-Za-z][A-Za-z0-9._-]{0,39})"|'([A-Za-z][A-Za-z0-9._-]{0,39})'))?(?:[ \t\r\n]+standalone[ \t\r\n]*=[ \t\r\n]*(?:"(yes|no)"|'(yes|no)'))?[ \t\r\n]*$/u.exec(body);
|
|
212
|
+
if (!match)
|
|
213
|
+
throw new Reject("invalid_declaration");
|
|
214
|
+
const standalone = match[5] ?? match[6];
|
|
215
|
+
return {
|
|
216
|
+
declaration: {
|
|
217
|
+
version: match[1] ?? match[2] ?? "1.0",
|
|
218
|
+
encoding: match[3] ?? match[4] ?? null,
|
|
219
|
+
standalone: standalone === "yes" || standalone === "no" ? standalone : null,
|
|
220
|
+
},
|
|
221
|
+
next: close + 2,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function readName(text, at) {
|
|
225
|
+
if (!isNameStart(text.charCodeAt(at))) {
|
|
226
|
+
throw new Reject(at < text.length && !isSpace(text.charCodeAt(at)) ? "invalid_name" : "malformed_tag");
|
|
227
|
+
}
|
|
228
|
+
let i = at + 1;
|
|
229
|
+
while (i < text.length && isNameChar(text.charCodeAt(i)))
|
|
230
|
+
i++;
|
|
231
|
+
if (i - at > exports.XML_READER_LIMITS.maxNameLength)
|
|
232
|
+
throw new Reject("name_too_long");
|
|
233
|
+
const next = text.charCodeAt(i);
|
|
234
|
+
// A name ends at whitespace, `/`, `>` or `=`; anything else (non-ASCII included) is not accepted.
|
|
235
|
+
if (i < text.length && !isSpace(next) && next !== 47 && next !== 62 && next !== 61) {
|
|
236
|
+
throw new Reject("invalid_name");
|
|
237
|
+
}
|
|
238
|
+
return i;
|
|
239
|
+
}
|
|
240
|
+
function skipSpace(text, at) {
|
|
241
|
+
let i = at;
|
|
242
|
+
while (i < text.length && isSpace(text.charCodeAt(i)))
|
|
243
|
+
i++;
|
|
244
|
+
return i;
|
|
245
|
+
}
|
|
246
|
+
function parse(text, limits, bytes) {
|
|
247
|
+
let i = 0;
|
|
248
|
+
if (text.charCodeAt(0) === BOM)
|
|
249
|
+
i = 1;
|
|
250
|
+
let declaration = null;
|
|
251
|
+
if (text.startsWith("<?xml", i) && isSpace(text.charCodeAt(i + 5))) {
|
|
252
|
+
const read = readDeclaration(text, i);
|
|
253
|
+
declaration = read.declaration;
|
|
254
|
+
i = read.next;
|
|
255
|
+
}
|
|
256
|
+
const stack = [];
|
|
257
|
+
let root = null;
|
|
258
|
+
let elementCount = 0;
|
|
259
|
+
let maxDepth = 0;
|
|
260
|
+
const length = text.length;
|
|
261
|
+
const addText = (value, cdata) => {
|
|
262
|
+
const parent = stack[stack.length - 1];
|
|
263
|
+
if (parent === undefined) {
|
|
264
|
+
if (cdata || !/^[ \t\r\n]*$/u.test(value))
|
|
265
|
+
throw new Reject("content_outside_root");
|
|
266
|
+
return;
|
|
267
|
+
}
|
|
268
|
+
if (value.length === 0)
|
|
269
|
+
return;
|
|
270
|
+
parent.text.push(value);
|
|
271
|
+
if (cdata)
|
|
272
|
+
parent.cdata = true;
|
|
273
|
+
};
|
|
274
|
+
/** Attaches a finished element to its parent, or makes it the root. */
|
|
275
|
+
const attach = (element) => {
|
|
276
|
+
const parent = stack[stack.length - 1];
|
|
277
|
+
if (parent === undefined) {
|
|
278
|
+
root = element;
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
flushText(parent);
|
|
282
|
+
parent.children.push(element);
|
|
283
|
+
};
|
|
284
|
+
while (i < length) {
|
|
285
|
+
const lt = text.indexOf("<", i);
|
|
286
|
+
const textEnd = lt < 0 ? length : lt;
|
|
287
|
+
if (textEnd > i) {
|
|
288
|
+
if (stack.length === 0) {
|
|
289
|
+
// Outside the root only whitespace may appear, and it carries no references.
|
|
290
|
+
for (let k = i; k < textEnd; k++) {
|
|
291
|
+
if (!isSpace(text.charCodeAt(k)))
|
|
292
|
+
throw new Reject("content_outside_root");
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
addText(decode(text, i, textEnd, false), false);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
if (lt < 0)
|
|
300
|
+
break;
|
|
301
|
+
i = lt;
|
|
302
|
+
const next = text.charCodeAt(i + 1);
|
|
303
|
+
if (next === 63)
|
|
304
|
+
throw new Reject("processing_instruction"); // <?
|
|
305
|
+
if (next === 33) {
|
|
306
|
+
// <!
|
|
307
|
+
if (text.startsWith("<!--", i)) {
|
|
308
|
+
const close = text.indexOf("-->", i + 4);
|
|
309
|
+
if (close < 0)
|
|
310
|
+
throw new Reject("unclosed_comment");
|
|
311
|
+
i = close + 3;
|
|
312
|
+
continue;
|
|
313
|
+
}
|
|
314
|
+
if (text.startsWith("<![CDATA[", i)) {
|
|
315
|
+
const close = text.indexOf("]]>", i + 9);
|
|
316
|
+
if (close < 0)
|
|
317
|
+
throw new Reject("unclosed_cdata");
|
|
318
|
+
addText(text.slice(i + 9, close), true);
|
|
319
|
+
i = close + 3;
|
|
320
|
+
continue;
|
|
321
|
+
}
|
|
322
|
+
const keyword = text.slice(i + 2, i + 9).toUpperCase();
|
|
323
|
+
if (keyword.startsWith("DOCTYPE"))
|
|
324
|
+
throw new Reject("doctype");
|
|
325
|
+
if (keyword.startsWith("ENTITY"))
|
|
326
|
+
throw new Reject("entity_declaration");
|
|
327
|
+
throw new Reject("markup_declaration");
|
|
328
|
+
}
|
|
329
|
+
if (next === 47) {
|
|
330
|
+
// </name>
|
|
331
|
+
const nameEnd = readName(text, i + 2);
|
|
332
|
+
const name = text.slice(i + 2, nameEnd);
|
|
333
|
+
const close = skipSpace(text, nameEnd);
|
|
334
|
+
if (text.charCodeAt(close) !== 62)
|
|
335
|
+
throw new Reject("malformed_tag");
|
|
336
|
+
const open = stack.pop();
|
|
337
|
+
if (open === undefined || open.name !== name)
|
|
338
|
+
throw new Reject("mismatched_tag");
|
|
339
|
+
flushText(open);
|
|
340
|
+
attach({ kind: "element", name, attributes: open.attributes, children: open.children });
|
|
341
|
+
i = close + 1;
|
|
342
|
+
continue;
|
|
343
|
+
}
|
|
344
|
+
// <name attr="value" …> or <name …/>
|
|
345
|
+
if (root !== null)
|
|
346
|
+
throw new Reject("multiple_roots");
|
|
347
|
+
const nameEnd = readName(text, i + 1);
|
|
348
|
+
const name = text.slice(i + 1, nameEnd);
|
|
349
|
+
elementCount++;
|
|
350
|
+
if (elementCount > limits.maxElements)
|
|
351
|
+
throw new Reject("element_limit");
|
|
352
|
+
if (stack.length + 1 > limits.maxDepth)
|
|
353
|
+
throw new Reject("depth_limit");
|
|
354
|
+
maxDepth = Math.max(maxDepth, stack.length + 1);
|
|
355
|
+
const attributes = new Map();
|
|
356
|
+
let j = nameEnd;
|
|
357
|
+
let selfClosing = false;
|
|
358
|
+
for (;;) {
|
|
359
|
+
const afterSpace = skipSpace(text, j);
|
|
360
|
+
const c = text.charCodeAt(afterSpace);
|
|
361
|
+
if (afterSpace >= length)
|
|
362
|
+
throw new Reject("malformed_tag");
|
|
363
|
+
if (c === 62) {
|
|
364
|
+
j = afterSpace + 1;
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
if (c === 47) {
|
|
368
|
+
if (text.charCodeAt(afterSpace + 1) !== 62)
|
|
369
|
+
throw new Reject("malformed_tag");
|
|
370
|
+
selfClosing = true;
|
|
371
|
+
j = afterSpace + 2;
|
|
372
|
+
break;
|
|
373
|
+
}
|
|
374
|
+
if (afterSpace === j)
|
|
375
|
+
throw new Reject("malformed_tag"); // attributes need whitespace before them
|
|
376
|
+
const attrEnd = readName(text, afterSpace);
|
|
377
|
+
const attrName = text.slice(afterSpace, attrEnd);
|
|
378
|
+
let k = skipSpace(text, attrEnd);
|
|
379
|
+
if (text.charCodeAt(k) !== 61)
|
|
380
|
+
throw new Reject("invalid_attribute"); // =
|
|
381
|
+
k = skipSpace(text, k + 1);
|
|
382
|
+
const quote = text.charCodeAt(k);
|
|
383
|
+
if (quote !== 34 && quote !== 39)
|
|
384
|
+
throw new Reject("invalid_attribute");
|
|
385
|
+
const valueEnd = text.indexOf(quote === 34 ? '"' : "'", k + 1);
|
|
386
|
+
if (valueEnd < 0)
|
|
387
|
+
throw new Reject("invalid_attribute");
|
|
388
|
+
if (attributes.has(attrName))
|
|
389
|
+
throw new Reject("duplicate_attribute");
|
|
390
|
+
if (attributes.size + 1 > limits.maxAttributesPerElement)
|
|
391
|
+
throw new Reject("attribute_limit");
|
|
392
|
+
// A raw `<` is not allowed in a value; the check stays inside the value.
|
|
393
|
+
for (let v = k + 1; v < valueEnd; v++) {
|
|
394
|
+
if (text.charCodeAt(v) === 60)
|
|
395
|
+
throw new Reject("invalid_attribute");
|
|
396
|
+
}
|
|
397
|
+
const value = decode(text, k + 1, valueEnd, true);
|
|
398
|
+
if (XINCLUDE_NAMESPACES.has(value.trim()))
|
|
399
|
+
throw new Reject("external_reference");
|
|
400
|
+
attributes.set(attrName, value);
|
|
401
|
+
j = valueEnd + 1;
|
|
402
|
+
}
|
|
403
|
+
if (selfClosing) {
|
|
404
|
+
attach({ kind: "element", name, attributes, children: [] });
|
|
405
|
+
}
|
|
406
|
+
else {
|
|
407
|
+
stack.push({ name, attributes, children: [], text: [], cdata: false });
|
|
408
|
+
}
|
|
409
|
+
i = j;
|
|
410
|
+
}
|
|
411
|
+
if (stack.length > 0)
|
|
412
|
+
throw new Reject("unclosed_element");
|
|
413
|
+
if (root === null)
|
|
414
|
+
throw new Reject("no_root");
|
|
415
|
+
return { declaration, root, elementCount, maxDepth, bytes };
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Reads a payload. Never throws on input; a bad option value (a programming
|
|
419
|
+
* error) throws a `RangeError` with a fixed message.
|
|
420
|
+
*/
|
|
421
|
+
function readXml(text, options) {
|
|
422
|
+
const limits = resolveLimits(options);
|
|
423
|
+
if (typeof text !== "string")
|
|
424
|
+
return fail("not_text");
|
|
425
|
+
// UTF-8 never takes fewer bytes than UTF-16 code units, so the length is a cheap first check.
|
|
426
|
+
if (text.length > limits.maxBytes)
|
|
427
|
+
return fail("too_large");
|
|
428
|
+
const bytes = Buffer.byteLength(text, "utf8");
|
|
429
|
+
if (bytes > limits.maxBytes)
|
|
430
|
+
return fail("too_large");
|
|
431
|
+
try {
|
|
432
|
+
return { ok: true, document: parse(text, limits, bytes) };
|
|
433
|
+
}
|
|
434
|
+
catch (error) {
|
|
435
|
+
if (error instanceof Reject)
|
|
436
|
+
return fail(error.code);
|
|
437
|
+
throw error;
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
// ---------------------------------------------------------------------------
|
|
441
|
+
// Tree helpers
|
|
442
|
+
// ---------------------------------------------------------------------------
|
|
443
|
+
function getAttribute(element, name) {
|
|
444
|
+
return element.attributes.get(name);
|
|
445
|
+
}
|
|
446
|
+
/** Direct child elements, optionally only those with `name`. */
|
|
447
|
+
function childElements(element, name) {
|
|
448
|
+
const out = [];
|
|
449
|
+
for (const child of element.children) {
|
|
450
|
+
if (child.kind === "element" && (name === undefined || child.name === name))
|
|
451
|
+
out.push(child);
|
|
452
|
+
}
|
|
453
|
+
return out;
|
|
454
|
+
}
|
|
455
|
+
function firstChildElement(element, name) {
|
|
456
|
+
for (const child of element.children) {
|
|
457
|
+
if (child.kind === "element" && child.name === name)
|
|
458
|
+
return child;
|
|
459
|
+
}
|
|
460
|
+
return undefined;
|
|
461
|
+
}
|
|
462
|
+
/** The element's own text: its direct text and CDATA children, joined. Descendants are not included. */
|
|
463
|
+
function textContent(element) {
|
|
464
|
+
let out = "";
|
|
465
|
+
for (const child of element.children)
|
|
466
|
+
if (child.kind === "text")
|
|
467
|
+
out += child.value;
|
|
468
|
+
return out;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Visits every element in document order, iteratively. `ancestors` runs from
|
|
472
|
+
* the root to the element's parent and is only valid during the call. Return
|
|
473
|
+
* `"skip"` to leave an element's children unvisited.
|
|
474
|
+
*/
|
|
475
|
+
function walkElements(root, visit) {
|
|
476
|
+
const ancestors = [];
|
|
477
|
+
const pending = [{ element: root, depth: 0 }];
|
|
478
|
+
while (pending.length > 0) {
|
|
479
|
+
const { element, depth } = pending.pop();
|
|
480
|
+
ancestors.length = depth;
|
|
481
|
+
if (visit(element, ancestors) === "skip")
|
|
482
|
+
continue;
|
|
483
|
+
ancestors.push(element);
|
|
484
|
+
for (let k = element.children.length - 1; k >= 0; k--) {
|
|
485
|
+
const child = element.children[k];
|
|
486
|
+
if (child.kind === "element")
|
|
487
|
+
pending.push({ element: child, depth: depth + 1 });
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
function escapeText(value) {
|
|
492
|
+
return value.replace(/[&<>\r]/gu, (c) => (c === "&" ? "&" : c === "<" ? "<" : c === ">" ? ">" : " "));
|
|
493
|
+
}
|
|
494
|
+
function escapeAttribute(value) {
|
|
495
|
+
return value.replace(/[&<>"\t\n\r]/gu, (c) => {
|
|
496
|
+
switch (c) {
|
|
497
|
+
case "&":
|
|
498
|
+
return "&";
|
|
499
|
+
case "<":
|
|
500
|
+
return "<";
|
|
501
|
+
case ">":
|
|
502
|
+
return ">";
|
|
503
|
+
case '"':
|
|
504
|
+
return """;
|
|
505
|
+
case "\t":
|
|
506
|
+
return "	";
|
|
507
|
+
case "\n":
|
|
508
|
+
return " ";
|
|
509
|
+
default:
|
|
510
|
+
return " ";
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
}
|
|
514
|
+
/**
|
|
515
|
+
* Writes a tree back out, iteratively. CDATA comes out as escaped text, so
|
|
516
|
+
* nothing in a value can close a section early. The hooks let a caller mask
|
|
517
|
+
* text and attribute values on the way out (the payload view's masking)
|
|
518
|
+
* without copying the tree.
|
|
519
|
+
*/
|
|
520
|
+
function serializeXml(root, options) {
|
|
521
|
+
const out = [];
|
|
522
|
+
if (options?.declaration)
|
|
523
|
+
out.push('<?xml version="1.0" encoding="UTF-8"?>');
|
|
524
|
+
const ancestors = [];
|
|
525
|
+
const frames = [];
|
|
526
|
+
const open = (element) => {
|
|
527
|
+
ancestors.push(element);
|
|
528
|
+
out.push("<", element.name);
|
|
529
|
+
for (const [name, value] of element.attributes) {
|
|
530
|
+
const written = options?.attribute ? options.attribute(name, value, element, ancestors) : value;
|
|
531
|
+
out.push(" ", name, '="', escapeAttribute(written), '"');
|
|
532
|
+
}
|
|
533
|
+
if (element.children.length === 0) {
|
|
534
|
+
out.push("/>");
|
|
535
|
+
ancestors.pop();
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
out.push(">");
|
|
539
|
+
frames.push({ element, index: 0 });
|
|
540
|
+
};
|
|
541
|
+
open(root);
|
|
542
|
+
while (frames.length > 0) {
|
|
543
|
+
const frame = frames[frames.length - 1];
|
|
544
|
+
if (frame.index >= frame.element.children.length) {
|
|
545
|
+
out.push("</", frame.element.name, ">");
|
|
546
|
+
frames.pop();
|
|
547
|
+
ancestors.pop();
|
|
548
|
+
continue;
|
|
549
|
+
}
|
|
550
|
+
const child = frame.element.children[frame.index++];
|
|
551
|
+
if (child.kind === "text") {
|
|
552
|
+
const written = options?.text ? options.text(child.value, frame.element, ancestors) : child.value;
|
|
553
|
+
out.push(escapeText(written));
|
|
554
|
+
}
|
|
555
|
+
else {
|
|
556
|
+
open(child);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
return out.join("");
|
|
560
|
+
}
|
|
561
|
+
//# sourceMappingURL=xml-reader.js.map
|