UNPKG

fetch-stream-reader

Version:

A lightweight client for reading streaming HTTP responses using Fetch and ReadableStream.

2 lines (1 loc) 2.88 kB
var t=require("tslib"),n=function(n){function r(t,r){void 0===r&&(r={});var e=n.call(this)||this;return e.requestUrl=t,e.requestHeaders=r,e.streamReader=null,e.abortController=null,e.buffer="",e.requestUrl=t,e.requestHeaders=r,e}return t.t(r,n),r.prototype.start=function(n){return t.i(this,void 0,void 0,function(){var r,e,i,s;return t.o(this,function(t){switch(t.label){case 0:return t.trys.push([0,2,,3]),this.emit("stream-start"),this.abortController=new AbortController,r=null===(s=this.abortController)||void 0===s?void 0:s.signal,[4,this.u(r,n)];case 1:if(!(e=t.sent()).body)throw Error("No response body found");return this.streamReader=e.body.getReader(),this.h(),[3,3];case 2:return i=t.sent(),this.l(),this.emit("stream-error",i),[3,3];case 3:return[2]}})})},r.prototype.u=function(n,r){return t.i(this,void 0,void 0,function(){var e;return t.o(this,function(i){switch(i.label){case 0:return[4,fetch(this.requestUrl,{method:"POST",headers:t.v({"Content-Type":"application/json"},this.requestHeaders),body:r?JSON.stringify(r):null,signal:n})];case 1:if(!(e=i.sent()).ok)throw Error("Failed to fetch: ".concat(e.statusText));return[2,e]}})})},r.prototype.h=function(){return t.i(this,void 0,void 0,function(){var n,e,i,s,o,u;return t.o(this,function(t){switch(t.label){case 0:n=r.utf8Decoder,t.label=1;case 1:t.trys.push([1,5,6,7]),t.label=2;case 2:return[4,this.streamReader.read()];case 3:return e=t.sent(),i=e.done,s=e.value,i?(this.buffer="",this.emit("stream-end"),[3,4]):(o=n.decode(s,{stream:1}),this.buffer+=o,this.m(),[3,2]);case 4:return[3,7];case 5:return u=t.sent(),this.emit("stream-error",u),[3,7];case 6:return this.streamReader.releaseLock(),this.streamReader=null,[7];case 7:return[2]}})})},r.prototype.m=function(){for(var t=this.buffer.split("\n"),n=0,e=t.slice(0,-1);n<e.length;n++){var i=e[n];if(i.startsWith(r.PROTOCOL.DATA)){var s=i.replace(r.PROTOCOL.DATA,"").trim();try{var o=JSON.parse(s);this.emit("stream-message",o)}catch(t){}}}this.buffer=t[t.length-1]},r.prototype.stop=function(){var t;null===(t=this.abortController)||void 0===t||t.abort(),this.l(),this.emit("stream-abort")},r.prototype.l=function(){this.abortController=null},r.utf8Decoder=new TextDecoder("utf-8",{fatal:1,ignoreBOM:0}),r.PROTOCOL={DATA:"data: ",EVENT:"event: "},r}(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}());module.exports=n;