Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
This release improves the performance of parsing the response stream and fixes some corner cases to better match the spec
  • Loading branch information
vishwam committed Dec 25, 2020
1 parent 6ff6dd1 commit 545ccdb
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 222 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
# CHANGELOG

## 2.0.0
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).

### Changed
- The `id`, `event`, and `data` fields are now initialized to empty strings, per the spec (they were previously `undefined`)
- The `onmessage` callback is now called for _all_ messages (it was previously triggered only for messages with a `data` field)
- If a message contains multiple `data` fields, they will be concatenated together into a single string. For example, the following message:
````
data: Foo
data:Bar
data
data: Baz
````
will result in `{ data: 'Foo\nBar\n\nBaz' }`
- If the `id` field is empty, the `last-event-id` header will no longer be sent on the next reconnect.
### Removed
- 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.
## 1.0.2
### Changed
- Updated examples in readme to fix typos, added more comments.
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@microsoft/fetch-event-source",
"version": "1.0.2",
"version": "2.0.0",
"description": "A better API for making Event Source requests, with all the features of fetch()",
"homepage": "https://github.com/Azure/fetch-event-source#readme",
"bugs": {
Expand Down
35 changes: 14 additions & 21 deletions src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { parseStream, EventSourceMessage } from './parse';
import { EventSourceMessage, getBytes, getLines, getMessages } from './parse';

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

const DefaultRetryInterval = 1000;
const LastEventId = 'last-event-id';

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

async function parseResponse(response: Response) {
for await (const msg of parseStream(response.body)) {
// first check for system events:
if (msg.id !== undefined) {
// keep track of the last-seen ID and send it back on the next retry:
headers['last-event-id'] = msg.id;
}

if (msg.retry !== undefined) {
retryInterval = msg.retry;
}

if (msg.data !== undefined && onmessage != null) {
onmessage(msg);
}
}
}

const fetch = inputFetch ?? window.fetch;
const onopen = inputOnOpen ?? defaultOnOpen;
async function create() {
Expand All @@ -126,8 +109,18 @@ export function fetchEventSource(input: RequestInfo, {
});

await onopen(response);

await parseResponse(response);

await getBytes(response.body, getLines(getMessages(id => {
if (id) {
// store the id and send it back on the next retry:
headers[LastEventId] = id;
} else {
// don't send the last-event-id header anymore:
delete headers[LastEventId];
}
}, retry => {
retryInterval = retry;
}, onmessage)));

onclose?.();
dispose();
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { fetchEventSource, FetchEventSourceInit, EventStreamContentType } from './fetch';
export { parseStream, EventSourceMessage } from './parse';
export { EventSourceMessage } from './parse';
Loading

0 comments on commit 545ccdb

Please sign in to comment.