better-sse 0.14.1 → 0.15.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/LICENSE +1 -1
- package/README.md +78 -41
- package/build/index.d.mts +70 -61
- package/build/index.d.ts +70 -61
- package/build/index.js +356 -159
- package/build/index.mjs +353 -156
- package/package.json +6 -10
package/build/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import * as stream from 'stream';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import { Http2ServerRequest, Http2ServerResponse } from 'http2';
|
|
6
|
-
import { EventEmitter } from 'events';
|
|
2
|
+
import { IncomingMessage, ServerResponse } from 'node:http';
|
|
3
|
+
import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
|
|
4
|
+
import { EventEmitter } from 'node:events';
|
|
7
5
|
|
|
8
6
|
interface IterateOptions {
|
|
9
7
|
/**
|
|
@@ -23,16 +21,12 @@ interface StreamOptions {
|
|
|
23
21
|
eventName?: string;
|
|
24
22
|
}
|
|
25
23
|
|
|
24
|
+
type SanitizerFunction = (text: string) => string;
|
|
25
|
+
|
|
26
26
|
/**
|
|
27
27
|
* Serialize arbitrary data to a string that can be sent over the wire to the client.
|
|
28
28
|
*/
|
|
29
|
-
|
|
30
|
-
(data: unknown): string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
interface SanitizerFunction {
|
|
34
|
-
(text: string): string;
|
|
35
|
-
}
|
|
29
|
+
type SerializerFunction = (data: unknown) => string;
|
|
36
30
|
|
|
37
31
|
interface EventBufferOptions {
|
|
38
32
|
/**
|
|
@@ -126,7 +120,7 @@ declare class EventBuffer {
|
|
|
126
120
|
*
|
|
127
121
|
* @returns A promise that resolves or rejects based on the success of the stream write finishing.
|
|
128
122
|
*/
|
|
129
|
-
stream: (stream: stream.Readable
|
|
123
|
+
stream: (stream: stream.Readable | ReadableStream<any>, options?: StreamOptions) => Promise<boolean>;
|
|
130
124
|
/**
|
|
131
125
|
* Iterate over an iterable and write yielded values as events into the buffer.
|
|
132
126
|
*
|
|
@@ -218,7 +212,7 @@ interface SessionOptions<State = DefaultSessionState> extends Pick<EventBufferOp
|
|
|
218
212
|
/**
|
|
219
213
|
* Additional headers to be sent along with the response.
|
|
220
214
|
*/
|
|
221
|
-
headers?:
|
|
215
|
+
headers?: Record<string, string | string[] | undefined>;
|
|
222
216
|
/**
|
|
223
217
|
* Custom state for this session.
|
|
224
218
|
*
|
|
@@ -239,15 +233,19 @@ interface SessionEvents extends EventMap {
|
|
|
239
233
|
*
|
|
240
234
|
* It extends from the {@link https://nodejs.org/api/events.html#events_class_eventemitter | EventEmitter} class.
|
|
241
235
|
*
|
|
242
|
-
* It emits the `connected` event after it has connected and sent
|
|
243
|
-
* `disconnected` event after the connection has been closed.
|
|
236
|
+
* It emits the `connected` event after it has connected and sent the response head to the client.
|
|
237
|
+
* It emits the `disconnected` event after the connection has been closed.
|
|
238
|
+
*
|
|
239
|
+
* When using the Fetch API, the session is considered connected only once the `ReadableStream` contained in the body
|
|
240
|
+
* of the `Response` returned by `getResponse` has began being consumed.
|
|
244
241
|
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
242
|
+
* When using the Node HTTP APIs, the session will send the response with status code, headers and other preamble data ahead of time,
|
|
243
|
+
* allowing the session to connect and start pushing events immediately. As such, keep in mind that attempting
|
|
244
|
+
* to write additional headers after the session has been created will result in an error being thrown.
|
|
247
245
|
*
|
|
248
|
-
* @param req - The Node HTTP {@link https://nodejs.org/api/http.html#http_class_http_incomingmessage | ServerResponse} object.
|
|
249
|
-
* @param res - The Node HTTP {@link https://nodejs.org/api/http.html#http_class_http_serverresponse | IncomingMessage} object.
|
|
250
|
-
* @param options -
|
|
246
|
+
* @param req - The Node HTTP/1 {@link https://nodejs.org/api/http.html#http_class_http_incomingmessage | ServerResponse}, HTTP/2 {@link https://nodejs.org/api/http2.html#class-http2http2serverrequest | Http2ServerRequest} or the Fetch API {@link https://developer.mozilla.org/en-US/docs/Web/API/Request | Request} object.
|
|
247
|
+
* @param res - The Node HTTP {@link https://nodejs.org/api/http.html#http_class_http_serverresponse | IncomingMessage}, HTTP/2 {@link https://nodejs.org/api/http2.html#class-http2http2serverresponse | Http2ServerResponse} or the Fetch API {@link https://developer.mozilla.org/en-US/docs/Web/API/Response | Response} object. Optional if using the Fetch API.
|
|
248
|
+
* @param options - Optional additional configuration for the session.
|
|
251
249
|
*/
|
|
252
250
|
declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionEvents> {
|
|
253
251
|
/**
|
|
@@ -276,57 +274,45 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
|
|
|
276
274
|
*/
|
|
277
275
|
state: State;
|
|
278
276
|
private buffer;
|
|
279
|
-
|
|
280
|
-
* Raw HTTP request.
|
|
281
|
-
*/
|
|
282
|
-
private req;
|
|
283
|
-
/**
|
|
284
|
-
* Raw HTTP response that is the minimal interface needed and forms the
|
|
285
|
-
* intersection between the HTTP/1.1 and HTTP/2 server response interfaces.
|
|
286
|
-
*/
|
|
287
|
-
private res;
|
|
288
|
-
private serialize;
|
|
277
|
+
private connection;
|
|
289
278
|
private sanitize;
|
|
290
|
-
private
|
|
279
|
+
private serialize;
|
|
291
280
|
private initialRetry;
|
|
292
281
|
private keepAliveInterval;
|
|
293
282
|
private keepAliveTimer?;
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
constructor(req:
|
|
283
|
+
constructor(req: IncomingMessage, res: ServerResponse, options?: SessionOptions<State>);
|
|
284
|
+
constructor(req: Http2ServerRequest, res: Http2ServerResponse, options?: SessionOptions<State>);
|
|
285
|
+
constructor(req: Request, res?: Response, options?: SessionOptions<State>);
|
|
286
|
+
constructor(req: Request, options?: SessionOptions<State>);
|
|
297
287
|
private initialize;
|
|
298
288
|
private onDisconnected;
|
|
299
|
-
private keepAlive;
|
|
300
|
-
/**
|
|
301
|
-
* @deprecated see https://github.com/MatthewWid/better-sse/issues/52
|
|
302
|
-
*/
|
|
303
|
-
event(type: string): this;
|
|
304
289
|
/**
|
|
305
|
-
*
|
|
290
|
+
* Write an empty comment and flush it to the client.
|
|
306
291
|
*/
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* @deprecated see https://github.com/MatthewWid/better-sse/issues/52
|
|
310
|
-
*/
|
|
311
|
-
id: (id?: string) => this;
|
|
292
|
+
private keepAlive;
|
|
312
293
|
/**
|
|
313
|
-
*
|
|
294
|
+
* Flush the contents of the internal buffer to the client and clear the buffer.
|
|
314
295
|
*/
|
|
315
|
-
|
|
296
|
+
private flush;
|
|
316
297
|
/**
|
|
317
|
-
*
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
*
|
|
298
|
+
* Get a Request object representing the request of the underlying connection this session manages.
|
|
299
|
+
*
|
|
300
|
+
* When using the Fetch API, this will be the original Request object passed to the session constructor.
|
|
301
|
+
*
|
|
302
|
+
* When using the Node HTTP APIs, this will be a new Request object with status code and headers copied from the original request.
|
|
303
|
+
* When the originally given request or response is closed, the abort signal attached to this Request will be triggered.
|
|
322
304
|
*/
|
|
323
|
-
|
|
305
|
+
getRequest: () => Request;
|
|
324
306
|
/**
|
|
325
|
-
*
|
|
307
|
+
* Get a Response object representing the response of the underlying connection this session manages.
|
|
308
|
+
*
|
|
309
|
+
* When using the Fetch API, this will be a new Response object with status code and headers copied from the original response if given.
|
|
310
|
+
* Its body will be a ReadableStream that should begin being consumed for the session to consider itself connected.
|
|
326
311
|
*
|
|
327
|
-
*
|
|
312
|
+
* When using the Node HTTP APIs, this will be a new Response object with status code and headers copied from the original response.
|
|
313
|
+
* Its body will be `null`, as data is instead written to the stream of the originally given response object.
|
|
328
314
|
*/
|
|
329
|
-
|
|
315
|
+
getResponse: () => Response;
|
|
330
316
|
/**
|
|
331
317
|
* Push an event to the client.
|
|
332
318
|
*
|
|
@@ -334,6 +320,8 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
|
|
|
334
320
|
*
|
|
335
321
|
* If no event ID is given, the event ID (and thus the `lastId` property) is set to a unique string generated using a cryptographic pseudorandom number generator.
|
|
336
322
|
*
|
|
323
|
+
* If the session has disconnected, an `SseError` will be thrown.
|
|
324
|
+
*
|
|
337
325
|
* Emits the `push` event with the given data, event name and event ID in that order.
|
|
338
326
|
*
|
|
339
327
|
* @param data - Data to write.
|
|
@@ -353,7 +341,7 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
|
|
|
353
341
|
*
|
|
354
342
|
* @returns A promise that resolves or rejects based on the success of the stream write finishing.
|
|
355
343
|
*/
|
|
356
|
-
stream: (stream: stream.Readable
|
|
344
|
+
stream: (stream: stream.Readable | ReadableStream<any>, options?: StreamOptions) => Promise<boolean>;
|
|
357
345
|
/**
|
|
358
346
|
* Iterate over an iterable and send yielded values as events to the client.
|
|
359
347
|
*
|
|
@@ -384,9 +372,30 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
|
|
|
384
372
|
}
|
|
385
373
|
|
|
386
374
|
/**
|
|
387
|
-
* Create a new session
|
|
375
|
+
* Create a new session.
|
|
376
|
+
*
|
|
377
|
+
* When using the Fetch API, resolves immediately with a session instance before it has connected.
|
|
378
|
+
* You can listen for the `connected` event on the session to know when it has connected, or
|
|
379
|
+
* otherwise use the shorthand `createResponse` function that does so for you instead.
|
|
380
|
+
*
|
|
381
|
+
* When using the Node HTTP APIs, waits for the session to connect before resolving with its instance.
|
|
382
|
+
*/
|
|
383
|
+
declare function createSession<State = DefaultSessionState>(req: IncomingMessage, res: ServerResponse, options?: SessionOptions<State>): Promise<Session<State>>;
|
|
384
|
+
declare function createSession<State = DefaultSessionState>(req: Http2ServerRequest, res: Http2ServerResponse, options?: SessionOptions<State>): Promise<Session<State>>;
|
|
385
|
+
declare function createSession<State = DefaultSessionState>(req: Request, res?: Response, options?: SessionOptions<State>): Promise<Session<State>>;
|
|
386
|
+
declare function createSession<State = DefaultSessionState>(req: Request, options?: SessionOptions<State>): Promise<Session<State>>;
|
|
387
|
+
|
|
388
|
+
type CreateResponseCallback<State> = (session: Session<State>) => void;
|
|
389
|
+
/**
|
|
390
|
+
* Create a new session using the Fetch API and return its corresponding `Response` object.
|
|
391
|
+
*
|
|
392
|
+
* The last argument should be a callback function that will be invoked with
|
|
393
|
+
* the session instance once it has connected.
|
|
388
394
|
*/
|
|
389
|
-
declare
|
|
395
|
+
declare function createResponse<State = DefaultSessionState>(request: Request, callback: CreateResponseCallback<State>): Response;
|
|
396
|
+
declare function createResponse<State = DefaultSessionState>(request: Request, response: Response, callback: CreateResponseCallback<State>): Response;
|
|
397
|
+
declare function createResponse<State = DefaultSessionState>(request: Request, options: SessionOptions<State>, callback: CreateResponseCallback<State>): Response;
|
|
398
|
+
declare function createResponse<State = DefaultSessionState>(request: Request, response: Response, options: SessionOptions<State>, callback: CreateResponseCallback<State>): Response;
|
|
390
399
|
|
|
391
400
|
interface ChannelOptions<State = DefaultChannelState> {
|
|
392
401
|
/**
|
|
@@ -482,4 +491,4 @@ declare class SseError extends Error {
|
|
|
482
491
|
constructor(message: string);
|
|
483
492
|
}
|
|
484
493
|
|
|
485
|
-
export { type BroadcastOptions, Channel, type ChannelEvents, type ChannelOptions, type DefaultChannelState, type DefaultSessionState, EventBuffer, type EventBufferOptions, type IterateOptions, Session, type SessionEvents, type SessionOptions, SseError, type StreamOptions, createChannel, createEventBuffer, createSession };
|
|
494
|
+
export { type BroadcastOptions, Channel, type ChannelEvents, type ChannelOptions, type CreateResponseCallback, type DefaultChannelState, type DefaultSessionState, EventBuffer, type EventBufferOptions, type IterateOptions, Session, type SessionEvents, type SessionOptions, SseError, type StreamOptions, createChannel, createEventBuffer, createResponse, createSession };
|