better-sse 0.14.1 → 0.15.1

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/build/index.d.ts CHANGED
@@ -1,9 +1,7 @@
1
- import * as stream from 'stream';
2
- import * as http from 'http';
3
- import { OutgoingHttpHeaders, IncomingMessage, ServerResponse } from 'http';
4
- import * as http2 from 'http2';
5
- import { Http2ServerRequest, Http2ServerResponse } from 'http2';
6
- import { EventEmitter } from 'events';
1
+ import { IncomingMessage, ServerResponse } from 'node:http';
2
+ import { Http2ServerRequest, Http2ServerResponse } from 'node:http2';
3
+ import { Readable } from 'node:stream';
4
+ import { EventEmitter } from 'node:events';
7
5
 
8
6
  interface IterateOptions {
9
7
  /**
@@ -13,7 +11,9 @@ interface IterateOptions {
13
11
  */
14
12
  eventName?: string;
15
13
  }
14
+ type PushFromIterable = (iterable: Iterable<unknown> | AsyncIterable<unknown>, options?: IterateOptions) => Promise<void>;
16
15
 
16
+ type WebReadableStream = ReadableStream;
17
17
  interface StreamOptions {
18
18
  /**
19
19
  * Event name/type to be emitted when stream data is sent to the client.
@@ -22,17 +22,14 @@ interface StreamOptions {
22
22
  */
23
23
  eventName?: string;
24
24
  }
25
+ type PushFromStream = (stream: Readable | WebReadableStream, options?: StreamOptions) => Promise<boolean>;
26
+
27
+ type SanitizerFunction = (text: string) => string;
25
28
 
26
29
  /**
27
30
  * Serialize arbitrary data to a string that can be sent over the wire to the client.
28
31
  */
29
- interface SerializerFunction {
30
- (data: unknown): string;
31
- }
32
-
33
- interface SanitizerFunction {
34
- (text: string): string;
35
- }
32
+ type SerializerFunction = (data: unknown) => string;
36
33
 
37
34
  interface EventBufferOptions {
38
35
  /**
@@ -124,9 +121,9 @@ declare class EventBuffer {
124
121
  * @param stream - Readable stream to consume data from.
125
122
  * @param options - Event name to use for each event created.
126
123
  *
127
- * @returns A promise that resolves or rejects based on the success of the stream write finishing.
124
+ * @returns A promise that resolves with `true` or rejects based on the success of the stream write finishing.
128
125
  */
129
- stream: (stream: stream.Readable, options?: StreamOptions) => Promise<boolean>;
126
+ stream: PushFromStream;
130
127
  /**
131
128
  * Iterate over an iterable and write yielded values as events into the buffer.
132
129
  *
@@ -138,7 +135,7 @@ declare class EventBuffer {
138
135
  *
139
136
  * @returns A promise that resolves once all data has been successfully yielded from the iterable.
140
137
  */
141
- iterate: <DataType = unknown>(iterable: Iterable<DataType> | AsyncIterable<DataType>, options?: IterateOptions) => Promise<void>;
138
+ iterate: PushFromIterable;
142
139
  /**
143
140
  * Clear the contents of the buffer.
144
141
  */
@@ -218,7 +215,7 @@ interface SessionOptions<State = DefaultSessionState> extends Pick<EventBufferOp
218
215
  /**
219
216
  * Additional headers to be sent along with the response.
220
217
  */
221
- headers?: OutgoingHttpHeaders;
218
+ headers?: Record<string, string | string[] | undefined>;
222
219
  /**
223
220
  * Custom state for this session.
224
221
  *
@@ -239,15 +236,19 @@ interface SessionEvents extends EventMap {
239
236
  *
240
237
  * It extends from the {@link https://nodejs.org/api/events.html#events_class_eventemitter | EventEmitter} class.
241
238
  *
242
- * It emits the `connected` event after it has connected and sent all headers to the client, and the
243
- * `disconnected` event after the connection has been closed.
239
+ * It emits the `connected` event after it has connected and sent the response head to the client.
240
+ * It emits the `disconnected` event after the connection has been closed.
241
+ *
242
+ * When using the Fetch API, the session is considered connected only once the `ReadableStream` contained in the body
243
+ * of the `Response` returned by `getResponse` has began being consumed.
244
244
  *
245
- * Note that creating a new session will immediately send the initial status code and headers to the client.
246
- * Attempting to write additional headers after you have created a new session will result in an error.
245
+ * When using the Node HTTP APIs, the session will send the response with status code, headers and other preamble data ahead of time,
246
+ * allowing the session to connect and start pushing events immediately. As such, keep in mind that attempting
247
+ * to write additional headers after the session has been created will result in an error being thrown.
247
248
  *
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 - Options given to the session instance.
249
+ * @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.
250
+ * @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.
251
+ * @param options - Optional additional configuration for the session.
251
252
  */
252
253
  declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionEvents> {
253
254
  /**
@@ -276,57 +277,45 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
276
277
  */
277
278
  state: State;
278
279
  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;
280
+ private connection;
289
281
  private sanitize;
290
- private trustClientEventId;
282
+ private serialize;
291
283
  private initialRetry;
292
284
  private keepAliveInterval;
293
285
  private keepAliveTimer?;
294
- private statusCode;
295
- private headers;
296
- constructor(req: IncomingMessage | Http2ServerRequest, res: ServerResponse | Http2ServerResponse, options?: SessionOptions<State>);
286
+ constructor(req: IncomingMessage, res: ServerResponse, options?: SessionOptions<State>);
287
+ constructor(req: Http2ServerRequest, res: Http2ServerResponse, options?: SessionOptions<State>);
288
+ constructor(req: Request, res?: Response, options?: SessionOptions<State>);
289
+ constructor(req: Request, options?: SessionOptions<State>);
297
290
  private initialize;
298
291
  private onDisconnected;
299
- private keepAlive;
300
- /**
301
- * @deprecated see https://github.com/MatthewWid/better-sse/issues/52
302
- */
303
- event(type: string): this;
304
292
  /**
305
- * @deprecated see https://github.com/MatthewWid/better-sse/issues/52
293
+ * Write an empty comment and flush it to the client.
306
294
  */
307
- data: (data: unknown) => this;
308
- /**
309
- * @deprecated see https://github.com/MatthewWid/better-sse/issues/52
310
- */
311
- id: (id?: string) => this;
295
+ private keepAlive;
312
296
  /**
313
- * @deprecated see https://github.com/MatthewWid/better-sse/issues/52
297
+ * Flush the contents of the internal buffer to the client and clear the buffer.
314
298
  */
315
- retry: (time: number) => this;
299
+ private flush;
316
300
  /**
317
- * @deprecated see https://github.com/MatthewWid/better-sse/issues/52
318
- */
319
- comment: (text?: string) => this;
320
- /**
321
- * @deprecated see https://github.com/MatthewWid/better-sse/issues/52
301
+ * Get a Request object representing the request of the underlying connection this session manages.
302
+ *
303
+ * When using the Fetch API, this will be the original Request object passed to the session constructor.
304
+ *
305
+ * When using the Node HTTP APIs, this will be a new Request object with status code and headers copied from the original request.
306
+ * When the originally given request or response is closed, the abort signal attached to this Request will be triggered.
322
307
  */
323
- dispatch: () => this;
308
+ getRequest: () => Request;
324
309
  /**
325
- * Flush the contents of the internal buffer to the client and clear the buffer.
310
+ * Get a Response object representing the response of the underlying connection this session manages.
311
+ *
312
+ * When using the Fetch API, this will be a new Response object with status code and headers copied from the original response if given.
313
+ * Its body will be a ReadableStream that should begin being consumed for the session to consider itself connected.
326
314
  *
327
- * @deprecated see https://github.com/MatthewWid/better-sse/issues/52
315
+ * When using the Node HTTP APIs, this will be a new Response object with status code and headers copied from the original response.
316
+ * Its body will be `null`, as data is instead written to the stream of the originally given response object.
328
317
  */
329
- flush: () => this;
318
+ getResponse: () => Response;
330
319
  /**
331
320
  * Push an event to the client.
332
321
  *
@@ -334,6 +323,8 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
334
323
  *
335
324
  * 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
325
  *
326
+ * If the session has disconnected, an `SseError` will be thrown.
327
+ *
337
328
  * Emits the `push` event with the given data, event name and event ID in that order.
338
329
  *
339
330
  * @param data - Data to write.
@@ -351,9 +342,9 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
351
342
  * @param stream - Readable stream to consume data from.
352
343
  * @param options - Options to alter how the stream is flushed to the client.
353
344
  *
354
- * @returns A promise that resolves or rejects based on the success of the stream write finishing.
345
+ * @returns A promise that resolves with `true` or rejects based on the success of the stream write finishing.
355
346
  */
356
- stream: (stream: stream.Readable, options?: StreamOptions) => Promise<boolean>;
347
+ stream: PushFromStream;
357
348
  /**
358
349
  * Iterate over an iterable and send yielded values as events to the client.
359
350
  *
@@ -365,7 +356,7 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
365
356
  *
366
357
  * @returns A promise that resolves once all data has been successfully yielded from the iterable.
367
358
  */
368
- iterate: <DataType = unknown>(iterable: Iterable<DataType> | AsyncIterable<DataType>, options?: IterateOptions) => Promise<void>;
359
+ iterate: PushFromIterable;
369
360
  /**
370
361
  * Batch and send multiple events at once.
371
362
  *
@@ -384,9 +375,30 @@ declare class Session<State = DefaultSessionState> extends TypedEmitter<SessionE
384
375
  }
385
376
 
386
377
  /**
387
- * Create a new session and return the session instance once it has connected.
378
+ * Create a new session.
379
+ *
380
+ * When using the Fetch API, resolves immediately with a session instance before it has connected.
381
+ * You can listen for the `connected` event on the session to know when it has connected, or
382
+ * otherwise use the shorthand `createResponse` function that does so for you instead.
383
+ *
384
+ * When using the Node HTTP APIs, waits for the session to connect before resolving with its instance.
385
+ */
386
+ declare function createSession<State = DefaultSessionState>(req: IncomingMessage, res: ServerResponse, options?: SessionOptions<State>): Promise<Session<State>>;
387
+ declare function createSession<State = DefaultSessionState>(req: Http2ServerRequest, res: Http2ServerResponse, options?: SessionOptions<State>): Promise<Session<State>>;
388
+ declare function createSession<State = DefaultSessionState>(req: Request, res?: Response, options?: SessionOptions<State>): Promise<Session<State>>;
389
+ declare function createSession<State = DefaultSessionState>(req: Request, options?: SessionOptions<State>): Promise<Session<State>>;
390
+
391
+ type CreateResponseCallback<State> = (session: Session<State>) => void;
392
+ /**
393
+ * Create a new session using the Fetch API and return its corresponding `Response` object.
394
+ *
395
+ * The last argument should be a callback function that will be invoked with
396
+ * the session instance once it has connected.
388
397
  */
389
- declare const createSession: <State = DefaultSessionState>(req: http.IncomingMessage | http2.Http2ServerRequest, res: http.ServerResponse<http.IncomingMessage> | http2.Http2ServerResponse<http2.Http2ServerRequest>, options?: SessionOptions<State> | undefined) => Promise<Session<State>>;
398
+ declare function createResponse<State = DefaultSessionState>(request: Request, callback: CreateResponseCallback<State>): Response;
399
+ declare function createResponse<State = DefaultSessionState>(request: Request, response: Response, callback: CreateResponseCallback<State>): Response;
400
+ declare function createResponse<State = DefaultSessionState>(request: Request, options: SessionOptions<State>, callback: CreateResponseCallback<State>): Response;
401
+ declare function createResponse<State = DefaultSessionState>(request: Request, response: Response, options: SessionOptions<State>, callback: CreateResponseCallback<State>): Response;
390
402
 
391
403
  interface ChannelOptions<State = DefaultChannelState> {
392
404
  /**
@@ -482,4 +494,4 @@ declare class SseError extends Error {
482
494
  constructor(message: string);
483
495
  }
484
496
 
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 };
497
+ 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 };