@artmate/chat
Version:
借鉴字节开源react库AntX,通过vue实现的版本
5 lines (3 loc) • 1.12 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const f=`
`,d=`
`,c=":",u=e=>(e??"").trim()!=="";function m(){let e="";return new TransformStream({transform(r,o){e+=r;const t=e.split(f);t.slice(0,-1).forEach(n=>{u(n)&&o.enqueue(n)}),e=t[t.length-1]},flush(r){u(e)&&r.enqueue(e)}})}function S(){return new TransformStream({transform(e,r){const t=e.split(d).reduce((n,s)=>{const a=s.indexOf(c);if(a===-1)throw new Error(`键值分隔符 "${c}" 在sse行块中找不到!`);const i=s.slice(0,a);if(!u(i))return n;const l=s.slice(a+1);return{...n,[i]:l}},{});Object.keys(t).length!==0&&r.enqueue(t)}})}function h(e){const{readableStream:r,transformStream:o}=e;if(!(r instanceof ReadableStream))throw new TypeError("options.readableStream 必须是 readableStream 的实例。");const t=new TextDecoderStream,n=o?r.pipeThrough(t).pipeThrough(o):r.pipeThrough(t).pipeThrough(m()).pipeThrough(S());return n[Symbol.asyncIterator]=async function*(){const s=this.getReader();for(;;){const{done:a,value:i}=await s.read();if(a)break;i&&(yield i)}},n}exports.default=h;