@ahoo-wang/fetcher-eventstream 3.16.6 → 3.16.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,10 +1,7 @@
1
+ import { SafeTransformer, TransformerPhase } from './safeTransformer';
1
2
  /**
2
3
  * Represents a message sent in an event stream.
3
4
  *
4
- * This interface defines the structure of Server-Sent Events (SSE) as specified by the W3C.
5
- * Each event contains metadata and data that can be processed by clients to handle real-time
6
- * updates from the server.
7
- *
8
5
  * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format}
9
6
  */
10
7
  export interface ServerSentEvent {
@@ -17,81 +14,31 @@ export interface ServerSentEvent {
17
14
  /** The reconnection interval (in milliseconds) to wait before retrying the connection */
18
15
  retry?: number;
19
16
  }
17
+ /**
18
+ * Constants for Server-Sent Event field names.
19
+ */
20
20
  export declare class ServerSentEventFields {
21
- /** The field name for event ID */
22
21
  static readonly ID = "id";
23
- /** The field name for retry interval */
24
22
  static readonly RETRY = "retry";
25
- /** The field name for event type */
26
23
  static readonly EVENT = "event";
27
- /** The field name for event data */
28
24
  static readonly DATA = "data";
29
25
  }
30
26
  /**
31
- * Transformer responsible for converting a string stream into a ServerSentEvent object stream.
32
- *
33
- * Implements the Transformer interface for processing data transformation in TransformStream.
34
- * This transformer handles the parsing of Server-Sent Events (SSE) according to the W3C specification.
35
- * It processes incoming text chunks and converts them into structured ServerSentEvent objects.
27
+ * Transformer responsible for converting a string stream into a
28
+ * ServerSentEvent object stream, following the W3C SSE specification.
36
29
  */
37
- export declare class ServerSentEventTransformer implements Transformer<string, ServerSentEvent> {
30
+ export declare class ServerSentEventTransformer extends SafeTransformer<string, ServerSentEvent> {
38
31
  private currentEventState;
39
- /**
40
- * Reset the current event state to default values.
41
- * This method is called after processing each complete event or when an error occurs.
42
- */
43
32
  private resetEventState;
44
- /**
45
- * Transform input string chunk into ServerSentEvent object.
46
- * This method processes individual chunks of text data, parsing them according to the SSE format.
47
- * It handles:
48
- * - Empty lines (used as event separators)
49
- * - Comment lines (starting with ':')
50
- * - Field lines (field: value format)
51
- * - Event completion and emission
52
- *
53
- * @param chunk Input string chunk
54
- * @param controller Controller for controlling the transform stream
55
- */
56
- transform(chunk: string, controller: TransformStreamDefaultController<ServerSentEvent>): void;
57
- /**
58
- * Called when the stream ends, used to process remaining data.
59
- *
60
- * @param controller Controller for controlling the transform stream
61
- */
62
- flush(controller: TransformStreamDefaultController<ServerSentEvent>): void;
33
+ protected onError(_error: unknown, _phase: TransformerPhase): void;
34
+ protected onTransform(chunk: string, controller: TransformStreamDefaultController<ServerSentEvent>): void;
35
+ protected onFlush(controller: TransformStreamDefaultController<ServerSentEvent>): void;
63
36
  }
64
37
  /**
65
- * A TransformStream that converts a stream of strings into a stream of ServerSentEvent objects.
66
- *
67
- * This class provides a convenient way to transform raw text streams containing Server-Sent Events
68
- * into structured event objects. It wraps the ServerSentEventTransformer in a TransformStream
69
- * for easy integration with other stream processing pipelines.
70
- *
71
- * The stream processes SSE format text and emits ServerSentEvent objects as they are completed.
72
- * Events are separated by empty lines, and the stream handles partial events across multiple chunks.
73
- *
74
- * @example
75
- * ```typescript
76
- * // Create a transform stream
77
- * const sseStream = new ServerSentEventTransformStream();
78
- *
79
- * // Pipe a text stream through it
80
- * const eventStream = textStream.pipeThrough(sseStream);
81
- *
82
- * // Consume the events
83
- * for await (const event of eventStream) {
84
- * console.log('Event:', event.event, 'Data:', event.data);
85
- * }
86
- * ```
38
+ * A TransformStream that converts a stream of strings into a stream of
39
+ * ServerSentEvent objects.
87
40
  */
88
41
  export declare class ServerSentEventTransformStream extends TransformStream<string, ServerSentEvent> {
89
- /**
90
- * Creates a new ServerSentEventTransformStream instance.
91
- *
92
- * Initializes the stream with a ServerSentEventTransformer that handles
93
- * the parsing of SSE format text into structured events.
94
- */
95
42
  constructor();
96
43
  }
97
44
  //# sourceMappingURL=serverSentEventTransformStream.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"serverSentEventTransformStream.d.ts","sourceRoot":"","sources":["../src/serverSentEventTransformStream.ts"],"names":[],"mappings":"AAaA;;;;;;;;GAQG;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;AAWD,qBAAa,qBAAqB;IAChC,kCAAkC;IAClC,MAAM,CAAC,QAAQ,CAAC,EAAE,QAAQ;IAC1B,wCAAwC;IACxC,MAAM,CAAC,QAAQ,CAAC,KAAK,WAAW;IAChC,oCAAoC;IACpC,MAAM,CAAC,QAAQ,CAAC,KAAK,WAAW;IAChC,oCAAoC;IACpC,MAAM,CAAC,QAAQ,CAAC,IAAI,UAAU;CAC/B;AAmED;;;;;;GAMG;AACH,qBAAa,0BAA2B,YAAW,WAAW,CAC5D,MAAM,EACN,eAAe,CAChB;IAEC,OAAO,CAAC,iBAAiB,CAKvB;IAEF;;;OAGG;IACH,OAAO,CAAC,eAAe;IAOvB;;;;;;;;;;;OAWG;IACH,SAAS,CACP,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;IA+D/D;;;;OAIG;IACH,KAAK,CAAC,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC;CAsBpE;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,qBAAa,8BAA+B,SAAQ,eAAe,CACjE,MAAM,EACN,eAAe,CAChB;IACC;;;;;OAKG;;CAIJ"}
1
+ {"version":3,"file":"serverSentEventTransformStream.d.ts","sourceRoot":"","sources":["../src/serverSentEventTransformStream.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,eAAe,EAAE,KAAK,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE3E;;;;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;;GAEG;AACH,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;AA4CD;;;GAGG;AACH,qBAAa,0BAA2B,SAAQ,eAAe,CAC7D,MAAM,EACN,eAAe,CAChB;IACC,OAAO,CAAC,iBAAiB,CAKvB;IAEF,OAAO,CAAC,eAAe;cAOJ,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,gBAAgB,GAAG,IAAI;IAI3E,SAAS,CAAC,WAAW,CACnB,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC,GAC5D,IAAI;IA4CP,SAAS,CAAC,OAAO,CACf,UAAU,EAAE,gCAAgC,CAAC,eAAe,CAAC,GAC5D,IAAI;CAeR;AAED;;;GAGG;AACH,qBAAa,8BAA+B,SAAQ,eAAe,CACjE,MAAM,EACN,eAAe,CAChB;;CAIA"}
@@ -1,64 +1,29 @@
1
- export declare class TextLineTransformer implements Transformer<string, string> {
1
+ import { SafeTransformer } from './safeTransformer';
2
+ /**
3
+ * Transformer that splits text into lines.
4
+ *
5
+ * Accumulates chunks of text and splits them by newline characters ('\n'),
6
+ * emitting each complete line as a separate chunk. Handles partial lines
7
+ * that span multiple input chunks by maintaining an internal buffer.
8
+ */
9
+ export declare class TextLineTransformer extends SafeTransformer<string, string> {
2
10
  private buffer;
3
- /**
4
- * Transform input string chunk by splitting it into lines.
5
- *
6
- * @param chunk Input string chunk
7
- * @param controller Controller for controlling the transform stream
8
- */
9
- transform(chunk: string, controller: TransformStreamDefaultController<string>): void;
10
- /**
11
- * Flush remaining buffer when the stream ends.
12
- *
13
- * @param controller Controller for controlling the transform stream
14
- */
15
- flush(controller: TransformStreamDefaultController<string>): void;
11
+ protected onTransform(chunk: string, controller: TransformStreamDefaultController<string>): void;
12
+ protected onFlush(controller: TransformStreamDefaultController<string>): void;
16
13
  }
17
14
  /**
18
15
  * A TransformStream that splits text into lines.
19
16
  *
20
- * This class provides a convenient way to transform a stream of text chunks into a stream
21
- * of individual lines. It wraps the TextLineTransformer in a TransformStream for easy
22
- * integration with other stream processing pipelines.
23
- *
24
- * The stream processes text data and emits each line as a separate chunk, handling
25
- * lines that may span multiple input chunks automatically.
26
- *
27
17
  * @example
28
18
  * ```typescript
29
- * // Create a line-splitting stream
30
19
  * const lineStream = new TextLineTransformStream();
31
- *
32
- * // Pipe text through it
33
20
  * const lines = textStream.pipeThrough(lineStream);
34
- *
35
- * // Process each line
36
21
  * for await (const line of lines) {
37
22
  * console.log('Line:', line);
38
23
  * }
39
24
  * ```
40
- *
41
- * @example
42
- * ```typescript
43
- * // Process SSE response line by line
44
- * const response = await fetch('/api/stream');
45
- * const lines = response.body!
46
- * .pipeThrough(new TextDecoderStream())
47
- * .pipeThrough(new TextLineTransformStream());
48
- *
49
- * for await (const line of lines) {
50
- * if (line.startsWith('data: ')) {
51
- * console.log('SSE data:', line.substring(6));
52
- * }
53
- * }
54
- * ```
55
25
  */
56
26
  export declare class TextLineTransformStream extends TransformStream<string, string> {
57
- /**
58
- * Creates a new TextLineTransformStream instance.
59
- *
60
- * Initializes the stream with a TextLineTransformer that handles the line splitting logic.
61
- */
62
27
  constructor();
63
28
  }
64
29
  //# sourceMappingURL=textLineTransformStream.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"textLineTransformStream.d.ts","sourceRoot":"","sources":["../src/textLineTransformStream.ts"],"names":[],"mappings":"AA0CA,qBAAa,mBAAoB,YAAW,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC;IACrE,OAAO,CAAC,MAAM,CAAM;IAEpB;;;;;OAKG;IACH,SAAS,CACP,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,gCAAgC,CAAC,MAAM,CAAC;IAetD;;;;OAIG;IACH,KAAK,CAAC,UAAU,EAAE,gCAAgC,CAAC,MAAM,CAAC;CAU3D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,qBAAa,uBAAwB,SAAQ,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC;IAC1E;;;;OAIG;;CAIJ"}
1
+ {"version":3,"file":"textLineTransformStream.d.ts","sourceRoot":"","sources":["../src/textLineTransformStream.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAEpD;;;;;;GAMG;AACH,qBAAa,mBAAoB,SAAQ,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC;IACtE,OAAO,CAAC,MAAM,CAAM;IAEpB,SAAS,CAAC,WAAW,CACnB,KAAK,EAAE,MAAM,EACb,UAAU,EAAE,gCAAgC,CAAC,MAAM,CAAC,GACnD,IAAI;IAUP,SAAS,CAAC,OAAO,CACf,UAAU,EAAE,gCAAgC,CAAC,MAAM,CAAC,GACnD,IAAI;CAMR;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,uBAAwB,SAAQ,eAAe,CAAC,MAAM,EAAE,MAAM,CAAC;;CAI3E"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher-eventstream",
3
- "version": "3.16.6",
3
+ "version": "3.16.8",
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",