@ahoo-wang/fetcher-eventstream 0.5.2 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/README.zh-CN.md +1 -0
- package/dist/index.es.js +51 -50
- package/dist/index.umd.js +4 -4
- package/dist/serverSentEventTransformStream.d.ts.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
[](https://codecov.io/gh/Ahoo-Wang/fetcher)
|
|
6
6
|
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
7
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventstream)
|
|
8
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventstream)
|
|
8
9
|
|
|
9
10
|
Support for text/event-stream in Fetcher, enabling Server-Sent Events (SSE) functionality for real-time data streaming.
|
|
10
11
|
|
package/README.zh-CN.md
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
[](https://codecov.io/gh/Ahoo-Wang/fetcher)
|
|
6
6
|
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
7
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventstream)
|
|
8
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventstream)
|
|
8
9
|
|
|
9
10
|
为 Fetcher 提供 text/event-stream 支持,实现服务器发送事件(SSE)功能,用于实时数据流。
|
|
10
11
|
|
package/dist/index.es.js
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { ContentTypeHeader as
|
|
1
|
+
import { ContentTypeHeader as i, ContentTypeValues as f } from "@ahoo-wang/fetcher";
|
|
2
2
|
class d {
|
|
3
3
|
constructor() {
|
|
4
4
|
this.buffer = "";
|
|
5
5
|
}
|
|
6
|
-
transform(t,
|
|
6
|
+
transform(t, e) {
|
|
7
7
|
try {
|
|
8
8
|
this.buffer += t;
|
|
9
|
-
const
|
|
9
|
+
const r = this.buffer.split(`
|
|
10
10
|
`);
|
|
11
|
-
this.buffer =
|
|
12
|
-
for (const n of
|
|
13
|
-
|
|
14
|
-
} catch (
|
|
15
|
-
|
|
11
|
+
this.buffer = r.pop() || "";
|
|
12
|
+
for (const n of r)
|
|
13
|
+
e.enqueue(n);
|
|
14
|
+
} catch (r) {
|
|
15
|
+
e.error(r);
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
flush(t) {
|
|
19
19
|
try {
|
|
20
20
|
this.buffer && t.enqueue(this.buffer);
|
|
21
|
-
} catch (
|
|
22
|
-
t.error(
|
|
21
|
+
} catch (e) {
|
|
22
|
+
t.error(e);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
}
|
|
@@ -29,24 +29,25 @@ class u extends TransformStream {
|
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
31
|
var c = /* @__PURE__ */ ((s) => (s.ID = "id", s.RETRY = "retry", s.EVENT = "event", s.DATA = "data", s))(c || {});
|
|
32
|
-
function m(s, t,
|
|
32
|
+
function m(s, t, e) {
|
|
33
33
|
switch (s) {
|
|
34
34
|
case "event":
|
|
35
|
-
|
|
35
|
+
e.event = t;
|
|
36
36
|
break;
|
|
37
37
|
case "data":
|
|
38
|
-
|
|
38
|
+
e.data.push(t);
|
|
39
39
|
break;
|
|
40
40
|
case "id":
|
|
41
|
-
|
|
41
|
+
e.id = t;
|
|
42
42
|
break;
|
|
43
|
-
case "retry":
|
|
44
|
-
const
|
|
45
|
-
isNaN(
|
|
43
|
+
case "retry": {
|
|
44
|
+
const r = parseInt(t, 10);
|
|
45
|
+
isNaN(r) || (e.retry = r);
|
|
46
46
|
break;
|
|
47
|
+
}
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
|
-
class
|
|
50
|
+
class p {
|
|
50
51
|
constructor() {
|
|
51
52
|
this.currentEvent = {
|
|
52
53
|
event: "message",
|
|
@@ -60,28 +61,28 @@ class y {
|
|
|
60
61
|
* @param chunk Input string chunk
|
|
61
62
|
* @param controller Controller for controlling the transform stream
|
|
62
63
|
*/
|
|
63
|
-
transform(t,
|
|
64
|
-
|
|
64
|
+
transform(t, e) {
|
|
65
|
+
const r = this.currentEvent;
|
|
65
66
|
try {
|
|
66
67
|
if (t.trim() === "") {
|
|
67
|
-
|
|
68
|
-
event:
|
|
69
|
-
data:
|
|
68
|
+
r.data.length > 0 && (e.enqueue({
|
|
69
|
+
event: r.event || "message",
|
|
70
|
+
data: r.data.join(`
|
|
70
71
|
`),
|
|
71
|
-
id:
|
|
72
|
-
retry:
|
|
73
|
-
}),
|
|
72
|
+
id: r.id || "",
|
|
73
|
+
retry: r.retry
|
|
74
|
+
}), r.event = "message", r.data = []);
|
|
74
75
|
return;
|
|
75
76
|
}
|
|
76
77
|
if (t.startsWith(":"))
|
|
77
78
|
return;
|
|
78
79
|
const n = t.indexOf(":");
|
|
79
|
-
let
|
|
80
|
-
n === -1 ? (
|
|
80
|
+
let o, a;
|
|
81
|
+
n === -1 ? (o = t.toLowerCase(), a = "") : (o = t.substring(0, n).toLowerCase(), a = t.substring(n + 1), a.startsWith(" ") && (a = a.substring(1))), o = o.trim(), a = a.trim(), m(o, a, r);
|
|
81
82
|
} catch (n) {
|
|
82
|
-
|
|
83
|
+
e.error(
|
|
83
84
|
n instanceof Error ? n : new Error(String(n))
|
|
84
|
-
),
|
|
85
|
+
), r.event = "message", r.id = void 0, r.retry = void 0, r.data = [];
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
/**
|
|
@@ -89,49 +90,49 @@ class y {
|
|
|
89
90
|
* @param controller Controller for controlling the transform stream
|
|
90
91
|
*/
|
|
91
92
|
flush(t) {
|
|
92
|
-
|
|
93
|
+
const e = this.currentEvent;
|
|
93
94
|
try {
|
|
94
|
-
|
|
95
|
-
event:
|
|
96
|
-
data:
|
|
95
|
+
e.data.length > 0 && t.enqueue({
|
|
96
|
+
event: e.event || "message",
|
|
97
|
+
data: e.data.join(`
|
|
97
98
|
`),
|
|
98
|
-
id:
|
|
99
|
-
retry:
|
|
99
|
+
id: e.id || "",
|
|
100
|
+
retry: e.retry
|
|
100
101
|
});
|
|
101
|
-
} catch (
|
|
102
|
+
} catch (r) {
|
|
102
103
|
t.error(
|
|
103
|
-
|
|
104
|
+
r instanceof Error ? r : new Error(String(r))
|
|
104
105
|
);
|
|
105
106
|
} finally {
|
|
106
|
-
|
|
107
|
+
e.event = "message", e.id = void 0, e.retry = void 0, e.data = [];
|
|
107
108
|
}
|
|
108
109
|
}
|
|
109
110
|
}
|
|
110
|
-
class
|
|
111
|
+
class h extends TransformStream {
|
|
111
112
|
constructor() {
|
|
112
|
-
super(new
|
|
113
|
+
super(new p());
|
|
113
114
|
}
|
|
114
115
|
}
|
|
115
|
-
function
|
|
116
|
+
function y(s) {
|
|
116
117
|
if (!s.body)
|
|
117
118
|
throw new Error("Response body is null");
|
|
118
|
-
return s.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new u()).pipeThrough(new
|
|
119
|
+
return s.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new u()).pipeThrough(new h());
|
|
119
120
|
}
|
|
120
121
|
class v {
|
|
121
122
|
intercept(t) {
|
|
122
|
-
const
|
|
123
|
-
if (!
|
|
123
|
+
const e = t.response;
|
|
124
|
+
if (!e)
|
|
124
125
|
return t;
|
|
125
|
-
const
|
|
126
|
-
return
|
|
126
|
+
const r = e.headers.get(i);
|
|
127
|
+
return r && r.includes(f.TEXT_EVENT_STREAM) && (e.eventStream = () => y(e)), t;
|
|
127
128
|
}
|
|
128
129
|
}
|
|
129
130
|
export {
|
|
130
131
|
v as EventStreamInterceptor,
|
|
131
132
|
c as ServerSentEventField,
|
|
132
|
-
|
|
133
|
-
|
|
133
|
+
h as ServerSentEventTransformStream,
|
|
134
|
+
p as ServerSentEventTransformer,
|
|
134
135
|
u as TextLineTransformStream,
|
|
135
136
|
d as TextLineTransformer,
|
|
136
|
-
|
|
137
|
+
y as toServerSentEventStream
|
|
137
138
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@ahoo-wang/fetcher")):typeof define=="function"&&define.amd?define(["exports","@ahoo-wang/fetcher"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.FetcherEventStream={},s.Fetcher))})(this,(function(s,o){"use strict";class d{constructor(){this.buffer=""}transform(t,
|
|
2
|
-
`);this.buffer=
|
|
3
|
-
`),id:
|
|
4
|
-
`),id:
|
|
1
|
+
(function(s,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@ahoo-wang/fetcher")):typeof define=="function"&&define.amd?define(["exports","@ahoo-wang/fetcher"],o):(s=typeof globalThis<"u"?globalThis:s||self,o(s.FetcherEventStream={},s.Fetcher))})(this,(function(s,o){"use strict";class d{constructor(){this.buffer=""}transform(t,e){try{this.buffer+=t;const r=this.buffer.split(`
|
|
2
|
+
`);this.buffer=r.pop()||"";for(const a of r)e.enqueue(a)}catch(r){e.error(r)}}flush(t){try{this.buffer&&t.enqueue(this.buffer)}catch(e){t.error(e)}}}class u extends TransformStream{constructor(){super(new d)}}var c=(n=>(n.ID="id",n.RETRY="retry",n.EVENT="event",n.DATA="data",n))(c||{});function h(n,t,e){switch(n){case"event":e.event=t;break;case"data":e.data.push(t);break;case"id":e.id=t;break;case"retry":{const r=parseInt(t,10);isNaN(r)||(e.retry=r);break}}}class m{constructor(){this.currentEvent={event:"message",id:void 0,retry:void 0,data:[]}}transform(t,e){const r=this.currentEvent;try{if(t.trim()===""){r.data.length>0&&(e.enqueue({event:r.event||"message",data:r.data.join(`
|
|
3
|
+
`),id:r.id||"",retry:r.retry}),r.event="message",r.data=[]);return}if(t.startsWith(":"))return;const a=t.indexOf(":");let f,i;a===-1?(f=t.toLowerCase(),i=""):(f=t.substring(0,a).toLowerCase(),i=t.substring(a+1),i.startsWith(" ")&&(i=i.substring(1))),f=f.trim(),i=i.trim(),h(f,i,r)}catch(a){e.error(a instanceof Error?a:new Error(String(a))),r.event="message",r.id=void 0,r.retry=void 0,r.data=[]}}flush(t){const e=this.currentEvent;try{e.data.length>0&&t.enqueue({event:e.event||"message",data:e.data.join(`
|
|
4
|
+
`),id:e.id||"",retry:e.retry})}catch(r){t.error(r instanceof Error?r:new Error(String(r)))}finally{e.event="message",e.id=void 0,e.retry=void 0,e.data=[]}}}class v extends TransformStream{constructor(){super(new m)}}function T(n){if(!n.body)throw new Error("Response body is null");return n.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new u).pipeThrough(new v)}class y{intercept(t){const e=t.response;if(!e)return t;const r=e.headers.get(o.ContentTypeHeader);return r&&r.includes(o.ContentTypeValues.TEXT_EVENT_STREAM)&&(e.eventStream=()=>T(e)),t}}s.EventStreamInterceptor=y,s.ServerSentEventField=c,s.ServerSentEventTransformStream=v,s.ServerSentEventTransformer=m,s.TextLineTransformStream=u,s.TextLineTransformer=d,s.toServerSentEventStream=T,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serverSentEventTransformStream.d.ts","sourceRoot":"","sources":["../src/serverSentEventTransformStream.ts"],"names":[],"mappings":"AAaA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,wEAAwE;IACxE,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,wDAAwD;IACxD,KAAK,EAAE,MAAM,CAAC;IACd,qBAAqB;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,yFAAyF;IACzF,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,oBAAY,oBAAoB;IAC9B,EAAE,OAAO;IACT,KAAK,UAAU;IACf,KAAK,UAAU;IACf,IAAI,SAAS;CACd;
|
|
1
|
+
{"version":3,"file":"serverSentEventTransformStream.d.ts","sourceRoot":"","sources":["../src/serverSentEventTransformStream.ts"],"names":[],"mappings":"AAaA;;;GAGG;AACH,MAAM,WAAW,eAAe;IAC9B,wEAAwE;IACxE,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,wDAAwD;IACxD,KAAK,EAAE,MAAM,CAAC;IACd,qBAAqB;IACrB,IAAI,EAAE,MAAM,CAAC;IACb,yFAAyF;IACzF,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB;AAED,oBAAY,oBAAoB;IAC9B,EAAE,OAAO;IACT,KAAK,UAAU;IACf,KAAK,UAAU;IACf,IAAI,SAAS;CACd;AA2CD;;;GAGG;AACH,qBAAa,0BACX,YAAW,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC;IAG/C,OAAO,CAAC,YAAY,CAKlB;IAEF;;;;OAIG;IACH,SAAS,CACP,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;IAiE/D;;;OAGG;IACH,KAAK,CAAC,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;CAwBpE;AAED;;;GAGG;AACH,qBAAa,8BAA+B,SAAQ,eAAe,CACjE,MAAM,EACN,eAAe,CAChB;;CAIA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-eventstream",
|
|
3
|
-
"version": "0.5.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.5.6",
|
|
4
|
+
"description": "Server-Sent Events (SSE) support for Fetcher HTTP client",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
7
7
|
"event-stream",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@ahoo-wang/fetcher": "0.5.
|
|
36
|
+
"@ahoo-wang/fetcher": "0.5.6"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@vitest/coverage-v8": "^3.2.4",
|
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "vite build",
|
|
49
49
|
"test": "vitest run --coverage",
|
|
50
|
-
"lint": "
|
|
50
|
+
"lint": "eslint . --fix",
|
|
51
51
|
"clean": "rm -rf dist"
|
|
52
52
|
}
|
|
53
53
|
}
|