parse5-parser-stream is a dedicated package within the parse5 HTML parsing toolset, providing a streaming API for parsing HTML documents. It processes HTML incrementally as a Node.js Transform stream, making it suitable for handling large files or real-time data without loading the entire document into memory. The current stable version is 8.0.1. The broader parse5 project, which this package is part of, maintains an active development cadence with frequent patch and minor releases, alongside significant major version updates roughly every one to two years. Its key differentiators include strict adherence to the HTML5 specification, detailed tracking of source code locations for parsed elements (beneficial for tools like linters and formatters), and robust scripting support during parsing.
npm install parse5-parser-streamVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `parse5-parser-stream` to parse HTML content using Node.js streams, including error handling and accessing the final document object. It shows piping a readable stream into the parser stream and then serializing the resulting document.
If using a custom `TreeAdapter`, ensure it implements `updateNodeSourceCodeLocation` as specified in the parse5 documentation for v6.x and higher.
Carefully review the `parse5` v7.0.0 changelog for the core package and test existing code thoroughly, especially if you directly manipulate or inspect the AST produced by the parser stream.
Upgrade to `parse5-parser-stream@7.1.2` or newer for robust CommonJS support, or ensure your project is configured for ESM if staying on older versions.
Always attach an `error` listener to the `ParserStream` instance and ensure that all input data is written and the `end()` method (or piping a complete stream) is called when no more data is expected. Example: `parserStream.on('error', handleError); readableStream.pipe(parserStream);`.Use named imports: `import { ParserStream } from 'parse5-parser-stream';` for ESM, or destructuring `const { ParserStream } = require('parse5-parser-stream');` for CommonJS.Access the `document` property only after the `parserStream` emits the `'end'` event, which signals that parsing is complete and the document tree is finalized.