feat: add WebWritableStream for Web Streams API support#2376
Merged
fb55 merged 1 commit intofb55:masterfrom Mar 19, 2026
Merged
feat: add WebWritableStream for Web Streams API support#2376fb55 merged 1 commit intofb55:masterfrom
fb55 merged 1 commit intofb55:masterfrom
Conversation
The existing WritableStream wraps Node.js stream.Writable, which isn't compatible with the Web Streams API. This makes it impossible to pipe fetch() response bodies directly into the parser. Add a new WebWritableStream class exposed via the subpath export "htmlparser2/WebWritableStream" that wraps the Parser using the standard Web Streams API WritableStream. It accepts both string and Uint8Array chunks, using TextDecoder in streaming mode for proper handling of fragmented multi-byte UTF-8 sequences. Closes fb55#1862
5480151 to
4339aab
Compare
Owner
|
Thanks @vimzh! Rebased & moved the Web to a prefix, will be out with the next release |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
WebWritableStreamclass that wraps theParserusing the Web Streams API'sWritableStream, enabling direct piping fromfetch()response bodies into the parser.Closes #1862
Motivation
The existing
WritableStreamwraps Node.jsstream.Writable, which is incompatible with the Web Streams API. Users in browsers, Deno, Bun, or modern Node.js cannot pipe afetch()response body into the parser without manual glue code.Design
src/WritableStreamWeb.ts— aWebWritableStreamclass extendingglobalThis.WritableStream<string | Uint8Array>htmlparser2/WebWritableStream(mirrors the existinghtmlparser2/WritableStreampattern)(cbs: Partial<Handler>, options?: ParserOptions)— same signature as the Node.jsWritableStreamstring(passed directly) andUint8Array(decoded viaTextDecoderin streaming mode for correct multi-byte UTF-8 handling)write()sink callsparser.write(),close()sink flushes the decoder then callsparser.end()Usage
Test plan
Uint8Arraychunksonendfires)ontextnoronendfires)ReadableStream.pipeTo()integration with HTML parsing assertionstsc --noEmit), and Biome all clean