@ahoo-wang/fetcher-eventstream 1.0.6 → 1.1.0

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.
@@ -0,0 +1,77 @@
1
+ import { ServerSentEventStream } from './eventStreamConverter';
2
+ import { JsonServerSentEventStream } from './jsonServerSentEventTransformStream';
3
+ declare global {
4
+ interface Response {
5
+ /**
6
+ * Gets the content type of the response.
7
+ *
8
+ * This property provides access to the Content-Type header of the response,
9
+ * which indicates the media type of the resource transmitted in the response.
10
+ *
11
+ * @returns The content type header value as a string, or null if the header is not set
12
+ */
13
+ get contentType(): string | null;
14
+ /**
15
+ * Checks if the response is an event stream.
16
+ *
17
+ * This property examines the Content-Type header to determine if the response
18
+ * contains server-sent events data (text/event-stream).
19
+ *
20
+ * @returns true if the response is an event stream, false otherwise
21
+ */
22
+ get isEventStream(): boolean;
23
+ /**
24
+ * Returns a ServerSentEventStream for consuming server-sent events.
25
+ *
26
+ * This method is added to Response objects by the EventStreamInterceptor
27
+ * when the response content type indicates a server-sent event stream.
28
+ *
29
+ * @returns A ReadableStream of ServerSentEvent objects, or null if not an event stream
30
+ */
31
+ eventStream(): ServerSentEventStream | null;
32
+ /**
33
+ * Returns a ServerSentEventStream for consuming server-sent events.
34
+ *
35
+ * This method is similar to eventStream() but will throw an error if the event stream is not available.
36
+ * It is added to Response objects by the EventStreamInterceptor when the response content type
37
+ * indicates a server-sent event stream.
38
+ *
39
+ * @returns A ReadableStream of ServerSentEvent objects
40
+ * @throws {Error} if the event stream is not available
41
+ */
42
+ requiredEventStream(): ServerSentEventStream;
43
+ /**
44
+ * Returns a JsonServerSentEventStream for consuming server-sent events with JSON data.
45
+ *
46
+ * This method is added to Response objects by the EventStreamInterceptor
47
+ * when the response content type indicates a server-sent event stream.
48
+ *
49
+ * @template DATA - The type of the JSON data in the server-sent events
50
+ * @returns A ReadableStream of ServerSentEvent objects with JSON data, or null if not an event stream
51
+ */
52
+ jsonEventStream<DATA>(): JsonServerSentEventStream<DATA> | null;
53
+ /**
54
+ * Returns a JsonServerSentEventStream for consuming server-sent events with JSON data.
55
+ *
56
+ * This method is similar to jsonEventStream() but will throw an error if the event stream is not available.
57
+ * It is added to Response objects by the EventStreamInterceptor when the response content type
58
+ * indicates a server-sent event stream with JSON data.
59
+ *
60
+ * @template DATA - The type of the JSON data in the server-sent events
61
+ * @returns A ReadableStream of ServerSentEvent objects with JSON data
62
+ * @throws {Error} if the event stream is not available
63
+ */
64
+ requiredJsonEventStream<DATA>(): JsonServerSentEventStream<DATA>;
65
+ }
66
+ interface ReadableStream<R = any> {
67
+ /**
68
+ * Makes ReadableStream async iterable for use with for-await loops.
69
+ *
70
+ * This allows the stream to be consumed using `for await (const chunk of stream)` syntax.
71
+ *
72
+ * @returns An async iterator for the stream
73
+ */
74
+ [Symbol.asyncIterator](): AsyncIterator<R>;
75
+ }
76
+ }
77
+ //# sourceMappingURL=responses.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"responses.d.ts","sourceRoot":"","sources":["../src/responses.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,qBAAqB,EAEtB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EACL,yBAAyB,EAE1B,MAAM,sCAAsC,CAAC;AAG9C,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,QAAQ;QAChB;;;;;;;WAOG;QACH,IAAI,WAAW,IAAI,MAAM,GAAG,IAAI,CAAC;QAEjC;;;;;;;WAOG;QACH,IAAI,aAAa,IAAI,OAAO,CAAC;QAE7B;;;;;;;WAOG;QACH,WAAW,IAAI,qBAAqB,GAAG,IAAI,CAAC;QAE5C;;;;;;;;;WASG;QACH,mBAAmB,IAAI,qBAAqB,CAAC;QAE7C;;;;;;;;WAQG;QACH,eAAe,CAAC,IAAI,KAAK,yBAAyB,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;QAEhE;;;;;;;;;;WAUG;QACH,uBAAuB,CAAC,IAAI,KAAK,yBAAyB,CAAC,IAAI,CAAC,CAAC;KAClE;IAED,UAAU,cAAc,CAAC,CAAC,GAAG,GAAG;QAC9B;;;;;;WAMG;QACH,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC;KAC5C;CACF"}
@@ -13,11 +13,11 @@ export interface ServerSentEvent {
13
13
  /** The reconnection interval (in milliseconds) to wait before retrying the connection */
14
14
  retry?: number;
15
15
  }
16
- export declare enum ServerSentEventField {
17
- ID = "id",
18
- RETRY = "retry",
19
- EVENT = "event",
20
- DATA = "data"
16
+ export declare class ServerSentEventFields {
17
+ static readonly ID = "id";
18
+ static readonly RETRY = "retry";
19
+ static readonly EVENT = "event";
20
+ static readonly DATA = "data";
21
21
  }
22
22
  /**
23
23
  * Transformer responsible for converting a string stream into a ServerSentEvent object stream.
@@ -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;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"}
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,qBAAa,qBAAqB;IAChC,MAAM,CAAC,QAAQ,CAAC,EAAE,QAAQ;IAC1B,MAAM,CAAC,QAAQ,CAAC,KAAK,WAAW;IAChC,MAAM,CAAC,QAAQ,CAAC,KAAK,WAAW;IAChC,MAAM,CAAC,QAAQ,CAAC,IAAI,UAAU;CAC/B;AA6CD;;;;GAIG;AACH,qBAAa,0BACX,YAAW,WAAW,CAAC,MAAM,EAAE,eAAe,CAAC;IAE/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": "1.0.6",
3
+ "version": "1.1.0",
4
4
  "description": "Server-Sent Events (SSE) support for Fetcher HTTP client with native LLM streaming API support. Enables real-time data streaming and token-by-token LLM response handling.",
5
5
  "keywords": [
6
6
  "fetch",
@@ -39,7 +39,7 @@
39
39
  "dist"
40
40
  ],
41
41
  "dependencies": {
42
- "@ahoo-wang/fetcher": "1.0.6"
42
+ "@ahoo-wang/fetcher": "1.1.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@vitest/coverage-v8": "^3.2.4",
@@ -1,62 +0,0 @@
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;
11
- /**
12
- * Interceptor that enhances Response objects with event stream capabilities.
13
- *
14
- * This interceptor detects responses with `text/event-stream` content type and adds
15
- * an `eventStream()` method to the Response object, which returns a readable stream
16
- * of Server-Sent Events that can be consumed using `for await` syntax.
17
- *
18
- * @remarks
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.
26
- *
27
- * @example
28
- * ```typescript
29
- * // Using the eventStream method
30
- * const response = await fetcher.get('/events');
31
- * if (response.headers.get('content-type')?.includes('text/event-stream')) {
32
- * const eventStream = response.eventStream();
33
- * for await (const event of eventStream) {
34
- * console.log('Received event:', event);
35
- * }
36
- * }
37
- * ```
38
- */
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
- */
60
- intercept(exchange: FetchExchange): void;
61
- }
62
- //# sourceMappingURL=eventStreamInterceptor.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"eventStreamInterceptor.d.ts","sourceRoot":"","sources":["../src/eventStreamInterceptor.ts"],"names":[],"mappings":"AAcA,OAAO,EAGL,aAAa,EACb,mBAAmB,EACpB,MAAM,oBAAoB,CAAC;AAG5B;;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;CAalC"}
package/dist/types.d.ts DELETED
@@ -1,32 +0,0 @@
1
- import { ServerSentEventStream } from './eventStreamConverter';
2
- import { JsonServerSentEventStream } from './jsonServerSentEventTransformStream';
3
- declare global {
4
- interface Response {
5
- /**
6
- * Returns a ServerSentEventStream for consuming server-sent events.
7
- *
8
- * This method is added to Response objects by the EventStreamInterceptor
9
- * when the response content type indicates a server-sent event stream.
10
- *
11
- * @returns A ReadableStream of ServerSentEvent objects
12
- */
13
- eventStream?(): ServerSentEventStream;
14
- /**
15
- * Returns a JsonServerSentEventStream for consuming server-sent events with JSON data.
16
- *
17
- * This method is added to Response objects by the EventStreamInterceptor
18
- * when the response content type indicates a server-sent event stream.
19
- *
20
- * @template DATA - The type of the JSON data in the server-sent events
21
- * @returns A ReadableStream of ServerSentEvent objects with JSON data
22
- */
23
- jsonEventStream?<DATA>(): JsonServerSentEventStream<DATA>;
24
- }
25
- interface ReadableStream<R = any> {
26
- /**
27
- * Makes ReadableStream async iterable for use with for-await loops.
28
- */
29
- [Symbol.asyncIterator](): AsyncIterator<R>;
30
- }
31
- }
32
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,qBAAqB,EAAE,MAAM,wBAAwB,CAAC;AAC/D,OAAO,EAAE,yBAAyB,EAAE,MAAM,sCAAsC,CAAC;AAEjF,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,QAAQ;QAChB;;;;;;;WAOG;QACH,WAAW,CAAC,IAAI,qBAAqB,CAAC;QAEtC;;;;;;;;WAQG;QACH,eAAe,CAAC,CAAC,IAAI,KAAK,yBAAyB,CAAC,IAAI,CAAC,CAAC;KAC3D;IAED,UAAU,cAAc,CAAC,CAAC,GAAG,GAAG;QAC9B;;WAEG;QACH,CAAC,MAAM,CAAC,aAAa,CAAC,IAAI,aAAa,CAAC,CAAC,CAAC,CAAC;KAC5C;CACF"}