Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6ff6dd1

Browse files
committedNov 27, 2020
Updated examples in readme to fix typos, added more comments.
1 parent 8d65b5d commit 6ff6dd1

File tree

5 files changed

+18
-9
lines changed

5 files changed

+18
-9
lines changed
 

‎CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# CHANGELOG
22

3+
## 1.0.2
4+
### Changed
5+
- Updated examples in readme to fix typos, added more comments.
6+
- Changed `if` statements in parse.ts to test for specific values instead of truthy/falsy values.
7+
38
## 1.0.1
49
### Changed
510
- Changed the default onOpen validator to allow charset and boundary directives in the content-type

‎README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This library provides an alternate interface for consuming server-sent events, b
1313
* You have access to the response object if you want to do some custom validation/processing before parsing the event source. This is useful in case you have API gateways (like nginx) in front of your application server: if the gateway returns an error, you might want to handle it correctly.
1414
* If the connection gets cut or an error occurs, you have full control over the retry strategy.
1515

16-
In addition, this library also plugs into the browser's [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unncessarily (but you can opt out of this behavior if you want.)
16+
In addition, this library also plugs into the browser's [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API) so the connection closes if the document is hidden (e.g., the user minimizes the window), and automatically retries with the last event ID when it becomes visible again. This reduces the load on your server by not having open connections unnecessarily (but you can opt out of this behavior if you want.)
1717

1818
# Install
1919
```sh
@@ -29,6 +29,8 @@ sse.onmessage = (ev) => {
2929
};
3030

3131
// AFTER:
32+
import { fetchEventSource } from '@microsoft/fetch-event-source';
33+
3234
await fetchEventSource('/api/sse', {
3335
onmessage(ev) {
3436
console.log(ev.data);
@@ -68,6 +70,8 @@ fetchEventSource('/api/sse', {
6870
}
6971
},
7072
onmessage(msg) {
73+
// if the server emits an error message, throw an exception
74+
// so it gets handled by the onerror callback below:
7175
if (msg.event === 'FatalError') {
7276
throw new FatalError(msg.data);
7377
}

‎package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@microsoft/fetch-event-source",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
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/parse.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ export async function* getLines(iter: AsyncIterableIterator<Uint8Array>) {
4848
let discardTrailingNewline = false;
4949

5050
for await (const arr of iter) {
51-
if (buffer) {
52-
// we're still parsing the old line. Append the new bytes into buffer:
53-
buffer = concat(buffer, arr);
54-
} else {
51+
if (buffer === undefined) {
5552
buffer = arr;
5653
position = 0;
5754
fieldLength = -1;
55+
} else {
56+
// we're still parsing the old line. Append the new bytes into buffer:
57+
buffer = concat(buffer, arr);
5858
}
5959

6060
const bufLength = buffer.length;
@@ -103,7 +103,7 @@ export async function* getLines(iter: AsyncIterableIterator<Uint8Array>) {
103103

104104
if (lineStart === bufLength) {
105105
buffer = undefined; // we've finished reading it
106-
} else if (lineStart) {
106+
} else if (lineStart !== 0) {
107107
// Create a new view into buffer beginning at lineStart so we don't
108108
// need to copy over the previous lines when we get the new arr:
109109
buffer = buffer.subarray(lineStart);
@@ -117,7 +117,7 @@ export async function* getMessages(iter: AsyncIterableIterator<{ line: Uint8Arra
117117
let message: EventSourceMessage = {};
118118
const decoder = new TextDecoder();
119119
for await (const { line, fieldLength } of iter) {
120-
if (!line.length) {
120+
if (line.length === 0) {
121121
// empty line denotes end of message. Yield our current message if it's not empty:
122122
for (const _ in message) {
123123
yield message;

0 commit comments

Comments
 (0)
Please sign in to comment.