en16931-parser 0.0.0-stage → 1.0.0
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/LICENSE +21 -0
- package/README.md +64 -2
- package/bin/cli.js +75 -0
- package/dist/einvoice-viewer.d.ts +129 -0
- package/dist/einvoice-viewer.js +651 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/internal/collections.d.ts +62 -0
- package/dist/internal/collections.js +146 -0
- package/dist/internal/csv.d.ts +63 -0
- package/dist/internal/csv.js +209 -0
- package/dist/internal/pii-patterns.d.ts +42 -0
- package/dist/internal/pii-patterns.js +162 -0
- package/dist/internal/xml-entities.d.ts +14 -0
- package/dist/internal/xml-entities.js +50 -0
- package/dist/internal/xml-formatter-options.d.ts +190 -0
- package/dist/internal/xml-formatter-options.js +848 -0
- package/dist/internal/xml-inspect.d.ts +38 -0
- package/dist/internal/xml-inspect.js +103 -0
- package/package.json +48 -4
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xml-inspect.ts — Namespace-agnostic XML tree inspection & XXE safety helpers.
|
|
3
|
+
*
|
|
4
|
+
* Wraps `validateXml` / `buildXmlTree` / `formatXml` from `xml-formatter-options.ts`
|
|
5
|
+
* so XML-based tools (`saml-decoder`, `einvoice-viewer`, `bank-statement-converter`)
|
|
6
|
+
* share BOM stripping, `<!DOCTYPE`/`<!ENTITY` detection, and local-name traversal
|
|
7
|
+
* without duplicating lexers or failing on namespace prefixes.
|
|
8
|
+
*/
|
|
9
|
+
import { formatXml, type XmlFormatResult, type XmlTreeNode, type XmlTreeSummary, type XmlValidationError } from './xml-formatter-options.js';
|
|
10
|
+
export { formatXml, type XmlFormatResult, type XmlTreeNode, type XmlTreeSummary, type XmlValidationError };
|
|
11
|
+
export type XmlInspectResult = {
|
|
12
|
+
readonly ok: true;
|
|
13
|
+
readonly root: XmlTreeNode;
|
|
14
|
+
readonly summary: XmlTreeSummary;
|
|
15
|
+
readonly cleanedXml: string;
|
|
16
|
+
readonly hasDoctypeOrEntity: boolean;
|
|
17
|
+
} | {
|
|
18
|
+
readonly ok: false;
|
|
19
|
+
readonly reason: string;
|
|
20
|
+
readonly error?: XmlValidationError;
|
|
21
|
+
readonly hasDoctypeOrEntity: boolean;
|
|
22
|
+
};
|
|
23
|
+
/** Strips a leading UTF-8 BOM (`\uFEFF`) if present and trims outer whitespace. */
|
|
24
|
+
export declare function stripXmlBom(raw: string): string;
|
|
25
|
+
/** Detects `<!DOCTYPE` or `<!ENTITY` declarations outside normal elements. */
|
|
26
|
+
export declare function containsXmlDoctypeOrEntity(xml: string): boolean;
|
|
27
|
+
/** Returns the local element/attribute name without any XML namespace prefix. */
|
|
28
|
+
export declare function localTagName(tagOrAttr: string): string;
|
|
29
|
+
/** Parses sanitized XML into an `XmlTreeNode` tree, flagging DOCTYPE/ENTITY declarations. */
|
|
30
|
+
export declare function inspectXmlDocument(raw: string): XmlInspectResult;
|
|
31
|
+
/** Finds all descendant (and optionally self) nodes matching a case-insensitive local tag name. */
|
|
32
|
+
export declare function findAllByLocalName(node: XmlTreeNode, targetLocalName: string, includeSelf?: boolean): XmlTreeNode[];
|
|
33
|
+
/** Finds the first node in depth-first order matching a case-insensitive local tag name. */
|
|
34
|
+
export declare function findFirstByLocalName(node: XmlTreeNode, targetLocalName: string, includeSelf?: boolean): XmlTreeNode | null;
|
|
35
|
+
/** Returns trimmed element text (joining CDATA blocks when element text is empty). */
|
|
36
|
+
export declare function getNodeText(node: XmlTreeNode | null | undefined): string;
|
|
37
|
+
/** Returns a direct attribute value matching by exact name or local name (case-insensitive). */
|
|
38
|
+
export declare function getNodeAttr(node: XmlTreeNode | null | undefined, attrLocalName: string): string;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* xml-inspect.ts — Namespace-agnostic XML tree inspection & XXE safety helpers.
|
|
3
|
+
*
|
|
4
|
+
* Wraps `validateXml` / `buildXmlTree` / `formatXml` from `xml-formatter-options.ts`
|
|
5
|
+
* so XML-based tools (`saml-decoder`, `einvoice-viewer`, `bank-statement-converter`)
|
|
6
|
+
* share BOM stripping, `<!DOCTYPE`/`<!ENTITY` detection, and local-name traversal
|
|
7
|
+
* without duplicating lexers or failing on namespace prefixes.
|
|
8
|
+
*/
|
|
9
|
+
import { buildXmlTree, formatXml, } from './xml-formatter-options.js';
|
|
10
|
+
export { formatXml };
|
|
11
|
+
/** Strips a leading UTF-8 BOM (`\uFEFF`) if present and trims outer whitespace. */
|
|
12
|
+
export function stripXmlBom(raw) {
|
|
13
|
+
const withoutBom = raw.charCodeAt(0) === 0xfeff ? raw.slice(1) : raw;
|
|
14
|
+
return withoutBom.trim();
|
|
15
|
+
}
|
|
16
|
+
/** Detects `<!DOCTYPE` or `<!ENTITY` declarations outside normal elements. */
|
|
17
|
+
export function containsXmlDoctypeOrEntity(xml) {
|
|
18
|
+
return /<!(?:DOCTYPE|ENTITY)\b/i.test(xml);
|
|
19
|
+
}
|
|
20
|
+
/** Returns the local element/attribute name without any XML namespace prefix. */
|
|
21
|
+
export function localTagName(tagOrAttr) {
|
|
22
|
+
const idx = tagOrAttr.indexOf(':');
|
|
23
|
+
return idx >= 0 ? tagOrAttr.slice(idx + 1) : tagOrAttr;
|
|
24
|
+
}
|
|
25
|
+
/** Parses sanitized XML into an `XmlTreeNode` tree, flagging DOCTYPE/ENTITY declarations. */
|
|
26
|
+
export function inspectXmlDocument(raw) {
|
|
27
|
+
const cleaned = stripXmlBom(raw);
|
|
28
|
+
const hasDoctypeOrEntity = containsXmlDoctypeOrEntity(cleaned);
|
|
29
|
+
if (hasDoctypeOrEntity) {
|
|
30
|
+
return {
|
|
31
|
+
ok: false,
|
|
32
|
+
reason: 'XML DOCTYPE/ENTITY declarations are disabled for offline XXE safety.',
|
|
33
|
+
hasDoctypeOrEntity: true,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
const tree = buildXmlTree(cleaned);
|
|
37
|
+
if (!tree.ok) {
|
|
38
|
+
return {
|
|
39
|
+
ok: false,
|
|
40
|
+
reason: tree.error.reason,
|
|
41
|
+
error: tree.error,
|
|
42
|
+
hasDoctypeOrEntity: false,
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return {
|
|
46
|
+
ok: true,
|
|
47
|
+
root: tree.root,
|
|
48
|
+
summary: tree.summary,
|
|
49
|
+
cleanedXml: cleaned,
|
|
50
|
+
hasDoctypeOrEntity: false,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** Finds all descendant (and optionally self) nodes matching a case-insensitive local tag name. */
|
|
54
|
+
export function findAllByLocalName(node, targetLocalName, includeSelf = true) {
|
|
55
|
+
const needle = targetLocalName.toLowerCase();
|
|
56
|
+
const out = [];
|
|
57
|
+
const visit = (cur, check) => {
|
|
58
|
+
if (check && localTagName(cur.tag).toLowerCase() === needle) {
|
|
59
|
+
out.push(cur);
|
|
60
|
+
}
|
|
61
|
+
for (const child of cur.children) {
|
|
62
|
+
visit(child, true);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
visit(node, includeSelf);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
/** Finds the first node in depth-first order matching a case-insensitive local tag name. */
|
|
69
|
+
export function findFirstByLocalName(node, targetLocalName, includeSelf = true) {
|
|
70
|
+
const needle = targetLocalName.toLowerCase();
|
|
71
|
+
if (includeSelf && localTagName(node.tag).toLowerCase() === needle) {
|
|
72
|
+
return node;
|
|
73
|
+
}
|
|
74
|
+
for (const child of node.children) {
|
|
75
|
+
const found = findFirstByLocalName(child, needle, true);
|
|
76
|
+
if (found)
|
|
77
|
+
return found;
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
/** Returns trimmed element text (joining CDATA blocks when element text is empty). */
|
|
82
|
+
export function getNodeText(node) {
|
|
83
|
+
if (!node)
|
|
84
|
+
return '';
|
|
85
|
+
const direct = node.text.trim();
|
|
86
|
+
if (direct.length > 0)
|
|
87
|
+
return direct;
|
|
88
|
+
if (node.cdata.length > 0)
|
|
89
|
+
return node.cdata.join('').trim();
|
|
90
|
+
return '';
|
|
91
|
+
}
|
|
92
|
+
/** Returns a direct attribute value matching by exact name or local name (case-insensitive). */
|
|
93
|
+
export function getNodeAttr(node, attrLocalName) {
|
|
94
|
+
if (!node)
|
|
95
|
+
return '';
|
|
96
|
+
const needle = attrLocalName.toLowerCase();
|
|
97
|
+
for (const attr of node.attributes) {
|
|
98
|
+
if (localTagName(attr.name).toLowerCase() === needle) {
|
|
99
|
+
return attr.value;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return '';
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "en16931-parser",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Zero-dependency EN 16931, Factur-X, ZUGFeRD, and XRechnung (CII & UBL 2.1) XML e-invoice parser, XXE guard, and arithmetic validator.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"bin": {
|
|
9
|
+
"en16931-parser": "bin/cli.js"
|
|
10
|
+
},
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"default": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"bin/cli.js",
|
|
19
|
+
"dist",
|
|
20
|
+
"LICENSE",
|
|
21
|
+
"README.md"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "node --test test/*.test.js"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"en16931",
|
|
28
|
+
"factur-x",
|
|
29
|
+
"zugferd",
|
|
30
|
+
"xrechnung",
|
|
31
|
+
"e-invoice",
|
|
32
|
+
"ubl",
|
|
33
|
+
"cii",
|
|
34
|
+
"xml",
|
|
35
|
+
"cli"
|
|
36
|
+
],
|
|
37
|
+
"author": "Bastien Sulyan <contact@sunasty.com> (https://sunasty.com)",
|
|
38
|
+
"license": "MIT",
|
|
39
|
+
"homepage": "https://sunasty.com/einvoice-viewer/",
|
|
40
|
+
"repository": {
|
|
41
|
+
"type": "git",
|
|
42
|
+
"url": "git+https://github.com/sunasty-labs/en16931-parser.git"
|
|
43
|
+
},
|
|
44
|
+
"bugs": {
|
|
45
|
+
"url": "https://github.com/sunasty-labs/en16931-parser/issues"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18.0.0"
|
|
49
|
+
}
|
|
50
|
+
}
|