isaacs / sax-js

A sax style parser for JS
Other
1.08k stars 325 forks source link

Adding the possibility to parse web streams (instead of nodestreams) #269

Open ecofi opened 4 months ago

ecofi commented 4 months ago

Is there an chance to implement web streams so that we can use sax.js in browser with fetch response directly? This would be really awesome and because nodejs is now also implementing web stream API it could be the approach for having a single stream implementation. I'm looking for something like this:

fetch("very-large.xml").then((response) => response.body.pipeThrough(saxWebStream))

linnhtun commented 1 month ago

you can use this way

import { Readable } from "node:stream"; import sax from "sax";

const xmlParser = sax.createStream(true, { trim: true, lowercase: true, }); const res = await fetch(url.url); const s = Readable.fromWeb(res.body).pipe(xmlParser);

ecofi commented 1 month ago

Thank you, but this would not work in a browser. I cloned and adjusted the saxparser to get it working with native fetch API webstream. Another alternative would be to use a js lib that converts webstream into nodestreams in a browser.