@grafctory/mime-xml 0.0.0-stage → 0.6.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.
Files changed (63) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +65 -2
  3. package/dist/denoise.core.d.ts +12 -0
  4. package/dist/denoise.core.d.ts.map +1 -0
  5. package/dist/denoise.core.js +78 -0
  6. package/dist/denoise.core.js.map +1 -0
  7. package/dist/denoise.d.ts +45 -0
  8. package/dist/denoise.d.ts.map +1 -0
  9. package/dist/denoise.js +63 -0
  10. package/dist/denoise.js.map +1 -0
  11. package/dist/focus.core.d.ts +9 -0
  12. package/dist/focus.core.d.ts.map +1 -0
  13. package/dist/focus.core.js +305 -0
  14. package/dist/focus.core.js.map +1 -0
  15. package/dist/focus.d.ts +64 -0
  16. package/dist/focus.d.ts.map +1 -0
  17. package/dist/focus.js +88 -0
  18. package/dist/focus.js.map +1 -0
  19. package/dist/html.core.d.ts +9 -0
  20. package/dist/html.core.d.ts.map +1 -0
  21. package/dist/html.core.js +229 -0
  22. package/dist/html.core.js.map +1 -0
  23. package/dist/html.d.ts +76 -0
  24. package/dist/html.d.ts.map +1 -0
  25. package/dist/html.js +104 -0
  26. package/dist/html.js.map +1 -0
  27. package/dist/index.core.d.ts +74 -0
  28. package/dist/index.core.d.ts.map +1 -0
  29. package/dist/index.core.js +117 -0
  30. package/dist/index.core.js.map +1 -0
  31. package/dist/index.d.ts +67 -0
  32. package/dist/index.d.ts.map +1 -0
  33. package/dist/index.js +116 -0
  34. package/dist/index.js.map +1 -0
  35. package/dist/untag.core.d.ts +9 -0
  36. package/dist/untag.core.d.ts.map +1 -0
  37. package/dist/untag.core.js +219 -0
  38. package/dist/untag.core.js.map +1 -0
  39. package/dist/untag.d.ts +90 -0
  40. package/dist/untag.d.ts.map +1 -0
  41. package/dist/untag.js +106 -0
  42. package/dist/untag.js.map +1 -0
  43. package/dist/unwrap.d.ts +37 -0
  44. package/dist/unwrap.d.ts.map +1 -0
  45. package/dist/unwrap.js +69 -0
  46. package/dist/unwrap.js.map +1 -0
  47. package/dist/xml.core.d.ts +9 -0
  48. package/dist/xml.core.d.ts.map +1 -0
  49. package/dist/xml.core.js +86 -0
  50. package/dist/xml.core.js.map +1 -0
  51. package/dist/xml.d.ts +69 -0
  52. package/dist/xml.d.ts.map +1 -0
  53. package/dist/xml.js +97 -0
  54. package/dist/xml.js.map +1 -0
  55. package/dist/xpath.core.d.ts +100 -0
  56. package/dist/xpath.core.d.ts.map +1 -0
  57. package/dist/xpath.core.js +279 -0
  58. package/dist/xpath.core.js.map +1 -0
  59. package/dist/xpath.d.ts +128 -0
  60. package/dist/xpath.d.ts.map +1 -0
  61. package/dist/xpath.js +33 -0
  62. package/dist/xpath.js.map +1 -0
  63. package/package.json +62 -4
@@ -0,0 +1 @@
1
+ {"version":3,"file":"untag.js","sourceRoot":"","sources":["../src/untag.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAG1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoFG;AACH,MAAM,UAAU,KAAK;IAEpB,OAAO,GAAG,CAAC,OAAO,CAAC,CAAC;AAErB,CAAC"}
@@ -0,0 +1,37 @@
1
+ import { type Value } from "@grafctory/core";
2
+ import type { Task } from "@grafctory/flow";
3
+ import type { AnyNode } from "domhandler";
4
+ /**
5
+ * Creates a structured data extractor.
6
+ *
7
+ * The task converts a feed of parsed X/HTML trees into a feed of the JSON-LD values each tree embeds, one list per
8
+ * tree. Consumers can thus read the structured data a site publishes even if a page renders its content client-side
9
+ * and carries no readable prose of its own, as single-page applications commonly do.
10
+ *
11
+ * Trees embedding no values produce an empty list, so that lists stay aligned with the input feed.
12
+ *
13
+ * Values are collected in document order from the `application/ld+json` scripts of each tree, wherever they are
14
+ * placed in the page, `head` included. Each script contributes the value it states as a single item, with arrays and
15
+ * `@graph` members left unexpanded, so that consumers pick the objects they need with plain array methods. Scripts
16
+ * that fail to parse are skipped, so that one malformed block cannot hide the well-formed blocks beside it.
17
+ *
18
+ * > [!NOTE]
19
+ * >
20
+ * > - **Incremental**: each list is emitted as soon as its tree is drawn, so endless sources are processed for as long
21
+ * > as the feed is consumed.
22
+ * > - **Streaming**: trees are processed one at a time and released once read, so memory use doesn't grow with the
23
+ * > length of the feed.
24
+ * > - **Stateless**: each tree is processed independently, so the result doesn't depend on how the feed is split
25
+ * > across nested feeds or runs.
26
+ *
27
+ * @returns A task converting a feed of parsed X/HTML trees into a feed of the JSON-LD values each tree embeds
28
+ *
29
+ * @throws {@link !Error Error} While the feed is consumed, if the source feed fails
30
+ *
31
+ * @see {@link https://json-ld.org/ JSON-LD}
32
+ * @see {@link https://html.spec.whatwg.org/multipage/scripting.html#data-block WHATWG HTML - Data blocks}
33
+ *
34
+ * @group Factories
35
+ */
36
+ export declare function unwrap(): Task<AnyNode, readonly Value[]>;
37
+ //# sourceMappingURL=unwrap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unwrap.d.ts","sourceRoot":"","sources":["../src/unwrap.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAW,KAAK,KAAK,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAK1C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,wBAAgB,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,SAAS,KAAK,EAAE,CAAC,CAwBxD"}
package/dist/unwrap.js ADDED
@@ -0,0 +1,69 @@
1
+ /*
2
+ * Copyright © 2026 Metreeca srl
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { isValue } from "@grafctory/core";
17
+ import { map } from "@grafctory/flow/tasks";
18
+ import { content } from "./xpath.core.js";
19
+ import { xpath } from "./xpath.js";
20
+ /**
21
+ * Creates a structured data extractor.
22
+ *
23
+ * The task converts a feed of parsed X/HTML trees into a feed of the JSON-LD values each tree embeds, one list per
24
+ * tree. Consumers can thus read the structured data a site publishes even if a page renders its content client-side
25
+ * and carries no readable prose of its own, as single-page applications commonly do.
26
+ *
27
+ * Trees embedding no values produce an empty list, so that lists stay aligned with the input feed.
28
+ *
29
+ * Values are collected in document order from the `application/ld+json` scripts of each tree, wherever they are
30
+ * placed in the page, `head` included. Each script contributes the value it states as a single item, with arrays and
31
+ * `@graph` members left unexpanded, so that consumers pick the objects they need with plain array methods. Scripts
32
+ * that fail to parse are skipped, so that one malformed block cannot hide the well-formed blocks beside it.
33
+ *
34
+ * > [!NOTE]
35
+ * >
36
+ * > - **Incremental**: each list is emitted as soon as its tree is drawn, so endless sources are processed for as long
37
+ * > as the feed is consumed.
38
+ * > - **Streaming**: trees are processed one at a time and released once read, so memory use doesn't grow with the
39
+ * > length of the feed.
40
+ * > - **Stateless**: each tree is processed independently, so the result doesn't depend on how the feed is split
41
+ * > across nested feeds or runs.
42
+ *
43
+ * @returns A task converting a feed of parsed X/HTML trees into a feed of the JSON-LD values each tree embeds
44
+ *
45
+ * @throws {@link !Error Error} While the feed is consumed, if the source feed fails
46
+ *
47
+ * @see {@link https://json-ld.org/ JSON-LD}
48
+ * @see {@link https://html.spec.whatwg.org/multipage/scripting.html#data-block WHATWG HTML - Data blocks}
49
+ *
50
+ * @group Factories
51
+ */
52
+ export function unwrap() {
53
+ return map(extract);
54
+ function extract(tree) {
55
+ return xpath(tree)("//script[@type='application/ld+json']")
56
+ .map(content)
57
+ .map(parse)
58
+ .filter(isValue);
59
+ }
60
+ function parse(text) {
61
+ try {
62
+ return JSON.parse(text);
63
+ }
64
+ catch {
65
+ return undefined; // not a value, so that the block is skipped
66
+ }
67
+ }
68
+ }
69
+ //# sourceMappingURL=unwrap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"unwrap.js","sourceRoot":"","sources":["../src/unwrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAEH,OAAO,EAAE,OAAO,EAAc,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EAAE,GAAG,EAAE,MAAM,uBAAuB,CAAC;AAE5C,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAGnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,MAAM,UAAU,MAAM;IAErB,OAAO,GAAG,CAAC,OAAO,CAAC,CAAC;IAGpB,SAAS,OAAO,CAAC,IAAa;QAC7B,OAAO,KAAK,CAAC,IAAI,CAAC,CAAC,uCAAuC,CAAC;aACzD,GAAG,CAAC,OAAO,CAAC;aACZ,GAAG,CAAC,KAAK,CAAC;aACV,MAAM,CAAC,OAAO,CAAC,CAAC;IACnB,CAAC;IAED,SAAS,KAAK,CAAC,IAAY;QAC1B,IAAI,CAAC;YAEJ,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAEzB,CAAC;QAAC,MAAM,CAAC;YAER,OAAO,SAAS,CAAC,CAAC,4CAA4C;QAE/D,CAAC;IACF,CAAC;AAEF,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { IRI } from "@grafctory/core/resource";
2
+ import type { Document } from "domhandler";
3
+ /**
4
+ * Parses an XML document.
5
+ *
6
+ * Helper backing the `xml()` task, which states the parsing contract.
7
+ */
8
+ export declare function process(document: string | Response, base?: IRI): Promise<undefined | Document>;
9
+ //# sourceMappingURL=xml.core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml.core.d.ts","sourceRoot":"","sources":["../src/xml.core.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AAGpD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAuB3C;;;;GAIG;AACH,wBAAsB,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,QAAQ,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,SAAS,GAAG,QAAQ,CAAC,CAgFpG"}
@@ -0,0 +1,86 @@
1
+ /*
2
+ * Copyright © 2026 Metreeca srl
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { parseItem } from "@grafctory/http";
17
+ import { log } from "@grafctory/tape";
18
+ import { isTag } from "domhandler";
19
+ import { parseDocument } from "htmlparser2";
20
+ import { isBase } from "./index.core.js";
21
+ /**
22
+ * The media types an XML document is served under.
23
+ *
24
+ * Matches any type whose subtype is `xml` or carries the `+xml` structured syntax suffix, so that XML-based formats,
25
+ * `application/rss+xml` and `application/xhtml+xml` among them, are recognised alongside plain `application/xml` and
26
+ * `text/xml`.
27
+ *
28
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7303#section-4.2 RFC 7303 § 4.2 - '+xml' Structured Syntax Suffix}
29
+ */
30
+ const XMLType = /^[-\w.]+\/(?:[-\w.]+\+)?xml$/i;
31
+ const logger = log(import.meta.url);
32
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
33
+ /**
34
+ * Parses an XML document.
35
+ *
36
+ * Helper backing the `xml()` task, which states the parsing contract.
37
+ */
38
+ export async function process(document, base) {
39
+ const text = (document instanceof Response ? await read(document) : document).trim();
40
+ // a document holding nothing but whitespace, a byte order mark included, holds no content
41
+ return text ? rebase(parseDocument(text, { xmlMode: true }), locate(document, base)) : undefined;
42
+ async function read(response) {
43
+ const [type, parameters] = parseItem(response.headers.get("Content-Type"));
44
+ const charset = parameters.get("charset") || "UTF-8";
45
+ if (type && !XMLType.test(type)) {
46
+ logger.warn `unexpected <${type}> content type`;
47
+ }
48
+ const decoder = decode(charset);
49
+ if (decoder === undefined) {
50
+ logger.warn `unknown <${charset}> charset`;
51
+ }
52
+ return (decoder ?? new TextDecoder()).decode(await response.arrayBuffer());
53
+ }
54
+ function decode(charset) {
55
+ try {
56
+ return new TextDecoder(charset);
57
+ }
58
+ catch {
59
+ return undefined;
60
+ }
61
+ }
62
+ function locate(document, base) {
63
+ // a stated base is taken as it stands, so the retrieval URL never stands in as the one to resolve it against
64
+ if (base !== undefined) {
65
+ if (!isBase(base)) {
66
+ throw new RangeError(`expected resolvable base URL <${base}>`);
67
+ }
68
+ return new URL(base);
69
+ }
70
+ else {
71
+ return document instanceof Response && document.url ? new URL(document.url) : undefined;
72
+ }
73
+ }
74
+ function rebase(document, base) {
75
+ if (base === undefined) {
76
+ return document;
77
+ }
78
+ else {
79
+ document.children.filter(isTag).forEach(root => {
80
+ root.attribs["xml:base"] = new URL(root.attribs["xml:base"] ?? "", base).href;
81
+ });
82
+ return document;
83
+ }
84
+ }
85
+ }
86
+ //# sourceMappingURL=xml.core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml.core.js","sourceRoot":"","sources":["../src/xml.core.ts"],"names":[],"mappings":"A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UAAU,CAAC,GAAG,IAAI,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE,EAAE,IAAI,CAAC,CAAC,IAAI,CAAC;YAC/E,CAAC,CAAC,CAAC;YAEH,OAAO,QAAQ,CAAC;QAEjB,CAAC;IAEF,CAAC;AAEF,CAAC"}
package/dist/xml.d.ts ADDED
@@ -0,0 +1,69 @@
1
+ import type { IRI } from "@grafctory/core/resource";
2
+ import type { Task } from "@grafctory/flow";
3
+ import type { Document } from "domhandler";
4
+ /**
5
+ * Creates an XML parser.
6
+ *
7
+ * The task converts a feed of XML documents into a feed of parsed trees, one tree per document, so that downstream
8
+ * tasks work on document structure rather than on text.
9
+ *
10
+ * Each document is given either as text or as a response carrying it in its body. Empty or whitespace-only documents
11
+ * produce no tree, and neither do responses without a body.
12
+ *
13
+ * Response bodies are decoded with the `charset` parameter of their content type. If no charset is stated, bodies are
14
+ * decoded as UTF-8 rather than as the US-ASCII default of `text` media types. A leading byte order mark is stripped,
15
+ * both from text and from bodies in a Unicode charset.
16
+ *
17
+ * A response with a content type that is not an XML one is logged as a warning and parsed anyway. XML content types
18
+ * are `application/xml`, `text/xml` and `+xml` formats such as `application/rss+xml`. A response with a charset the
19
+ * platform can't decode is also logged, and its body is decoded as UTF-8. Since parsing never fails, these warnings
20
+ * are the only sign that a source is mis-declared.
21
+ *
22
+ * Trees carry the base URL needed to resolve the references they contain, recorded as an `xml:base` attribute on each
23
+ * root element. A root element that already declares `xml:base` keeps it, resolved against the recorded base URL.
24
+ *
25
+ * The base URL is the `base` argument, if given, or else the final URL of the response, after any redirects. No base
26
+ * URL is recorded for documents given as text or for responses without a URL.
27
+ *
28
+ * > [!NOTE]
29
+ * >
30
+ * > - **Incremental**: each tree is emitted as soon as its document is parsed, so endless sources are processed for as
31
+ * > long as the feed is consumed.
32
+ * > - **Materialising**: each document is held in memory as a whole while it is parsed, so peak memory use is about
33
+ * > twice the size of the largest document, regardless of the length of the feed.
34
+ * > - **Stateless**: each document is parsed independently, so the result doesn't depend on how the feed is split
35
+ * > across nested feeds or runs.
36
+ *
37
+ * > [!WARNING]
38
+ * >
39
+ * > Parsing is forgiving and never fails. Emitted trees are always structurally sound, since unclosed elements are
40
+ * > closed at the end of the input, but malformed input may be misrepresented rather than rejected: an unclosed element
41
+ * > absorbs the content that follows it, an unterminated attribute value swallows the rest of the input, and a tree
42
+ * > may have any number of root elements, including none. Consumers requiring well-formed input must validate the
43
+ * > emitted trees themselves.
44
+ *
45
+ * > [!WARNING]
46
+ * >
47
+ * > The encoding declared in the XML prolog is ignored: response bodies are decoded with the charset stated by their
48
+ * > content type, even if the document declares a different one.
49
+ *
50
+ * @param base The base URL for resolving references, overriding the URL of responses; must be a hierarchical
51
+ * identifier, that is a scheme followed by a root-relative path
52
+ *
53
+ * @returns A task converting a feed of XML documents, given as text or as responses, into a feed of parsed trees
54
+ *
55
+ * @throws {@link !RangeError RangeError} If `base` is not a hierarchical identifier, for instance a relative reference
56
+ * or an opaque identifier such as `urn:example:x`, which would leave references
57
+ * silently unresolved
58
+ *
59
+ * @throws {@link !Error Error} While the feed is consumed, if the source feed fails or a response body can't be
60
+ * read
61
+ *
62
+ * @see {@link https://www.w3.org/TR/xml/ Extensible Markup Language (XML) 1.0}
63
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7303 RFC 7303 XML Media Types}
64
+ * @see {@link https://www.w3.org/TR/xmlbase/ XML Base}
65
+ *
66
+ * @group Factories
67
+ */
68
+ export declare function xml(base?: IRI): Task<string | Response, Document>;
69
+ //# sourceMappingURL=xml.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml.d.ts","sourceRoot":"","sources":["../src/xml.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,0BAA0B,CAAC;AACpD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAO3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,wBAAgB,GAAG,CAAC,IAAI,CAAC,EAAE,GAAG,GAAG,IAAI,CAAC,MAAM,GAAG,QAAQ,EAAE,QAAQ,CAAC,CAsBjE"}
package/dist/xml.js ADDED
@@ -0,0 +1,97 @@
1
+ /*
2
+ * Copyright © 2026 Metreeca srl
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ import { items } from "@grafctory/flow/feeds";
17
+ import { isBase } from "./index.core.js";
18
+ import { process } from "./xml.core.js";
19
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
+ /**
21
+ * Creates an XML parser.
22
+ *
23
+ * The task converts a feed of XML documents into a feed of parsed trees, one tree per document, so that downstream
24
+ * tasks work on document structure rather than on text.
25
+ *
26
+ * Each document is given either as text or as a response carrying it in its body. Empty or whitespace-only documents
27
+ * produce no tree, and neither do responses without a body.
28
+ *
29
+ * Response bodies are decoded with the `charset` parameter of their content type. If no charset is stated, bodies are
30
+ * decoded as UTF-8 rather than as the US-ASCII default of `text` media types. A leading byte order mark is stripped,
31
+ * both from text and from bodies in a Unicode charset.
32
+ *
33
+ * A response with a content type that is not an XML one is logged as a warning and parsed anyway. XML content types
34
+ * are `application/xml`, `text/xml` and `+xml` formats such as `application/rss+xml`. A response with a charset the
35
+ * platform can't decode is also logged, and its body is decoded as UTF-8. Since parsing never fails, these warnings
36
+ * are the only sign that a source is mis-declared.
37
+ *
38
+ * Trees carry the base URL needed to resolve the references they contain, recorded as an `xml:base` attribute on each
39
+ * root element. A root element that already declares `xml:base` keeps it, resolved against the recorded base URL.
40
+ *
41
+ * The base URL is the `base` argument, if given, or else the final URL of the response, after any redirects. No base
42
+ * URL is recorded for documents given as text or for responses without a URL.
43
+ *
44
+ * > [!NOTE]
45
+ * >
46
+ * > - **Incremental**: each tree is emitted as soon as its document is parsed, so endless sources are processed for as
47
+ * > long as the feed is consumed.
48
+ * > - **Materialising**: each document is held in memory as a whole while it is parsed, so peak memory use is about
49
+ * > twice the size of the largest document, regardless of the length of the feed.
50
+ * > - **Stateless**: each document is parsed independently, so the result doesn't depend on how the feed is split
51
+ * > across nested feeds or runs.
52
+ *
53
+ * > [!WARNING]
54
+ * >
55
+ * > Parsing is forgiving and never fails. Emitted trees are always structurally sound, since unclosed elements are
56
+ * > closed at the end of the input, but malformed input may be misrepresented rather than rejected: an unclosed element
57
+ * > absorbs the content that follows it, an unterminated attribute value swallows the rest of the input, and a tree
58
+ * > may have any number of root elements, including none. Consumers requiring well-formed input must validate the
59
+ * > emitted trees themselves.
60
+ *
61
+ * > [!WARNING]
62
+ * >
63
+ * > The encoding declared in the XML prolog is ignored: response bodies are decoded with the charset stated by their
64
+ * > content type, even if the document declares a different one.
65
+ *
66
+ * @param base The base URL for resolving references, overriding the URL of responses; must be a hierarchical
67
+ * identifier, that is a scheme followed by a root-relative path
68
+ *
69
+ * @returns A task converting a feed of XML documents, given as text or as responses, into a feed of parsed trees
70
+ *
71
+ * @throws {@link !RangeError RangeError} If `base` is not a hierarchical identifier, for instance a relative reference
72
+ * or an opaque identifier such as `urn:example:x`, which would leave references
73
+ * silently unresolved
74
+ *
75
+ * @throws {@link !Error Error} While the feed is consumed, if the source feed fails or a response body can't be
76
+ * read
77
+ *
78
+ * @see {@link https://www.w3.org/TR/xml/ Extensible Markup Language (XML) 1.0}
79
+ * @see {@link https://www.rfc-editor.org/rfc/rfc7303 RFC 7303 XML Media Types}
80
+ * @see {@link https://www.w3.org/TR/xmlbase/ XML Base}
81
+ *
82
+ * @group Factories
83
+ */
84
+ export function xml(base) {
85
+ if (base !== undefined && !isBase(base)) {
86
+ throw new RangeError(`expected resolvable base URL <${base}>`);
87
+ }
88
+ return documents => items((async function* () {
89
+ for await (const document of documents) {
90
+ const tree = await process(document, base);
91
+ if (tree !== undefined) { // a document holding no text contributes no value
92
+ yield tree;
93
+ }
94
+ }
95
+ })());
96
+ }
97
+ //# sourceMappingURL=xml.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xml.js","sourceRoot":"","sources":["../src/xml.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAIH,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAGxC,wHAAwH;AAExH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,MAAM,UAAU,GAAG,CAAC,IAAU;IAE7B,IAAK,IAAI,KAAK,SAAS,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,EAAG,CAAC;QAC3C,MAAM,IAAI,UAAU,CAAC,iCAAiC,IAAI,GAAG,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,SAAS,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,KAAK,SAAS,CAAC;QAEzC,IAAI,KAAK,EAAE,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YAExC,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;YAE3C,IAAK,IAAI,KAAK,SAAS,EAAG,CAAC,CAAC,kDAAkD;gBAE7E,MAAM,IAAI,CAAC;YAEZ,CAAC;QAEF,CAAC;IAEF,CAAC,CAAC,EAAE,CAAC,CAAC;AAEP,CAAC"}
@@ -0,0 +1,100 @@
1
+ import type { AnyNode, Element } from "domhandler";
2
+ /**
3
+ * A value an expression is evaluated against or returns.
4
+ *
5
+ * Covers the value types defined by XPath 1.0: tree nodes, attributes, and computed strings, numbers and booleans. Any
6
+ * selected value can be the target of a further selection; computed values have no tree, so selecting from them
7
+ * returns nothing.
8
+ *
9
+ * @see {@link https://www.w3.org/TR/1999/REC-xpath-19991116/#section-Introduction XML Path Language (XPath) 1.0 -
10
+ * Introduction}
11
+ */
12
+ export type Target = AnyNode | Attribute | string | number | boolean;
13
+ /**
14
+ * A node a tree holds, or an attribute of one.
15
+ */
16
+ type Node = AnyNode | Attribute;
17
+ /**
18
+ * An attribute of an element, as a node.
19
+ *
20
+ * Selected by attribute steps and usable as the target of further selections, like any element. The same attribute of
21
+ * the same element is always the same object, so it can be compared by identity and appears only once in a selection.
22
+ */
23
+ export type Attribute = {
24
+ /**
25
+ * The name of the attribute, as stored in the tree, prefix included.
26
+ */
27
+ readonly name: string;
28
+ /**
29
+ * The value of the attribute.
30
+ */
31
+ readonly value: string;
32
+ /**
33
+ * The element stating the attribute.
34
+ */
35
+ readonly parent: Element;
36
+ };
37
+ /**
38
+ * Checks whether a value is a node.
39
+ *
40
+ * @param value The value to check
41
+ *
42
+ * @returns `true` if `value` is a node held by a tree, or an attribute of one; `false` if it is a value an expression
43
+ * computed
44
+ */
45
+ export declare function isNode(value: Target): value is Node;
46
+ /**
47
+ * Checks whether a value is an attribute.
48
+ *
49
+ * @param value The value to check
50
+ *
51
+ * @returns `true` if `value` is an attribute of an element; `false` otherwise
52
+ */
53
+ export declare function isAttribute(value: Target): value is Attribute;
54
+ /**
55
+ * Checks whether a value is an element.
56
+ *
57
+ * @param value The value to check
58
+ *
59
+ * @returns `true` if `value` is an element; `false` otherwise
60
+ */
61
+ export declare function isElement(value: Target): value is Element;
62
+ /**
63
+ * Reads the text a value holds.
64
+ *
65
+ * @param value The value to read
66
+ *
67
+ * @returns The text `value` is written as, as the XPath `string()` function converts it: the value of an attribute, the
68
+ * text a comment carries, the character data held by the tree rooted at any other node, the comments and
69
+ * directives within it contributing none, and the written form of a computed string, number or boolean
70
+ *
71
+ * @see {@link https://www.w3.org/TR/1999/REC-xpath-19991116/#function-string XML Path Language (XPath) 1.0 - string()}
72
+ */
73
+ export declare function content(value: Target): string;
74
+ /**
75
+ * Draws the URL the relative references held by a value resolve against.
76
+ *
77
+ * Reads the base URL a tree records as it is parsed, so that a reference is resolved by the standard rules wherever it
78
+ * sits in the tree, without the request the tree was drawn from being tracked alongside it.
79
+ *
80
+ * The base is the one stated by the nearest `xml:base` attribute in scope of the value, that is on the value itself, if
81
+ * it is an element, or on its closest ancestor stating one, each value resolved against the ones stated further up. A
82
+ * tree that states none, as one parsed from text under no stated base does, and one whose outermost `xml:base` is
83
+ * itself a relative reference state no base, as does a value an expression computed, which holds no tree.
84
+ *
85
+ * @param value The value whose base URL is to be drawn
86
+ *
87
+ * @returns The absolute URL relative references held by `value` resolve against; `undefined` if the tree holding
88
+ * `value` states none
89
+ *
90
+ * @see {@link https://www.w3.org/TR/xmlbase/ XML Base}
91
+ */
92
+ export declare function base(value: Target): undefined | URL;
93
+ /**
94
+ * Selects values from a value.
95
+ *
96
+ * Helper backing the `XPath` selector, which states the selection contract.
97
+ */
98
+ export declare function select(value: Target, path: string): readonly Target[];
99
+ export {};
100
+ //# sourceMappingURL=xpath.core.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"xpath.core.d.ts","sourceRoot":"","sources":["../src/xpath.core.ts"],"names":[],"mappings":"AAoBA,OAAO,KAAK,EAAE,OAAO,EAAa,OAAO,EAAc,MAAM,YAAY,CAAC;AAK1E;;;;;;;;;GASG;AACH,MAAM,MAAM,MAAM,GAAG,OAAO,GAAG,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAErE;;GAEG;AACH,KAAK,IAAI,GAAG,OAAO,GAAG,SAAS,CAAC;AAEhC;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG;IAEvB;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB;;OAEG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAEvB;;OAEG;IACH,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;CAEzB,CAAA;AAiPD;;;;;;;GAOG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,IAAI,CAEnD;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,SAAS,CAE7D;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,KAAK,IAAI,OAAO,CAEzD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAM7C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,SAAS,GAAG,GAAG,CA8BnD;AAED;;;;GAIG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,MAAM,EAAE,CA8BrE"}