@ahoo-wang/fetcher-eventstream 0.0.7 → 0.0.8
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.
|
@@ -1,16 +1,11 @@
|
|
|
1
1
|
import { ServerSentEventStream } from './serverSentEventTransformStream';
|
|
2
|
-
|
|
3
|
-
intercept(response: Response): Response;
|
|
4
|
-
}
|
|
2
|
+
import { ResponseInterceptor } from '@ahoo-wang/fetcher';
|
|
5
3
|
declare global {
|
|
6
4
|
interface Response {
|
|
7
5
|
eventStream(): ServerSentEventStream | undefined;
|
|
8
6
|
}
|
|
9
7
|
}
|
|
10
|
-
export declare const ContentTypeHeader = "content-type";
|
|
11
|
-
export declare const EventStreamContentType = "text/event-stream";
|
|
12
8
|
export declare class EventStreamInterceptor implements ResponseInterceptor {
|
|
13
9
|
intercept(response: Response): Response;
|
|
14
10
|
}
|
|
15
|
-
export {};
|
|
16
11
|
//# sourceMappingURL=eventStreamInterceptor.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventStreamInterceptor.d.ts","sourceRoot":"","sources":["../src/eventStreamInterceptor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;
|
|
1
|
+
{"version":3,"file":"eventStreamInterceptor.d.ts","sourceRoot":"","sources":["../src/eventStreamInterceptor.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,qBAAqB,EAAE,MAAM,kCAAkC,CAAC;AACzE,OAAO,EAGL,mBAAmB,EACpB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,QAAQ;QAChB,WAAW,IAAI,qBAAqB,GAAG,SAAS,CAAC;KAClD;CACF;AAED,qBAAa,sBAAuB,YAAW,mBAAmB;IAChE,SAAS,CAAC,QAAQ,EAAE,QAAQ,GAAG,QAAQ;CAYxC"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,51 +1,52 @@
|
|
|
1
|
-
|
|
1
|
+
import { ContentTypeHeader as o, ContentTypeValues as f } from "@ahoo-wang/fetcher";
|
|
2
|
+
class d {
|
|
2
3
|
constructor() {
|
|
3
4
|
this.buffer = "";
|
|
4
5
|
}
|
|
5
|
-
transform(
|
|
6
|
+
transform(r, t) {
|
|
6
7
|
try {
|
|
7
|
-
this.buffer +=
|
|
8
|
+
this.buffer += r;
|
|
8
9
|
const e = this.buffer.split(`
|
|
9
10
|
`);
|
|
10
11
|
this.buffer = e.pop() || "";
|
|
11
|
-
for (const
|
|
12
|
-
|
|
12
|
+
for (const n of e)
|
|
13
|
+
t.enqueue(n);
|
|
13
14
|
} catch (e) {
|
|
14
|
-
|
|
15
|
+
t.error(e);
|
|
15
16
|
}
|
|
16
17
|
}
|
|
17
|
-
flush(
|
|
18
|
+
flush(r) {
|
|
18
19
|
try {
|
|
19
|
-
this.buffer &&
|
|
20
|
-
} catch (
|
|
21
|
-
|
|
20
|
+
this.buffer && r.enqueue(this.buffer);
|
|
21
|
+
} catch (t) {
|
|
22
|
+
r.error(t);
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
|
-
class
|
|
26
|
+
class c extends TransformStream {
|
|
26
27
|
constructor() {
|
|
27
|
-
super(new
|
|
28
|
+
super(new d());
|
|
28
29
|
}
|
|
29
30
|
}
|
|
30
|
-
var
|
|
31
|
-
function
|
|
32
|
-
switch (
|
|
31
|
+
var u = /* @__PURE__ */ ((a) => (a.ID = "id", a.RETRY = "retry", a.EVENT = "event", a.DATA = "data", a))(u || {});
|
|
32
|
+
function m(a, r, t) {
|
|
33
|
+
switch (a) {
|
|
33
34
|
case "event":
|
|
34
|
-
|
|
35
|
+
t.event = r;
|
|
35
36
|
break;
|
|
36
37
|
case "data":
|
|
37
|
-
|
|
38
|
+
t.data.push(r);
|
|
38
39
|
break;
|
|
39
40
|
case "id":
|
|
40
|
-
|
|
41
|
+
t.id = r;
|
|
41
42
|
break;
|
|
42
43
|
case "retry":
|
|
43
|
-
const e = parseInt(
|
|
44
|
-
isNaN(e) || (
|
|
44
|
+
const e = parseInt(r, 10);
|
|
45
|
+
isNaN(e) || (t.retry = e);
|
|
45
46
|
break;
|
|
46
47
|
}
|
|
47
48
|
}
|
|
48
|
-
class
|
|
49
|
+
class h {
|
|
49
50
|
constructor() {
|
|
50
51
|
this.currentEvent = {
|
|
51
52
|
event: "message",
|
|
@@ -59,11 +60,11 @@ class u {
|
|
|
59
60
|
* @param chunk Input string chunk
|
|
60
61
|
* @param controller Controller for controlling the transform stream
|
|
61
62
|
*/
|
|
62
|
-
transform(
|
|
63
|
+
transform(r, t) {
|
|
63
64
|
let e = this.currentEvent;
|
|
64
65
|
try {
|
|
65
|
-
if (
|
|
66
|
-
e.data.length > 0 && (
|
|
66
|
+
if (r.trim() === "") {
|
|
67
|
+
e.data.length > 0 && (t.enqueue({
|
|
67
68
|
event: e.event || "message",
|
|
68
69
|
data: e.data.join(`
|
|
69
70
|
`),
|
|
@@ -72,14 +73,14 @@ class u {
|
|
|
72
73
|
}), e.event = "message", e.id = e.id, e.retry = e.retry, e.data = []);
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
75
|
-
if (
|
|
76
|
+
if (r.startsWith(":"))
|
|
76
77
|
return;
|
|
77
|
-
const
|
|
78
|
-
let i,
|
|
79
|
-
|
|
80
|
-
} catch (
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
const n = r.indexOf(":");
|
|
79
|
+
let i, s;
|
|
80
|
+
n === -1 ? (i = r.toLowerCase(), s = "") : (i = r.substring(0, n).toLowerCase(), s = r.substring(n + 1), s.startsWith(" ") && (s = s.substring(1))), i = i.trim(), s = s.trim(), m(i, s, e);
|
|
81
|
+
} catch (n) {
|
|
82
|
+
t.error(
|
|
83
|
+
n instanceof Error ? n : new Error(String(n))
|
|
83
84
|
), e.event = "message", e.id = void 0, e.retry = void 0, e.data = [];
|
|
84
85
|
}
|
|
85
86
|
}
|
|
@@ -87,31 +88,31 @@ class u {
|
|
|
87
88
|
* Called when the stream ends, used to process remaining data
|
|
88
89
|
* @param controller Controller for controlling the transform stream
|
|
89
90
|
*/
|
|
90
|
-
flush(
|
|
91
|
-
let
|
|
91
|
+
flush(r) {
|
|
92
|
+
let t = this.currentEvent;
|
|
92
93
|
try {
|
|
93
|
-
|
|
94
|
-
event:
|
|
95
|
-
data:
|
|
94
|
+
t.data.length > 0 && r.enqueue({
|
|
95
|
+
event: t.event || "message",
|
|
96
|
+
data: t.data.join(`
|
|
96
97
|
`),
|
|
97
|
-
id:
|
|
98
|
-
retry:
|
|
98
|
+
id: t.id || "",
|
|
99
|
+
retry: t.retry
|
|
99
100
|
});
|
|
100
101
|
} catch (e) {
|
|
101
|
-
|
|
102
|
+
r.error(
|
|
102
103
|
e instanceof Error ? e : new Error(String(e))
|
|
103
104
|
);
|
|
104
105
|
} finally {
|
|
105
|
-
|
|
106
|
+
t.event = "message", t.id = void 0, t.retry = void 0, t.data = [];
|
|
106
107
|
}
|
|
107
108
|
}
|
|
108
109
|
}
|
|
109
|
-
class
|
|
110
|
+
class y extends TransformStream {
|
|
110
111
|
constructor() {
|
|
111
|
-
super(new
|
|
112
|
+
super(new h());
|
|
112
113
|
}
|
|
113
114
|
}
|
|
114
|
-
class
|
|
115
|
+
class v {
|
|
115
116
|
/**
|
|
116
117
|
* Convert HTTP response to server-sent event stream
|
|
117
118
|
*
|
|
@@ -119,27 +120,24 @@ class y {
|
|
|
119
120
|
* @returns ServerSentEventStream - Server event stream, can be used to read events sent by the server
|
|
120
121
|
* @throws Error - Throws error when response body is null
|
|
121
122
|
*/
|
|
122
|
-
static toEventStream(
|
|
123
|
-
if (!
|
|
123
|
+
static toEventStream(r) {
|
|
124
|
+
if (!r.body)
|
|
124
125
|
throw new Error("Response body is null");
|
|
125
|
-
return
|
|
126
|
+
return r.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new c()).pipeThrough(new y());
|
|
126
127
|
}
|
|
127
128
|
}
|
|
128
|
-
const h = "content-type", v = "text/event-stream";
|
|
129
129
|
class p {
|
|
130
|
-
intercept(
|
|
131
|
-
const
|
|
132
|
-
return
|
|
130
|
+
intercept(r) {
|
|
131
|
+
const t = r.headers.get(o);
|
|
132
|
+
return t && t.includes(f.TEXT_EVENT_STREAM) && (r.eventStream = () => v.toEventStream(r)), r;
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
135
|
export {
|
|
136
|
-
|
|
137
|
-
v as EventStreamContentType,
|
|
138
|
-
y as EventStreamConverter,
|
|
136
|
+
v as EventStreamConverter,
|
|
139
137
|
p as EventStreamInterceptor,
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
138
|
+
u as ServerSentEventField,
|
|
139
|
+
y as ServerSentEventTransformStream,
|
|
140
|
+
h as ServerSentEventTransformer,
|
|
141
|
+
c as TextLineTransformStream,
|
|
142
|
+
d as TextLineTransformer
|
|
145
143
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(n,o){typeof exports=="object"&&typeof module<"u"?o(exports):typeof define=="function"&&define.amd?define(["exports"],o):(n=typeof globalThis<"u"?globalThis:n||self,o(n.FetcherEventStream={}))})(this,(function(n){"use strict";class
|
|
2
|
-
`);this.buffer=e.pop()||"";for(const s of e)
|
|
3
|
-
`),id:e.id||"",retry:e.retry}),e.event="message",e.id=e.id,e.retry=e.retry,e.data=[]);return}if(
|
|
4
|
-
`),id:
|
|
1
|
+
(function(n,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("@ahoo-wang/fetcher")):typeof define=="function"&&define.amd?define(["exports","@ahoo-wang/fetcher"],o):(n=typeof globalThis<"u"?globalThis:n||self,o(n.FetcherEventStream={},n.Fetcher))})(this,(function(n,o){"use strict";class d{constructor(){this.buffer=""}transform(r,t){try{this.buffer+=r;const e=this.buffer.split(`
|
|
2
|
+
`);this.buffer=e.pop()||"";for(const s of e)t.enqueue(s)}catch(e){t.error(e)}}flush(r){try{this.buffer&&r.enqueue(this.buffer)}catch(t){r.error(t)}}}class u extends TransformStream{constructor(){super(new d)}}var c=(a=>(a.ID="id",a.RETRY="retry",a.EVENT="event",a.DATA="data",a))(c||{});function T(a,r,t){switch(a){case"event":t.event=r;break;case"data":t.data.push(r);break;case"id":t.id=r;break;case"retry":const e=parseInt(r,10);isNaN(e)||(t.retry=e);break}}class m{constructor(){this.currentEvent={event:"message",id:void 0,retry:void 0,data:[]}}transform(r,t){let e=this.currentEvent;try{if(r.trim()===""){e.data.length>0&&(t.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(r.startsWith(":"))return;const s=r.indexOf(":");let f,i;s===-1?(f=r.toLowerCase(),i=""):(f=r.substring(0,s).toLowerCase(),i=r.substring(s+1),i.startsWith(" ")&&(i=i.substring(1))),f=f.trim(),i=i.trim(),T(f,i,e)}catch(s){t.error(s instanceof Error?s:new Error(String(s))),e.event="message",e.id=void 0,e.retry=void 0,e.data=[]}}flush(r){let t=this.currentEvent;try{t.data.length>0&&r.enqueue({event:t.event||"message",data:t.data.join(`
|
|
4
|
+
`),id:t.id||"",retry:t.retry})}catch(e){r.error(e instanceof Error?e:new Error(String(e)))}finally{t.event="message",t.id=void 0,t.retry=void 0,t.data=[]}}}class v extends TransformStream{constructor(){super(new m)}}class h{static toEventStream(r){if(!r.body)throw new Error("Response body is null");return r.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new u).pipeThrough(new v)}}class y{intercept(r){const t=r.headers.get(o.ContentTypeHeader);return t&&t.includes(o.ContentTypeValues.TEXT_EVENT_STREAM)&&(r.eventStream=()=>h.toEventStream(r)),r}}n.EventStreamConverter=h,n.EventStreamInterceptor=y,n.ServerSentEventField=c,n.ServerSentEventTransformStream=v,n.ServerSentEventTransformer=m,n.TextLineTransformStream=u,n.TextLineTransformer=d,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher-eventstream",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
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.0.
|
|
36
|
+
"@ahoo-wang/fetcher": "0.0.8"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
39
|
"@vitest/coverage-v8": "^3.2.4",
|