@stackline/sse 1.0.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.
- package/CHANGELOG.md +22 -0
- package/CONTRIBUTING.md +45 -0
- package/LICENSE +21 -0
- package/NOTICE +6 -0
- package/README.md +340 -0
- package/SECURITY.md +35 -0
- package/dist/index.cjs +1560 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.cts +293 -0
- package/dist/index.d.mts +293 -0
- package/dist/index.d.ts +293 -0
- package/dist/index.js +1539 -0
- package/dist/index.js.map +7 -0
- package/dist/index.min.js +13 -0
- package/dist/index.min.js.map +7 -0
- package/package.json +102 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
export type SSEChunk = string | Uint8Array;
|
|
2
|
+
|
|
3
|
+
export interface SSEEvent<T = string> {
|
|
4
|
+
data: T;
|
|
5
|
+
event?: string;
|
|
6
|
+
id?: string;
|
|
7
|
+
lastEventId: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface SSEMessage {
|
|
11
|
+
comment?: string | number | boolean | bigint;
|
|
12
|
+
data?: string | number | boolean | bigint;
|
|
13
|
+
event?: string | number | boolean | bigint;
|
|
14
|
+
id?: string | number | boolean | bigint;
|
|
15
|
+
retry?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SSEParserState {
|
|
19
|
+
readonly buffered: number;
|
|
20
|
+
readonly bytes: number;
|
|
21
|
+
readonly characters: number;
|
|
22
|
+
readonly comments: number;
|
|
23
|
+
readonly events: number;
|
|
24
|
+
readonly lastEventId: string;
|
|
25
|
+
readonly retries: number;
|
|
26
|
+
readonly terminated: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SSEParserOptions {
|
|
30
|
+
fatalUTF8?: boolean;
|
|
31
|
+
lastEventId?: string;
|
|
32
|
+
maxBufferSize?: number;
|
|
33
|
+
maxEventSize?: number;
|
|
34
|
+
maxLineLength?: number;
|
|
35
|
+
strict?: boolean;
|
|
36
|
+
onComment?(comment: string): void;
|
|
37
|
+
onError?(error: SSEParseError): void;
|
|
38
|
+
onEvent?(event: SSEEvent): void;
|
|
39
|
+
onId?(lastEventId: string): void;
|
|
40
|
+
onRetry?(milliseconds: number): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SSEParser {
|
|
44
|
+
readonly state: SSEParserState;
|
|
45
|
+
feed(chunk: SSEChunk): SSEParser;
|
|
46
|
+
end(): SSEParser;
|
|
47
|
+
reset(options?: { consume?: boolean; preserveLastEventId?: boolean }): SSEParser;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SSEEncoderOptions {
|
|
51
|
+
bytes?: boolean;
|
|
52
|
+
json?: boolean;
|
|
53
|
+
newline?: '\n' | '\r\n';
|
|
54
|
+
replacer?: (this: any, key: string, value: any) => any;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface AsyncIteratorLike<T> {
|
|
58
|
+
next(): Promise<IteratorResult<T>>;
|
|
59
|
+
return?(value?: any): Promise<IteratorResult<T>>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface AsyncIterableLike<T> {
|
|
63
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ReadableStreamReaderLike<T> {
|
|
67
|
+
read(): Promise<IteratorResult<T>>;
|
|
68
|
+
cancel?(reason?: any): Promise<void>;
|
|
69
|
+
releaseLock?(): void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ReadableStreamLike<T> {
|
|
73
|
+
getReader(): ReadableStreamReaderLike<T>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface SSEHeadersLike {
|
|
77
|
+
get(name: string): string | null;
|
|
78
|
+
forEach?(callback: (value: string, key: string) => void): void;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface SSEResponseLike {
|
|
82
|
+
readonly body: ReadableStreamLike<Uint8Array> | AsyncIterableLike<Uint8Array> | null;
|
|
83
|
+
readonly headers: SSEHeadersLike;
|
|
84
|
+
readonly ok?: boolean;
|
|
85
|
+
readonly redirected?: boolean;
|
|
86
|
+
readonly status: number;
|
|
87
|
+
readonly statusText?: string;
|
|
88
|
+
readonly url?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type SSESource =
|
|
92
|
+
| { body: ReadableStreamLike<SSEChunk> | AsyncIterableLike<SSEChunk> | null }
|
|
93
|
+
| ReadableStreamLike<SSEChunk>
|
|
94
|
+
| AsyncIterable<SSEChunk>
|
|
95
|
+
| Iterable<SSEChunk>;
|
|
96
|
+
|
|
97
|
+
export interface SSEDecodeOptions extends SSEParserOptions {
|
|
98
|
+
feedSize?: number;
|
|
99
|
+
maxQueuedEvents?: number;
|
|
100
|
+
parser?: SSEParserOptions;
|
|
101
|
+
signal?: AbortSignalLike;
|
|
102
|
+
onChunk?(chunk: SSEChunk): void;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface SSEJSONDecodeOptions extends SSEDecodeOptions {
|
|
106
|
+
doneSentinel?: string | false;
|
|
107
|
+
ignoreInvalidJSON?: boolean;
|
|
108
|
+
reviver?: (this: any, key: string, value: any) => any;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface AbortSignalLike {
|
|
112
|
+
readonly aborted: boolean;
|
|
113
|
+
readonly reason?: any;
|
|
114
|
+
addEventListener(type: 'abort', listener: () => void, options?: any): void;
|
|
115
|
+
removeEventListener(type: 'abort', listener: () => void): void;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type FetchLike = (input: any, init?: any) => Promise<SSEResponseLike>;
|
|
119
|
+
|
|
120
|
+
export interface SSERetryOptions {
|
|
121
|
+
factor?: number;
|
|
122
|
+
jitter?: 'full' | 'none' | false | ((maximumDelay: number) => number);
|
|
123
|
+
maxDelay?: number;
|
|
124
|
+
minDelay?: number;
|
|
125
|
+
retries?: number;
|
|
126
|
+
statusCodes?: Iterable<number>;
|
|
127
|
+
shouldRetry?(context: { error: any; response?: SSEResponseLike }): boolean;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface SSEClientContext {
|
|
131
|
+
attempts: number;
|
|
132
|
+
lastEventId: string;
|
|
133
|
+
reconnects: number;
|
|
134
|
+
response?: SSEResponseLike;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface SSERetryContext {
|
|
138
|
+
attempt: number;
|
|
139
|
+
delay: number;
|
|
140
|
+
error?: any;
|
|
141
|
+
lastEventId: string;
|
|
142
|
+
reconnects: number;
|
|
143
|
+
response?: SSEResponseLike;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface SSEClientErrorContext {
|
|
147
|
+
attempt: number;
|
|
148
|
+
error: any;
|
|
149
|
+
lastEventId: string;
|
|
150
|
+
reconnects: number;
|
|
151
|
+
response?: SSEResponseLike;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface SSEFetchOptions {
|
|
155
|
+
body?: any;
|
|
156
|
+
bodyFactory?(context: { attempt: number; lastEventId: string; signal: AbortSignalLike }): any | Promise<any>;
|
|
157
|
+
cache?: string;
|
|
158
|
+
connectTimeout?: number;
|
|
159
|
+
decode?: SSEDecodeOptions;
|
|
160
|
+
fetch?: FetchLike;
|
|
161
|
+
headers?: any;
|
|
162
|
+
idleTimeout?: number;
|
|
163
|
+
lastEventId?: string;
|
|
164
|
+
method?: string;
|
|
165
|
+
parser?: SSEParserOptions;
|
|
166
|
+
retry?: false | number | SSERetryOptions;
|
|
167
|
+
signal?: AbortSignalLike;
|
|
168
|
+
totalTimeout?: number;
|
|
169
|
+
onClose?(context: SSEClientContext & { reason: string }): void | Promise<void>;
|
|
170
|
+
onEvent?(event: SSEEvent, context: SSEClientContext): void | Promise<void>;
|
|
171
|
+
onError?(context: SSEClientErrorContext): void | false | number | Promise<void | false | number>;
|
|
172
|
+
onOpen?(response: SSEResponseLike, context: SSEClientContext): void | boolean | Promise<void | boolean>;
|
|
173
|
+
onRetry?(context: SSERetryContext): void | false | number | Promise<void | false | number>;
|
|
174
|
+
[key: string]: any;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface FetchEventSourceOptions extends SSEFetchOptions {
|
|
178
|
+
onclose?(context?: any): void | Promise<void>;
|
|
179
|
+
onerror?(error: any): any;
|
|
180
|
+
onmessage?(event: SSEEvent): void | Promise<void>;
|
|
181
|
+
onopen?(response: SSEResponseLike): void | boolean | Promise<void | boolean>;
|
|
182
|
+
openWhenHidden?: boolean;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface SSEConsumeResult {
|
|
186
|
+
events: number;
|
|
187
|
+
lastEventId: string;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface SSEChannel {
|
|
191
|
+
readonly stream: ReadableStreamLike<Uint8Array>;
|
|
192
|
+
readonly closed: Promise<any>;
|
|
193
|
+
readonly open: boolean;
|
|
194
|
+
readonly ready: Promise<void>;
|
|
195
|
+
close(): void;
|
|
196
|
+
comment(value: string): boolean;
|
|
197
|
+
error(reason: any): void;
|
|
198
|
+
send(message: SSEMessage): boolean;
|
|
199
|
+
sendJSON(data: any, fields?: Omit<SSEMessage, 'data'>): boolean;
|
|
200
|
+
toResponse(init?: any): any;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export interface SSEChannelOptions extends SSEEncoderOptions {
|
|
204
|
+
heartbeatComment?: string;
|
|
205
|
+
heartbeatInterval?: number;
|
|
206
|
+
highWaterMark?: number;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export class SSEError extends Error {
|
|
210
|
+
constructor(message: string, code: string, details?: { cause?: any });
|
|
211
|
+
readonly code: string;
|
|
212
|
+
readonly cause?: any;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export class SSEParseError extends SSEError {
|
|
216
|
+
constructor(message: string, details?: any);
|
|
217
|
+
readonly fatal: boolean;
|
|
218
|
+
readonly field?: string;
|
|
219
|
+
readonly line?: string;
|
|
220
|
+
readonly limit?: number;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export class SSEEncodeError extends SSEError {
|
|
224
|
+
constructor(message: string, field: string, cause?: any);
|
|
225
|
+
readonly field: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export class SSEHTTPError extends SSEError {
|
|
229
|
+
constructor(message: string, response: SSEResponseLike, code?: string);
|
|
230
|
+
readonly response: SSEResponseLike;
|
|
231
|
+
readonly status: number;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export class SSETimeoutError extends SSEError {
|
|
235
|
+
constructor(phase: string, timeout: number, cause?: any);
|
|
236
|
+
readonly phase: string;
|
|
237
|
+
readonly timeout: number;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export class SSERetryError extends SSEError {
|
|
241
|
+
constructor(attempts: number, cause?: any);
|
|
242
|
+
readonly attempts: number;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export class SSEReplayError extends SSEError {}
|
|
246
|
+
|
|
247
|
+
export const EVENT_STREAM_CONTENT_TYPE: 'text/event-stream';
|
|
248
|
+
export const EventStreamContentType: 'text/event-stream';
|
|
249
|
+
|
|
250
|
+
export function createParser(config?: SSEParserOptions | ((event: SSEEvent) => void)): SSEParser;
|
|
251
|
+
export function encodeSSE(message: SSEMessage, options?: SSEEncoderOptions): string;
|
|
252
|
+
export const encode: typeof encodeSSE;
|
|
253
|
+
export function encodeJSON(data: any, fields?: Omit<SSEMessage, 'data'>, options?: SSEEncoderOptions): string;
|
|
254
|
+
export function encodeComment(comment: string, options?: SSEEncoderOptions): string;
|
|
255
|
+
export function createEncoderStream(options?: SSEEncoderOptions): any;
|
|
256
|
+
export function decodeSSE(source: SSESource, options?: SSEDecodeOptions): AsyncGenerator<SSEEvent, void, unknown>;
|
|
257
|
+
export function decodeJSON<T = any>(source: SSESource, options?: SSEJSONDecodeOptions): AsyncGenerator<SSEEvent<T>, void, unknown>;
|
|
258
|
+
export function createDecoderStream(options?: SSEDecodeOptions): any;
|
|
259
|
+
export function toAsyncIterable(source: SSESource): AsyncIterable<SSEChunk>;
|
|
260
|
+
export function fetchSSE(input: any, options?: SSEFetchOptions): AsyncGenerator<SSEEvent, void, unknown>;
|
|
261
|
+
export function consumeSSE(input: any, options?: SSEFetchOptions): Promise<SSEConsumeResult>;
|
|
262
|
+
export function fetchEventSource(input: any, options?: FetchEventSourceOptions): Promise<SSEConsumeResult>;
|
|
263
|
+
export function createSSEChannel(options?: SSEChannelOptions): SSEChannel;
|
|
264
|
+
export function eventStreamResponse(source: Iterable<SSEMessage> | AsyncIterable<SSEMessage>, options?: SSEChannelOptions & { headers?: any; status?: number; statusText?: string; json?: boolean }): any;
|
|
265
|
+
|
|
266
|
+
declare const api: {
|
|
267
|
+
EVENT_STREAM_CONTENT_TYPE: typeof EVENT_STREAM_CONTENT_TYPE;
|
|
268
|
+
EventStreamContentType: typeof EventStreamContentType;
|
|
269
|
+
SSEEncodeError: typeof SSEEncodeError;
|
|
270
|
+
SSEError: typeof SSEError;
|
|
271
|
+
SSEHTTPError: typeof SSEHTTPError;
|
|
272
|
+
SSEParseError: typeof SSEParseError;
|
|
273
|
+
SSEReplayError: typeof SSEReplayError;
|
|
274
|
+
SSERetryError: typeof SSERetryError;
|
|
275
|
+
SSETimeoutError: typeof SSETimeoutError;
|
|
276
|
+
consumeSSE: typeof consumeSSE;
|
|
277
|
+
createDecoderStream: typeof createDecoderStream;
|
|
278
|
+
createEncoderStream: typeof createEncoderStream;
|
|
279
|
+
createParser: typeof createParser;
|
|
280
|
+
createSSEChannel: typeof createSSEChannel;
|
|
281
|
+
decodeJSON: typeof decodeJSON;
|
|
282
|
+
decodeSSE: typeof decodeSSE;
|
|
283
|
+
encode: typeof encode;
|
|
284
|
+
encodeComment: typeof encodeComment;
|
|
285
|
+
encodeJSON: typeof encodeJSON;
|
|
286
|
+
encodeSSE: typeof encodeSSE;
|
|
287
|
+
eventStreamResponse: typeof eventStreamResponse;
|
|
288
|
+
fetchEventSource: typeof fetchEventSource;
|
|
289
|
+
fetchSSE: typeof fetchSSE;
|
|
290
|
+
toAsyncIterable: typeof toAsyncIterable;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
export default api;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
export type SSEChunk = string | Uint8Array;
|
|
2
|
+
|
|
3
|
+
export interface SSEEvent<T = string> {
|
|
4
|
+
data: T;
|
|
5
|
+
event?: string;
|
|
6
|
+
id?: string;
|
|
7
|
+
lastEventId: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface SSEMessage {
|
|
11
|
+
comment?: string | number | boolean | bigint;
|
|
12
|
+
data?: string | number | boolean | bigint;
|
|
13
|
+
event?: string | number | boolean | bigint;
|
|
14
|
+
id?: string | number | boolean | bigint;
|
|
15
|
+
retry?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface SSEParserState {
|
|
19
|
+
readonly buffered: number;
|
|
20
|
+
readonly bytes: number;
|
|
21
|
+
readonly characters: number;
|
|
22
|
+
readonly comments: number;
|
|
23
|
+
readonly events: number;
|
|
24
|
+
readonly lastEventId: string;
|
|
25
|
+
readonly retries: number;
|
|
26
|
+
readonly terminated: boolean;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface SSEParserOptions {
|
|
30
|
+
fatalUTF8?: boolean;
|
|
31
|
+
lastEventId?: string;
|
|
32
|
+
maxBufferSize?: number;
|
|
33
|
+
maxEventSize?: number;
|
|
34
|
+
maxLineLength?: number;
|
|
35
|
+
strict?: boolean;
|
|
36
|
+
onComment?(comment: string): void;
|
|
37
|
+
onError?(error: SSEParseError): void;
|
|
38
|
+
onEvent?(event: SSEEvent): void;
|
|
39
|
+
onId?(lastEventId: string): void;
|
|
40
|
+
onRetry?(milliseconds: number): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SSEParser {
|
|
44
|
+
readonly state: SSEParserState;
|
|
45
|
+
feed(chunk: SSEChunk): SSEParser;
|
|
46
|
+
end(): SSEParser;
|
|
47
|
+
reset(options?: { consume?: boolean; preserveLastEventId?: boolean }): SSEParser;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SSEEncoderOptions {
|
|
51
|
+
bytes?: boolean;
|
|
52
|
+
json?: boolean;
|
|
53
|
+
newline?: '\n' | '\r\n';
|
|
54
|
+
replacer?: (this: any, key: string, value: any) => any;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface AsyncIteratorLike<T> {
|
|
58
|
+
next(): Promise<IteratorResult<T>>;
|
|
59
|
+
return?(value?: any): Promise<IteratorResult<T>>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface AsyncIterableLike<T> {
|
|
63
|
+
[Symbol.asyncIterator](): AsyncIterator<T>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface ReadableStreamReaderLike<T> {
|
|
67
|
+
read(): Promise<IteratorResult<T>>;
|
|
68
|
+
cancel?(reason?: any): Promise<void>;
|
|
69
|
+
releaseLock?(): void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export interface ReadableStreamLike<T> {
|
|
73
|
+
getReader(): ReadableStreamReaderLike<T>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export interface SSEHeadersLike {
|
|
77
|
+
get(name: string): string | null;
|
|
78
|
+
forEach?(callback: (value: string, key: string) => void): void;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface SSEResponseLike {
|
|
82
|
+
readonly body: ReadableStreamLike<Uint8Array> | AsyncIterableLike<Uint8Array> | null;
|
|
83
|
+
readonly headers: SSEHeadersLike;
|
|
84
|
+
readonly ok?: boolean;
|
|
85
|
+
readonly redirected?: boolean;
|
|
86
|
+
readonly status: number;
|
|
87
|
+
readonly statusText?: string;
|
|
88
|
+
readonly url?: string;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type SSESource =
|
|
92
|
+
| { body: ReadableStreamLike<SSEChunk> | AsyncIterableLike<SSEChunk> | null }
|
|
93
|
+
| ReadableStreamLike<SSEChunk>
|
|
94
|
+
| AsyncIterable<SSEChunk>
|
|
95
|
+
| Iterable<SSEChunk>;
|
|
96
|
+
|
|
97
|
+
export interface SSEDecodeOptions extends SSEParserOptions {
|
|
98
|
+
feedSize?: number;
|
|
99
|
+
maxQueuedEvents?: number;
|
|
100
|
+
parser?: SSEParserOptions;
|
|
101
|
+
signal?: AbortSignalLike;
|
|
102
|
+
onChunk?(chunk: SSEChunk): void;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export interface SSEJSONDecodeOptions extends SSEDecodeOptions {
|
|
106
|
+
doneSentinel?: string | false;
|
|
107
|
+
ignoreInvalidJSON?: boolean;
|
|
108
|
+
reviver?: (this: any, key: string, value: any) => any;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export interface AbortSignalLike {
|
|
112
|
+
readonly aborted: boolean;
|
|
113
|
+
readonly reason?: any;
|
|
114
|
+
addEventListener(type: 'abort', listener: () => void, options?: any): void;
|
|
115
|
+
removeEventListener(type: 'abort', listener: () => void): void;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type FetchLike = (input: any, init?: any) => Promise<SSEResponseLike>;
|
|
119
|
+
|
|
120
|
+
export interface SSERetryOptions {
|
|
121
|
+
factor?: number;
|
|
122
|
+
jitter?: 'full' | 'none' | false | ((maximumDelay: number) => number);
|
|
123
|
+
maxDelay?: number;
|
|
124
|
+
minDelay?: number;
|
|
125
|
+
retries?: number;
|
|
126
|
+
statusCodes?: Iterable<number>;
|
|
127
|
+
shouldRetry?(context: { error: any; response?: SSEResponseLike }): boolean;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export interface SSEClientContext {
|
|
131
|
+
attempts: number;
|
|
132
|
+
lastEventId: string;
|
|
133
|
+
reconnects: number;
|
|
134
|
+
response?: SSEResponseLike;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export interface SSERetryContext {
|
|
138
|
+
attempt: number;
|
|
139
|
+
delay: number;
|
|
140
|
+
error?: any;
|
|
141
|
+
lastEventId: string;
|
|
142
|
+
reconnects: number;
|
|
143
|
+
response?: SSEResponseLike;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export interface SSEClientErrorContext {
|
|
147
|
+
attempt: number;
|
|
148
|
+
error: any;
|
|
149
|
+
lastEventId: string;
|
|
150
|
+
reconnects: number;
|
|
151
|
+
response?: SSEResponseLike;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export interface SSEFetchOptions {
|
|
155
|
+
body?: any;
|
|
156
|
+
bodyFactory?(context: { attempt: number; lastEventId: string; signal: AbortSignalLike }): any | Promise<any>;
|
|
157
|
+
cache?: string;
|
|
158
|
+
connectTimeout?: number;
|
|
159
|
+
decode?: SSEDecodeOptions;
|
|
160
|
+
fetch?: FetchLike;
|
|
161
|
+
headers?: any;
|
|
162
|
+
idleTimeout?: number;
|
|
163
|
+
lastEventId?: string;
|
|
164
|
+
method?: string;
|
|
165
|
+
parser?: SSEParserOptions;
|
|
166
|
+
retry?: false | number | SSERetryOptions;
|
|
167
|
+
signal?: AbortSignalLike;
|
|
168
|
+
totalTimeout?: number;
|
|
169
|
+
onClose?(context: SSEClientContext & { reason: string }): void | Promise<void>;
|
|
170
|
+
onEvent?(event: SSEEvent, context: SSEClientContext): void | Promise<void>;
|
|
171
|
+
onError?(context: SSEClientErrorContext): void | false | number | Promise<void | false | number>;
|
|
172
|
+
onOpen?(response: SSEResponseLike, context: SSEClientContext): void | boolean | Promise<void | boolean>;
|
|
173
|
+
onRetry?(context: SSERetryContext): void | false | number | Promise<void | false | number>;
|
|
174
|
+
[key: string]: any;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface FetchEventSourceOptions extends SSEFetchOptions {
|
|
178
|
+
onclose?(context?: any): void | Promise<void>;
|
|
179
|
+
onerror?(error: any): any;
|
|
180
|
+
onmessage?(event: SSEEvent): void | Promise<void>;
|
|
181
|
+
onopen?(response: SSEResponseLike): void | boolean | Promise<void | boolean>;
|
|
182
|
+
openWhenHidden?: boolean;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export interface SSEConsumeResult {
|
|
186
|
+
events: number;
|
|
187
|
+
lastEventId: string;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface SSEChannel {
|
|
191
|
+
readonly stream: ReadableStreamLike<Uint8Array>;
|
|
192
|
+
readonly closed: Promise<any>;
|
|
193
|
+
readonly open: boolean;
|
|
194
|
+
readonly ready: Promise<void>;
|
|
195
|
+
close(): void;
|
|
196
|
+
comment(value: string): boolean;
|
|
197
|
+
error(reason: any): void;
|
|
198
|
+
send(message: SSEMessage): boolean;
|
|
199
|
+
sendJSON(data: any, fields?: Omit<SSEMessage, 'data'>): boolean;
|
|
200
|
+
toResponse(init?: any): any;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export interface SSEChannelOptions extends SSEEncoderOptions {
|
|
204
|
+
heartbeatComment?: string;
|
|
205
|
+
heartbeatInterval?: number;
|
|
206
|
+
highWaterMark?: number;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export class SSEError extends Error {
|
|
210
|
+
constructor(message: string, code: string, details?: { cause?: any });
|
|
211
|
+
readonly code: string;
|
|
212
|
+
readonly cause?: any;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export class SSEParseError extends SSEError {
|
|
216
|
+
constructor(message: string, details?: any);
|
|
217
|
+
readonly fatal: boolean;
|
|
218
|
+
readonly field?: string;
|
|
219
|
+
readonly line?: string;
|
|
220
|
+
readonly limit?: number;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export class SSEEncodeError extends SSEError {
|
|
224
|
+
constructor(message: string, field: string, cause?: any);
|
|
225
|
+
readonly field: string;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export class SSEHTTPError extends SSEError {
|
|
229
|
+
constructor(message: string, response: SSEResponseLike, code?: string);
|
|
230
|
+
readonly response: SSEResponseLike;
|
|
231
|
+
readonly status: number;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export class SSETimeoutError extends SSEError {
|
|
235
|
+
constructor(phase: string, timeout: number, cause?: any);
|
|
236
|
+
readonly phase: string;
|
|
237
|
+
readonly timeout: number;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export class SSERetryError extends SSEError {
|
|
241
|
+
constructor(attempts: number, cause?: any);
|
|
242
|
+
readonly attempts: number;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
export class SSEReplayError extends SSEError {}
|
|
246
|
+
|
|
247
|
+
export const EVENT_STREAM_CONTENT_TYPE: 'text/event-stream';
|
|
248
|
+
export const EventStreamContentType: 'text/event-stream';
|
|
249
|
+
|
|
250
|
+
export function createParser(config?: SSEParserOptions | ((event: SSEEvent) => void)): SSEParser;
|
|
251
|
+
export function encodeSSE(message: SSEMessage, options?: SSEEncoderOptions): string;
|
|
252
|
+
export const encode: typeof encodeSSE;
|
|
253
|
+
export function encodeJSON(data: any, fields?: Omit<SSEMessage, 'data'>, options?: SSEEncoderOptions): string;
|
|
254
|
+
export function encodeComment(comment: string, options?: SSEEncoderOptions): string;
|
|
255
|
+
export function createEncoderStream(options?: SSEEncoderOptions): any;
|
|
256
|
+
export function decodeSSE(source: SSESource, options?: SSEDecodeOptions): AsyncGenerator<SSEEvent, void, unknown>;
|
|
257
|
+
export function decodeJSON<T = any>(source: SSESource, options?: SSEJSONDecodeOptions): AsyncGenerator<SSEEvent<T>, void, unknown>;
|
|
258
|
+
export function createDecoderStream(options?: SSEDecodeOptions): any;
|
|
259
|
+
export function toAsyncIterable(source: SSESource): AsyncIterable<SSEChunk>;
|
|
260
|
+
export function fetchSSE(input: any, options?: SSEFetchOptions): AsyncGenerator<SSEEvent, void, unknown>;
|
|
261
|
+
export function consumeSSE(input: any, options?: SSEFetchOptions): Promise<SSEConsumeResult>;
|
|
262
|
+
export function fetchEventSource(input: any, options?: FetchEventSourceOptions): Promise<SSEConsumeResult>;
|
|
263
|
+
export function createSSEChannel(options?: SSEChannelOptions): SSEChannel;
|
|
264
|
+
export function eventStreamResponse(source: Iterable<SSEMessage> | AsyncIterable<SSEMessage>, options?: SSEChannelOptions & { headers?: any; status?: number; statusText?: string; json?: boolean }): any;
|
|
265
|
+
|
|
266
|
+
declare const api: {
|
|
267
|
+
EVENT_STREAM_CONTENT_TYPE: typeof EVENT_STREAM_CONTENT_TYPE;
|
|
268
|
+
EventStreamContentType: typeof EventStreamContentType;
|
|
269
|
+
SSEEncodeError: typeof SSEEncodeError;
|
|
270
|
+
SSEError: typeof SSEError;
|
|
271
|
+
SSEHTTPError: typeof SSEHTTPError;
|
|
272
|
+
SSEParseError: typeof SSEParseError;
|
|
273
|
+
SSEReplayError: typeof SSEReplayError;
|
|
274
|
+
SSERetryError: typeof SSERetryError;
|
|
275
|
+
SSETimeoutError: typeof SSETimeoutError;
|
|
276
|
+
consumeSSE: typeof consumeSSE;
|
|
277
|
+
createDecoderStream: typeof createDecoderStream;
|
|
278
|
+
createEncoderStream: typeof createEncoderStream;
|
|
279
|
+
createParser: typeof createParser;
|
|
280
|
+
createSSEChannel: typeof createSSEChannel;
|
|
281
|
+
decodeJSON: typeof decodeJSON;
|
|
282
|
+
decodeSSE: typeof decodeSSE;
|
|
283
|
+
encode: typeof encode;
|
|
284
|
+
encodeComment: typeof encodeComment;
|
|
285
|
+
encodeJSON: typeof encodeJSON;
|
|
286
|
+
encodeSSE: typeof encodeSSE;
|
|
287
|
+
eventStreamResponse: typeof eventStreamResponse;
|
|
288
|
+
fetchEventSource: typeof fetchEventSource;
|
|
289
|
+
fetchSSE: typeof fetchSSE;
|
|
290
|
+
toAsyncIterable: typeof toAsyncIterable;
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
export default api;
|