UEA-Prodem
This commit is contained in:
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2018-2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
# @aws-sdk/xml-builder
|
||||
|
||||
[](https://www.npmjs.com/package/@aws-sdk/xml-builder)
|
||||
[](https://www.npmjs.com/package/@aws-sdk/xml-builder)
|
||||
|
||||
### :warning: Internal API :warning:
|
||||
|
||||
> This is an internal package.
|
||||
> That means this is used as a dependency for other, public packages, but
|
||||
> should not be taken directly as a dependency in your application's `package.json`.
|
||||
|
||||
> If you are updating the version of this package, for example to bring in a
|
||||
> bug-fix, you should do so by updating your application lockfile with
|
||||
> e.g. `npm up @scope/package` or equivalent command in another
|
||||
> package manager, rather than taking a direct dependency.
|
||||
|
||||
---
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
'use strict';
|
||||
|
||||
var xmlParser = require('./xml-parser');
|
||||
|
||||
const ATTR_ESCAPE_RE = /[&<>"]/g;
|
||||
const ATTR_ESCAPE_MAP = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
};
|
||||
function escapeAttribute(value) {
|
||||
return value.replace(ATTR_ESCAPE_RE, (ch) => ATTR_ESCAPE_MAP[ch]);
|
||||
}
|
||||
|
||||
const ELEMENT_ESCAPE_RE = /[&"'<>\r\n\u0085\u2028]/g;
|
||||
const ELEMENT_ESCAPE_MAP = {
|
||||
"&": "&",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
"\r": "
",
|
||||
"\n": "
",
|
||||
"\u0085": "…",
|
||||
"\u2028": "
",
|
||||
};
|
||||
function escapeElement(value) {
|
||||
return value.replace(ELEMENT_ESCAPE_RE, (ch) => ELEMENT_ESCAPE_MAP[ch]);
|
||||
}
|
||||
|
||||
class XmlText {
|
||||
value;
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
toString() {
|
||||
return escapeElement("" + this.value);
|
||||
}
|
||||
}
|
||||
|
||||
class XmlNode {
|
||||
name;
|
||||
children;
|
||||
attributes = {};
|
||||
static of(name, childText, withName) {
|
||||
const node = new XmlNode(name);
|
||||
if (childText !== undefined) {
|
||||
node.addChildNode(new XmlText(childText));
|
||||
}
|
||||
if (withName !== undefined) {
|
||||
node.withName(withName);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
constructor(name, children = []) {
|
||||
this.name = name;
|
||||
this.children = children;
|
||||
}
|
||||
withName(name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
addAttribute(name, value) {
|
||||
this.attributes[name] = value;
|
||||
return this;
|
||||
}
|
||||
addChildNode(child) {
|
||||
this.children.push(child);
|
||||
return this;
|
||||
}
|
||||
removeAttribute(name) {
|
||||
delete this.attributes[name];
|
||||
return this;
|
||||
}
|
||||
n(name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
c(child) {
|
||||
this.children.push(child);
|
||||
return this;
|
||||
}
|
||||
a(name, value) {
|
||||
if (value != null) {
|
||||
this.attributes[name] = value;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
cc(input, field, withName = field) {
|
||||
if (input[field] != null) {
|
||||
const node = XmlNode.of(field, input[field]).withName(withName);
|
||||
this.c(node);
|
||||
}
|
||||
}
|
||||
l(input, listName, memberName, valueProvider) {
|
||||
if (input[listName] != null) {
|
||||
const nodes = valueProvider();
|
||||
nodes.map((node) => {
|
||||
node.withName(memberName);
|
||||
this.c(node);
|
||||
});
|
||||
}
|
||||
}
|
||||
lc(input, listName, memberName, valueProvider) {
|
||||
if (input[listName] != null) {
|
||||
const nodes = valueProvider();
|
||||
const containerNode = new XmlNode(memberName);
|
||||
nodes.map((node) => {
|
||||
containerNode.c(node);
|
||||
});
|
||||
this.c(containerNode);
|
||||
}
|
||||
}
|
||||
toString() {
|
||||
const hasChildren = Boolean(this.children.length);
|
||||
let xmlText = `<${this.name}`;
|
||||
const attributes = this.attributes;
|
||||
for (const attributeName of Object.keys(attributes)) {
|
||||
const attribute = attributes[attributeName];
|
||||
if (attribute != null) {
|
||||
xmlText += ` ${attributeName}="${escapeAttribute("" + attribute)}"`;
|
||||
}
|
||||
}
|
||||
return (xmlText += !hasChildren ? "/>" : `>${this.children.map((c) => c.toString()).join("")}</${this.name}>`);
|
||||
}
|
||||
}
|
||||
|
||||
exports.parseXML = xmlParser.parseXML;
|
||||
exports.XmlNode = XmlNode;
|
||||
exports.XmlText = XmlText;
|
||||
+336
@@ -0,0 +1,336 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.EntityDecoderImpl = exports.CURRENCY = exports.COMMON_HTML = exports.XML = void 0;
|
||||
exports.XML = {
|
||||
amp: "&",
|
||||
apos: "'",
|
||||
gt: ">",
|
||||
lt: "<",
|
||||
quot: '"',
|
||||
};
|
||||
exports.COMMON_HTML = {
|
||||
nbsp: "\u00a0",
|
||||
copy: "\u00a9",
|
||||
reg: "\u00ae",
|
||||
trade: "\u2122",
|
||||
mdash: "\u2014",
|
||||
ndash: "\u2013",
|
||||
hellip: "\u2026",
|
||||
laquo: "\u00ab",
|
||||
raquo: "\u00bb",
|
||||
lsquo: "\u2018",
|
||||
rsquo: "\u2019",
|
||||
ldquo: "\u201c",
|
||||
rdquo: "\u201d",
|
||||
bull: "\u2022",
|
||||
para: "\u00b6",
|
||||
sect: "\u00a7",
|
||||
deg: "\u00b0",
|
||||
frac12: "\u00bd",
|
||||
frac14: "\u00bc",
|
||||
frac34: "\u00be",
|
||||
};
|
||||
exports.CURRENCY = {
|
||||
cent: "\u00a2",
|
||||
pound: "\u00a3",
|
||||
curren: "\u00a4",
|
||||
yen: "\u00a5",
|
||||
euro: "\u20ac",
|
||||
dollar: "$",
|
||||
fnof: "\u0192",
|
||||
inr: "\u20b9",
|
||||
af: "\u060b",
|
||||
birr: "\u1265\u122d",
|
||||
peso: "\u20b1",
|
||||
rub: "\u20bd",
|
||||
won: "\u20a9",
|
||||
yuan: "\u00a5",
|
||||
cedil: "\u00b8",
|
||||
};
|
||||
const SPECIAL_CHARS = new Set("!?\\/[]$%{}^&*()<>|+");
|
||||
function validateEntityName(name) {
|
||||
if (name[0] === "#") {
|
||||
throw new Error(`[EntityReplacer] Invalid character '#' in entity name: "${name}"`);
|
||||
}
|
||||
for (const ch of name) {
|
||||
if (SPECIAL_CHARS.has(ch)) {
|
||||
throw new Error(`[EntityReplacer] Invalid character '${ch}' in entity name: "${name}"`);
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
function mergeEntityMaps(...maps) {
|
||||
const out = Object.create(null);
|
||||
for (const map of maps) {
|
||||
if (!map) {
|
||||
continue;
|
||||
}
|
||||
for (const key of Object.keys(map)) {
|
||||
const raw = map[key];
|
||||
if (typeof raw === "string") {
|
||||
out[key] = raw;
|
||||
}
|
||||
else if (raw && typeof raw === "object" && raw.val !== undefined) {
|
||||
const val = raw.val;
|
||||
if (typeof val === "string") {
|
||||
out[key] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
const LIMIT_TIER_EXTERNAL = "external";
|
||||
const LIMIT_TIER_BASE = "base";
|
||||
const LIMIT_TIER_ALL = "all";
|
||||
function parseLimitTiers(raw) {
|
||||
if (!raw || raw === LIMIT_TIER_EXTERNAL) {
|
||||
return new Set([LIMIT_TIER_EXTERNAL]);
|
||||
}
|
||||
if (raw === LIMIT_TIER_ALL) {
|
||||
return new Set([LIMIT_TIER_ALL]);
|
||||
}
|
||||
if (raw === LIMIT_TIER_BASE) {
|
||||
return new Set([LIMIT_TIER_BASE]);
|
||||
}
|
||||
if (Array.isArray(raw)) {
|
||||
return new Set(raw);
|
||||
}
|
||||
return new Set([LIMIT_TIER_EXTERNAL]);
|
||||
}
|
||||
const NCR_LEVEL = Object.freeze({ allow: 0, leave: 1, remove: 2, throw: 3 });
|
||||
const XML10_ALLOWED_C0 = new Set([0x09, 0x0a, 0x0d]);
|
||||
function parseNCRConfig(ncr) {
|
||||
if (!ncr) {
|
||||
return { xmlVersion: 1.0, onLevel: NCR_LEVEL.allow, nullLevel: NCR_LEVEL.remove };
|
||||
}
|
||||
const xmlVersion = ncr.xmlVersion === 1.1 ? 1.1 : 1.0;
|
||||
const onLevel = NCR_LEVEL[ncr.onNCR ?? "allow"] ?? NCR_LEVEL.allow;
|
||||
const nullLevel = NCR_LEVEL[ncr.nullNCR ?? "remove"] ?? NCR_LEVEL.remove;
|
||||
const clampedNull = Math.max(nullLevel, NCR_LEVEL.remove);
|
||||
return { xmlVersion, onLevel, nullLevel: clampedNull };
|
||||
}
|
||||
const EntityDecoderImpl = class EntityDecoderImpl {
|
||||
_limit;
|
||||
_maxTotalExpansions;
|
||||
_maxExpandedLength;
|
||||
_postCheck;
|
||||
_limitTiers;
|
||||
_numericAllowed;
|
||||
_baseMap;
|
||||
_externalMap;
|
||||
_inputMap;
|
||||
_totalExpansions;
|
||||
_expandedLength;
|
||||
_removeSet;
|
||||
_leaveSet;
|
||||
_ncrXmlVersion;
|
||||
_ncrOnLevel;
|
||||
_ncrNullLevel;
|
||||
constructor(options = {}) {
|
||||
this._limit = options.limit || {};
|
||||
this._maxTotalExpansions = this._limit.maxTotalExpansions || 0;
|
||||
this._maxExpandedLength = this._limit.maxExpandedLength || 0;
|
||||
this._postCheck = typeof options.postCheck === "function" ? options.postCheck : (r) => r;
|
||||
this._limitTiers = parseLimitTiers(this._limit.applyLimitsTo ?? LIMIT_TIER_EXTERNAL);
|
||||
this._numericAllowed = options.numericAllowed ?? true;
|
||||
this._baseMap = mergeEntityMaps(exports.XML, options.namedEntities || null);
|
||||
this._externalMap = Object.create(null);
|
||||
this._inputMap = Object.create(null);
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
this._removeSet = new Set(options.remove && Array.isArray(options.remove) ? options.remove : []);
|
||||
this._leaveSet = new Set(options.leave && Array.isArray(options.leave) ? options.leave : []);
|
||||
const ncrCfg = parseNCRConfig(options.ncr);
|
||||
this._ncrXmlVersion = ncrCfg.xmlVersion;
|
||||
this._ncrOnLevel = ncrCfg.onLevel;
|
||||
this._ncrNullLevel = ncrCfg.nullLevel;
|
||||
}
|
||||
setExternalEntities(map) {
|
||||
if (map) {
|
||||
for (const key of Object.keys(map)) {
|
||||
validateEntityName(key);
|
||||
}
|
||||
}
|
||||
this._externalMap = mergeEntityMaps(map);
|
||||
}
|
||||
addExternalEntity(key, value) {
|
||||
validateEntityName(key);
|
||||
if (typeof value === "string" && value.indexOf("&") === -1) {
|
||||
this._externalMap[key] = value;
|
||||
}
|
||||
}
|
||||
addInputEntities(map) {
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
this._inputMap = mergeEntityMaps(map);
|
||||
}
|
||||
reset() {
|
||||
this._inputMap = Object.create(null);
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
return this;
|
||||
}
|
||||
setXmlVersion(version) {
|
||||
this._ncrXmlVersion = version === "1.1" || version === 1.1 ? 1.1 : 1.0;
|
||||
}
|
||||
decode(str) {
|
||||
if (typeof str !== "string" || str.length === 0) {
|
||||
return str;
|
||||
}
|
||||
const original = str;
|
||||
const chunks = [];
|
||||
const len = str.length;
|
||||
let last = 0;
|
||||
let i = 0;
|
||||
const limitExpansions = this._maxTotalExpansions > 0;
|
||||
const limitLength = this._maxExpandedLength > 0;
|
||||
const checkLimits = limitExpansions || limitLength;
|
||||
while (i < len) {
|
||||
if (str.charCodeAt(i) !== 38) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
let j = i + 1;
|
||||
while (j < len && str.charCodeAt(j) !== 59 && j - i <= 32) {
|
||||
j++;
|
||||
}
|
||||
if (j >= len || str.charCodeAt(j) !== 59) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
const token = str.slice(i + 1, j);
|
||||
if (token.length === 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
let replacement;
|
||||
let tier;
|
||||
if (this._removeSet.has(token)) {
|
||||
replacement = "";
|
||||
if (tier === undefined) {
|
||||
tier = LIMIT_TIER_EXTERNAL;
|
||||
}
|
||||
}
|
||||
else if (this._leaveSet.has(token)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
else if (token.charCodeAt(0) === 35) {
|
||||
const ncrResult = this._resolveNCR(token);
|
||||
if (ncrResult === undefined) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
replacement = ncrResult;
|
||||
tier = LIMIT_TIER_BASE;
|
||||
}
|
||||
else {
|
||||
const resolved = this._resolveName(token);
|
||||
replacement = resolved?.value;
|
||||
tier = resolved?.tier;
|
||||
}
|
||||
if (replacement === undefined) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (i > last) {
|
||||
chunks.push(str.slice(last, i));
|
||||
}
|
||||
chunks.push(replacement);
|
||||
last = j + 1;
|
||||
i = last;
|
||||
if (checkLimits && this._tierCounts(tier)) {
|
||||
if (limitExpansions) {
|
||||
this._totalExpansions++;
|
||||
if (this._totalExpansions > this._maxTotalExpansions) {
|
||||
throw new Error(`[EntityReplacer] Entity expansion count limit exceeded: ` +
|
||||
`${this._totalExpansions} > ${this._maxTotalExpansions}`);
|
||||
}
|
||||
}
|
||||
if (limitLength) {
|
||||
const delta = replacement.length - (token.length + 2);
|
||||
if (delta > 0) {
|
||||
this._expandedLength += delta;
|
||||
if (this._expandedLength > this._maxExpandedLength) {
|
||||
throw new Error(`[EntityReplacer] Expanded content length limit exceeded: ` +
|
||||
`${this._expandedLength} > ${this._maxExpandedLength}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (last < len) {
|
||||
chunks.push(str.slice(last));
|
||||
}
|
||||
const result = chunks.length === 0 ? str : chunks.join("");
|
||||
return this._postCheck(result, original);
|
||||
}
|
||||
_tierCounts(tier) {
|
||||
if (this._limitTiers.has(LIMIT_TIER_ALL)) {
|
||||
return true;
|
||||
}
|
||||
return this._limitTiers.has(tier);
|
||||
}
|
||||
_resolveName(name) {
|
||||
if (name in this._inputMap) {
|
||||
return { value: this._inputMap[name], tier: LIMIT_TIER_EXTERNAL };
|
||||
}
|
||||
if (name in this._externalMap) {
|
||||
return { value: this._externalMap[name], tier: LIMIT_TIER_EXTERNAL };
|
||||
}
|
||||
if (name in this._baseMap) {
|
||||
return { value: this._baseMap[name], tier: LIMIT_TIER_BASE };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
_classifyNCR(cp) {
|
||||
if (cp === 0) {
|
||||
return this._ncrNullLevel;
|
||||
}
|
||||
if (cp >= 0xd800 && cp <= 0xdfff) {
|
||||
return NCR_LEVEL.remove;
|
||||
}
|
||||
if (this._ncrXmlVersion === 1.0) {
|
||||
if (cp >= 0x01 && cp <= 0x1f && !XML10_ALLOWED_C0.has(cp)) {
|
||||
return NCR_LEVEL.remove;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
_applyNCRAction(action, token, cp) {
|
||||
switch (action) {
|
||||
case NCR_LEVEL.allow:
|
||||
return String.fromCodePoint(cp);
|
||||
case NCR_LEVEL.remove:
|
||||
return "";
|
||||
case NCR_LEVEL.leave:
|
||||
return undefined;
|
||||
case NCR_LEVEL.throw:
|
||||
throw new Error(`[EntityDecoder] Prohibited numeric character reference ` +
|
||||
`&${token}; (U+${cp.toString(16).toUpperCase().padStart(4, "0")})`);
|
||||
default:
|
||||
return String.fromCodePoint(cp);
|
||||
}
|
||||
}
|
||||
_resolveNCR(token) {
|
||||
const second = token.charCodeAt(1);
|
||||
let cp;
|
||||
if (second === 120 || second === 88) {
|
||||
cp = parseInt(token.slice(2), 16);
|
||||
}
|
||||
else {
|
||||
cp = parseInt(token.slice(1), 10);
|
||||
}
|
||||
if (Number.isNaN(cp) || cp < 0 || cp > 0x10ffff) {
|
||||
return undefined;
|
||||
}
|
||||
const minimum = this._classifyNCR(cp);
|
||||
if (!this._numericAllowed && minimum < NCR_LEVEL.remove) {
|
||||
return undefined;
|
||||
}
|
||||
const effective = minimum === -1 ? this._ncrOnLevel : Math.max(this._ncrOnLevel, minimum);
|
||||
return this._applyNCRAction(effective, token, cp);
|
||||
}
|
||||
};
|
||||
exports.EntityDecoderImpl = EntityDecoderImpl;
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.parseXML = parseXML;
|
||||
let parser;
|
||||
function parseXML(xmlString) {
|
||||
if (!parser) {
|
||||
parser = new DOMParser();
|
||||
}
|
||||
const xmlDocument = parser.parseFromString(xmlString, "application/xml");
|
||||
if (xmlDocument.getElementsByTagName("parsererror").length > 0) {
|
||||
throw new Error("DOMParser XML parsing error.");
|
||||
}
|
||||
const xmlToObj = (node) => {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
if (node.textContent?.trim()) {
|
||||
return node.textContent;
|
||||
}
|
||||
}
|
||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
const element = node;
|
||||
if (element.attributes.length === 0 && element.childNodes.length === 0) {
|
||||
return "";
|
||||
}
|
||||
const obj = {};
|
||||
const attributes = Array.from(element.attributes);
|
||||
for (const attr of attributes) {
|
||||
obj[`${attr.name}`] = attr.value;
|
||||
}
|
||||
const childNodes = Array.from(element.childNodes);
|
||||
for (const child of childNodes) {
|
||||
const childResult = xmlToObj(child);
|
||||
if (childResult != null) {
|
||||
const childName = child.nodeName;
|
||||
if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") {
|
||||
return childResult;
|
||||
}
|
||||
if (obj[childName]) {
|
||||
if (Array.isArray(obj[childName])) {
|
||||
obj[childName].push(childResult);
|
||||
}
|
||||
else {
|
||||
obj[childName] = [obj[childName], childResult];
|
||||
}
|
||||
}
|
||||
else {
|
||||
obj[childName] = childResult;
|
||||
}
|
||||
}
|
||||
else if (childNodes.length === 1 && attributes.length === 0) {
|
||||
return element.textContent;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return {
|
||||
[xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement),
|
||||
};
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.parseXML = parseXML;
|
||||
const fast_xml_parser_1 = require("fast-xml-parser");
|
||||
const nodable_entities_1 = require("./xml-external/nodable_entities");
|
||||
const entityDecoder = new nodable_entities_1.EntityDecoderImpl({
|
||||
namedEntities: { ...nodable_entities_1.XML, ...nodable_entities_1.COMMON_HTML, ...nodable_entities_1.CURRENCY },
|
||||
numericAllowed: true,
|
||||
limit: {
|
||||
maxTotalExpansions: Infinity,
|
||||
},
|
||||
ncr: {
|
||||
xmlVersion: 1.1,
|
||||
},
|
||||
});
|
||||
const parser = new fast_xml_parser_1.XMLParser({
|
||||
attributeNamePrefix: "",
|
||||
processEntities: {
|
||||
enabled: true,
|
||||
maxTotalExpansions: Infinity,
|
||||
},
|
||||
htmlEntities: true,
|
||||
entityDecoder: {
|
||||
setExternalEntities: (entities) => {
|
||||
entityDecoder.setExternalEntities(entities);
|
||||
},
|
||||
addInputEntities: (entities) => {
|
||||
entityDecoder.addInputEntities(entities);
|
||||
},
|
||||
reset: () => {
|
||||
entityDecoder.reset();
|
||||
},
|
||||
decode: (text) => {
|
||||
return entityDecoder.decode(text);
|
||||
},
|
||||
setXmlVersion: (version) => void {},
|
||||
},
|
||||
ignoreAttributes: false,
|
||||
ignoreDeclaration: true,
|
||||
parseTagValue: false,
|
||||
trimValues: false,
|
||||
tagValueProcessor: (_, val) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
|
||||
maxNestedTags: Infinity,
|
||||
});
|
||||
function parseXML(xmlString) {
|
||||
return parser.parse(xmlString, true);
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
import { escapeAttribute } from "./escape-attribute";
|
||||
import { XmlText } from "./XmlText";
|
||||
export class XmlNode {
|
||||
name;
|
||||
children;
|
||||
attributes = {};
|
||||
static of(name, childText, withName) {
|
||||
const node = new XmlNode(name);
|
||||
if (childText !== undefined) {
|
||||
node.addChildNode(new XmlText(childText));
|
||||
}
|
||||
if (withName !== undefined) {
|
||||
node.withName(withName);
|
||||
}
|
||||
return node;
|
||||
}
|
||||
constructor(name, children = []) {
|
||||
this.name = name;
|
||||
this.children = children;
|
||||
}
|
||||
withName(name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
addAttribute(name, value) {
|
||||
this.attributes[name] = value;
|
||||
return this;
|
||||
}
|
||||
addChildNode(child) {
|
||||
this.children.push(child);
|
||||
return this;
|
||||
}
|
||||
removeAttribute(name) {
|
||||
delete this.attributes[name];
|
||||
return this;
|
||||
}
|
||||
n(name) {
|
||||
this.name = name;
|
||||
return this;
|
||||
}
|
||||
c(child) {
|
||||
this.children.push(child);
|
||||
return this;
|
||||
}
|
||||
a(name, value) {
|
||||
if (value != null) {
|
||||
this.attributes[name] = value;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
cc(input, field, withName = field) {
|
||||
if (input[field] != null) {
|
||||
const node = XmlNode.of(field, input[field]).withName(withName);
|
||||
this.c(node);
|
||||
}
|
||||
}
|
||||
l(input, listName, memberName, valueProvider) {
|
||||
if (input[listName] != null) {
|
||||
const nodes = valueProvider();
|
||||
nodes.map((node) => {
|
||||
node.withName(memberName);
|
||||
this.c(node);
|
||||
});
|
||||
}
|
||||
}
|
||||
lc(input, listName, memberName, valueProvider) {
|
||||
if (input[listName] != null) {
|
||||
const nodes = valueProvider();
|
||||
const containerNode = new XmlNode(memberName);
|
||||
nodes.map((node) => {
|
||||
containerNode.c(node);
|
||||
});
|
||||
this.c(containerNode);
|
||||
}
|
||||
}
|
||||
toString() {
|
||||
const hasChildren = Boolean(this.children.length);
|
||||
let xmlText = `<${this.name}`;
|
||||
const attributes = this.attributes;
|
||||
for (const attributeName of Object.keys(attributes)) {
|
||||
const attribute = attributes[attributeName];
|
||||
if (attribute != null) {
|
||||
xmlText += ` ${attributeName}="${escapeAttribute("" + attribute)}"`;
|
||||
}
|
||||
}
|
||||
return (xmlText += !hasChildren ? "/>" : `>${this.children.map((c) => c.toString()).join("")}</${this.name}>`);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import { escapeElement } from "./escape-element";
|
||||
export class XmlText {
|
||||
value;
|
||||
constructor(value) {
|
||||
this.value = value;
|
||||
}
|
||||
toString() {
|
||||
return escapeElement("" + this.value);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
const ATTR_ESCAPE_RE = /[&<>"]/g;
|
||||
const ATTR_ESCAPE_MAP = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
};
|
||||
export function escapeAttribute(value) {
|
||||
return value.replace(ATTR_ESCAPE_RE, (ch) => ATTR_ESCAPE_MAP[ch]);
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
const ELEMENT_ESCAPE_RE = /[&"'<>\r\n\u0085\u2028]/g;
|
||||
const ELEMENT_ESCAPE_MAP = {
|
||||
"&": "&",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
"\r": "
",
|
||||
"\n": "
",
|
||||
"\u0085": "…",
|
||||
"\u2028": "
",
|
||||
};
|
||||
export function escapeElement(value) {
|
||||
return value.replace(ELEMENT_ESCAPE_RE, (ch) => ELEMENT_ESCAPE_MAP[ch]);
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export * from "./XmlNode";
|
||||
export * from "./XmlText";
|
||||
export { parseXML } from "./xml-parser";
|
||||
+1
@@ -0,0 +1 @@
|
||||
export {};
|
||||
+332
@@ -0,0 +1,332 @@
|
||||
export const XML = {
|
||||
amp: "&",
|
||||
apos: "'",
|
||||
gt: ">",
|
||||
lt: "<",
|
||||
quot: '"',
|
||||
};
|
||||
export const COMMON_HTML = {
|
||||
nbsp: "\u00a0",
|
||||
copy: "\u00a9",
|
||||
reg: "\u00ae",
|
||||
trade: "\u2122",
|
||||
mdash: "\u2014",
|
||||
ndash: "\u2013",
|
||||
hellip: "\u2026",
|
||||
laquo: "\u00ab",
|
||||
raquo: "\u00bb",
|
||||
lsquo: "\u2018",
|
||||
rsquo: "\u2019",
|
||||
ldquo: "\u201c",
|
||||
rdquo: "\u201d",
|
||||
bull: "\u2022",
|
||||
para: "\u00b6",
|
||||
sect: "\u00a7",
|
||||
deg: "\u00b0",
|
||||
frac12: "\u00bd",
|
||||
frac14: "\u00bc",
|
||||
frac34: "\u00be",
|
||||
};
|
||||
export const CURRENCY = {
|
||||
cent: "\u00a2",
|
||||
pound: "\u00a3",
|
||||
curren: "\u00a4",
|
||||
yen: "\u00a5",
|
||||
euro: "\u20ac",
|
||||
dollar: "$",
|
||||
fnof: "\u0192",
|
||||
inr: "\u20b9",
|
||||
af: "\u060b",
|
||||
birr: "\u1265\u122d",
|
||||
peso: "\u20b1",
|
||||
rub: "\u20bd",
|
||||
won: "\u20a9",
|
||||
yuan: "\u00a5",
|
||||
cedil: "\u00b8",
|
||||
};
|
||||
const SPECIAL_CHARS = new Set("!?\\/[]$%{}^&*()<>|+");
|
||||
function validateEntityName(name) {
|
||||
if (name[0] === "#") {
|
||||
throw new Error(`[EntityReplacer] Invalid character '#' in entity name: "${name}"`);
|
||||
}
|
||||
for (const ch of name) {
|
||||
if (SPECIAL_CHARS.has(ch)) {
|
||||
throw new Error(`[EntityReplacer] Invalid character '${ch}' in entity name: "${name}"`);
|
||||
}
|
||||
}
|
||||
return name;
|
||||
}
|
||||
function mergeEntityMaps(...maps) {
|
||||
const out = Object.create(null);
|
||||
for (const map of maps) {
|
||||
if (!map) {
|
||||
continue;
|
||||
}
|
||||
for (const key of Object.keys(map)) {
|
||||
const raw = map[key];
|
||||
if (typeof raw === "string") {
|
||||
out[key] = raw;
|
||||
}
|
||||
else if (raw && typeof raw === "object" && raw.val !== undefined) {
|
||||
const val = raw.val;
|
||||
if (typeof val === "string") {
|
||||
out[key] = val;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
const LIMIT_TIER_EXTERNAL = "external";
|
||||
const LIMIT_TIER_BASE = "base";
|
||||
const LIMIT_TIER_ALL = "all";
|
||||
function parseLimitTiers(raw) {
|
||||
if (!raw || raw === LIMIT_TIER_EXTERNAL) {
|
||||
return new Set([LIMIT_TIER_EXTERNAL]);
|
||||
}
|
||||
if (raw === LIMIT_TIER_ALL) {
|
||||
return new Set([LIMIT_TIER_ALL]);
|
||||
}
|
||||
if (raw === LIMIT_TIER_BASE) {
|
||||
return new Set([LIMIT_TIER_BASE]);
|
||||
}
|
||||
if (Array.isArray(raw)) {
|
||||
return new Set(raw);
|
||||
}
|
||||
return new Set([LIMIT_TIER_EXTERNAL]);
|
||||
}
|
||||
const NCR_LEVEL = Object.freeze({ allow: 0, leave: 1, remove: 2, throw: 3 });
|
||||
const XML10_ALLOWED_C0 = new Set([0x09, 0x0a, 0x0d]);
|
||||
function parseNCRConfig(ncr) {
|
||||
if (!ncr) {
|
||||
return { xmlVersion: 1.0, onLevel: NCR_LEVEL.allow, nullLevel: NCR_LEVEL.remove };
|
||||
}
|
||||
const xmlVersion = ncr.xmlVersion === 1.1 ? 1.1 : 1.0;
|
||||
const onLevel = NCR_LEVEL[ncr.onNCR ?? "allow"] ?? NCR_LEVEL.allow;
|
||||
const nullLevel = NCR_LEVEL[ncr.nullNCR ?? "remove"] ?? NCR_LEVEL.remove;
|
||||
const clampedNull = Math.max(nullLevel, NCR_LEVEL.remove);
|
||||
return { xmlVersion, onLevel, nullLevel: clampedNull };
|
||||
}
|
||||
export const EntityDecoderImpl = class EntityDecoderImpl {
|
||||
_limit;
|
||||
_maxTotalExpansions;
|
||||
_maxExpandedLength;
|
||||
_postCheck;
|
||||
_limitTiers;
|
||||
_numericAllowed;
|
||||
_baseMap;
|
||||
_externalMap;
|
||||
_inputMap;
|
||||
_totalExpansions;
|
||||
_expandedLength;
|
||||
_removeSet;
|
||||
_leaveSet;
|
||||
_ncrXmlVersion;
|
||||
_ncrOnLevel;
|
||||
_ncrNullLevel;
|
||||
constructor(options = {}) {
|
||||
this._limit = options.limit || {};
|
||||
this._maxTotalExpansions = this._limit.maxTotalExpansions || 0;
|
||||
this._maxExpandedLength = this._limit.maxExpandedLength || 0;
|
||||
this._postCheck = typeof options.postCheck === "function" ? options.postCheck : (r) => r;
|
||||
this._limitTiers = parseLimitTiers(this._limit.applyLimitsTo ?? LIMIT_TIER_EXTERNAL);
|
||||
this._numericAllowed = options.numericAllowed ?? true;
|
||||
this._baseMap = mergeEntityMaps(XML, options.namedEntities || null);
|
||||
this._externalMap = Object.create(null);
|
||||
this._inputMap = Object.create(null);
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
this._removeSet = new Set(options.remove && Array.isArray(options.remove) ? options.remove : []);
|
||||
this._leaveSet = new Set(options.leave && Array.isArray(options.leave) ? options.leave : []);
|
||||
const ncrCfg = parseNCRConfig(options.ncr);
|
||||
this._ncrXmlVersion = ncrCfg.xmlVersion;
|
||||
this._ncrOnLevel = ncrCfg.onLevel;
|
||||
this._ncrNullLevel = ncrCfg.nullLevel;
|
||||
}
|
||||
setExternalEntities(map) {
|
||||
if (map) {
|
||||
for (const key of Object.keys(map)) {
|
||||
validateEntityName(key);
|
||||
}
|
||||
}
|
||||
this._externalMap = mergeEntityMaps(map);
|
||||
}
|
||||
addExternalEntity(key, value) {
|
||||
validateEntityName(key);
|
||||
if (typeof value === "string" && value.indexOf("&") === -1) {
|
||||
this._externalMap[key] = value;
|
||||
}
|
||||
}
|
||||
addInputEntities(map) {
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
this._inputMap = mergeEntityMaps(map);
|
||||
}
|
||||
reset() {
|
||||
this._inputMap = Object.create(null);
|
||||
this._totalExpansions = 0;
|
||||
this._expandedLength = 0;
|
||||
return this;
|
||||
}
|
||||
setXmlVersion(version) {
|
||||
this._ncrXmlVersion = version === "1.1" || version === 1.1 ? 1.1 : 1.0;
|
||||
}
|
||||
decode(str) {
|
||||
if (typeof str !== "string" || str.length === 0) {
|
||||
return str;
|
||||
}
|
||||
const original = str;
|
||||
const chunks = [];
|
||||
const len = str.length;
|
||||
let last = 0;
|
||||
let i = 0;
|
||||
const limitExpansions = this._maxTotalExpansions > 0;
|
||||
const limitLength = this._maxExpandedLength > 0;
|
||||
const checkLimits = limitExpansions || limitLength;
|
||||
while (i < len) {
|
||||
if (str.charCodeAt(i) !== 38) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
let j = i + 1;
|
||||
while (j < len && str.charCodeAt(j) !== 59 && j - i <= 32) {
|
||||
j++;
|
||||
}
|
||||
if (j >= len || str.charCodeAt(j) !== 59) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
const token = str.slice(i + 1, j);
|
||||
if (token.length === 0) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
let replacement;
|
||||
let tier;
|
||||
if (this._removeSet.has(token)) {
|
||||
replacement = "";
|
||||
if (tier === undefined) {
|
||||
tier = LIMIT_TIER_EXTERNAL;
|
||||
}
|
||||
}
|
||||
else if (this._leaveSet.has(token)) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
else if (token.charCodeAt(0) === 35) {
|
||||
const ncrResult = this._resolveNCR(token);
|
||||
if (ncrResult === undefined) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
replacement = ncrResult;
|
||||
tier = LIMIT_TIER_BASE;
|
||||
}
|
||||
else {
|
||||
const resolved = this._resolveName(token);
|
||||
replacement = resolved?.value;
|
||||
tier = resolved?.tier;
|
||||
}
|
||||
if (replacement === undefined) {
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (i > last) {
|
||||
chunks.push(str.slice(last, i));
|
||||
}
|
||||
chunks.push(replacement);
|
||||
last = j + 1;
|
||||
i = last;
|
||||
if (checkLimits && this._tierCounts(tier)) {
|
||||
if (limitExpansions) {
|
||||
this._totalExpansions++;
|
||||
if (this._totalExpansions > this._maxTotalExpansions) {
|
||||
throw new Error(`[EntityReplacer] Entity expansion count limit exceeded: ` +
|
||||
`${this._totalExpansions} > ${this._maxTotalExpansions}`);
|
||||
}
|
||||
}
|
||||
if (limitLength) {
|
||||
const delta = replacement.length - (token.length + 2);
|
||||
if (delta > 0) {
|
||||
this._expandedLength += delta;
|
||||
if (this._expandedLength > this._maxExpandedLength) {
|
||||
throw new Error(`[EntityReplacer] Expanded content length limit exceeded: ` +
|
||||
`${this._expandedLength} > ${this._maxExpandedLength}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (last < len) {
|
||||
chunks.push(str.slice(last));
|
||||
}
|
||||
const result = chunks.length === 0 ? str : chunks.join("");
|
||||
return this._postCheck(result, original);
|
||||
}
|
||||
_tierCounts(tier) {
|
||||
if (this._limitTiers.has(LIMIT_TIER_ALL)) {
|
||||
return true;
|
||||
}
|
||||
return this._limitTiers.has(tier);
|
||||
}
|
||||
_resolveName(name) {
|
||||
if (name in this._inputMap) {
|
||||
return { value: this._inputMap[name], tier: LIMIT_TIER_EXTERNAL };
|
||||
}
|
||||
if (name in this._externalMap) {
|
||||
return { value: this._externalMap[name], tier: LIMIT_TIER_EXTERNAL };
|
||||
}
|
||||
if (name in this._baseMap) {
|
||||
return { value: this._baseMap[name], tier: LIMIT_TIER_BASE };
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
_classifyNCR(cp) {
|
||||
if (cp === 0) {
|
||||
return this._ncrNullLevel;
|
||||
}
|
||||
if (cp >= 0xd800 && cp <= 0xdfff) {
|
||||
return NCR_LEVEL.remove;
|
||||
}
|
||||
if (this._ncrXmlVersion === 1.0) {
|
||||
if (cp >= 0x01 && cp <= 0x1f && !XML10_ALLOWED_C0.has(cp)) {
|
||||
return NCR_LEVEL.remove;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
_applyNCRAction(action, token, cp) {
|
||||
switch (action) {
|
||||
case NCR_LEVEL.allow:
|
||||
return String.fromCodePoint(cp);
|
||||
case NCR_LEVEL.remove:
|
||||
return "";
|
||||
case NCR_LEVEL.leave:
|
||||
return undefined;
|
||||
case NCR_LEVEL.throw:
|
||||
throw new Error(`[EntityDecoder] Prohibited numeric character reference ` +
|
||||
`&${token}; (U+${cp.toString(16).toUpperCase().padStart(4, "0")})`);
|
||||
default:
|
||||
return String.fromCodePoint(cp);
|
||||
}
|
||||
}
|
||||
_resolveNCR(token) {
|
||||
const second = token.charCodeAt(1);
|
||||
let cp;
|
||||
if (second === 120 || second === 88) {
|
||||
cp = parseInt(token.slice(2), 16);
|
||||
}
|
||||
else {
|
||||
cp = parseInt(token.slice(1), 10);
|
||||
}
|
||||
if (Number.isNaN(cp) || cp < 0 || cp > 0x10ffff) {
|
||||
return undefined;
|
||||
}
|
||||
const minimum = this._classifyNCR(cp);
|
||||
if (!this._numericAllowed && minimum < NCR_LEVEL.remove) {
|
||||
return undefined;
|
||||
}
|
||||
const effective = minimum === -1 ? this._ncrOnLevel : Math.max(this._ncrOnLevel, minimum);
|
||||
return this._applyNCRAction(effective, token, cp);
|
||||
}
|
||||
};
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
let parser;
|
||||
export function parseXML(xmlString) {
|
||||
if (!parser) {
|
||||
parser = new DOMParser();
|
||||
}
|
||||
const xmlDocument = parser.parseFromString(xmlString, "application/xml");
|
||||
if (xmlDocument.getElementsByTagName("parsererror").length > 0) {
|
||||
throw new Error("DOMParser XML parsing error.");
|
||||
}
|
||||
const xmlToObj = (node) => {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
if (node.textContent?.trim()) {
|
||||
return node.textContent;
|
||||
}
|
||||
}
|
||||
if (node.nodeType === Node.ELEMENT_NODE) {
|
||||
const element = node;
|
||||
if (element.attributes.length === 0 && element.childNodes.length === 0) {
|
||||
return "";
|
||||
}
|
||||
const obj = {};
|
||||
const attributes = Array.from(element.attributes);
|
||||
for (const attr of attributes) {
|
||||
obj[`${attr.name}`] = attr.value;
|
||||
}
|
||||
const childNodes = Array.from(element.childNodes);
|
||||
for (const child of childNodes) {
|
||||
const childResult = xmlToObj(child);
|
||||
if (childResult != null) {
|
||||
const childName = child.nodeName;
|
||||
if (childNodes.length === 1 && attributes.length === 0 && childName === "#text") {
|
||||
return childResult;
|
||||
}
|
||||
if (obj[childName]) {
|
||||
if (Array.isArray(obj[childName])) {
|
||||
obj[childName].push(childResult);
|
||||
}
|
||||
else {
|
||||
obj[childName] = [obj[childName], childResult];
|
||||
}
|
||||
}
|
||||
else {
|
||||
obj[childName] = childResult;
|
||||
}
|
||||
}
|
||||
else if (childNodes.length === 1 && attributes.length === 0) {
|
||||
return element.textContent;
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
return {
|
||||
[xmlDocument.documentElement.nodeName]: xmlToObj(xmlDocument.documentElement),
|
||||
};
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import { XMLParser } from "fast-xml-parser";
|
||||
import { COMMON_HTML, CURRENCY, EntityDecoderImpl, XML } from "./xml-external/nodable_entities";
|
||||
const entityDecoder = new EntityDecoderImpl({
|
||||
namedEntities: { ...XML, ...COMMON_HTML, ...CURRENCY },
|
||||
numericAllowed: true,
|
||||
limit: {
|
||||
maxTotalExpansions: Infinity,
|
||||
},
|
||||
ncr: {
|
||||
xmlVersion: 1.1,
|
||||
},
|
||||
});
|
||||
const parser = new XMLParser({
|
||||
attributeNamePrefix: "",
|
||||
processEntities: {
|
||||
enabled: true,
|
||||
maxTotalExpansions: Infinity,
|
||||
},
|
||||
htmlEntities: true,
|
||||
entityDecoder: {
|
||||
setExternalEntities: (entities) => {
|
||||
entityDecoder.setExternalEntities(entities);
|
||||
},
|
||||
addInputEntities: (entities) => {
|
||||
entityDecoder.addInputEntities(entities);
|
||||
},
|
||||
reset: () => {
|
||||
entityDecoder.reset();
|
||||
},
|
||||
decode: (text) => {
|
||||
return entityDecoder.decode(text);
|
||||
},
|
||||
setXmlVersion: (version) => void {},
|
||||
},
|
||||
ignoreAttributes: false,
|
||||
ignoreDeclaration: true,
|
||||
parseTagValue: false,
|
||||
trimValues: false,
|
||||
tagValueProcessor: (_, val) => (val.trim() === "" && val.includes("\n") ? "" : undefined),
|
||||
maxNestedTags: Infinity,
|
||||
});
|
||||
export function parseXML(xmlString) {
|
||||
return parser.parse(xmlString, true);
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
import type { Stringable } from "./stringable";
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* Represents an XML node.
|
||||
*/
|
||||
export declare class XmlNode {
|
||||
private name;
|
||||
readonly children: Stringable[];
|
||||
private attributes;
|
||||
static of(name: string, childText?: string, withName?: string): XmlNode;
|
||||
constructor(name: string, children?: Stringable[]);
|
||||
withName(name: string): XmlNode;
|
||||
addAttribute(name: string, value: any): XmlNode;
|
||||
addChildNode(child: Stringable): XmlNode;
|
||||
removeAttribute(name: string): XmlNode;
|
||||
/**
|
||||
* @internal
|
||||
* Alias of {@link XmlNode#withName(string)} for codegen brevity.
|
||||
*/
|
||||
n(name: string): XmlNode;
|
||||
/**
|
||||
* @internal
|
||||
* Alias of {@link XmlNode#addChildNode(string)} for codegen brevity.
|
||||
*/
|
||||
c(child: Stringable): XmlNode;
|
||||
/**
|
||||
* @internal
|
||||
* Checked version of {@link XmlNode#addAttribute(string)} for codegen brevity.
|
||||
*/
|
||||
a(name: string, value: any): XmlNode;
|
||||
/**
|
||||
* Create a child node.
|
||||
* Used in serialization of string fields.
|
||||
* @internal
|
||||
*/
|
||||
cc(input: any, field: string, withName?: string): void;
|
||||
/**
|
||||
* Creates list child nodes.
|
||||
* @internal
|
||||
*/
|
||||
l(input: any, listName: string, memberName: string, valueProvider: Function): void;
|
||||
/**
|
||||
* Creates list child nodes with container.
|
||||
* @internal
|
||||
*/
|
||||
lc(input: any, listName: string, memberName: string, valueProvider: Function): void;
|
||||
toString(): string;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import type { Stringable } from "./stringable";
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* Represents an XML text value.
|
||||
*/
|
||||
export declare class XmlText implements Stringable {
|
||||
private value;
|
||||
constructor(value: string);
|
||||
toString(): string;
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* Escapes characters that can not be in an XML attribute.
|
||||
*/
|
||||
export declare function escapeAttribute(value: string): string;
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* @internal
|
||||
*
|
||||
* Escapes characters that can not be in an XML element.
|
||||
*/
|
||||
export declare function escapeElement(value: string): string;
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export * from "./XmlNode";
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export * from "./XmlText";
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export { parseXML } from "./xml-parser";
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export interface Stringable {
|
||||
toString(): string;
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { Stringable } from "./stringable";
|
||||
export declare class XmlNode {
|
||||
private name;
|
||||
readonly children: Stringable[];
|
||||
private attributes;
|
||||
static of(name: string, childText?: string, withName?: string): XmlNode;
|
||||
constructor(name: string, children?: Stringable[]);
|
||||
withName(name: string): XmlNode;
|
||||
addAttribute(name: string, value: any): XmlNode;
|
||||
addChildNode(child: Stringable): XmlNode;
|
||||
removeAttribute(name: string): XmlNode;
|
||||
n(name: string): XmlNode;
|
||||
c(child: Stringable): XmlNode;
|
||||
a(name: string, value: any): XmlNode;
|
||||
cc(input: any, field: string, withName?: string): void;
|
||||
l(
|
||||
input: any,
|
||||
listName: string,
|
||||
memberName: string,
|
||||
valueProvider: Function
|
||||
): void;
|
||||
lc(
|
||||
input: any,
|
||||
listName: string,
|
||||
memberName: string,
|
||||
valueProvider: Function
|
||||
): void;
|
||||
toString(): string;
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import { Stringable } from "./stringable";
|
||||
export declare class XmlText implements Stringable {
|
||||
private value;
|
||||
constructor(value: string);
|
||||
toString(): string;
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function escapeAttribute(value: string): string;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function escapeElement(value: string): string;
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export * from "./XmlNode";
|
||||
export * from "./XmlText";
|
||||
export { parseXML } from "./xml-parser";
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
export interface Stringable {
|
||||
toString(): string;
|
||||
}
|
||||
Generated
Vendored
+66
@@ -0,0 +1,66 @@
|
||||
export declare const XML: Record<string, string>;
|
||||
export declare const COMMON_HTML: Record<string, string>;
|
||||
export declare const CURRENCY: Record<string, string>;
|
||||
type EntityValFn = (
|
||||
match: string,
|
||||
captured: string,
|
||||
...rest: unknown[]
|
||||
) => string;
|
||||
type ApplyLimitsTo = "external" | "base" | "all" | Array<"external" | "base">;
|
||||
interface EntityDecoderLimitOptions {
|
||||
maxTotalExpansions?: number;
|
||||
maxExpandedLength?: number;
|
||||
applyLimitsTo?: ApplyLimitsTo;
|
||||
}
|
||||
interface EntityDecoderNCROptions {
|
||||
xmlVersion?: 1.0 | 1.1;
|
||||
onNCR?: "allow" | "leave" | "remove" | "throw";
|
||||
nullNCR?: "remove" | "throw";
|
||||
}
|
||||
interface EntityDecoderOptions {
|
||||
namedEntities?: Record<
|
||||
string,
|
||||
| string
|
||||
| {
|
||||
regex: RegExp;
|
||||
val: string | EntityValFn;
|
||||
}
|
||||
> | null;
|
||||
postCheck?: ((resolved: string, original: string) => string) | null;
|
||||
numericAllowed?: boolean;
|
||||
leave?: string[];
|
||||
remove?: string[];
|
||||
limit?: EntityDecoderLimitOptions;
|
||||
ncr?: EntityDecoderNCROptions;
|
||||
}
|
||||
export interface EntityDecoder {
|
||||
setExternalEntities(
|
||||
map: Record<
|
||||
string,
|
||||
| string
|
||||
| {
|
||||
regex: RegExp;
|
||||
val: string | EntityValFn;
|
||||
}
|
||||
>
|
||||
): void;
|
||||
addExternalEntity(key: string, value: string): void;
|
||||
addInputEntities(
|
||||
map: Record<
|
||||
string,
|
||||
| string
|
||||
| {
|
||||
regx?: RegExp;
|
||||
regex?: RegExp;
|
||||
val: string | EntityValFn;
|
||||
}
|
||||
>
|
||||
): void;
|
||||
reset(): this;
|
||||
decode(str: string): string;
|
||||
setXmlVersion(version: string): void;
|
||||
}
|
||||
export declare const EntityDecoderImpl: new (
|
||||
options?: EntityDecoderOptions
|
||||
) => EntityDecoder;
|
||||
export {};
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function parseXML(xmlString: string): any;
|
||||
+1
@@ -0,0 +1 @@
|
||||
export declare function parseXML(xmlString: string): any;
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Contains code from \@nodable/entities v2.1.0
|
||||
* Copyright (c) Amit Gupta (https://solothought.com)
|
||||
* https://github.com/nodable/val-parsers
|
||||
*
|
||||
* This file bundles the EntityDecoder class and the named-entity maps
|
||||
* (XML, COMMON_HTML, CURRENCY).
|
||||
*
|
||||
* This is a temporary solution while using this particular custom
|
||||
* EntityDecoder class. The module-only nature of the original version
|
||||
* is incompatible with some of our users' applications.
|
||||
*
|
||||
* Given that our CJS dist must call `require` to bring in the module, and
|
||||
* because the EntityDecoder class and object are inaccessible via the runtime object surface
|
||||
* of the XMLParser, we must inline the package.
|
||||
*
|
||||
* Q: Why is this necessary given that fast-xml-parser itself uses \@nodable/entities?
|
||||
* A: FXP only uses \@nodable/entities when imported in ESM mode. When importing FXP
|
||||
* via require, a bundled version is used, unaffected by the module-only nature
|
||||
* of the entities package.
|
||||
*/
|
||||
export declare const XML: Record<string, string>;
|
||||
export declare const COMMON_HTML: Record<string, string>;
|
||||
export declare const CURRENCY: Record<string, string>;
|
||||
type EntityValFn = (match: string, captured: string, ...rest: unknown[]) => string;
|
||||
type ApplyLimitsTo = "external" | "base" | "all" | Array<"external" | "base">;
|
||||
interface EntityDecoderLimitOptions {
|
||||
maxTotalExpansions?: number;
|
||||
maxExpandedLength?: number;
|
||||
applyLimitsTo?: ApplyLimitsTo;
|
||||
}
|
||||
interface EntityDecoderNCROptions {
|
||||
xmlVersion?: 1.0 | 1.1;
|
||||
onNCR?: "allow" | "leave" | "remove" | "throw";
|
||||
nullNCR?: "remove" | "throw";
|
||||
}
|
||||
interface EntityDecoderOptions {
|
||||
namedEntities?: Record<string, string | {
|
||||
regex: RegExp;
|
||||
val: string | EntityValFn;
|
||||
}> | null;
|
||||
postCheck?: ((resolved: string, original: string) => string) | null;
|
||||
numericAllowed?: boolean;
|
||||
leave?: string[];
|
||||
remove?: string[];
|
||||
limit?: EntityDecoderLimitOptions;
|
||||
ncr?: EntityDecoderNCROptions;
|
||||
}
|
||||
export interface EntityDecoder {
|
||||
setExternalEntities(map: Record<string, string | {
|
||||
regex: RegExp;
|
||||
val: string | EntityValFn;
|
||||
}>): void;
|
||||
addExternalEntity(key: string, value: string): void;
|
||||
addInputEntities(map: Record<string, string | {
|
||||
regx?: RegExp;
|
||||
regex?: RegExp;
|
||||
val: string | EntityValFn;
|
||||
}>): void;
|
||||
reset(): this;
|
||||
decode(str: string): string;
|
||||
setXmlVersion(version: string): void;
|
||||
}
|
||||
export declare const EntityDecoderImpl: new (options?: EntityDecoderOptions) => EntityDecoder;
|
||||
export {};
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Cases where this differs from fast-xml-parser:
|
||||
*
|
||||
* 1. Mixing text with nested tags (does not occur in AWS REST XML).
|
||||
* <mixed-text> hello, <bold>world</bold>, how are you?</mixed-text>
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export declare function parseXML(xmlString: string): any;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
export declare function parseXML(xmlString: string): any;
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
{
|
||||
"name": "@aws-sdk/xml-builder",
|
||||
"version": "3.972.29",
|
||||
"description": "XML utilities for the AWS SDK",
|
||||
"dependencies": {
|
||||
"@smithy/types": "^4.14.3",
|
||||
"fast-xml-parser": "5.7.3",
|
||||
"tslib": "^2.6.2"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "concurrently 'yarn:build:types' 'yarn:build:es' && yarn build:cjs",
|
||||
"build:cjs": "node ../../scripts/compilation/inline",
|
||||
"build:es": "tsc -p tsconfig.es.json",
|
||||
"build:include:deps": "yarn g:turbo run build -F=\"$npm_package_name\"",
|
||||
"build:types": "tsc -p tsconfig.types.json",
|
||||
"build:types:downlevel": "downlevel-dts dist-types dist-types/ts3.4",
|
||||
"clean": "premove dist-cjs dist-es dist-types tsconfig.cjs.tsbuildinfo tsconfig.es.tsbuildinfo tsconfig.types.tsbuildinfo",
|
||||
"test": "yarn g:vitest run",
|
||||
"test:watch": "yarn g:vitest watch"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"author": {
|
||||
"name": "AWS SDK for JavaScript Team",
|
||||
"url": "https://aws.amazon.com/sdk-for-javascript/"
|
||||
},
|
||||
"license": "Apache-2.0",
|
||||
"main": "./dist-cjs/index.js",
|
||||
"module": "./dist-es/index.js",
|
||||
"types": "./dist-types/index.d.ts",
|
||||
"engines": {
|
||||
"node": ">=20.0.0"
|
||||
},
|
||||
"typesVersions": {
|
||||
"<4.5": {
|
||||
"dist-types/*": [
|
||||
"dist-types/ts3.4/*"
|
||||
]
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist-*/**"
|
||||
],
|
||||
"browser": {
|
||||
"./dist-es/xml-parser": "./dist-es/xml-parser.browser"
|
||||
},
|
||||
"react-native": {
|
||||
"./dist-es/xml-parser": "./dist-es/xml-parser.browser",
|
||||
"./dist-cjs/xml-parser": "./dist-cjs/xml-parser.browser"
|
||||
},
|
||||
"homepage": "https://github.com/aws/aws-sdk-js-v3/tree/main/packages-internal/xml-builder",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/aws/aws-sdk-js-v3.git",
|
||||
"directory": "packages-internal/xml-builder"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@nodable/entities": "2.1.0",
|
||||
"@tsconfig/recommended": "1.0.1",
|
||||
"concurrently": "7.0.0",
|
||||
"downlevel-dts": "0.10.1",
|
||||
"premove": "4.0.0",
|
||||
"typescript": "~5.8.3"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user