@stll/docx-core 0.40.0 → 0.41.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/dist/docx_kernel_bg.wasm +0 -0
- package/dist/index.d.ts +6 -6
- package/dist/index.js +86 -161
- package/dist/schema/sequenceChildren.js +1 -1
- package/dist/{sequenceChildren-CrMcryEF.js → sequenceChildren-CSn0-1ov.js} +17 -45
- package/dist/streamingXmlParser-GnKxUuw_.js +743 -0
- package/dist/xml/streamingXmlParser.d.ts +109 -0
- package/dist/xml/streamingXmlParser.js +1 -0
- package/dist/xml/xmlNamespaceContext.d.ts +6 -0
- package/dist/xml/xmlNamespaceContext.js +53 -0
- package/dist/xml/xmlResourceLimits.d.ts +107 -0
- package/dist/xml/xmlResourceLimits.js +194 -0
- package/dist/xmlElement-C-vuKoul.d.ts +31 -0
- package/package.json +16 -3
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { n as XmlNamespaceScope, t as XmlElement } from "../xmlElement-C-vuKoul.js";
|
|
2
|
+
import { TaggedErrorClass } from "better-result";
|
|
3
|
+
type ParseXmlResult = {
|
|
4
|
+
status: "parsed";
|
|
5
|
+
value: XmlElement;
|
|
6
|
+
} | {
|
|
7
|
+
status: "unsupported";
|
|
8
|
+
};
|
|
9
|
+
type XmlSourceRange = {
|
|
10
|
+
readonly start: number;
|
|
11
|
+
readonly end: number;
|
|
12
|
+
};
|
|
13
|
+
/** Exact element source ranges, available only from the opt-in source-range reader. */
|
|
14
|
+
export declare const getXmlSourceRange: (element: XmlElement) => XmlSourceRange | undefined;
|
|
15
|
+
/** How much markup a read accepts; see the module comment. */
|
|
16
|
+
export type XmlConformance = "ooxml" | "well-formed" | "tolerant";
|
|
17
|
+
/** Whether references are decoded or kept as written. */
|
|
18
|
+
export type XmlReferenceHandling = "decode" | "raw";
|
|
19
|
+
/**
|
|
20
|
+
* What a reference to an entity no declaration defines does. `refuse` is
|
|
21
|
+
* XML 1.0: without a DTD only the five built-ins exist. `accept` admits any
|
|
22
|
+
* reference that is syntactically one (`&Name;`, `&#digits;`, `&#xhex;`) and
|
|
23
|
+
* keeps it as written, for checks that must accept what earlier releases
|
|
24
|
+
* accepted. Not used by the tolerant reader, which accepts every reference.
|
|
25
|
+
*/
|
|
26
|
+
export type XmlUndeclaredEntities = "refuse" | "accept";
|
|
27
|
+
declare const XmlReadErrorBase: TaggedErrorClass<"XmlReadError">;
|
|
28
|
+
/** An XML part the reader refused under the conformance its caller asked for. */
|
|
29
|
+
export declare class XmlReadError extends XmlReadErrorBase<{
|
|
30
|
+
message: string;
|
|
31
|
+
reason: string;
|
|
32
|
+
}> {}
|
|
33
|
+
type AttributeValueSpan = {
|
|
34
|
+
start: number;
|
|
35
|
+
end: number;
|
|
36
|
+
};
|
|
37
|
+
type OpenTagVisitor = (element: XmlElement, attributeValueSpans: ReadonlyMap<string, AttributeValueSpan>) => ReadonlyMap<string, string> | null;
|
|
38
|
+
type OpenTagScanVisitor = (...args: Parameters<OpenTagVisitor>) => null;
|
|
39
|
+
type ElementScanOptions = {
|
|
40
|
+
element: XmlElement;
|
|
41
|
+
attributeValueSpans: ReadonlyMap<string, AttributeValueSpan>;
|
|
42
|
+
nameEnd: number;
|
|
43
|
+
parent: XmlElement | undefined;
|
|
44
|
+
};
|
|
45
|
+
type ElementScanVisitor = (options: ElementScanOptions) => void;
|
|
46
|
+
/** What a read returned: the tree, or why the reader refused the input. */
|
|
47
|
+
export type XmlReadResult = {
|
|
48
|
+
status: "parsed";
|
|
49
|
+
value: XmlElement;
|
|
50
|
+
} | {
|
|
51
|
+
status: "rejected";
|
|
52
|
+
reason: string;
|
|
53
|
+
};
|
|
54
|
+
export type XmlReadOptions = {
|
|
55
|
+
conformance: XmlConformance;
|
|
56
|
+
/** Defaults to `"decode"`. */
|
|
57
|
+
references?: XmlReferenceHandling;
|
|
58
|
+
/** Defaults to `"refuse"`. */
|
|
59
|
+
undeclaredEntities?: XmlUndeclaredEntities;
|
|
60
|
+
/**
|
|
61
|
+
* `"retain"` keeps processing instructions, the XML declaration included,
|
|
62
|
+
* as `{ type: "instruction", name, instruction }` nodes; comments are always
|
|
63
|
+
* dropped.
|
|
64
|
+
*/
|
|
65
|
+
instructions?: "skip" | "retain";
|
|
66
|
+
/** The bindings in scope around `xml`, for a fragment captured out of a part. */
|
|
67
|
+
inheritedNamespaceScope?: XmlNamespaceScope;
|
|
68
|
+
};
|
|
69
|
+
/** Read `xml` into a tree under the stated conformance, in one pass. */
|
|
70
|
+
export declare const readXml: (xml: string, { conformance, references, undeclaredEntities, instructions, inheritedNamespaceScope }: XmlReadOptions) => XmlReadResult;
|
|
71
|
+
/**
|
|
72
|
+
* The reason `xml` is not well-formed XML, or `null` when it is.
|
|
73
|
+
*
|
|
74
|
+
* Stricter than XML 1.0 in one respect: an element or attribute named like a
|
|
75
|
+
* built-in object property (`__proto__`, `constructor`, `toString`, ...) is
|
|
76
|
+
* refused even though XML allows the name, because every folio reader keys
|
|
77
|
+
* plain objects by name and refuses it too; a part this accepts is one they
|
|
78
|
+
* can read.
|
|
79
|
+
*/
|
|
80
|
+
export declare const wellFormedXmlIssue: (xml: string, undeclaredEntities?: XmlUndeclaredEntities) => string | null;
|
|
81
|
+
/**
|
|
82
|
+
* @param inheritedNamespaceScope the bindings in scope around `xml`, for a
|
|
83
|
+
* fragment captured out of a part whose root declared them.
|
|
84
|
+
*/
|
|
85
|
+
export declare const parseStreamingXml: (xml: string, inheritedNamespaceScope?: XmlNamespaceScope) => ParseXmlResult;
|
|
86
|
+
/** Retain source spans for every element without changing the parsed namespace tree. */
|
|
87
|
+
export declare const parseStreamingXmlWithSourceRanges: (xml: string, inheritedNamespaceScope?: XmlNamespaceScope) => ParseXmlResult;
|
|
88
|
+
/** Retain the body tree while collecting identities for import repair. */
|
|
89
|
+
export declare const parseStreamingXmlWithIdentityVisitor: (xml: string, visitOpenTag: OpenTagScanVisitor) => ParseXmlResult;
|
|
90
|
+
/** Rewrite selected decimal attribute values while preserving every other source byte. */
|
|
91
|
+
export declare const rewriteStreamingXmlDecimalAttributes: (xml: string, visitOpenTag: OpenTagVisitor) => {
|
|
92
|
+
status: "rewritten";
|
|
93
|
+
value: string;
|
|
94
|
+
} | {
|
|
95
|
+
status: "unsupported";
|
|
96
|
+
};
|
|
97
|
+
/** Visit identity attributes and namespace bindings without building an XML tree. */
|
|
98
|
+
export declare const scanStreamingXmlNumericIdAttributes: (xml: string, visitOpenTag: OpenTagScanVisitor) => {
|
|
99
|
+
status: "unsupported";
|
|
100
|
+
} | {
|
|
101
|
+
status: "scanned";
|
|
102
|
+
};
|
|
103
|
+
/** Visit namespace-resolved start tags and source spans without building a tree. */
|
|
104
|
+
export declare const scanStreamingXmlElements: (xml: string, visitElement: ElementScanVisitor) => {
|
|
105
|
+
status: "unsupported";
|
|
106
|
+
} | {
|
|
107
|
+
status: "scanned";
|
|
108
|
+
};
|
|
109
|
+
export type { XmlElement, XmlNamespaceScope };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as XmlReadError, n as getXmlSourceRange, r as parseStreamingXml, i as parseStreamingXmlWithIdentityVisitor, a as parseStreamingXmlWithSourceRanges, o as readXml, s as rewriteStreamingXmlDecimalAttributes, c as scanStreamingXmlElements, l as scanStreamingXmlNumericIdAttributes, u as wellFormedXmlIssue } from "../streamingXmlParser-GnKxUuw_.js";
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { n as XmlNamespaceScope, t as XmlElement } from "../xmlElement-C-vuKoul.js";
|
|
2
|
+
export declare const EMPTY_NAMESPACE_SCOPE: XmlNamespaceScope;
|
|
3
|
+
export declare const XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";
|
|
4
|
+
export declare const resolveNamespaceUri: (scope: XmlNamespaceScope | undefined, prefix: string) => string | undefined;
|
|
5
|
+
/** Attach the element's resolved namespace metadata from its in-scope declarations. */
|
|
6
|
+
export declare const attachXmlNamespaceContext: (element: XmlElement, inheritedNamespaceScope?: XmlNamespaceScope) => XmlNamespaceScope;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// src/xml/xmlNamespaceContext.ts
|
|
2
|
+
const EMPTY_NAMESPACE_SCOPE = { bindings: /* @__PURE__ */ new Map() };
|
|
3
|
+
/**
|
|
4
|
+
* Reserved by the XML Namespaces recommendation (§3): every document binds
|
|
5
|
+
* this prefix to this URI whether or not it declares `xmlns:xml`, and no
|
|
6
|
+
* document may bind it to anything else. A source that omits the declaration
|
|
7
|
+
* — the overwhelming majority — must still resolve `xml:space`, `xml:lang`
|
|
8
|
+
* and `xml:id` to it, or a reader mistakes the attribute for an unprefixed
|
|
9
|
+
* one and a save drops its prefix.
|
|
10
|
+
*/
|
|
11
|
+
const XML_RESERVED_PREFIX = "xml";
|
|
12
|
+
const XML_NAMESPACE_URI = "http://www.w3.org/XML/1998/namespace";
|
|
13
|
+
const resolveNamespaceUri = (scope, prefix) => {
|
|
14
|
+
let current = scope;
|
|
15
|
+
while (current) {
|
|
16
|
+
const value = current.bindings.get(prefix);
|
|
17
|
+
if (value !== void 0) return value;
|
|
18
|
+
current = current.parent;
|
|
19
|
+
}
|
|
20
|
+
return prefix === XML_RESERVED_PREFIX ? XML_NAMESPACE_URI : void 0;
|
|
21
|
+
};
|
|
22
|
+
/** Attach the element's resolved namespace metadata from its in-scope declarations. */
|
|
23
|
+
const attachXmlNamespaceContext = (element, inheritedNamespaceScope = EMPTY_NAMESPACE_SCOPE) => {
|
|
24
|
+
let localBindings = null;
|
|
25
|
+
if (element.attributes) for (const [attribute, value] of Object.entries(element.attributes)) {
|
|
26
|
+
if (typeof value !== "string" || attribute !== "xmlns" && !attribute.startsWith("xmlns:")) continue;
|
|
27
|
+
localBindings ??= /* @__PURE__ */ new Map();
|
|
28
|
+
const prefix = attribute === "xmlns" ? "" : attribute.slice(6);
|
|
29
|
+
localBindings.set(prefix, value);
|
|
30
|
+
}
|
|
31
|
+
const namespaceScope = localBindings === null ? inheritedNamespaceScope : {
|
|
32
|
+
bindings: localBindings,
|
|
33
|
+
parent: inheritedNamespaceScope
|
|
34
|
+
};
|
|
35
|
+
Object.defineProperty(element, "namespaceScope", {
|
|
36
|
+
configurable: false,
|
|
37
|
+
enumerable: false,
|
|
38
|
+
value: namespaceScope,
|
|
39
|
+
writable: false
|
|
40
|
+
});
|
|
41
|
+
const name = element.name ?? "";
|
|
42
|
+
const colonIndex = name.indexOf(":");
|
|
43
|
+
const prefix = colonIndex === -1 ? "" : name.slice(0, colonIndex);
|
|
44
|
+
const namespaceUri = resolveNamespaceUri(namespaceScope, prefix);
|
|
45
|
+
if (namespaceUri !== void 0) Object.defineProperty(element, "namespaceUri", {
|
|
46
|
+
configurable: false,
|
|
47
|
+
enumerable: false,
|
|
48
|
+
value: namespaceUri,
|
|
49
|
+
writable: false
|
|
50
|
+
});
|
|
51
|
+
return namespaceScope;
|
|
52
|
+
};
|
|
53
|
+
export { EMPTY_NAMESPACE_SCOPE, XML_NAMESPACE_URI, attachXmlNamespaceContext, resolveNamespaceUri };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { TaggedErrorClass } from "better-result";
|
|
2
|
+
declare const XmlResourceLimitErrorBase: TaggedErrorClass<"XmlResourceLimitError">;
|
|
3
|
+
/**
|
|
4
|
+
* Shared bounds for XML parts parsed by Folio.
|
|
5
|
+
*
|
|
6
|
+
* `maxBytes` and the package expansion ceiling bound the *markup*; they do not
|
|
7
|
+
* bound what parsing that markup allocates. A parsed element retains far more
|
|
8
|
+
* than the bytes it was written as, so a part that satisfies a byte bound can
|
|
9
|
+
* still cost multiples of it in tree. Measured on this repository's corpus
|
|
10
|
+
* generators (Bun 1.4, arm64, heap retained after a forced GC):
|
|
11
|
+
*
|
|
12
|
+
* shape bytes/element tree heap / part bytes
|
|
13
|
+
* `<w:r/>` 49.5 B 8.3x
|
|
14
|
+
* `<w:r a=".." x4/>` 105-124 B 2.8-3.3x
|
|
15
|
+
* `<w:r><w:t>x</w:t></w:r>` 162-171 B 14.1-14.8x
|
|
16
|
+
*
|
|
17
|
+
* An element is therefore the unit a memory bound has to count, because the
|
|
18
|
+
* adversarial shape is the cheap one: `<w:r/>` is 7 bytes of markup and 49.5
|
|
19
|
+
* bytes of tree, so 128 MiB of markup buys ~19M elements and ~950 MB of tree
|
|
20
|
+
* inside a byte budget that never trips.
|
|
21
|
+
*
|
|
22
|
+
* Corpus distribution (5,314 readable packages, every XML and .rels part):
|
|
23
|
+
*
|
|
24
|
+
* metric p50 p90 p99 p99.9 max
|
|
25
|
+
* elements / part 16 227 1,590 24,719 596,668
|
|
26
|
+
* elements / package 917 3,230 29,106 71,975 602,212
|
|
27
|
+
* attributes / part 27 577 1,993 21,037 630,374
|
|
28
|
+
* attributes / package 1,783 4,711 25,896 91,946 639,110
|
|
29
|
+
* depth / part 3 9 15 22 15,005
|
|
30
|
+
* elements per byte 0.012 0.027 0.036 0.043 0.111
|
|
31
|
+
*
|
|
32
|
+
* The defaults below reject no corpus package that today's bounds accept. The
|
|
33
|
+
* package budget is the one that matters: it caps a whole package at ~2.5M
|
|
34
|
+
* elements and ~3M attributes, which is at most ~425 MB of tree at the densest
|
|
35
|
+
* measured shape and ~124 MB at the cheapest. Before it existed, only
|
|
36
|
+
* `word/document.xml`, `word/styles.xml` and `word/numbering.xml` were counted
|
|
37
|
+
* at all, so a package of many merely-large parts could reach the expansion
|
|
38
|
+
* ceiling of 250 MiB, ~37M elements and well past 1.8 GB of tree while passing
|
|
39
|
+
* every bound. Lower these to trade format reach for a smaller ceiling.
|
|
40
|
+
*/
|
|
41
|
+
export declare const FOLIO_XML_RESOURCE_LIMITS: {
|
|
42
|
+
readonly maxBytes: number;
|
|
43
|
+
readonly maxDepth: 100;
|
|
44
|
+
/** 1.68x the corpus maximum (596,668). Unchanged; a shipped bound is not loosened. */
|
|
45
|
+
readonly maxElementsPerPart: 1000000;
|
|
46
|
+
/** 3.97x the corpus maximum (630,374). */
|
|
47
|
+
readonly maxAttributesPerPart: 2500000;
|
|
48
|
+
/** 4.15x the corpus maximum (602,212). */
|
|
49
|
+
readonly maxElementsPerPackage: 2500000;
|
|
50
|
+
/** 4.69x the corpus maximum (639,110). */
|
|
51
|
+
readonly maxAttributesPerPackage: 3000000;
|
|
52
|
+
};
|
|
53
|
+
export type XmlResourceLimits = {
|
|
54
|
+
maxBytes: number;
|
|
55
|
+
maxDepth: number;
|
|
56
|
+
maxElementsPerPart: number;
|
|
57
|
+
maxAttributesPerPart: number;
|
|
58
|
+
maxElementsPerPackage: number;
|
|
59
|
+
maxAttributesPerPackage: number;
|
|
60
|
+
};
|
|
61
|
+
type XmlResourceLimitKind = "bytes" | "depth" | "elements" | "attributes" | "package-elements" | "package-attributes" | "syntax";
|
|
62
|
+
/** XML input exceeded a parser resource bound or could not be scanned safely. */
|
|
63
|
+
export declare class XmlResourceLimitError extends XmlResourceLimitErrorBase<{
|
|
64
|
+
message: string;
|
|
65
|
+
limit: XmlResourceLimitKind;
|
|
66
|
+
/** The package part being scanned, when the caller named one. */
|
|
67
|
+
partPath?: string;
|
|
68
|
+
/** The count reached at the point of refusal, not the count of the whole input. */
|
|
69
|
+
observed: number;
|
|
70
|
+
allowed: number;
|
|
71
|
+
}> {}
|
|
72
|
+
/**
|
|
73
|
+
* What a package has spent so far, shared by every part in one package.
|
|
74
|
+
*
|
|
75
|
+
* Per-part bounds alone do not bound a package: a package may hold hundreds of
|
|
76
|
+
* parts, each individually modest. The budget is the accumulator the readers
|
|
77
|
+
* carry across parts so the ceiling is the package's, not each part's.
|
|
78
|
+
*/
|
|
79
|
+
export type XmlPackageBudget = {
|
|
80
|
+
elements: number;
|
|
81
|
+
attributes: number;
|
|
82
|
+
};
|
|
83
|
+
export declare const createXmlPackageBudget: () => XmlPackageBudget;
|
|
84
|
+
export declare const exceedsUtf8ByteLimit: (value: string, maxBytes: number) => boolean;
|
|
85
|
+
export type XmlResourceScanOptions = {
|
|
86
|
+
xml: string;
|
|
87
|
+
limits?: XmlResourceLimits;
|
|
88
|
+
/** The package path scanned, carried on any refusal so a host can name it. */
|
|
89
|
+
partPath?: string;
|
|
90
|
+
/** Charged as the scan proceeds; omit for a part with no package context. */
|
|
91
|
+
budget?: XmlPackageBudget;
|
|
92
|
+
};
|
|
93
|
+
/** What the preflight counted, for callers that reconcile it against the tree. */
|
|
94
|
+
export type XmlResourceScanResult = {
|
|
95
|
+
elements: number;
|
|
96
|
+
attributes: number;
|
|
97
|
+
maxDepth: number;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Bound XML bytes, element count, attribute count and nesting before building
|
|
101
|
+
* an object tree. The lexical scan is iterative, so deeply nested input cannot
|
|
102
|
+
* consume the JS call stack before the depth limit is enforced, and every
|
|
103
|
+
* bound is checked at the point it is crossed, so the work done before a
|
|
104
|
+
* refusal is proportional to the limit rather than to the input.
|
|
105
|
+
*/
|
|
106
|
+
export declare const assertXmlResourceLimits: ({ xml, limits, partPath, budget }: XmlResourceScanOptions) => XmlResourceScanResult;
|
|
107
|
+
export {};
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { TaggedError } from "better-result";
|
|
2
|
+
// src/xml/xmlResourceLimits.ts
|
|
3
|
+
const MEBIBYTE = 1048576;
|
|
4
|
+
const XmlResourceLimitErrorBase = TaggedError("XmlResourceLimitError");
|
|
5
|
+
/**
|
|
6
|
+
* Shared bounds for XML parts parsed by Folio.
|
|
7
|
+
*
|
|
8
|
+
* `maxBytes` and the package expansion ceiling bound the *markup*; they do not
|
|
9
|
+
* bound what parsing that markup allocates. A parsed element retains far more
|
|
10
|
+
* than the bytes it was written as, so a part that satisfies a byte bound can
|
|
11
|
+
* still cost multiples of it in tree. Measured on this repository's corpus
|
|
12
|
+
* generators (Bun 1.4, arm64, heap retained after a forced GC):
|
|
13
|
+
*
|
|
14
|
+
* shape bytes/element tree heap / part bytes
|
|
15
|
+
* `<w:r/>` 49.5 B 8.3x
|
|
16
|
+
* `<w:r a=".." x4/>` 105-124 B 2.8-3.3x
|
|
17
|
+
* `<w:r><w:t>x</w:t></w:r>` 162-171 B 14.1-14.8x
|
|
18
|
+
*
|
|
19
|
+
* An element is therefore the unit a memory bound has to count, because the
|
|
20
|
+
* adversarial shape is the cheap one: `<w:r/>` is 7 bytes of markup and 49.5
|
|
21
|
+
* bytes of tree, so 128 MiB of markup buys ~19M elements and ~950 MB of tree
|
|
22
|
+
* inside a byte budget that never trips.
|
|
23
|
+
*
|
|
24
|
+
* Corpus distribution (5,314 readable packages, every XML and .rels part):
|
|
25
|
+
*
|
|
26
|
+
* metric p50 p90 p99 p99.9 max
|
|
27
|
+
* elements / part 16 227 1,590 24,719 596,668
|
|
28
|
+
* elements / package 917 3,230 29,106 71,975 602,212
|
|
29
|
+
* attributes / part 27 577 1,993 21,037 630,374
|
|
30
|
+
* attributes / package 1,783 4,711 25,896 91,946 639,110
|
|
31
|
+
* depth / part 3 9 15 22 15,005
|
|
32
|
+
* elements per byte 0.012 0.027 0.036 0.043 0.111
|
|
33
|
+
*
|
|
34
|
+
* The defaults below reject no corpus package that today's bounds accept. The
|
|
35
|
+
* package budget is the one that matters: it caps a whole package at ~2.5M
|
|
36
|
+
* elements and ~3M attributes, which is at most ~425 MB of tree at the densest
|
|
37
|
+
* measured shape and ~124 MB at the cheapest. Before it existed, only
|
|
38
|
+
* `word/document.xml`, `word/styles.xml` and `word/numbering.xml` were counted
|
|
39
|
+
* at all, so a package of many merely-large parts could reach the expansion
|
|
40
|
+
* ceiling of 250 MiB, ~37M elements and well past 1.8 GB of tree while passing
|
|
41
|
+
* every bound. Lower these to trade format reach for a smaller ceiling.
|
|
42
|
+
*/
|
|
43
|
+
const FOLIO_XML_RESOURCE_LIMITS = {
|
|
44
|
+
maxBytes: 128 * MEBIBYTE,
|
|
45
|
+
maxDepth: 100,
|
|
46
|
+
/** 1.68x the corpus maximum (596,668). Unchanged; a shipped bound is not loosened. */
|
|
47
|
+
maxElementsPerPart: 1e6,
|
|
48
|
+
/** 3.97x the corpus maximum (630,374). */
|
|
49
|
+
maxAttributesPerPart: 25e5,
|
|
50
|
+
/** 4.15x the corpus maximum (602,212). */
|
|
51
|
+
maxElementsPerPackage: 25e5,
|
|
52
|
+
/** 4.69x the corpus maximum (639,110). */
|
|
53
|
+
maxAttributesPerPackage: 3e6
|
|
54
|
+
};
|
|
55
|
+
/** XML input exceeded a parser resource bound or could not be scanned safely. */
|
|
56
|
+
var XmlResourceLimitError = class extends XmlResourceLimitErrorBase {};
|
|
57
|
+
const createXmlPackageBudget = () => ({
|
|
58
|
+
elements: 0,
|
|
59
|
+
attributes: 0
|
|
60
|
+
});
|
|
61
|
+
/** No UTF-16 code unit encodes to more than three UTF-8 bytes; a pair encodes to four. */
|
|
62
|
+
const MAX_UTF8_BYTES_PER_CODE_UNIT = 3;
|
|
63
|
+
const exceedsUtf8ByteLimit = (value, maxBytes) => {
|
|
64
|
+
if (value.length * MAX_UTF8_BYTES_PER_CODE_UNIT <= maxBytes) return false;
|
|
65
|
+
let bytes = 0;
|
|
66
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
67
|
+
const codeUnit = value.charCodeAt(index);
|
|
68
|
+
if (codeUnit <= 127) bytes += 1;
|
|
69
|
+
else if (codeUnit <= 2047) bytes += 2;
|
|
70
|
+
else if (codeUnit >= 55296 && codeUnit <= 56319 && index + 1 < value.length && value.charCodeAt(index + 1) >= 56320 && value.charCodeAt(index + 1) <= 57343) {
|
|
71
|
+
bytes += 4;
|
|
72
|
+
index += 1;
|
|
73
|
+
} else bytes += 3;
|
|
74
|
+
if (bytes > maxBytes) return true;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
};
|
|
78
|
+
const isXmlWhitespace = (code) => code === 9 || code === 10 || code === 13 || code === 32;
|
|
79
|
+
const scanTag = (xml, start) => {
|
|
80
|
+
let quote = 0;
|
|
81
|
+
let attributes = 0;
|
|
82
|
+
for (let cursor = start; cursor < xml.length; cursor += 1) {
|
|
83
|
+
const code = xml.charCodeAt(cursor);
|
|
84
|
+
if (quote !== 0) {
|
|
85
|
+
if (code === quote) quote = 0;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (code === 34 || code === 39) {
|
|
89
|
+
quote = code;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
if (code === 61) {
|
|
93
|
+
attributes += 1;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (code === 62) return {
|
|
97
|
+
close: cursor,
|
|
98
|
+
attributes
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
close: -1,
|
|
103
|
+
attributes
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
const throwSyntaxLimit = () => {
|
|
107
|
+
throw new XmlResourceLimitError({
|
|
108
|
+
message: "XML resource preflight could not safely scan malformed markup",
|
|
109
|
+
limit: "syntax",
|
|
110
|
+
observed: 0,
|
|
111
|
+
allowed: 0
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* Bound XML bytes, element count, attribute count and nesting before building
|
|
116
|
+
* an object tree. The lexical scan is iterative, so deeply nested input cannot
|
|
117
|
+
* consume the JS call stack before the depth limit is enforced, and every
|
|
118
|
+
* bound is checked at the point it is crossed, so the work done before a
|
|
119
|
+
* refusal is proportional to the limit rather than to the input.
|
|
120
|
+
*/
|
|
121
|
+
const assertXmlResourceLimits = ({ xml, limits = FOLIO_XML_RESOURCE_LIMITS, partPath, budget }) => {
|
|
122
|
+
const refuse = (limit, message, observed, allowed) => {
|
|
123
|
+
throw new XmlResourceLimitError({
|
|
124
|
+
message: partPath === void 0 ? message : `${message} (part ${partPath})`,
|
|
125
|
+
limit,
|
|
126
|
+
...partPath === void 0 ? {} : { partPath },
|
|
127
|
+
observed,
|
|
128
|
+
allowed
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
if (exceedsUtf8ByteLimit(xml, limits.maxBytes)) refuse("bytes", `XML part exceeds ${String(limits.maxBytes)} bytes`, limits.maxBytes + 1, limits.maxBytes);
|
|
132
|
+
const budgetElements = budget?.elements ?? 0;
|
|
133
|
+
const budgetAttributes = budget?.attributes ?? 0;
|
|
134
|
+
let cursor = 0;
|
|
135
|
+
let depth = 0;
|
|
136
|
+
let maxDepth = 0;
|
|
137
|
+
let nodes = 0;
|
|
138
|
+
let attributes = 0;
|
|
139
|
+
while (cursor < xml.length) {
|
|
140
|
+
const open = xml.indexOf("<", cursor);
|
|
141
|
+
if (open === -1) break;
|
|
142
|
+
if (xml.startsWith("<!--", open)) {
|
|
143
|
+
const close = xml.indexOf("-->", open + 4);
|
|
144
|
+
if (close === -1) throwSyntaxLimit();
|
|
145
|
+
cursor = close + 3;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (xml.startsWith("<![CDATA[", open)) {
|
|
149
|
+
const close = xml.indexOf("]]>", open + 9);
|
|
150
|
+
if (close === -1) throwSyntaxLimit();
|
|
151
|
+
cursor = close + 3;
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
if (xml.startsWith("<?", open)) {
|
|
155
|
+
const close = xml.indexOf("?>", open + 2);
|
|
156
|
+
if (close === -1) throwSyntaxLimit();
|
|
157
|
+
cursor = close + 2;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (xml.startsWith("<!", open)) throwSyntaxLimit();
|
|
161
|
+
const { close, attributes: tagAttributes } = scanTag(xml, open + 1);
|
|
162
|
+
if (close === -1) throwSyntaxLimit();
|
|
163
|
+
if (xml.charCodeAt(open + 1) === 47) {
|
|
164
|
+
if (depth === 0) throwSyntaxLimit();
|
|
165
|
+
depth -= 1;
|
|
166
|
+
cursor = close + 1;
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
nodes += 1;
|
|
170
|
+
if (nodes > limits.maxElementsPerPart) refuse("elements", `XML part contains more than ${String(limits.maxElementsPerPart)} elements`, nodes, limits.maxElementsPerPart);
|
|
171
|
+
if (budgetElements + nodes > limits.maxElementsPerPackage) refuse("package-elements", `DOCX package contains more than ${String(limits.maxElementsPerPackage)} XML elements`, budgetElements + nodes, limits.maxElementsPerPackage);
|
|
172
|
+
attributes += tagAttributes;
|
|
173
|
+
if (attributes > limits.maxAttributesPerPart) refuse("attributes", `XML part contains more than ${String(limits.maxAttributesPerPart)} attributes`, attributes, limits.maxAttributesPerPart);
|
|
174
|
+
if (budgetAttributes + attributes > limits.maxAttributesPerPackage) refuse("package-attributes", `DOCX package contains more than ${String(limits.maxAttributesPerPackage)} XML attributes`, budgetAttributes + attributes, limits.maxAttributesPerPackage);
|
|
175
|
+
let lastContent = close - 1;
|
|
176
|
+
while (lastContent > open && isXmlWhitespace(xml.charCodeAt(lastContent))) lastContent -= 1;
|
|
177
|
+
const elementDepth = depth + 1;
|
|
178
|
+
if (elementDepth > limits.maxDepth) refuse("depth", `XML part is nested deeper than ${String(limits.maxDepth)} elements`, elementDepth, limits.maxDepth);
|
|
179
|
+
if (elementDepth > maxDepth) maxDepth = elementDepth;
|
|
180
|
+
if (xml.charCodeAt(lastContent) !== 47) depth = elementDepth;
|
|
181
|
+
cursor = close + 1;
|
|
182
|
+
}
|
|
183
|
+
if (depth !== 0) throwSyntaxLimit();
|
|
184
|
+
if (budget) {
|
|
185
|
+
budget.elements += nodes;
|
|
186
|
+
budget.attributes += attributes;
|
|
187
|
+
}
|
|
188
|
+
return {
|
|
189
|
+
elements: nodes,
|
|
190
|
+
attributes,
|
|
191
|
+
maxDepth
|
|
192
|
+
};
|
|
193
|
+
};
|
|
194
|
+
export { FOLIO_XML_RESOURCE_LIMITS, XmlResourceLimitError, assertXmlResourceLimits, createXmlPackageBudget, exceedsUtf8ByteLimit };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parsed XML tree every folio reader shares.
|
|
3
|
+
*
|
|
4
|
+
* The shape is the one `xml-js` produced in non-compact mode, which folio's
|
|
5
|
+
* readers were written against: a root `{ elements }` holding element and
|
|
6
|
+
* text nodes, attributes as a record keyed by their source spelling.
|
|
7
|
+
*/
|
|
8
|
+
type XmlElement = {
|
|
9
|
+
declaration?: {
|
|
10
|
+
attributes?: Record<string, string | number>;
|
|
11
|
+
};
|
|
12
|
+
instruction?: string;
|
|
13
|
+
attributes?: Record<string, string | number | undefined>;
|
|
14
|
+
cdata?: string;
|
|
15
|
+
doctype?: string;
|
|
16
|
+
comment?: string;
|
|
17
|
+
text?: string | number | boolean;
|
|
18
|
+
type?: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
/** Resolved namespace URI; non-enumerable so legacy structural comparisons stay stable. */
|
|
21
|
+
namespaceUri?: string;
|
|
22
|
+
/** In-scope namespace declarations; non-enumerable so legacy comparisons stay stable. */
|
|
23
|
+
namespaceScope?: XmlNamespaceScope;
|
|
24
|
+
elements?: XmlElement[];
|
|
25
|
+
};
|
|
26
|
+
/** Parent-linked XML namespace declarations; each scope stores only local overrides. */
|
|
27
|
+
type XmlNamespaceScope = {
|
|
28
|
+
bindings: ReadonlyMap<string, string>;
|
|
29
|
+
parent?: XmlNamespaceScope;
|
|
30
|
+
};
|
|
31
|
+
export { XmlNamespaceScope as n, XmlElement as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/docx-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.41.0",
|
|
4
4
|
"description": "Typed OOXML/DOCX model, validation, serialization, legal-source compilation, and browser-native package projection.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"document-model",
|
|
@@ -57,6 +57,21 @@
|
|
|
57
57
|
"import": "./dist/schema/sequenceChildren.js",
|
|
58
58
|
"default": "./dist/schema/sequenceChildren.js"
|
|
59
59
|
},
|
|
60
|
+
"./xml": {
|
|
61
|
+
"types": "./dist/xml/streamingXmlParser.d.ts",
|
|
62
|
+
"import": "./dist/xml/streamingXmlParser.js",
|
|
63
|
+
"default": "./dist/xml/streamingXmlParser.js"
|
|
64
|
+
},
|
|
65
|
+
"./xml/limits": {
|
|
66
|
+
"types": "./dist/xml/xmlResourceLimits.d.ts",
|
|
67
|
+
"import": "./dist/xml/xmlResourceLimits.js",
|
|
68
|
+
"default": "./dist/xml/xmlResourceLimits.js"
|
|
69
|
+
},
|
|
70
|
+
"./xml/namespaces": {
|
|
71
|
+
"types": "./dist/xml/xmlNamespaceContext.d.ts",
|
|
72
|
+
"import": "./dist/xml/xmlNamespaceContext.js",
|
|
73
|
+
"default": "./dist/xml/xmlNamespaceContext.js"
|
|
74
|
+
},
|
|
60
75
|
"./zip": {
|
|
61
76
|
"types": "./dist/zip/archive.d.ts",
|
|
62
77
|
"import": "./dist/zip/archive.js",
|
|
@@ -82,11 +97,9 @@
|
|
|
82
97
|
"prepack": "bun run build"
|
|
83
98
|
},
|
|
84
99
|
"dependencies": {
|
|
85
|
-
"@nodable/entities": "^3.0.0",
|
|
86
100
|
"@stll/docx-utils": "^0.2.0",
|
|
87
101
|
"@stll/utf8-boundaries": "0.2.0",
|
|
88
102
|
"better-result": "3.0.1",
|
|
89
|
-
"fast-xml-parser": "^5.11.2",
|
|
90
103
|
"marked": "^18.1.1"
|
|
91
104
|
},
|
|
92
105
|
"devDependencies": {
|