better-sse 0.6.0 → 0.8.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 CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2020 Matthew
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Matthew
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,129 +1,177 @@
1
- # Better SSE
2
-
3
- <p>
4
- <img src="https://img.shields.io/npm/v/better-sse?color=blue&style=flat-square" />
5
- <img src="https://img.shields.io/npm/l/better-sse?color=green&style=flat-square" />
6
- <img src="https://img.shields.io/npm/dt/better-sse?color=grey&style=flat-square" />
7
- <a href="https://github.com/MatthewWid/better-sse"><img src="https://img.shields.io/github/stars/MatthewWid/better-sse?style=social" /></a>
8
- </p>
9
-
10
- A dead simple, dependency-less, spec-compliant server-side events implementation for Node, written in TypeScript.
11
-
12
- This package aims to be the easiest to use, most compliant and most streamlined solution to server-side events with Node that is framework agnostic and feature rich.
13
-
14
- Please consider starring the project [on GitHub ⭐](https://github.com/MatthewWid/better-sse).
15
-
16
- ## Why use Server-sent Events?
17
-
18
- Server-sent events (SSE) is a standardised protocol that allows web-servers to push data to clients without the need for alternative mechanisms such as pinging or long-polling.
19
-
20
- Using SSE can allow for significant savings in bandwidth and battery life on portable devices, and will work with your existing infrastructure as it operates directly over the HTTP protocol without the need for the connection upgrade that [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) require.
21
-
22
- Compared to WebSockets it has comparable performance and bandwidth usage, especially over HTTP/2, and natively includes event ID generation and automatic reconnection when clients are disconnected.
23
-
24
- * [Comparison: Server-sent Events vs WebSockets vs Polling](https://medium.com/dailyjs/a-comparison-between-websockets-server-sent-events-and-polling-7a27c98cb1e3)
25
- * [WHATWG standards section for server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html)
26
- * [MDN guide to server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)
27
-
28
- ## Highlights
29
-
30
- * Compatible with all popular Node HTTP frameworks ([http](https://nodejs.org/api/http.html), [Express](https://nodejs.org/api/http.html), [Koa](https://www.npmjs.com/package/koa), [Fastify](https://www.npmjs.com/package/fastify), etc.)
31
- * Fully written in TypeScript (+ ships with types directly).
32
- * [Thoroughly tested](./src/Session.test.ts) (+ 100% code coverage!).
33
- * [Comprehensively documented](./docs) with guides and API documentation.
34
- * [Channels](./docs/channels.md) allow you to broadcast events to many clients at once.
35
- * Configurable reconnection time, message serialization and data sanitization (but with good defaults).
36
- * Trust or ignore the client-given last event ID.
37
- * Automatically send keep-alive pings to keep connections open.
38
- * Add or override the response status code and headers.
39
- * Fine-grained control by either sending [individual fields](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#fields) of events or sending full events with simple helpers.
40
- * Pipe [streams](https://nodejs.org/api/stream.html#stream_readable_streams) and [iterables](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) directly from the server to the client as a stream of events.
41
- * Support for popular EventStream polyfills [`event-source-polyfill`](https://www.npmjs.com/package/event-source-polyfill) and [`eventsource-polyfill`](https://www.npmjs.com/package/eventsource-polyfill).
42
-
43
- [See a comparison with other Node SSE libraries in the documentation.](./docs/comparison.md)
44
-
45
- # Installation
46
-
47
- ```bash
48
- # npm
49
- npm install better-sse
50
-
51
- # Yarn
52
- yarn add better-sse
53
-
54
- # pnpm
55
- pnpm add better-sse
56
- ```
57
-
58
- _Better SSE ships with types built in. No need to install from `@types` for TypeScript users!_
59
-
60
- # Basic Usage
61
-
62
- The following example shows usage with [Express](http://expressjs.com/), but Better SSE works with any web-server framework (that uses the underlying Node [HTTP module](https://nodejs.org/api/http.html)).
63
-
64
- See the [Recipes](./docs/recipes.md) section of the documentation for use with other frameworks and libraries.
65
-
66
- ```javascript
67
- // Server
68
- import {createSession} from "better-sse";
69
-
70
- app.get("/sse", async (req, res) => {
71
- const session = await createSession(req, res);
72
-
73
- session.push("Hello world!");
74
- });
75
- ```
76
-
77
- ```javascript
78
- // Client
79
- const sse = new EventSource("/sse");
80
-
81
- sse.addEventListener("message", ({data}) => {
82
- console.log(data);
83
- });
84
- ```
85
-
86
- Check [the API documentation](./docs/api.md) and [live examples](https://github.com/MatthewWid/better-sse/tree/master/examples) for information on getting more fine-tuned control over your data such as managing event IDs, data serialization, streams, dispatch controls and more!
87
-
88
- # Documentation
89
-
90
- API documentation, getting started guides and usage with other frameworks is [available on GitHub](https://github.com/MatthewWid/better-sse/tree/master/docs).
91
-
92
- # Contributing
93
-
94
- This library is always open to contributions, whether it be code, bug reports, documentation or anything else.
95
-
96
- Please submit suggestions, bugs and issues to the [GitHub issues page](https://github.com/MatthewWid/better-sse/issues).
97
-
98
- For code or documentation changes, [submit a pull request on GitHub](https://github.com/MatthewWid/better-sse/pulls).
99
-
100
- ## Local Development
101
-
102
- Install Node:
103
-
104
- ```bash
105
- curl -L https://git.io/n-install | bash
106
- n auto
107
- ```
108
-
109
- Install pnpm:
110
-
111
- ```bash
112
- npm i -g pnpm
113
- ```
114
-
115
- Install dependencies:
116
-
117
- ```bash
118
- pnpm i
119
- ```
120
-
121
- Run tests:
122
-
123
- ```bash
124
- pnpm t
125
- ```
126
-
127
- # License
128
-
129
- This project is licensed under the MIT license.
1
+ # Better SSE
2
+
3
+ <p>
4
+ <img src="https://img.shields.io/npm/v/better-sse?color=blue&style=flat-square" />
5
+ <img src="https://img.shields.io/npm/l/better-sse?color=green&style=flat-square" />
6
+ <img src="https://img.shields.io/npm/dt/better-sse?color=grey&style=flat-square" />
7
+ <a href="https://github.com/MatthewWid/better-sse"><img src="https://img.shields.io/github/stars/MatthewWid/better-sse?style=social" /></a>
8
+ </p>
9
+
10
+ A dead simple, dependency-less, spec-compliant server-side events implementation for Node, written in TypeScript.
11
+
12
+ This package aims to be the easiest to use, most compliant and most streamlined solution to server-side events with Node that is framework agnostic and feature rich.
13
+
14
+ Please consider starring the project [on GitHub ⭐](https://github.com/MatthewWid/better-sse).
15
+
16
+ ## Why use Server-sent Events?
17
+
18
+ Server-sent events (SSE) is a standardised protocol that allows web-servers to push data to clients without the need for alternative mechanisms such as pinging or long-polling.
19
+
20
+ Using SSE can allow for significant savings in bandwidth and battery life on portable devices, and will work with your existing infrastructure as it operates directly over the HTTP protocol without the need for the connection upgrade that [WebSockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API) require.
21
+
22
+ Compared to WebSockets it has comparable performance and bandwidth usage, especially over HTTP/2, and natively includes event ID generation and automatic reconnection when clients are disconnected.
23
+
24
+ * [Comparison: Server-sent Events vs WebSockets vs Polling](https://medium.com/dailyjs/a-comparison-between-websockets-server-sent-events-and-polling-7a27c98cb1e3)
25
+ * [WHATWG standards section for server-sent events](https://html.spec.whatwg.org/multipage/server-sent-events.html)
26
+ * [MDN guide to server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events)
27
+
28
+ ## Highlights
29
+
30
+ * Compatible with all popular Node HTTP frameworks ([http](https://nodejs.org/api/http.html), [Express](https://nodejs.org/api/http.html), [Koa](https://www.npmjs.com/package/koa), [Fastify](https://www.npmjs.com/package/fastify), etc.)
31
+ * Fully written in TypeScript (+ ships with types directly).
32
+ * [Thoroughly tested](./src/Session.test.ts) (+ 100% code coverage!).
33
+ * [Comprehensively documented](./docs) with guides and API documentation.
34
+ * [Channels](./docs/channels.md) allow you to broadcast events to many clients at once.
35
+ * Configurable reconnection time, message serialization and data sanitization (but with good defaults).
36
+ * Trust or ignore the client-given last event ID.
37
+ * Automatically send keep-alive pings to keep connections open.
38
+ * Add or override the response status code and headers.
39
+ * Fine-grained control by either sending [individual fields](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#fields) of events or sending full events with simple helpers.
40
+ * Pipe [streams](https://nodejs.org/api/stream.html#stream_readable_streams) and [iterables](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators) directly from the server to the client as a stream of events.
41
+ * Support for popular EventStream polyfills [`event-source-polyfill`](https://www.npmjs.com/package/event-source-polyfill) and [`eventsource-polyfill`](https://www.npmjs.com/package/eventsource-polyfill).
42
+
43
+ [See a comparison with other Node SSE libraries in the documentation.](./docs/comparison.md)
44
+
45
+ # Installation
46
+
47
+ ```bash
48
+ # npm
49
+ npm install better-sse
50
+
51
+ # Yarn
52
+ yarn add better-sse
53
+
54
+ # pnpm
55
+ pnpm add better-sse
56
+ ```
57
+
58
+ _Better SSE ships with types built in. No need to install from `@types` for TypeScript users!_
59
+
60
+ # Usage
61
+
62
+ The following example shows usage with [Express](http://expressjs.com/), but Better SSE works with any web-server framework (that uses the underlying Node [HTTP module](https://nodejs.org/api/http.html)).
63
+
64
+ See the [Recipes](./docs/recipes.md) section of the documentation for use with other frameworks and libraries.
65
+
66
+ ---
67
+
68
+ Use [sessions](./docs/api.md#session) to push events to clients:
69
+
70
+ ```typescript
71
+ // Server
72
+ import {createSession} from "better-sse";
73
+
74
+ app.get("/sse", async (req, res) => {
75
+ const session = await createSession(req, res);
76
+
77
+ session.push("Hello world!");
78
+ });
79
+ ```
80
+
81
+ ```typescript
82
+ // Client
83
+ const sse = new EventSource("/sse");
84
+
85
+ sse.addEventListener("message", ({data}) => {
86
+ console.log(data);
87
+ });
88
+ ```
89
+
90
+ ---
91
+
92
+ Use [channels](./docs/channels.md) to send events to many clients at once:
93
+
94
+ ```typescript
95
+ import {createSession, createChannel} from "better-sse";
96
+
97
+ const channel = createChannel();
98
+
99
+ app.get("/sse", async (req, res) => {
100
+ const session = await createSession(req, res);
101
+
102
+ channel.register(session);
103
+
104
+ channel.broadcast("A user has joined.", "join-notification");
105
+ });
106
+ ```
107
+
108
+ ---
109
+
110
+ Loop over sync and async [iterables](./docs/api.md#sessioniterate-iterable-iterable--asynciterable-options-object--promisevoid) and send each value as an event:
111
+
112
+ ```typescript
113
+ const session = await createSession(req, res);
114
+
115
+ const list = [1, 2, 3];
116
+
117
+ await session.iterate(list);
118
+ ```
119
+
120
+ ---
121
+
122
+ Pipe [readable stream](#sessionstream-stream-readable-options-object--promiseboolean) data to the client as a stream of events:
123
+
124
+ ```typescript
125
+ const session = await createSession(req, res);
126
+
127
+ const stream = Readable.from([1, 2, 3]);
128
+
129
+ await session.stream(stream);
130
+ ```
131
+
132
+ ---
133
+
134
+ Check the [API documentation](./docs/api.md) and [live examples](./examples) for information on getting more fine-tuned control over your data such as managing event IDs, data serialization, event filtering, dispatch controls and more!
135
+
136
+ # Documentation
137
+
138
+ API documentation, getting started guides and usage with other frameworks is [available on GitHub](./docs).
139
+
140
+ # Contributing
141
+
142
+ This library is always open to contributions, whether it be code, bug reports, documentation or anything else.
143
+
144
+ Please submit suggestions, bugs and issues to the [GitHub issues page](https://github.com/MatthewWid/better-sse/issues).
145
+
146
+ For code or documentation changes, [submit a pull request on GitHub](https://github.com/MatthewWid/better-sse/pulls).
147
+
148
+ ## Local Development
149
+
150
+ Install Node:
151
+
152
+ ```bash
153
+ curl -L https://git.io/n-install | bash
154
+ n auto
155
+ ```
156
+
157
+ Install pnpm:
158
+
159
+ ```bash
160
+ npm i -g pnpm
161
+ ```
162
+
163
+ Install dependencies:
164
+
165
+ ```bash
166
+ pnpm i
167
+ ```
168
+
169
+ Run tests:
170
+
171
+ ```bash
172
+ pnpm t
173
+ ```
174
+
175
+ # License
176
+
177
+ This project is licensed under the MIT license.
@@ -1,25 +1,30 @@
1
1
  import { TypedEmitter, EventMap } from "./lib/TypedEmitter";
2
- import Session from "./Session";
2
+ import { Session } from "./Session";
3
3
  interface BroadcastOptions {
4
4
  /**
5
5
  * Filter sessions that should receive the event.
6
6
  *
7
- * Called with each session and should return a truthy value to allow the event to be sent, otherwise return a falsy value to prevent the session from receiving the event.
7
+ * Called with each session and should return `true` to allow the event to be sent and otherwise return `false` to prevent the session from receiving the event.
8
8
  */
9
- filter?: (session: Session) => unknown;
9
+ filter?: (session: Session) => boolean;
10
10
  }
11
- interface Events extends EventMap {
11
+ interface ChannelEvents extends EventMap {
12
12
  "session-registered": (session: Session) => void;
13
13
  "session-deregistered": (session: Session) => void;
14
14
  "session-disconnected": (session: Session) => void;
15
- broadcast: (eventName: string, data: unknown) => void;
15
+ broadcast: (data: unknown, eventName: string, eventId: string) => void;
16
16
  }
17
17
  /**
18
18
  * A Channel is used to broadcast events to many sessions at once.
19
19
  *
20
20
  * It extends from the {@link https://nodejs.org/api/events.html#events_class_eventemitter | EventEmitter} class.
21
21
  */
22
- declare class Channel extends TypedEmitter<Events> {
22
+ declare class Channel<State extends Record<string, unknown> = Record<string, unknown>> extends TypedEmitter<ChannelEvents> {
23
+ /**
24
+ * Custom state for this channel.
25
+ * Use this object to safely store information related to the channel.
26
+ */
27
+ state: State;
23
28
  private sessions;
24
29
  constructor();
25
30
  /**
@@ -33,12 +38,16 @@ declare class Channel extends TypedEmitter<Events> {
33
38
  /**
34
39
  * Register a session so that it can start receiving events from this channel.
35
40
  *
41
+ * If the session is already registered this method does nothing.
42
+ *
36
43
  * @param session - Session to register.
37
44
  */
38
45
  register(session: Session): this;
39
46
  /**
40
47
  * Deregister a session so that it no longer receives events from this channel.
41
48
  *
49
+ * If the session was not registered to begin with this method does nothing.
50
+ *
42
51
  * @param session - Session to deregister.
43
52
  */
44
53
  deregister(session: Session): this;
@@ -47,7 +56,7 @@ declare class Channel extends TypedEmitter<Events> {
47
56
  *
48
57
  * Takes the same arguments as the `Session#push` method.
49
58
  */
50
- broadcast(eventName: string, data: unknown, options?: BroadcastOptions): this;
59
+ broadcast: (data: unknown, eventName?: string, options?: BroadcastOptions) => this;
51
60
  }
52
- export type { BroadcastOptions };
53
- export default Channel;
61
+ export type { BroadcastOptions, ChannelEvents };
62
+ export { Channel };
@@ -1,6 +1,8 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="node" />
2
3
  import { Readable } from "stream";
3
- import { IncomingMessage, ServerResponse, OutgoingHttpHeaders } from "http";
4
+ import { IncomingMessage as Http1ServerRequest, ServerResponse as Http1ServerResponse, OutgoingHttpHeaders } from "http";
5
+ import { Http2ServerRequest, Http2ServerResponse } from "http2";
4
6
  import { TypedEmitter, EventMap } from "./lib/TypedEmitter";
5
7
  import { SerializerFunction } from "./lib/serialize";
6
8
  import { SanitizerFunction } from "./lib/sanitize";
@@ -28,9 +30,11 @@ interface SessionOptions {
28
30
  */
29
31
  trustClientEventId?: boolean;
30
32
  /**
31
- * Time in milliseconds for the client to wait before attempting to reconnect if the connection is closed. This is a request to the client browser, and does not guarantee that the client will actually respect the given time.
33
+ * Time in milliseconds for the client to wait before attempting to reconnect if the connection is closed.
34
+ *
35
+ * This is a request to the client browser, and does not guarantee that the client will actually respect the given time.
32
36
  *
33
- * This is equivalent to immediately calling `.retry().dispatch()` after a connection is made.
37
+ * Equivalent to immediately calling `.retry().dispatch().flush()` after a connection is made.
34
38
  *
35
39
  * Give as `null` to avoid sending an explicit reconnection time and allow the client browser to decide itself.
36
40
  *
@@ -80,9 +84,13 @@ interface IterateOptions {
80
84
  */
81
85
  eventName?: string;
82
86
  }
83
- interface Events extends EventMap {
87
+ interface SessionState {
88
+ [key: string]: unknown;
89
+ }
90
+ interface SessionEvents extends EventMap {
84
91
  connected: () => void;
85
92
  disconnected: () => void;
93
+ push: (data: unknown, eventName: string, eventId: string) => void;
86
94
  }
87
95
  /**
88
96
  * A Session represents an open connection between the server and a client.
@@ -96,9 +104,10 @@ interface Events extends EventMap {
96
104
  * @param res - The Node HTTP {@link https://nodejs.org/api/http.html#http_class_http_serverresponse | IncomingMessage} object.
97
105
  * @param options - Options given to the session instance.
98
106
  */
99
- declare class Session<State extends Record<string, unknown> = Record<string, unknown>> extends TypedEmitter<Events> {
107
+ declare class Session<State extends Record<string, unknown> = SessionState> extends TypedEmitter<SessionEvents> {
100
108
  /**
101
109
  * The last ID sent to the client.
110
+ *
102
111
  * This is initialized to the last event ID given by the user, and otherwise is equal to the last number given to the `.id` method.
103
112
  *
104
113
  * @readonly
@@ -112,10 +121,24 @@ declare class Session<State extends Record<string, unknown> = Record<string, unk
112
121
  isConnected: boolean;
113
122
  /**
114
123
  * Custom state for this session.
124
+ *
115
125
  * Use this object to safely store information related to the session and user.
116
126
  */
117
127
  state: State;
128
+ /**
129
+ * Internal buffer used to store raw data from written fields.
130
+ *
131
+ * When Session#dispatch is called its buffer data will be flushed.
132
+ */
133
+ private buffer;
134
+ /**
135
+ * Raw HTTP request.
136
+ */
118
137
  private req;
138
+ /**
139
+ * Raw HTTP response that is the minimal interface needed and forms the
140
+ * intersection between the HTTP/1.1 and HTTP/2 server response interfaces.
141
+ */
119
142
  private res;
120
143
  private serialize;
121
144
  private sanitize;
@@ -125,18 +148,14 @@ declare class Session<State extends Record<string, unknown> = Record<string, unk
125
148
  private keepAliveTimer?;
126
149
  private statusCode;
127
150
  private headers;
128
- constructor(req: IncomingMessage, res: ServerResponse, options?: SessionOptions);
129
- private onConnected;
151
+ constructor(req: Http1ServerRequest | Http2ServerRequest, res: Http1ServerResponse | Http2ServerResponse, options?: SessionOptions);
152
+ private initialize;
130
153
  private onDisconnected;
131
154
  /**
132
155
  * Write a line with a field key and value appended with a newline character.
133
156
  */
134
157
  private writeField;
135
158
  private keepAlive;
136
- /**
137
- * Flush the buffered data to the client by writing an additional newline.
138
- */
139
- dispatch: () => this;
140
159
  /**
141
160
  * Set the event to the given name (also referred to as "type" in the specification).
142
161
  *
@@ -144,7 +163,7 @@ declare class Session<State extends Record<string, unknown> = Record<string, unk
144
163
  */
145
164
  event(type: string): this;
146
165
  /**
147
- * Write arbitrary data onto the wire that is automatically serialized to a string using the given `serializer` function option or JSON stringification by default.
166
+ * Write an arbitrary data field that is automatically serialized to a string using the given `serializer` function option or JSON stringification by default.
148
167
  *
149
168
  * @param data - Data to serialize and write.
150
169
  */
@@ -152,11 +171,11 @@ declare class Session<State extends Record<string, unknown> = Record<string, unk
152
171
  /**
153
172
  * Set the event ID to the given string.
154
173
  *
155
- * Passing `null` will set the event ID to an empty string value.
174
+ * Defaults to an empty string if no argument is given.
156
175
  *
157
176
  * @param id - Identification string to write.
158
177
  */
159
- id: (id: string | null) => this;
178
+ id: (id?: string) => this;
160
179
  /**
161
180
  * Set the suggested reconnection time to the given milliseconds.
162
181
  *
@@ -170,19 +189,33 @@ declare class Session<State extends Record<string, unknown> = Record<string, unk
170
189
  *
171
190
  * @param text - Text of the comment. Otherwise writes an empty field value.
172
191
  */
173
- comment: (text?: string | undefined) => this;
192
+ comment: (text?: string) => this;
174
193
  /**
175
- * Create and dispatch an event with the given data all at once.
176
- * This is equivalent to calling `.event()`, `.id()`, `.data()` and `.dispatch()` in that order.
194
+ * Indicate that the event has finished being created by writing an additional newline character.
195
+ *
196
+ * Note that this does **not** send the written data to the client. Use `flush` to flush the internal buffer.
197
+ */
198
+ dispatch: () => this;
199
+ /**
200
+ * Flush the buffered data to the client and clear the buffer.
201
+ */
202
+ flush: () => this;
203
+ /**
204
+ * Create, dispatch and flush an event with the given data all at once.
205
+ *
206
+ * This is equivalent to calling the methods `event`, `id`, `data`, `dispatch` and `flush` in that order.
207
+ *
208
+ * If no event name is given, the event name is set to `"message"`.
177
209
  *
178
- * If no event name is given, the event name (type) is set to `"message"`.
210
+ * 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.
179
211
  *
180
- * Note that this sets the event ID (and thus the `lastId` property) to a string of eight random characters (`a-z0-9`).
212
+ * Emits the `push` event with the given data, event name and event ID in that order.
181
213
  *
182
- * @param eventOrData - Event name or data to write.
183
- * @param data - Data to write if `eventOrData` was an event name.
214
+ * @param data - Data to write.
215
+ * @param eventName - Event name to write.
216
+ * @param eventId - Event ID to write.
184
217
  */
185
- push: (eventOrData: string | unknown, data?: unknown) => this;
218
+ push: (data: unknown, eventName?: string, eventId?: string) => this;
186
219
  /**
187
220
  * Pipe readable stream data to the client.
188
221
  *
@@ -211,5 +244,5 @@ declare class Session<State extends Record<string, unknown> = Record<string, unk
211
244
  */
212
245
  iterate: <DataType = unknown>(iterable: Iterable<DataType> | AsyncIterable<DataType>, options?: IterateOptions) => Promise<void>;
213
246
  }
214
- export type { SessionOptions };
215
- export default Session;
247
+ export type { SessionOptions, StreamOptions, IterateOptions, SessionState, SessionEvents, };
248
+ export { Session };
@@ -1,3 +1,3 @@
1
- import Channel from "./Channel";
2
- declare const createChannel: () => Channel;
3
- export default createChannel;
1
+ import { Channel } from "./Channel";
2
+ declare const createChannel: <State extends Record<string, unknown>>() => Channel<State>;
3
+ export { createChannel };
@@ -1,7 +1,6 @@
1
- /// <reference types="node" />
2
- import Session from "./Session";
1
+ import { Session, SessionState } from "./Session";
3
2
  /**
4
3
  * Create a new session and return the session instance once it has connected.
5
4
  */
6
- declare const createSession: <State extends Record<string, unknown> = Record<string, unknown>>(req: import("http").IncomingMessage, res: import("http").ServerResponse, options?: import("./Session").SessionOptions | undefined) => Promise<Session<State>>;
7
- export default createSession;
5
+ declare const createSession: <State extends Record<string, unknown> = SessionState>(req: import("http").IncomingMessage | import("http2").Http2ServerRequest, res: import("http").ServerResponse | import("http2").Http2ServerResponse, options?: import("./Session").SessionOptions | undefined) => Promise<Session<State>>;
6
+ export { createSession };
package/build/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { default as Session } from "./Session";
2
- export { default as createSession } from "./createSession";
3
- export { default as Channel } from "./Channel";
4
- export { default as createChannel } from "./createChannel";
1
+ export * from "./Session";
2
+ export * from "./createSession";
3
+ export * from "./Channel";
4
+ export * from "./createChannel";
package/build/index.js CHANGED
@@ -1,2 +1,2 @@
1
- !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var s=t();for(var i in s)("object"==typeof exports?exports:e)[i]=s[i]}}(global,(function(){return(()=>{"use strict";var e={n:t=>{var s=t&&t.__esModule?()=>t.default:()=>t;return e.d(s,{a:s}),s},d:(t,s)=>{for(var i in s)e.o(s,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:s[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Channel:()=>d,Session:()=>a,createChannel:()=>c,createSession:()=>l});const s=require("crypto"),i=require("events");var r=e.n(i);class n extends(r()){addListener(e,t){return super.addListener(e,t)}prependListener(e,t){return super.prependListener(e,t)}prependOnceListener(e,t){return super.prependOnceListener(e,t)}on(e,t){return super.on(e,t)}once(e,t){return super.once(e,t)}emit(e,...t){return super.emit(e,...t)}off(e,t){return super.off(e,t)}removeListener(e,t){return super.removeListener(e,t)}}const o=e=>JSON.stringify(e),h=e=>{let t=e;return t=t.replace(/(\r\n|\r|\n)/g,"\n"),t=t.replace(/\n+$/g,""),t},a=class extends n{constructor(e,t,i={}){var r,n,a,l,d,c,u;super(),this.lastId="",this.isConnected=!1,this.state={},this.onConnected=()=>{var e,t,s;const i=`http://${this.req.headers.host}${this.req.url}`,r=new URL(i).searchParams;if(this.trustClientEventId){const i=null!==(s=null!==(t=null!==(e=this.req.headers["last-event-id"])&&void 0!==e?e:r.get("lastEventId"))&&void 0!==t?t:r.get("evs_last_event_id"))&&void 0!==s?s:"";this.lastId=i}Object.entries(this.headers).forEach((([e,t])=>{this.res.setHeader(e,null!=t?t:"")})),this.res.statusCode=this.statusCode,this.res.setHeader("Content-Type","text/event-stream"),this.res.setHeader("Cache-Control","no-cache, no-transform"),this.res.setHeader("Connection","keep-alive"),this.res.flushHeaders(),r.has("padding")&&this.comment(" ".repeat(2049)).dispatch(),r.has("evs_preamble")&&this.comment(" ".repeat(2056)).dispatch(),null!==this.initialRetry&&this.retry(this.initialRetry).dispatch(),null!==this.keepAliveInterval&&(this.keepAliveTimer=setInterval(this.keepAlive,this.keepAliveInterval)),this.isConnected=!0,this.emit("connected")},this.onDisconnected=()=>{this.keepAliveTimer&&clearInterval(this.keepAliveTimer),this.isConnected=!1,this.emit("disconnected")},this.writeField=(e,t)=>{const s=`${e}:${this.sanitize(t)}\n`;return this.res.write(s),this},this.keepAlive=()=>{this.comment().dispatch()},this.dispatch=()=>(this.res.write("\n"),this),this.data=e=>{const t=this.serialize(e);return this.writeField("data",t),this},this.id=e=>{const t=e||"";return this.writeField("id",t),this.lastId=t,this},this.retry=e=>{const t=e.toString();return this.writeField("retry",t),this},this.comment=e=>(this.writeField("",null!=e?e:""),this),this.push=(e,t)=>{let i,r;e&&void 0===t?(i="message",r=e):(i=e.toString(),r=t);const n=(0,s.randomBytes)(4).toString("hex");return this.event(i).id(n).data(r).dispatch(),this},this.stream=async(e,t={})=>{const{eventName:s="stream"}=t;return new Promise(((t,i)=>{e.on("data",(e=>{let t;t=Buffer.isBuffer(e)?e.toString():e,this.push(s,t)})),e.once("end",(()=>t(!0))),e.once("close",(()=>t(!0))),e.once("error",(e=>i(e)))}))},this.iterate=async(e,t={})=>{const{eventName:s="iteration"}=t;for await(const t of e)this.push(s,t)},this.req=e,this.res=t,this.serialize=null!==(r=i.serializer)&&void 0!==r?r:o,this.sanitize=null!==(n=i.sanitizer)&&void 0!==n?n:h,this.trustClientEventId=null===(a=i.trustClientEventId)||void 0===a||a,this.initialRetry=null===i.retry?null:null!==(l=i.retry)&&void 0!==l?l:2e3,this.keepAliveInterval=null===i.keepAlive?null:null!==(d=i.keepAlive)&&void 0!==d?d:1e4,this.statusCode=null!==(c=i.statusCode)&&void 0!==c?c:200,this.headers=null!==(u=i.headers)&&void 0!==u?u:{},this.req.on("close",this.onDisconnected),setImmediate(this.onConnected)}event(e){return this.writeField("event",e),this}},l=(...e)=>new Promise((t=>{const s=new a(...e);s.once("connected",(()=>{t(s)}))})),d=class extends n{constructor(){super(),this.sessions=[]}get activeSessions(){return this.sessions}get sessionCount(){return this.sessions.length}register(e){if(!e.isConnected)throw new Error("Cannot register a non-active session.");return e.once("disconnected",(()=>{this.deregister(e),this.emit("session-disconnected",e)})),this.sessions.push(e),this.emit("session-registered",e),this}deregister(e){return this.sessions=this.sessions.filter((t=>t!==e)),this.emit("session-deregistered",e),this}broadcast(e,t,s={}){for(const i of this.sessions)s.filter&&!s.filter(i)||i.push(e,t);return this.emit("broadcast",e,t),this}},c=(...e)=>new d(...e);return t})()}));
1
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var s=t();for(var i in s)("object"==typeof exports?exports:e)[i]=s[i]}}(global,(()=>(()=>{"use strict";var e={n:t=>{var s=t&&t.__esModule?()=>t.default:()=>t;return e.d(s,{a:s}),s},d:(t,s)=>{for(var i in s)e.o(s,i)&&!e.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:s[i]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},t={};e.r(t),e.d(t,{Channel:()=>f,Session:()=>u,createChannel:()=>v,createSession:()=>p});const s=require("http"),i=require("events");var r=e.n(i);class n extends(r()){addListener(e,t){return super.addListener(e,t)}prependListener(e,t){return super.prependListener(e,t)}prependOnceListener(e,t){return super.prependOnceListener(e,t)}on(e,t){return super.on(e,t)}once(e,t){return super.once(e,t)}emit(e,...t){return super.emit(e,...t)}off(e,t){return super.off(e,t)}removeListener(e,t){return super.removeListener(e,t)}}const o=e=>JSON.stringify(e),a=/(\r\n|\r|\n)/g,h=/\n+$/g,l=e=>{let t=e;return t=t.replace(a,"\n"),t=t.replace(h,""),t},d=require("crypto");let c;c=d.randomUUID?()=>(0,d.randomUUID)():()=>(0,d.randomBytes)(4).toString("hex");class u extends n{constructor(e,t,i={}){var r,n,a,h,d,u,p;super(),this.lastId="",this.isConnected=!1,this.state={},this.buffer="",this.initialize=()=>{var e,t,i;const r=`http://${this.req.headers.host}${this.req.url}`,n=new URL(r).searchParams;if(this.trustClientEventId){const s=null!==(i=null!==(t=null!==(e=this.req.headers["last-event-id"])&&void 0!==e?e:n.get("lastEventId"))&&void 0!==t?t:n.get("evs_last_event_id"))&&void 0!==i?i:"";this.lastId=s}const o={};this.res instanceof s.ServerResponse?(o["Content-Type"]="text/event-stream",o["Cache-Control"]="private, no-cache, no-store, no-transform, must-revalidate, max-age=0",o.Connection="keep-alive",o.Pragma="no-cache",o["X-Accel-Buffering"]="no"):(o["content-type"]="text/event-stream",o["cache-control"]="private, no-cache, no-store, no-transform, must-revalidate, max-age=0",o.pragma="no-cache",o["x-accel-buffering"]="no");for(const[e,t]of Object.entries(this.headers))o[e]=null!=t?t:"";this.res.writeHead(this.statusCode,o),n.has("padding")&&this.comment(" ".repeat(2049)).dispatch(),n.has("evs_preamble")&&this.comment(" ".repeat(2056)).dispatch(),null!==this.initialRetry&&this.retry(this.initialRetry).dispatch(),this.flush(),null!==this.keepAliveInterval&&(this.keepAliveTimer=setInterval(this.keepAlive,this.keepAliveInterval)),this.isConnected=!0,this.emit("connected")},this.onDisconnected=()=>{this.keepAliveTimer&&clearInterval(this.keepAliveTimer),this.isConnected=!1,this.emit("disconnected")},this.writeField=(e,t)=>{const s=this.sanitize(t);return this.buffer+=e+":"+s+"\n",this},this.keepAlive=()=>{this.comment().dispatch().flush()},this.data=e=>{const t=this.serialize(e);return this.writeField("data",t),this},this.id=(e="")=>(this.writeField("id",e),this.lastId=e,this),this.retry=e=>{const t=e.toString();return this.writeField("retry",t),this},this.comment=(e="")=>(this.writeField("",e),this),this.dispatch=()=>(this.buffer+="\n",this),this.flush=()=>(this.res.write(this.buffer),this.buffer="",this),this.push=(e,t="message",s=c())=>(this.event(t).id(s).data(e).dispatch().flush(),this.emit("push",e,t,s),this),this.stream=async(e,t={})=>{const{eventName:s="stream"}=t;return new Promise(((t,i)=>{e.on("data",(e=>{let t;t=Buffer.isBuffer(e)?e.toString():e,this.push(t,s)})),e.once("end",(()=>t(!0))),e.once("close",(()=>t(!0))),e.once("error",(e=>i(e)))}))},this.iterate=async(e,t={})=>{const{eventName:s="iteration"}=t;for await(const t of e)this.push(t,s)},this.req=e,this.res=t,this.serialize=null!==(r=i.serializer)&&void 0!==r?r:o,this.sanitize=null!==(n=i.sanitizer)&&void 0!==n?n:l,this.trustClientEventId=null===(a=i.trustClientEventId)||void 0===a||a,this.initialRetry=null===i.retry?null:null!==(h=i.retry)&&void 0!==h?h:2e3,this.keepAliveInterval=null===i.keepAlive?null:null!==(d=i.keepAlive)&&void 0!==d?d:1e4,this.statusCode=null!==(u=i.statusCode)&&void 0!==u?u:200,this.headers=null!==(p=i.headers)&&void 0!==p?p:{},this.req.once("close",this.onDisconnected),setImmediate(this.initialize)}event(e){return this.writeField("event",e),this}}const p=(...e)=>new Promise((t=>{const s=new u(...e);s.once("connected",(()=>{t(s)}))}));class f extends n{constructor(){super(),this.state={},this.sessions=new Set,this.broadcast=(e,t="message",s={})=>{const i=c();let r;r=s.filter?Array.from(this.sessions).filter(s.filter):this.sessions;for(const s of r)s.push(e,t,i);return this.emit("broadcast",e,t,i),this}}get activeSessions(){return Array.from(this.sessions)}get sessionCount(){return this.sessions.size}register(e){if(this.sessions.has(e))return this;if(!e.isConnected)throw new Error("Cannot register a non-active session.");return e.once("disconnected",(()=>{this.emit("session-disconnected",e),this.deregister(e)})),this.sessions.add(e),this.emit("session-registered",e),this}deregister(e){return this.sessions.has(e)?(this.sessions.delete(e),this.emit("session-deregistered",e),this):this}}const v=(...e)=>new f(...e);return t})()));
2
2
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,2 @@
1
+ declare let generateId: () => string;
2
+ export { generateId };
@@ -1,5 +1,6 @@
1
- export interface SanitizerFunction {
1
+ interface SanitizerFunction {
2
2
  (text: string): string;
3
3
  }
4
4
  declare const sanitize: SanitizerFunction;
5
- export default sanitize;
5
+ export type { SanitizerFunction };
6
+ export { sanitize };
@@ -1,8 +1,9 @@
1
1
  /**
2
2
  * Serialize arbitrary data to a string that can be sent over the wire to the client.
3
3
  */
4
- export interface SerializerFunction {
4
+ interface SerializerFunction {
5
5
  (data: unknown): string;
6
6
  }
7
7
  declare const serialize: SerializerFunction;
8
- export default serialize;
8
+ export type { SerializerFunction };
9
+ export { serialize };
@@ -0,0 +1,10 @@
1
+ import http from "http";
2
+ import http2 from "http2";
3
+ import net from "net";
4
+ import { Session } from "../Session";
5
+ declare const createHttpServer: () => Promise<http.Server>;
6
+ declare const createHttp2Server: () => Promise<http2.Http2Server>;
7
+ declare const closeServer: (server: net.Server) => Promise<void>;
8
+ declare const getUrl: (server: net.Server) => string;
9
+ declare const waitForConnect: (session: Session) => Promise<void>;
10
+ export { createHttpServer, createHttp2Server, closeServer, getUrl, waitForConnect, };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "better-sse",
3
3
  "description": "Dead simple, dependency-less, spec-compliant server-side events implementation for Node, written in TypeScript.",
4
- "version": "0.6.0",
4
+ "version": "0.8.0",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
7
7
  "license": "MIT",
8
8
  "author": "Matthew W. <matthew.widdi@gmail.com>",
9
9
  "repository": "github:MatthewWid/better-sse",
10
- "homepage": "https://github.com/MatthewWid/better-sse/blob/master/README.md",
10
+ "homepage": "https://github.com/MatthewWid/better-sse",
11
11
  "bugs": "https://github.com/MatthewWid/better-sse/issues",
12
12
  "keywords": [
13
13
  "server-sent-events",
@@ -26,33 +26,32 @@
26
26
  "pnpm": ">=6"
27
27
  },
28
28
  "devDependencies": {
29
- "@types/eventsource": "^1.1.5",
30
- "@types/express": "^4.17.9",
31
- "@types/jest": "^26.0.19",
32
- "@types/node": "^14.14.20",
33
- "@typescript-eslint/eslint-plugin": "^4.11.0",
34
- "@typescript-eslint/parser": "^4.11.0",
35
- "eslint": "^7.16.0",
36
- "eslint-plugin-tsdoc": "^0.2.10",
37
- "eventsource": "^1.1.0",
29
+ "@jest/types": "^28.1.0",
30
+ "@types/eventsource": "^1.1.8",
31
+ "@types/express": "^4.17.13",
32
+ "@types/jest": "^26.0.24",
33
+ "@types/node": "^17.0.36",
34
+ "@typescript-eslint/eslint-plugin": "^5.26.0",
35
+ "@typescript-eslint/parser": "^5.26.0",
36
+ "eslint": "^8.16.0",
37
+ "eslint-plugin-tsdoc": "^0.2.16",
38
+ "eventsource": "^1.1.1",
38
39
  "jest": "^26.6.3",
39
40
  "npm-run-all": "^4.1.5",
40
- "prettier": "^2.2.1",
41
+ "prettier": "^2.6.2",
41
42
  "rimraf": "^3.0.2",
42
- "ts-jest": "^26.4.4",
43
- "ts-loader": "^9.2.3",
44
- "ts-node": "^10.0.0",
45
- "typescript": "^4.4.4",
46
- "webpack": "^5.38.1",
47
- "webpack-cli": "^4.7.0"
43
+ "ts-jest": "^26.5.6",
44
+ "ts-loader": "^9.3.0",
45
+ "ts-node": "^10.8.0",
46
+ "typescript": "^4.7.2",
47
+ "webpack": "^5.72.1",
48
+ "webpack-cli": "^4.9.2"
48
49
  },
49
50
  "scripts": {
50
51
  "build": "webpack --env production",
51
- "dev": "webpack --env development",
52
52
  "test": "jest",
53
53
  "clean": "rimraf ./build",
54
54
  "format": "prettier --write ./src/**/*.ts",
55
- "typedefs": "tsc --emitDeclarationOnly",
56
55
  "lint": "eslint \"./src/**/*.ts\""
57
56
  }
58
57
  }