Skip to content

Commit 545ccdb

Browse files
committed
v2.0.0
This release improves the performance of parsing the response stream and fixes some corner cases to better match the spec
1 parent 6ff6dd1 commit 545ccdb

File tree

7 files changed

+371
-222
lines changed

7 files changed

+371
-222
lines changed

CHANGELOG.md

+20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
# CHANGELOG
22

3+
## 2.0.0
4+
This release improves the performance of parsing the response stream and fixes some corner cases to better match [the spec](https://html.spec.whatwg.org/multipage/server-sent-events.html#event-stream-interpretation).
5+
6+
### Changed
7+
- The `id`, `event`, and `data` fields are now initialized to empty strings, per the spec (they were previously `undefined`)
8+
- The `onmessage` callback is now called for _all_ messages (it was previously triggered only for messages with a `data` field)
9+
- If a message contains multiple `data` fields, they will be concatenated together into a single string. For example, the following message:
10+
````
11+
data: Foo
12+
data:Bar
13+
data
14+
data: Baz
15+
````
16+
will result in `{ data: 'Foo\nBar\n\nBaz' }`
17+
18+
- If the `id` field is empty, the `last-event-id` header will no longer be sent on the next reconnect.
19+
20+
### Removed
21+
- The internal `parseStream` function has been removed. The parse implementation was previously based on async generators, which required a lot of supporting code in both the typescript-generated polyfill as well as the javascript engine. The new implementation is based on simple callbacks, which should be much faster.
22+
323
## 1.0.2
424
### Changed
525
- Updated examples in readme to fix typos, added more comments.

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/fetch-event-source",
3-
"version": "1.0.2",
3+
"version": "2.0.0",
44
"description": "A better API for making Event Source requests, with all the features of fetch()",
55
"homepage": "https://github.com/Azure/fetch-event-source#readme",
66
"bugs": {

src/fetch.ts

+14-21
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { parseStream, EventSourceMessage } from './parse';
1+
import { EventSourceMessage, getBytes, getLines, getMessages } from './parse';
22

33
export const EventStreamContentType = 'text/event-stream';
44

55
const DefaultRetryInterval = 1000;
6+
const LastEventId = 'last-event-id';
67

78
export interface FetchEventSourceInit extends RequestInit {
89
/**
@@ -96,24 +97,6 @@ export function fetchEventSource(input: RequestInfo, {
9697
resolve(); // don't waste time constructing/logging errors
9798
});
9899

99-
async function parseResponse(response: Response) {
100-
for await (const msg of parseStream(response.body)) {
101-
// first check for system events:
102-
if (msg.id !== undefined) {
103-
// keep track of the last-seen ID and send it back on the next retry:
104-
headers['last-event-id'] = msg.id;
105-
}
106-
107-
if (msg.retry !== undefined) {
108-
retryInterval = msg.retry;
109-
}
110-
111-
if (msg.data !== undefined && onmessage != null) {
112-
onmessage(msg);
113-
}
114-
}
115-
}
116-
117100
const fetch = inputFetch ?? window.fetch;
118101
const onopen = inputOnOpen ?? defaultOnOpen;
119102
async function create() {
@@ -126,8 +109,18 @@ export function fetchEventSource(input: RequestInfo, {
126109
});
127110

128111
await onopen(response);
129-
130-
await parseResponse(response);
112+
113+
await getBytes(response.body, getLines(getMessages(id => {
114+
if (id) {
115+
// store the id and send it back on the next retry:
116+
headers[LastEventId] = id;
117+
} else {
118+
// don't send the last-event-id header anymore:
119+
delete headers[LastEventId];
120+
}
121+
}, retry => {
122+
retryInterval = retry;
123+
}, onmessage)));
131124

132125
onclose?.();
133126
dispose();

src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
export { fetchEventSource, FetchEventSourceInit, EventStreamContentType } from './fetch';
2-
export { parseStream, EventSourceMessage } from './parse';
2+
export { EventSourceMessage } from './parse';

0 commit comments

Comments
 (0)