fetch-stream-reader
Advanced tools
@@ -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(),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; | ||
| 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.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; |
+4
-4
@@ -83,11 +83,11 @@ /** | ||
| */ | ||
| private fetchStream; | ||
| private _fetchStream; | ||
| /** | ||
| * 从响应体流中逐步读取数据并解码处理 | ||
| */ | ||
| private readStreamChunks; | ||
| private _readStreamChunks; | ||
| /** | ||
| * 处理 buffer 中的完整数据块 | ||
| */ | ||
| private processBuffer; | ||
| private _processBuffer; | ||
| /** | ||
@@ -100,5 +100,5 @@ * 停止当前的流请求和数据读取 | ||
| */ | ||
| private cleanup; | ||
| private _cleanup; | ||
| } | ||
| export { FetchStreamReader as default }; |
@@ -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(),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}; | ||
| 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.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}; |
+1
-1
| { | ||
| "name": "fetch-stream-reader", | ||
| "version": "1.0.4", | ||
| "version": "1.0.5", | ||
| "description": "A lightweight client for reading streaming HTTP responses using Fetch and ReadableStream.", | ||
@@ -5,0 +5,0 @@ "exports": { |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
13674
-1.39%122
-1.61%