@ahoo-wang/fetcher-eventstream 0.2.0 → 0.2.3
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
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventstream)
|
|
4
4
|
[](https://github.com/Ahoo-Wang/fetcher/actions)
|
|
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
8
|
|
|
@@ -136,17 +136,17 @@ Converts a Response object with a `text/event-stream` body to a readable stream
|
|
|
136
136
|
|
|
137
137
|
A response interceptor that automatically adds an `eventStream()` method to responses with `text/event-stream` content type.
|
|
138
138
|
|
|
139
|
-
#### `intercept(
|
|
139
|
+
#### `intercept(exchange: FetchExchange): FetchExchange`
|
|
140
140
|
|
|
141
141
|
Intercepts a response and adds the `eventStream()` method if the content type is `text/event-stream`.
|
|
142
142
|
|
|
143
143
|
**Parameters:**
|
|
144
144
|
|
|
145
|
-
- `
|
|
145
|
+
- `exchange`: The fetch exchange containing the response to intercept
|
|
146
146
|
|
|
147
147
|
**Returns:**
|
|
148
148
|
|
|
149
|
-
- `
|
|
149
|
+
- `FetchExchange`: The intercepted exchange with response potentially modified to include `eventStream()` method
|
|
150
150
|
|
|
151
151
|
### ServerSentEvent
|
|
152
152
|
|
package/README.zh-CN.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher-eventstream)
|
|
4
4
|
[](https://github.com/Ahoo-Wang/fetcher/actions)
|
|
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
8
|
|
|
@@ -136,17 +136,17 @@ if (response.eventStream) {
|
|
|
136
136
|
|
|
137
137
|
响应拦截器,自动为 `text/event-stream` 内容类型的响应添加 `eventStream()` 方法。
|
|
138
138
|
|
|
139
|
-
#### `intercept(
|
|
139
|
+
#### `intercept(exchange: FetchExchange): FetchExchange`
|
|
140
140
|
|
|
141
141
|
拦截响应,如果内容类型是 `text/event-stream` 则添加 `eventStream()` 方法。
|
|
142
142
|
|
|
143
143
|
**参数:**
|
|
144
144
|
|
|
145
|
-
- `
|
|
145
|
+
- `exchange`:包含要拦截响应的 fetch exchange
|
|
146
146
|
|
|
147
147
|
**返回:**
|
|
148
148
|
|
|
149
|
-
- `
|
|
149
|
+
- `FetchExchange`:可能修改了响应以包含 `eventStream()` 方法的拦截 exchange
|
|
150
150
|
|
|
151
151
|
### ServerSentEvent
|
|
152
152
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventStreamConverter.d.ts","sourceRoot":"","sources":["../src/eventStreamConverter.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EAEhB,MAAM,kCAAkC,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,cAAc,CAAC,eAAe,CAAC,CAAC;AAEpE,wBAAgB,uBAAuB,
|
|
1
|
+
{"version":3,"file":"eventStreamConverter.d.ts","sourceRoot":"","sources":["../src/eventStreamConverter.ts"],"names":[],"mappings":"AACA,OAAO,EACL,eAAe,EAEhB,MAAM,kCAAkC,CAAC;AAE1C;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG,cAAc,CAAC,eAAe,CAAC,CAAC;AAEpE,wBAAgB,uBAAuB,CACrC,QAAQ,EAAE,QAAQ,GACjB,qBAAqB,CAavB"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare class EventStreamInterceptor implements
|
|
3
|
-
intercept(
|
|
1
|
+
import { FetchExchange, Interceptor } from '@ahoo-wang/fetcher';
|
|
2
|
+
export declare class EventStreamInterceptor implements Interceptor {
|
|
3
|
+
intercept(exchange: FetchExchange): FetchExchange;
|
|
4
4
|
}
|
|
5
5
|
//# sourceMappingURL=eventStreamInterceptor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventStreamInterceptor.d.ts","sourceRoot":"","sources":["../src/eventStreamInterceptor.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,
|
|
1
|
+
{"version":3,"file":"eventStreamInterceptor.d.ts","sourceRoot":"","sources":["../src/eventStreamInterceptor.ts"],"names":[],"mappings":"AACA,OAAO,EAGL,aAAa,EACb,WAAW,EACZ,MAAM,oBAAoB,CAAC;AAE5B,qBAAa,sBAAuB,YAAW,WAAW;IACxD,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa;CAgBlD"}
|
package/dist/index.es.js
CHANGED
|
@@ -9,8 +9,8 @@ class d {
|
|
|
9
9
|
const e = this.buffer.split(`
|
|
10
10
|
`);
|
|
11
11
|
this.buffer = e.pop() || "";
|
|
12
|
-
for (const
|
|
13
|
-
r.enqueue(
|
|
12
|
+
for (const n of e)
|
|
13
|
+
r.enqueue(n);
|
|
14
14
|
} catch (e) {
|
|
15
15
|
r.error(e);
|
|
16
16
|
}
|
|
@@ -28,9 +28,9 @@ class u extends TransformStream {
|
|
|
28
28
|
super(new d());
|
|
29
29
|
}
|
|
30
30
|
}
|
|
31
|
-
var c = /* @__PURE__ */ ((
|
|
32
|
-
function m(
|
|
33
|
-
switch (
|
|
31
|
+
var c = /* @__PURE__ */ ((s) => (s.ID = "id", s.RETRY = "retry", s.EVENT = "event", s.DATA = "data", s))(c || {});
|
|
32
|
+
function m(s, t, r) {
|
|
33
|
+
switch (s) {
|
|
34
34
|
case "event":
|
|
35
35
|
r.event = t;
|
|
36
36
|
break;
|
|
@@ -46,7 +46,7 @@ function m(n, t, r) {
|
|
|
46
46
|
break;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
|
-
class
|
|
49
|
+
class y {
|
|
50
50
|
constructor() {
|
|
51
51
|
this.currentEvent = {
|
|
52
52
|
event: "message",
|
|
@@ -75,12 +75,12 @@ class h {
|
|
|
75
75
|
}
|
|
76
76
|
if (t.startsWith(":"))
|
|
77
77
|
return;
|
|
78
|
-
const
|
|
78
|
+
const n = t.indexOf(":");
|
|
79
79
|
let i, a;
|
|
80
|
-
|
|
81
|
-
} catch (
|
|
80
|
+
n === -1 ? (i = t.toLowerCase(), a = "") : (i = t.substring(0, n).toLowerCase(), a = t.substring(n + 1), a.startsWith(" ") && (a = a.substring(1))), i = i.trim(), a = a.trim(), m(i, a, e);
|
|
81
|
+
} catch (n) {
|
|
82
82
|
r.error(
|
|
83
|
-
|
|
83
|
+
n instanceof Error ? n : new Error(String(n))
|
|
84
84
|
), e.event = "message", e.id = void 0, e.retry = void 0, e.data = [];
|
|
85
85
|
}
|
|
86
86
|
}
|
|
@@ -107,28 +107,31 @@ class h {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
-
class
|
|
110
|
+
class p extends TransformStream {
|
|
111
111
|
constructor() {
|
|
112
|
-
super(new
|
|
112
|
+
super(new y());
|
|
113
113
|
}
|
|
114
114
|
}
|
|
115
|
-
function
|
|
116
|
-
if (!
|
|
115
|
+
function h(s) {
|
|
116
|
+
if (!s.body)
|
|
117
117
|
throw new Error("Response body is null");
|
|
118
|
-
return
|
|
118
|
+
return s.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new u()).pipeThrough(new p());
|
|
119
119
|
}
|
|
120
|
-
class
|
|
120
|
+
class v {
|
|
121
121
|
intercept(t) {
|
|
122
|
-
const r = t.
|
|
123
|
-
|
|
122
|
+
const r = t.response;
|
|
123
|
+
if (!r)
|
|
124
|
+
return t;
|
|
125
|
+
const e = r.headers.get(o);
|
|
126
|
+
return e && e.includes(f.TEXT_EVENT_STREAM) && (r.eventStream = () => h(r)), t;
|
|
124
127
|
}
|
|
125
128
|
}
|
|
126
129
|
export {
|
|
127
|
-
|
|
130
|
+
v as EventStreamInterceptor,
|
|
128
131
|
c as ServerSentEventField,
|
|
129
|
-
|
|
130
|
-
|
|
132
|
+
p as ServerSentEventTransformStream,
|
|
133
|
+
y as ServerSentEventTransformer,
|
|
131
134
|
u as TextLineTransformStream,
|
|
132
135
|
d as TextLineTransformer,
|
|
133
|
-
|
|
136
|
+
h as toServerSentEventStream
|
|
134
137
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(
|
|
2
|
-
`);this.buffer=e.pop()||"";for(const
|
|
3
|
-
`),id:e.id||"",retry:e.retry}),e.event="message",e.id=e.id,e.retry=e.retry,e.data=[]);return}if(t.startsWith(":"))return;const
|
|
4
|
-
`),id:r.id||"",retry:r.retry})}catch(e){t.error(e instanceof Error?e:new Error(String(e)))}finally{r.event="message",r.id=void 0,r.retry=void 0,r.data=[]}}}class
|
|
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,r){try{this.buffer+=t;const e=this.buffer.split(`
|
|
2
|
+
`);this.buffer=e.pop()||"";for(const a of e)r.enqueue(a)}catch(e){r.error(e)}}flush(t){try{this.buffer&&t.enqueue(this.buffer)}catch(r){t.error(r)}}}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,r){switch(n){case"event":r.event=t;break;case"data":r.data.push(t);break;case"id":r.id=t;break;case"retry":const e=parseInt(t,10);isNaN(e)||(r.retry=e);break}}class m{constructor(){this.currentEvent={event:"message",id:void 0,retry:void 0,data:[]}}transform(t,r){let e=this.currentEvent;try{if(t.trim()===""){e.data.length>0&&(r.enqueue({event:e.event||"message",data:e.data.join(`
|
|
3
|
+
`),id:e.id||"",retry:e.retry}),e.event="message",e.id=e.id,e.retry=e.retry,e.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,e)}catch(a){r.error(a instanceof Error?a:new Error(String(a))),e.event="message",e.id=void 0,e.retry=void 0,e.data=[]}}flush(t){let r=this.currentEvent;try{r.data.length>0&&t.enqueue({event:r.event||"message",data:r.data.join(`
|
|
4
|
+
`),id:r.id||"",retry:r.retry})}catch(e){t.error(e instanceof Error?e:new Error(String(e)))}finally{r.event="message",r.id=void 0,r.retry=void 0,r.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 r=t.response;if(!r)return t;const e=r.headers.get(o.ContentTypeHeader);return e&&e.includes(o.ContentTypeValues.TEXT_EVENT_STREAM)&&(r.eventStream=()=>T(r)),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"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-eventstream",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.3",
|
|
4
4
|
"description": "Support for text/event-stream in Fetcher",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"dist"
|
|
34
34
|
],
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@ahoo-wang/fetcher": "0.2.
|
|
36
|
+
"@ahoo-wang/fetcher": "0.2.3"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@vitest/coverage-v8": "^3.2.4",
|