/* esm.sh - assistant-stream@0.3.54/dist/core/utils/stream/SSEEventDecoder */
var h=class extends Error{code;constructor(e,n){super(n),this.name="SSEEventDecoderError",this.code=e}},L=(e,n,s)=>{let t=e??n;if(!Number.isSafeInteger(t)||t<=0)throw new h("invalid-limit",`${s} must be a positive safe integer`);return t},l=class{lineChunks=[];lineLength=0;linePreview="";dataLines=[];eventLength=0;eventName;lastEventId;retry;pendingLF=!1;started=!1;trailing;maxLineLength;maxEventLength;constructor(e){this.trailing=e?.trailing??"drop",this.maxLineLength=L(e?.maxLineLength,16777216,"maxLineLength"),this.maxEventLength=L(e?.maxEventLength,16777216,"maxEventLength")}push(e){let n=[];if(e==="")return n;this.started||(this.started=!0,e.startsWith("\uFEFF")&&(e=e.slice(1))),this.pendingLF&&e.startsWith(`
`)&&(e=e.slice(1)),this.pendingLF=e.endsWith("\r");let s=e.split(/\r\n|\r|\n/),t=s.pop();for(let i of s){let r=i;if(this.lineChunks.length>0)this.appendLineChunk(i),r=this.lineChunks.join(""),this.resetLine();else if(i.length>this.maxLineLength)throw this.resetFrame(),this.resetLine(),new h("line-too-long",`SSE line exceeds maxLineLength (${i.length} > ${this.maxLineLength})`);let a=this.processLine(r);a&&n.push(a)}return t!==""&&this.appendLineChunk(t),n}flush(){return this.trailing==="drop"?(this.resetFrame(),this.resetLine(),this.pendingLF=!1,null):(this.lineChunks.length>0&&this.processLine(this.lineChunks.join("")),this.resetLine(),this.pendingLF=!1,this.dispatchEvent())}processLine(e){if(e==="")return this.dispatchEvent();if(e.startsWith(":"))return null;let n=e.indexOf(":"),s=n===-1?e:e.slice(0,n),t=n===-1?"":e.slice(n+1);switch(t.startsWith(" ")&&(t=t.slice(1)),s){case"data":{let i=this.eventLength+t.length+(this.dataLines.length>0?1:0);if(i>this.maxEventLength)throw this.resetFrame(),new h("event-too-long",`SSE event exceeds maxEventLength (${i} > ${this.maxEventLength})`);this.eventLength=i,this.dataLines.push(t);break}case"event":this.eventName=t;break;case"id":t.includes("\0")||(this.lastEventId=t);break;case"retry":{let i=Number(t);/^\d+$/.test(t)&&Number.isSafeInteger(i)&&(this.retry=i);break}}return null}dispatchEvent(){if(this.dataLines.length===0)return this.resetFrame(),null;let e={data:this.dataLines.join(`
`)};return this.eventName!==void 0&&(e.event=this.eventName),this.lastEventId!==void 0&&(e.id=this.lastEventId),this.retry!==void 0&&(e.retry=this.retry),this.resetFrame(),e}resetFrame(){this.dataLines=[],this.eventLength=0,this.eventName=void 0}appendLineChunk(e){let n=this.lineLength+e.length;if(n>this.maxLineLength)throw this.resetFrame(),this.resetLine(),new h("line-too-long",`SSE line exceeds maxLineLength (${n} > ${this.maxLineLength})`);if(this.lineChunks.push(e),this.lineLength=n,this.linePreview.length<6&&(this.linePreview+=e.slice(0,6-this.linePreview.length)),this.linePreview.length>=5&&this.linePreview.startsWith("data:")){let s=this.linePreview.length>=6&&this.linePreview[5]===" "?1:0,t=this.lineLength-5-s,i=this.eventLength+t+(this.dataLines.length>0?1:0);if(i>this.maxEventLength)throw this.resetFrame(),this.resetLine(),new h("event-too-long",`SSE event exceeds maxEventLength (${i} > ${this.maxEventLength})`)}}resetLine(){this.lineChunks=[],this.lineLength=0,this.linePreview=""}};export{l as SSEEventDecoder,h as SSEEventDecoderError};
//# sourceMappingURL=SSEEventDecoder.mjs.map