UNPKG

@aegisai/opik

Version:

Opik TypeScript and JavaScript SDK

1 lines 2.43 kB
import'./chunk-WOT6VMZA.js';var s=class a{constructor(e){this.readableStream=e,this.reader=this.readableStream.getReader(),this.events={data:[],end:[],error:[],readable:[],close:[],pause:[],resume:[]},this.paused=false,this.resumeCallback=null,this.encoding=null;}on(e,r){var t;(t=this.events[e])==null||t.push(r);}off(e,r){var t;this.events[e]=(t=this.events[e])==null?void 0:t.filter(i=>i!==r);}pipe(e){return this.on("data",r=>{if(e instanceof a)e._write(r);else {let t=e.getWriter();t.write(r).then(()=>t.releaseLock());}}),this.on("end",()=>{e instanceof a?e._end():e.getWriter().close();}),this.on("error",r=>{e instanceof a?e._error(r):e.getWriter().abort(r);}),this._startReading(),e}pipeTo(e){return this.pipe(e)}unpipe(e){this.off("data",r=>{if(e instanceof a)e._write(r);else {let t=e.getWriter();t.write(r).then(()=>t.releaseLock());}}),this.off("end",()=>{e instanceof a?e._end():e.getWriter().close();}),this.off("error",r=>{e instanceof a?e._error(r):e.getWriter().abort(r);});}destroy(e){this.reader.cancel(e).then(()=>{this._emit("close");}).catch(r=>{this._emit("error",r);});}pause(){this.paused=true,this._emit("pause");}resume(){this.paused&&(this.paused=false,this._emit("resume"),this.resumeCallback&&(this.resumeCallback(),this.resumeCallback=null));}get isPaused(){return this.paused}async read(){this.paused&&await new Promise(t=>{this.resumeCallback=t;});let{done:e,value:r}=await this.reader.read();if(!e)return r}setEncoding(e){this.encoding=e;}async text(){let e=[];for(;;){let{done:t,value:i}=await this.reader.read();if(t)break;i&&e.push(i);}return new TextDecoder(this.encoding||"utf-8").decode(await new Blob(e).arrayBuffer())}async json(){let e=await this.text();return JSON.parse(e)}_write(e){this._emit("data",e);}_end(){this._emit("end");}_error(e){this._emit("error",e);}_emit(e,r){if(this.events[e])for(let t of this.events[e]||[])t(r);}async _startReading(){try{for(this._emit("readable");;){this.paused&&await new Promise(t=>{this.resumeCallback=t;});let{done:e,value:r}=await this.reader.read();if(e){this._emit("end"),this._emit("close");break}r&&this._emit("data",r);}}catch(e){this._emit("error",e);}}[Symbol.asyncIterator](){return {next:async()=>{this.paused&&await new Promise(t=>{this.resumeCallback=t;});let{done:e,value:r}=await this.reader.read();return e?{done:true,value:void 0}:{done:false,value:r}},[Symbol.asyncIterator](){return this}}}};export{s as UndiciStreamWrapper};