@federicocarboni/saxe

中文文档
0.8.0发布于 2025/12/04Apache-2.0TypeScriptES Module零依赖近 30 天下载 3385

Light-weight and efficient SAX-style XML parser

原始使用文档
文档目录7 节
  1. Goals
  2. Non-Goals
  3. Example
  4. Runtime Support
  5. Document Type Declaration
  6. Security
  7. License

Saxe

Docs Coverage Bundle size

Light-weight and efficient SAX-style XML parser for JavaScript.

Goals

  • Full XML 1.0 and Namespaces in XML 1.0 standard conformance
  • Simple and terse API
  • Reduced code footprint
  • Set a base for other standards built on XML (e.g. XHTML)

Non-Goals

  • XML DTD validation
  • Full DOM implementation
  • Syntax error tolerance
  • Source code analysis or LSP features

XML 1.1 and Namespaces in XML 1.1

XML 1.1 and Namespaces in XML 1.1 are not supported. Documents declaring version 1.1 are parsed as XML 1.0, so features exclusive to version 1.1 are not recognized.

Modern UTF-8 web content is exclusively XML 1.0, which makes XML 1.1 and its namespaces mostly irrelevant.

XML 1.1 is used almost exclusively in legacy or specialized contexts where its niche features and better EBCDIC support might be useful. See XML - Wikipedia § Versions 1.0 and 1.1.

Example

import {SaxParser} from "@federicocarboni/saxe";

const parser = new SaxParser({
  startTag(name, attributes) {
    // Start tag: example
    // Start tag: empty-tag [attr, value]
    console.log("Start tag:", name, ...attributes);
  },
  endTag(name) {
    // End tag: example
    // End tag: empty-tag
    console.log("End tag:", name);
  },
  text(content) {
    // Text: Hello, world!
    console.log("Text:", content);
  },
});
parser.parse("<example>Hello, world!", {stream: true});
parser.parse(`<empty-tag attr="value" />`, {stream: true});
parser.parse("</example>");

Runtime Support

  • Basic XML parsing: any ES2017 runtime. For older runtimes transpiling and polyfilling should be enough.

Document Type Declaration

Many[^1] JavaScript XML parsers simplify handling of the internal DTD subset, by either not checking for well-formedness or ignoring its declarations.

Internal DTD subset parsing is required even for non-validating[^2] processors, this parser implements the entire specification:

  • The internal DTD subset is parsed and checked for well-formedness.
  • ATTLIST declarations are recognized to apply normalization and default values to attributes.
  • ENTITY declarations are recognized to expand entity references.

This process has security implications; so DTD processing can be enabled by configuring SaxOptions.dtd.

External markup declarations and external entities are not required for non-validating[^2] processors and are explicitly not supported.

Security

XML parsers may be subject to a number of possible vulnerabilities, most common attacks exploit external entity resolution and entity expansion.

This parser is strictly non-validating, so by design it should not be vulnerable to any XXE[^3] based attack. Additionally the length of strings collected during parsing is capped to limit the efficacy of other denial-of-service attacks[^4].

Following OWASP recommendations DTD processing is prohibited by default.

new SaxParser(handler, {
  // Reject any DOCTYPE declaration
  dtd: "prohibit", // default
  // Alternatively, allow it but ignore any declarations
  // dtd: "ignore",

  // Enforce stricter limits over strings and values
  // collected during parsing.
  maxAttributesLength: 10000,
  maxElementDepth: 30,
  maxEntityDepth: 5,
  maxEntityLength: 1000,
  maxNameLength: 500,
  maxTextLength: 10000,
})

Known XML Bombs are tested for as part of regular integration tests and the parser is fuzz tested regularly. Despite this being the case, for very sensible or security oriented apps you may want to conduct your own security audit.

[^1]: Other JavaScript XML parsers inspected include isaacs/sax-js, NaturalIntelligence/fast-xml-parser and lddubeau/saxes [^2]: Non-validating XML processors (parsers) do not validate documents, but must still recognize and report well-formedness (syntax) errors. Non-validating processors are not required to fetch and parse external markup declarations and external entities. XML Standard § 5.1 Validating and Non-Validating Processors [^3]: XML External Entity (XXE) Processing | OWASP Foundation [^4]: XML Denial of Service Attacks and Defenses | Microsoft Learn

License

Licensed under the Apache License, Version 2.0. See the LICENSE file for details.

版本与发布记录

@federicocarboni/saxe 共发布 8 个版本,首次发布于 2025/10/14;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.8.0latest2025/12/0411 个月前
0.7.02025/11/141 年前
0.6.02025/11/031 年前
0.5.02025/10/301 年前
0.4.02025/10/211.1 年前
0.3.02025/10/201.1 年前

关于 @federicocarboni/saxe 的常见问题

@federicocarboni/saxe 是什么?

Light-weight and efficient SAX-style XML parser

如何安装 @federicocarboni/saxe?

在项目目录运行 npm install @federicocarboni/saxe,或使用 pnpm add / yarn add / bun add。

@federicocarboni/saxe 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@federicocarboni/saxe 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@federicocarboni/saxe 可以商用吗?

@federicocarboni/saxe 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@federicocarboni/saxe 的下载量有多少?

根据 npm 官方下载统计,@federicocarboni/saxe 在 2026/10/15 – 2026/11/13 这 30 天内被下载 3,385 次,日均约 113 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@federicocarboni/saxe 还在维护吗?

最新版本 0.8.0 发布于 2025/12/04(11 个月前);至今共发布 8 个版本,其中过去 12 个月发布了 1 个;首次发布于 2025/10/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。