const{SaxesParser}=require('saxes');const{PassThrough}=require('readable-stream');const{bufferToString}=require('./browser-buffer-decode');module.exports=asyncfunction*(iterable){// TODO: Remove once node v8 is deprecated// Detect and upgrade old streamsif(iterable.pipe&&!iterable[Symbol.asyncIterator]){iterable=iterable.pipe(newPassThrough());}constsaxesParser=newSaxesParser();leterror;saxesParser.on('error',err=>{error=err;});letevents=[];saxesParser.on('opentag',value=>events.push({eventType:'opentag',value}));saxesParser.on('text',value=>events.push({eventType:'text',value}));saxesParser.on('closetag',value=>events.push({eventType:'closetag',value}));forawait(constchunkofiterable){saxesParser.write(bufferToString(chunk));// saxesParser.write and saxesParser.on() are synchronous,// so we can only reach the below line once all events have been emittedif(error)throwerror;// As a performance optimization, we gather all events instead of passing// them one by one, which would cause each event to go through the event queueyieldevents;events=[];}};