Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

fetch-stream-reader

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fetch-stream-reader - npm Package Compare versions

Comparing version
1.0.3
to
1.0.4
+1
-1
dist/index.cjs.js

@@ -1,1 +0,1 @@

"use strict";var t=require("tslib"),n=function(n){function r(t,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.fetchStream(r,n)];case 1:if(!(e=t.sent()).body)throw Error("No response body found");return this.streamReader=e.body.getReader(),this.readStreamChunks(),[3,3];case 2:return i=t.sent(),this.cleanup(),this.emit("stream-error",i),[3,3];case 3:return[2]}})})},r.prototype.fetchStream=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.u({"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.readStreamChunks=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.processBuffer(),[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(),[7];case 7:return[2]}})})},r.prototype.processBuffer=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.cleanup(),this.emit("stream-abort")},r.prototype.cleanup=function(){this.streamReader&&(this.streamReader.releaseLock(),this.streamReader=null),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;
"use strict";var t=require("tslib"),n=function(n){function r(t,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.fetchStream(r,n)];case 1:if(!(e=t.sent()).body)throw Error("No response body found");return this.streamReader=e.body.getReader(),this.readStreamChunks(),[3,3];case 2:return i=t.sent(),this.cleanup(),this.emit("stream-error",i),[3,3];case 3:return[2]}})})},r.prototype.fetchStream=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.u({"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.readStreamChunks=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.processBuffer(),[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.processBuffer=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.cleanup(),this.emit("stream-abort")},r.prototype.cleanup=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;

@@ -1,1 +0,1 @@

import{__extends as t,__awaiter as n,__generator as r,__assign as e}from"tslib";var i=function(i){function s(t,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.fetchStream(n,t)];case 1:if(!(e=r.sent()).body)throw Error("No response body found");return this.streamReader=e.body.getReader(),this.readStreamChunks(),[3,3];case 2:return i=r.sent(),this.cleanup(),this.emit("stream-error",i),[3,3];case 3:return[2]}})})},s.prototype.fetchStream=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.readStreamChunks=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.processBuffer(),[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(),[7];case 7:return[2]}})})},s.prototype.processBuffer=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.cleanup(),this.emit("stream-abort")},s.prototype.cleanup=function(){this.streamReader&&(this.streamReader.releaseLock(),this.streamReader=null),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};
import{__extends as t,__awaiter as n,__generator as r,__assign as e}from"tslib";var i=function(i){function s(t,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.fetchStream(n,t)];case 1:if(!(e=r.sent()).body)throw Error("No response body found");return this.streamReader=e.body.getReader(),this.readStreamChunks(),[3,3];case 2:return i=r.sent(),this.cleanup(),this.emit("stream-error",i),[3,3];case 3:return[2]}})})},s.prototype.fetchStream=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.readStreamChunks=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.processBuffer(),[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.processBuffer=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.cleanup(),this.emit("stream-abort")},s.prototype.cleanup=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};
{
"name": "fetch-stream-reader",
"version": "1.0.3",
"version": "1.0.4",
"description": "A lightweight client for reading streaming HTTP responses using Fetch and ReadableStream.",

@@ -5,0 +5,0 @@ "exports": {