Registry / serialization / parse5-parser-stream

parse5-parser-stream

JSON →
library8.0.0jsnpmunverified

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-stream
INSTALL
IMPORT
SIG · PARSE5-PARSER-STRE
P
parse5-parser-stream
serializationjavascriptv8.0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ParserStream
import { ParserStream } from 'parse5-parser-stream';
const ParserStream = require('parse5-parser-stream');
The main class is a named export. Default import or direct `require('pkg')` will not work as expected.
ParserStream
const { ParserStream } = require('parse5-parser-stream');
const ParserStream = require('parse5-parser-stream').default;
For CommonJS environments, destructure the named export. Direct `require('pkg')` without destructuring will yield an object, not the class constructor.
DefaultTreeAdapter
import { DefaultTreeAdapter } from 'parse5';
While ParserStream is from 'parse5-parser-stream', tree adapters like DefaultTreeAdapter are typically imported from the core 'parse5' package.

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.

import { ParserStream } from 'parse5-parser-stream'; import { serialize } from 'parse5'; import { pipeline } from 'stream/promises'; async function parseHtmlStream(htmlContent: string) { const parserStream = new ParserStream(); let document: any; parserStream.on('end', () => { document = parserStream.document; console.log('Parsing complete. Document:', serialize(document)); }); parserStream.on('error', (err) => { console.error('Parsing error:', err.message); }); // A simple readable stream to feed content const readableHtml = new (class extends require('stream').Readable { _read() { this.push(htmlContent); this.push(null); } })(); try { await pipeline(readableHtml, parserStream); console.log('Successfully processed HTML stream.'); } catch (error) { console.error('Stream pipeline failed:', error); } } // Example usage const sampleHtml = `<!DOCTYPE html> <html> <head> <title>Test Page</title> </head> <body> <p>Hello, <span class="name">World</span>!</p> <!-- A comment --> </body> </html>`; parseHtmlStream(sampleHtml);
Debug
Known issues
breakingThe `TreeAdapter` interface underwent a breaking change in v6.0.0, requiring a new `updateNodeSourceCodeLocation` method. Custom tree adapter implementations must be updated.
fix
If using a custom `TreeAdapter`, ensure it implements `updateNodeSourceCodeLocation` as specified in the parse5 documentation for v6.x and higher.
affects: >=6.0.0
breakingVersion 7.0.0 introduced significant internal changes across the parse5 ecosystem. While `parse5-parser-stream` might not have had direct API changes explicitly listed as 'breaking' in its own changelog, underlying core `parse5` changes (e.g., node name string types) could affect consumers who deeply interact with the parsed AST.
fix
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.
affects: >=7.0.0
gotchaPrior to version 7.1.2, official CommonJS builds for `parse5-parser-stream` were not consistently available or fully supported, potentially leading to import issues in CJS-only Node.js environments.
fix
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.
affects: <7.1.2
gotchaWhen consuming `parse5-parser-stream`, it's crucial to correctly handle the stream's `error` event and ensure the stream is `end`ed. Failure to do so can lead to silent failures, incomplete parsing, or hung processes.
fix
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);`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: ParserStream is not a constructor
Attempting to `require('parse5-parser-stream')` directly or using `import ParserStream from 'parse5-parser-stream';`
fix
Use named imports: `import { ParserStream } from 'parse5-parser-stream';` for ESM, or destructuring `const { ParserStream } = require('parse5-parser-stream');` for CommonJS.
Cannot read properties of undefined (reading 'document')
Attempting to access `parserStream.document` before the parsing process has completed (i.e., before the 'end' event has fired).
fix
Access the `document` property only after the `parserStream` emits the `'end'` event, which signals that parsing is complete and the document tree is finalized.
Upgrade
Version history
8.0.0latest on npm
Audit
Dependencies
parse5requiredCore parsing logic and AST generation, which this streaming component utilizes.
Agent activity
2 hits · last 30 days
node
2
Resources
parse5-parser-stream — npm install parse5-parser-stream · libregistry