@ahoo-wang/fetcher-eventstream 0.9.2 → 0.9.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.
@@ -1,4 +1,13 @@
1
- import { FetchExchange, Interceptor } from '@ahoo-wang/fetcher';
1
+ import { FetchExchange, ResponseInterceptor } from '@ahoo-wang/fetcher';
2
+ /**
3
+ * The name of the EventStreamInterceptor.
4
+ */
5
+ export declare const EVENT_STREAM_INTERCEPTOR_NAME = "EventStreamInterceptor";
6
+ /**
7
+ * The order of the EventStreamInterceptor.
8
+ * Set to Number.MAX_SAFE_INTEGER - 1000 to ensure it runs latest among response interceptors.
9
+ */
10
+ export declare const EVENT_STREAM_INTERCEPTOR_ORDER: number;
2
11
  /**
3
12
  * Interceptor that enhances Response objects with event stream capabilities.
4
13
  *
@@ -7,11 +16,13 @@ import { FetchExchange, Interceptor } from '@ahoo-wang/fetcher';
7
16
  * of Server-Sent Events that can be consumed using `for await` syntax.
8
17
  *
9
18
  * @remarks
10
- * This interceptor runs after the HTTP response is received but before the response
11
- * is returned to the caller. The order is set to Number.MAX_SAFE_INTEGER - 100 to
12
- * ensure it runs after all standard response processing is complete, as it adds
13
- * specialized functionality to the response object. This order allows other
14
- * response interceptors to run after it if needed.
19
+ * This interceptor runs at the very end of the response interceptor chain to ensure
20
+ * it runs after all standard response processing is complete, as it adds
21
+ * specialized functionality to the response object. The order is set to
22
+ * EVENT_STREAM_INTERCEPTOR_ORDER to ensure it executes latest among response interceptors,
23
+ * allowing for other response interceptors to run before it if needed. This positioning
24
+ * ensures that all response processing is completed before specialized event stream
25
+ * functionality is added to the response object.
15
26
  *
16
27
  * @example
17
28
  * ```typescript
@@ -25,9 +36,27 @@ import { FetchExchange, Interceptor } from '@ahoo-wang/fetcher';
25
36
  * }
26
37
  * ```
27
38
  */
28
- export declare class EventStreamInterceptor implements Interceptor {
29
- name: string;
30
- order: number;
39
+ export declare class EventStreamInterceptor implements ResponseInterceptor {
40
+ readonly name = "EventStreamInterceptor";
41
+ readonly order: number;
42
+ /**
43
+ * Intercepts responses to add event stream capabilities.
44
+ *
45
+ * This method runs at the very end of the response interceptor chain to ensure
46
+ * it runs after all standard response processing is complete. It detects responses
47
+ * with `text/event-stream` content type and adds an `eventStream()` method to
48
+ * the Response object, which returns a readable stream of Server-Sent Events.
49
+ *
50
+ * @param exchange - The exchange containing the response to enhance
51
+ *
52
+ * @remarks
53
+ * This method executes latest among response interceptors to ensure all response
54
+ * processing is completed before specialized event stream functionality is added.
55
+ * It only enhances responses with `text/event-stream` content type, leaving other
56
+ * responses unchanged. The positioning at the end of the response chain ensures
57
+ * that all response transformations and validations are completed before event
58
+ * stream capabilities are added to the response object.
59
+ */
31
60
  intercept(exchange: FetchExchange): void;
32
61
  }
33
62
  //# sourceMappingURL=eventStreamInterceptor.d.ts.map
@@ -1 +1 @@
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;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,qBAAa,sBAAuB,YAAW,WAAW;IACxD,IAAI,SAA4B;IAChC,KAAK,SAAiC;IAEtC,SAAS,CAAC,QAAQ,EAAE,aAAa;CAWlC"}
1
+ {"version":3,"file":"eventStreamInterceptor.d.ts","sourceRoot":"","sources":["../src/eventStreamInterceptor.ts"],"names":[],"mappings":"AAcA,OAAO,EAGL,aAAa,EACb,mBAAmB,EACpB,MAAM,oBAAoB,CAAC;AAE5B;;GAEG;AACH,eAAO,MAAM,6BAA6B,2BAA2B,CAAC;AAEtE;;;GAGG;AACH,eAAO,MAAM,8BAA8B,QAAiC,CAAC;AAE7E;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,qBAAa,sBAAuB,YAAW,mBAAmB;IAChE,QAAQ,CAAC,IAAI,4BAAiC;IAC9C,QAAQ,CAAC,KAAK,SAAkC;IAEhD;;;;;;;;;;;;;;;;;OAiBG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa;CAWlC"}
package/dist/index.es.js CHANGED
@@ -1,5 +1,5 @@
1
- import { ContentTypeHeader as i, ContentTypeValues as f } from "@ahoo-wang/fetcher";
2
- class c {
1
+ import { ContentTypeHeader as c, ContentTypeValues as f } from "@ahoo-wang/fetcher";
2
+ class d {
3
3
  constructor() {
4
4
  this.buffer = "";
5
5
  }
@@ -34,13 +34,13 @@ class c {
34
34
  }
35
35
  }
36
36
  }
37
- class d extends TransformStream {
37
+ class u extends TransformStream {
38
38
  constructor() {
39
- super(new c());
39
+ super(new d());
40
40
  }
41
41
  }
42
- var u = /* @__PURE__ */ ((n) => (n.ID = "id", n.RETRY = "retry", n.EVENT = "event", n.DATA = "data", n))(u || {});
43
- function m(n, t, e) {
42
+ var E = /* @__PURE__ */ ((n) => (n.ID = "id", n.RETRY = "retry", n.EVENT = "event", n.DATA = "data", n))(E || {});
43
+ function T(n, t, e) {
44
44
  switch (n) {
45
45
  case "event":
46
46
  e.event = t;
@@ -58,10 +58,11 @@ function m(n, t, e) {
58
58
  }
59
59
  }
60
60
  }
61
+ const a = "message";
61
62
  class h {
62
63
  constructor() {
63
64
  this.currentEvent = {
64
- event: "message",
65
+ event: a,
65
66
  id: void 0,
66
67
  retry: void 0,
67
68
  data: []
@@ -78,23 +79,23 @@ class h {
78
79
  try {
79
80
  if (t.trim() === "") {
80
81
  r.data.length > 0 && (e.enqueue({
81
- event: r.event || "message",
82
+ event: r.event || a,
82
83
  data: r.data.join(`
83
84
  `),
84
85
  id: r.id || "",
85
86
  retry: r.retry
86
- }), r.event = "message", r.data = []);
87
+ }), r.event = a, r.data = []);
87
88
  return;
88
89
  }
89
90
  if (t.startsWith(":"))
90
91
  return;
91
92
  const s = t.indexOf(":");
92
- let o, a;
93
- s === -1 ? (o = t.toLowerCase(), a = "") : (o = t.substring(0, s).toLowerCase(), a = t.substring(s + 1), a.startsWith(" ") && (a = a.substring(1))), o = o.trim(), a = a.trim(), m(o, a, r);
93
+ let i, o;
94
+ s === -1 ? (i = t.toLowerCase(), o = "") : (i = t.substring(0, s).toLowerCase(), o = t.substring(s + 1), o.startsWith(" ") && (o = o.substring(1))), i = i.trim(), o = o.trim(), T(i, o, r);
94
95
  } catch (s) {
95
96
  e.error(
96
97
  s instanceof Error ? s : new Error(String(s))
97
- ), r.event = "message", r.id = void 0, r.retry = void 0, r.data = [];
98
+ ), r.event = a, r.id = void 0, r.retry = void 0, r.data = [];
98
99
  }
99
100
  }
100
101
  /**
@@ -106,7 +107,7 @@ class h {
106
107
  const e = this.currentEvent;
107
108
  try {
108
109
  e.data.length > 0 && t.enqueue({
109
- event: e.event || "message",
110
+ event: e.event || a,
110
111
  data: e.data.join(`
111
112
  `),
112
113
  id: e.id || "",
@@ -117,37 +118,58 @@ class h {
117
118
  r instanceof Error ? r : new Error(String(r))
118
119
  );
119
120
  } finally {
120
- e.event = "message", e.id = void 0, e.retry = void 0, e.data = [];
121
+ e.event = a, e.id = void 0, e.retry = void 0, e.data = [];
121
122
  }
122
123
  }
123
124
  }
124
- class p extends TransformStream {
125
+ class m extends TransformStream {
125
126
  constructor() {
126
127
  super(new h());
127
128
  }
128
129
  }
129
- function T(n) {
130
+ function p(n) {
130
131
  if (!n.body)
131
132
  throw new Error("Response body is null");
132
- return n.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new d()).pipeThrough(new p());
133
+ return n.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new u()).pipeThrough(new m());
133
134
  }
134
- class v {
135
+ const y = "EventStreamInterceptor", v = Number.MAX_SAFE_INTEGER - 1e3;
136
+ class S {
135
137
  constructor() {
136
- this.name = "EventStreamInterceptor", this.order = Number.MAX_SAFE_INTEGER - 100;
138
+ this.name = y, this.order = v;
137
139
  }
140
+ /**
141
+ * Intercepts responses to add event stream capabilities.
142
+ *
143
+ * This method runs at the very end of the response interceptor chain to ensure
144
+ * it runs after all standard response processing is complete. It detects responses
145
+ * with `text/event-stream` content type and adds an `eventStream()` method to
146
+ * the Response object, which returns a readable stream of Server-Sent Events.
147
+ *
148
+ * @param exchange - The exchange containing the response to enhance
149
+ *
150
+ * @remarks
151
+ * This method executes latest among response interceptors to ensure all response
152
+ * processing is completed before specialized event stream functionality is added.
153
+ * It only enhances responses with `text/event-stream` content type, leaving other
154
+ * responses unchanged. The positioning at the end of the response chain ensures
155
+ * that all response transformations and validations are completed before event
156
+ * stream capabilities are added to the response object.
157
+ */
138
158
  intercept(t) {
139
159
  const e = t.response;
140
160
  if (!e)
141
161
  return;
142
- e.headers.get(i)?.includes(f.TEXT_EVENT_STREAM) && (e.eventStream = () => T(e));
162
+ e.headers.get(c)?.includes(f.TEXT_EVENT_STREAM) && (e.eventStream = () => p(e));
143
163
  }
144
164
  }
145
165
  export {
146
- v as EventStreamInterceptor,
147
- u as ServerSentEventField,
148
- p as ServerSentEventTransformStream,
166
+ y as EVENT_STREAM_INTERCEPTOR_NAME,
167
+ v as EVENT_STREAM_INTERCEPTOR_ORDER,
168
+ S as EventStreamInterceptor,
169
+ E as ServerSentEventField,
170
+ m as ServerSentEventTransformStream,
149
171
  h as ServerSentEventTransformer,
150
- d as TextLineTransformStream,
151
- c as TextLineTransformer,
152
- T as toServerSentEventStream
172
+ u as TextLineTransformStream,
173
+ d as TextLineTransformer,
174
+ p as toServerSentEventStream
153
175
  };
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(s,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("@ahoo-wang/fetcher")):typeof define=="function"&&define.amd?define(["exports","@ahoo-wang/fetcher"],i):(s=typeof globalThis<"u"?globalThis:s||self,i(s.FetcherEventStream={},s.Fetcher))})(this,(function(s,i){"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 c extends TransformStream{constructor(){super(new d)}}var u=(n=>(n.ID="id",n.RETRY="retry",n.EVENT="event",n.DATA="data",n))(u||{});function v(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,o;a===-1?(f=t.toLowerCase(),o=""):(f=t.substring(0,a).toLowerCase(),o=t.substring(a+1),o.startsWith(" ")&&(o=o.substring(1))),f=f.trim(),o=o.trim(),v(f,o,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 h 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 c).pipeThrough(new h)}class S{constructor(){this.name="EventStreamInterceptor",this.order=Number.MAX_SAFE_INTEGER-100}intercept(t){const e=t.response;if(!e)return;e.headers.get(i.ContentTypeHeader)?.includes(i.ContentTypeValues.TEXT_EVENT_STREAM)&&(e.eventStream=()=>T(e))}}s.EventStreamInterceptor=S,s.ServerSentEventField=u,s.ServerSentEventTransformStream=h,s.ServerSentEventTransformer=m,s.TextLineTransformStream=c,s.TextLineTransformer=d,s.toServerSentEventStream=T,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(n,i){typeof exports=="object"&&typeof module<"u"?i(exports,require("@ahoo-wang/fetcher")):typeof define=="function"&&define.amd?define(["exports","@ahoo-wang/fetcher"],i):(n=typeof globalThis<"u"?globalThis:n||self,i(n.FetcherEventStream={},n.Fetcher))})(this,(function(n,i){"use strict";class E{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 d extends TransformStream{constructor(){super(new E)}}var T=(s=>(s.ID="id",s.RETRY="retry",s.EVENT="event",s.DATA="data",s))(T||{});function y(s,t,e){switch(s){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}}}const f="message";class u{constructor(){this.currentEvent={event:f,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||f,data:r.data.join(`
3
+ `),id:r.id||"",retry:r.retry}),r.event=f,r.data=[]);return}if(t.startsWith(":"))return;const a=t.indexOf(":");let c,o;a===-1?(c=t.toLowerCase(),o=""):(c=t.substring(0,a).toLowerCase(),o=t.substring(a+1),o.startsWith(" ")&&(o=o.substring(1))),c=c.trim(),o=o.trim(),y(c,o,r)}catch(a){e.error(a instanceof Error?a:new Error(String(a))),r.event=f,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||f,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=f,e.id=void 0,e.retry=void 0,e.data=[]}}}class m extends TransformStream{constructor(){super(new u)}}function h(s){if(!s.body)throw new Error("Response body is null");return s.body.pipeThrough(new TextDecoderStream("utf-8")).pipeThrough(new d).pipeThrough(new m)}const v="EventStreamInterceptor",S=Number.MAX_SAFE_INTEGER-1e3;class p{constructor(){this.name=v,this.order=S}intercept(t){const e=t.response;if(!e)return;e.headers.get(i.ContentTypeHeader)?.includes(i.ContentTypeValues.TEXT_EVENT_STREAM)&&(e.eventStream=()=>h(e))}}n.EVENT_STREAM_INTERCEPTOR_NAME=v,n.EVENT_STREAM_INTERCEPTOR_ORDER=S,n.EventStreamInterceptor=p,n.ServerSentEventField=T,n.ServerSentEventTransformStream=m,n.ServerSentEventTransformer=u,n.TextLineTransformStream=d,n.TextLineTransformer=E,n.toServerSentEventStream=h,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
@@ -1 +1 @@
1
- {"version":3,"file":"serverSentEventTransformStream.d.ts","sourceRoot":"","sources":["../src/serverSentEventTransformStream.ts"],"names":[],"mappings":"AAaA;;;;GAIG;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;;;;GAIG;AACH,qBAAa,0BACX,YAAW,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC;IAG/C,OAAO,CAAC,YAAY,CAKlB;IAEF;;;;;OAKG;IACH,SAAS,CACP,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;IAiE/D;;;;OAIG;IACH,KAAK,CAAC,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;CAwBpE;AAED;;GAEG;AACH,qBAAa,8BAA+B,SAAQ,eAAe,CACjE,MAAM,EACN,eAAe,CAChB;;CAIA"}
1
+ {"version":3,"file":"serverSentEventTransformStream.d.ts","sourceRoot":"","sources":["../src/serverSentEventTransformStream.ts"],"names":[],"mappings":"AAaA;;;;GAIG;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;AA6CD;;;;GAIG;AACH,qBAAa,0BACX,YAAW,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC;IAG/C,OAAO,CAAC,YAAY,CAKlB;IAEF;;;;;OAKG;IACH,SAAS,CACP,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;IAiE/D;;;;OAIG;IACH,KAAK,CAAC,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;CAwBpE;AAED;;GAEG;AACH,qBAAa,8BAA+B,SAAQ,eAAe,CACjE,MAAM,EACN,eAAe,CAChB;;CAIA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher-eventstream",
3
- "version": "0.9.2",
3
+ "version": "0.9.6",
4
4
  "description": "Server-Sent Events (SSE) support for Fetcher HTTP client",
5
5
  "keywords": [
6
6
  "fetch",
@@ -19,6 +19,7 @@
19
19
  "bugs": {
20
20
  "url": "https://github.com/Ahoo-Wang/fetcher/issues"
21
21
  },
22
+ "type": "module",
22
23
  "main": "./dist/index.umd.js",
23
24
  "module": "./dist/index.es.js",
24
25
  "types": "./dist/index.d.ts",
@@ -33,7 +34,7 @@
33
34
  "dist"
34
35
  ],
35
36
  "dependencies": {
36
- "@ahoo-wang/fetcher": "0.9.2"
37
+ "@ahoo-wang/fetcher": "0.9.6"
37
38
  },
38
39
  "devDependencies": {
39
40
  "@vitest/coverage-v8": "^3.2.4",