fetch-stream-reader
Version:
A lightweight client for reading streaming HTTP responses using Fetch and ReadableStream.
2 lines (1 loc) • 2.92 kB
JavaScript
import{__extends as t,__awaiter as n,__generator as r,__assign as e}from"tslib";var i=function(i){function s(t,n){void 0===n&&(n={});var r=i.call(this)||this;return r.requestUrl=t,r.requestHeaders=n,r.streamReader=null,r.abortController=null,r.buffer="",r.requestUrl=t,r.requestHeaders=n,r}return t(s,i),s.prototype.start=function(t){return n(this,void 0,void 0,function(){var n,e,i,s;return r(this,function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),this.emit("stream-start"),this.abortController=new AbortController,n=null===(s=this.abortController)||void 0===s?void 0:s.signal,[4,this.t(n,t)];case 1:if(!(e=r.sent()).body)throw Error("No response body found");return this.streamReader=e.body.getReader(),this.i(),[3,3];case 2:return i=r.sent(),this.o(),this.emit("stream-error",i),[3,3];case 3:return[2]}})})},s.prototype.t=function(t,i){return n(this,void 0,void 0,function(){var n;return r(this,function(r){switch(r.label){case 0:return[4,fetch(this.requestUrl,{method:"POST",headers:e({"Content-Type":"application/json"},this.requestHeaders),body:i?JSON.stringify(i):null,signal:t})];case 1:if(!(n=r.sent()).ok)throw Error("Failed to fetch: ".concat(n.statusText));return[2,n]}})})},s.prototype.i=function(){return n(this,void 0,void 0,function(){var t,n,e,i,o,u;return r(this,function(r){switch(r.label){case 0:t=s.utf8Decoder,r.label=1;case 1:r.trys.push([1,5,6,7]),r.label=2;case 2:return[4,this.streamReader.read()];case 3:return n=r.sent(),e=n.done,i=n.value,e?(this.buffer="",this.emit("stream-end"),[3,4]):(o=t.decode(i,{stream:1}),this.buffer+=o,this.u(),[3,2]);case 4:return[3,7];case 5:return u=r.sent(),this.emit("stream-error",u),[3,7];case 6:return this.streamReader.releaseLock(),this.streamReader=null,[7];case 7:return[2]}})})},s.prototype.u=function(){for(var t=this.buffer.split("\n"),n=0,r=t.slice(0,-1);n<r.length;n++){var e=r[n];if(e.startsWith(s.PROTOCOL.DATA)){var i=e.replace(s.PROTOCOL.DATA,"").trim();try{var o=JSON.parse(i);this.emit("stream-message",o)}catch(t){}}}this.buffer=t[t.length-1]},s.prototype.stop=function(){var t;null===(t=this.abortController)||void 0===t||t.abort(),this.o(),this.emit("stream-abort")},s.prototype.o=function(){this.abortController=null},s.utf8Decoder=new TextDecoder("utf-8",{fatal:1,ignoreBOM:0}),s.PROTOCOL={DATA:"data: ",EVENT:"event: "},s}(function(){function t(){this.target=new EventTarget}return t.prototype.on=function(t,n){if("function"!=typeof n)throw new TypeError("Event handler must be a function");if(!t||""===t.trim())throw Error("Event name cannot be empty");this.target.addEventListener(t,n)},t.prototype.off=function(t,n){if(!t||""===t.trim())throw Error("Event name cannot be empty");if(n&&"function"!=typeof n)throw new TypeError("Event handler must be a function");this.target.removeEventListener(t,n||null)},t.prototype.emit=function(t,n){this.target.dispatchEvent(new CustomEvent(t,{detail:n}))},t}());export{i as default};