htmlparser2
Version:
Fast & forgiving HTML/XML parser
44 lines • 1.51 kB
JavaScript
import { Parser } from "./Parser.js";
/**
* WebWritableStream makes the `Parser` interface available as a Web Streams API WritableStream.
*
* This is useful for piping `fetch()` response bodies directly into the parser.
* @see Parser
* @example
* ```typescript
* import { WebWritableStream } from "htmlparser2/WebWritableStream";
*
* const stream = new WebWritableStream({
* onopentag(name, attribs) {
* console.log("Opened:", name);
* },
* });
*
* const response = await fetch("https://example.com");
* await response.body.pipeTo(stream);
* ```
*/
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- Web Streams API; requires a runtime with WritableStream (browsers, Deno, Node ≥18.0)
export class WebWritableStream extends WritableStream {
constructor(cbs, options) {
const parser = new Parser(cbs, options);
const decoder = new TextDecoder();
const streamOption = { stream: true };
super({
write(chunk) {
parser.write(typeof chunk === "string"
? chunk
: decoder.decode(chunk, streamOption));
},
close() {
// Flush any remaining bytes in the decoder
const remaining = decoder.decode();
if (remaining) {
parser.write(remaining);
}
parser.end();
},
});
}
}
//# sourceMappingURL=WebWritableStream.js.map