xml-sax-ts 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Aaron Zielstorff
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,162 @@
1
+ # xml-sax-ts
2
+
3
+ [![npm version](https://img.shields.io/npm/v/xml-sax-ts?color=blue)](https://www.npmjs.com/package/xml-sax-ts)
4
+ [![license](https://img.shields.io/npm/l/xml-sax-ts)](./LICENSE)
5
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/xml-sax-ts)](https://bundlephobia.com/package/xml-sax-ts)
6
+ [![TypeScript](https://img.shields.io/badge/TypeScript-5.6+-blue?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
7
+ [![Node.js](https://img.shields.io/badge/Node.js-%E2%89%A518-green?logo=node.js&logoColor=white)](https://nodejs.org/)
8
+ [![zero dependencies](https://img.shields.io/badge/dependencies-0-brightgreen)](./package.json)
9
+
10
+ > One-pass, streaming (SAX-style) XML parser for TypeScript — works in Node.js and browsers.
11
+
12
+ ## Highlights
13
+
14
+ - **Streaming** — feed chunks of XML as they arrive; no need to buffer the whole document
15
+ - **Lightweight** — zero runtime dependencies, tree-shakeable ESM + CJS
16
+ - **Type-safe** — written in TypeScript with full type exports
17
+ - **Namespace-aware** — resolves prefixes, URIs, and local names out of the box
18
+ - **Two-way** — parse XML to a tree (`parseXmlString`) or serialize a tree back to XML (`serializeXml`)
19
+ - **Design-by-contract** — invariant checks in development, stripped in production
20
+
21
+ ## Install
22
+
23
+ ```bash
24
+ npm install xml-sax-ts
25
+ ```
26
+
27
+ ## Quick start
28
+
29
+ ### SAX streaming
30
+
31
+ ```ts
32
+ import { XmlSaxParser } from "xml-sax-ts";
33
+
34
+ const parser = new XmlSaxParser({
35
+ onOpenTag: (tag) => console.log("open", tag.name, tag.attributes),
36
+ onText: (text) => console.log("text", text),
37
+ onCloseTag: (tag) => console.log("close", tag.name),
38
+ });
39
+
40
+ parser.feed("<root>");
41
+ parser.feed("Hello</root>");
42
+ parser.close();
43
+ ```
44
+
45
+ ### Parse to tree
46
+
47
+ ```ts
48
+ import { parseXmlString } from "xml-sax-ts";
49
+
50
+ const root = parseXmlString("<root><a>1</a><b/></root>");
51
+ console.log(root.name); // "root"
52
+ ```
53
+
54
+ ### Serialize to XML
55
+
56
+ ```ts
57
+ import { serializeXml } from "xml-sax-ts";
58
+
59
+ const xml = serializeXml(
60
+ {
61
+ name: "root",
62
+ attributes: { id: "1" },
63
+ children: ["Hello", { name: "child", children: ["World"] }],
64
+ },
65
+ { pretty: true, xmlDeclaration: true },
66
+ );
67
+ // <?xml version="1.0" encoding="UTF-8"?>
68
+ // <root id="1">
69
+ // Hello
70
+ // <child>
71
+ // World
72
+ // </child>
73
+ // </root>
74
+ ```
75
+
76
+ ## API
77
+
78
+ ### `XmlSaxParser`
79
+
80
+ ```ts
81
+ new XmlSaxParser(options?: ParserOptions)
82
+ ```
83
+
84
+ | Method | Description |
85
+ | --------- | ------------------------------------------------- |
86
+ | `feed(chunk)` | Feed a string chunk to the parser |
87
+ | `close()` | Signal end-of-input and validate open tags |
88
+
89
+ #### `ParserOptions`
90
+
91
+ | Option | Type | Default | Description |
92
+ | ----------------------------- | ---------- | ------- | ---------------------------------------------- |
93
+ | `xmlns` | `boolean` | `true` | Enable namespace resolution |
94
+ | `includeNamespaceAttributes` | `boolean` | `false` | Include `xmlns:*` attributes in tag output |
95
+ | `allowDoctype` | `boolean` | `true` | Allow `<!DOCTYPE …>` declarations |
96
+ | `onOpenTag` | `function` | — | Called for each opening / self-closing tag |
97
+ | `onCloseTag` | `function` | — | Called for each closing tag |
98
+ | `onText` | `function` | — | Called for text nodes |
99
+ | `onCdata` | `function` | — | Called for CDATA sections |
100
+ | `onComment` | `function` | — | Called for comments |
101
+ | `onProcessingInstruction` | `function` | — | Called for processing instructions (`<?…?>`) |
102
+ | `onDoctype` | `function` | — | Called for DOCTYPE declarations |
103
+ | `onError` | `function` | — | Called on parse errors |
104
+
105
+ ### `parseXmlString(xml, options?)`
106
+
107
+ Convenience function that parses a complete XML string into an `XmlNode` tree using `XmlSaxParser` + `TreeBuilder` internally.
108
+
109
+ ### `TreeBuilder`
110
+
111
+ Low-level tree builder. Attach its `onOpenTag`, `onText`, `onCdata`, and `onCloseTag` methods to a parser, then call `getRoot()` to retrieve the resulting `XmlNode`.
112
+
113
+ ### `serializeXml(node, options?)`
114
+
115
+ Serializes an `XmlNode` back to an XML string.
116
+
117
+ #### `SerializeOptions`
118
+
119
+ | Option | Type | Default | Description |
120
+ | ----------------- | --------- | -------- | ---------------------------------------- |
121
+ | `xmlDeclaration` | `boolean` | `false` | Prepend `<?xml …?>` declaration |
122
+ | `pretty` | `boolean` | `false` | Enable indented output |
123
+ | `indent` | `string` | `" "` | Indentation string (when `pretty`) |
124
+ | `newline` | `string` | `"\n"` | Newline string (when `pretty`) |
125
+
126
+ ### `XmlSaxError`
127
+
128
+ Custom error class thrown on parse errors. Includes `offset`, `line`, and `column` properties for precise error location.
129
+
130
+ ### Exported types
131
+
132
+ `OpenTag` · `CloseTag` · `XmlAttribute` · `ProcessingInstruction` · `Doctype` · `XmlNode` · `XmlChild` · `XmlPosition` · `ParserOptions` · `SerializeOptions`
133
+
134
+ ## Features
135
+
136
+ - Namespace resolution (`xmlns`)
137
+ - CDATA sections
138
+ - Entity decoding (named + numeric)
139
+ - Processing instructions
140
+ - DOCTYPE handling (parse + emit)
141
+ - Comments
142
+ - Precise error positions (line, column, offset)
143
+ - Pretty-print serialization with XML declaration
144
+
145
+ ## Design-by-contract
146
+
147
+ Internal invariants are checked during development. Set `NODE_ENV=production` to strip them from production bundles — no runtime overhead.
148
+
149
+ ## Development
150
+
151
+ ```bash
152
+ npm install # install dependencies
153
+ npm run build # build ESM + CJS with tsup
154
+ npm test # run tests with vitest
155
+ npm run test:watch # run tests in watch mode
156
+ npm run lint # eslint + tsc type check
157
+ npm run lint:fix # auto-fix lint issues
158
+ ```
159
+
160
+ ## License
161
+
162
+ [MIT](./LICENSE) © Aaron Zielstorff