Skip to content

Commit c402efd

Browse files
committed
see changelog 6.0.0
1 parent dbab399 commit c402efd

6 files changed

Lines changed: 69 additions & 18 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ js/network/socketiyo/examples/upload
7474
js/network/forms/upload/
7575
js/network/socketiyo-client/built/
7676
js/network/socketiyo/built/socketiyo.es.js
77-
.vscode/settings.json
77+
**/.vscode/settings.json

js/network/onewaydata/changelog.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 6.0.0
2+
3+
* Start Changelog
4+
* Use node protocol
5+
* Completed Readme
6+
* Better handling of badly formatted requests
7+
* Set reconnection time to 10000ms
8+
* Add possibility to change reconnection time with reconnectionTime
9+
* Replaced path option with general purpose condition
10+
* Use the following to keep using path
11+
12+
```js
13+
const path = ...
14+
const condition = (request) => {
15+
request.url === path;
16+
};
17+
```

js/network/onewaydata/examples/server.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
import http from "http";
3-
import fs from "fs";
4-
import url from "url";
5-
import path from "path";
2+
import http from "node:http";
3+
import fs from "node:fs";
4+
import url from "node:url";
5+
import path from "node:path";
66
import {
77
createEventStream,
88
sendOne,
@@ -12,17 +12,24 @@ import {
1212
useDefaultLogging
1313
} from "../source/defaultLogging.js";
1414

15+
1516
const __filename = url.fileURLToPath(import.meta.url);
1617
const __dirname = path.dirname(__filename);
1718

1819
const PORT = 8080;
20+
21+
1922
const server = http.createServer((req, res) => {
2023
if (req.url === `/`) {
2124
fs.createReadStream(`${__dirname}/client.html`).pipe(res);
2225
}
2326
});
2427

25-
const eventStream = createEventStream({ server, path: `/sse` });
28+
const path = `/sse`;
29+
const condition = (request) => {
30+
request.url === path;
31+
};
32+
const eventStream = createEventStream({ server, condition });
2633
useDefaultLogging({ eventStream });
2734

2835

@@ -38,5 +45,6 @@ setInterval(() => {
3845
eventStream.send({
3946
event: `football/goal`,
4047
data: `team 1`,
48+
id: String(Date.now()),
4149
});
42-
}, 10000);
50+
}, 5000);

js/network/onewaydata/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@
3535
"files": [
3636
"source",
3737
"readme.md",
38-
"license.txt"
38+
"license.txt",
39+
"changelog.md"
3940
],
4041
"repository": {
4142
"type": "git",

js/network/onewaydata/readme.md

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,34 @@ to be used with [EventSource API](https://developer.mozilla.org/en-US/docs/Web/A
1313
## usage
1414

1515
```js
16-
import { createEventStream } from "onewaydata";
16+
import {
17+
createEventStream,
18+
sendOne,
19+
RECONNECT,
20+
CONNECT,
21+
DISCONNECT,
22+
defaultChannel,
23+
} from "onewaydata";
1724
import { useDefaultLogging } from "onewaydata/source/defaultLogging.js";
1825

1926

2027
const server = ...
21-
22-
const eventStream = createEventStream({server, path: `/sse`});
28+
const path = `/sse`;
29+
const condition = (request) => {
30+
request.url === path;
31+
};
32+
const eventStream = createEventStream({server, condition, reconnectionTime: 5000 });
2333
useDefaultLogging({ eventStream });
2434

2535

26-
eventStream.send({ data: `something`, event: `eventName`});
36+
eventStream.send({ data: `data only`)});
37+
eventStream.send({ data: `something`, event: `eventName`, id: String(Date.now())});
38+
eventStream.on(RECONNECT, ({lastId, response}) => {
39+
if (lastId) {
40+
// opportunity to resume with sendOne
41+
sendOne(response, `here is what you missed since last disconnection`)
42+
}
43+
})
2744
```
2845

2946
### with express/polka
@@ -53,6 +70,10 @@ See [examples](./examples)
5370
https://hpbn.co/server-sent-events-sse/
5471

5572

73+
### Changelog
74+
75+
[changelog.md](./changelog.md)
76+
5677
### License
5778

5879
[CC0](./license.txt)

js/network/onewaydata/source/onewaydata.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ const MIME = `text/event-stream`;
1313
const LAST_ID = `Last-Event-ID`;
1414
const defaultChannel = `message`;
1515
const HTTP_OK = 200;
16+
const HTTP_PROBLEM = 400;
1617

17-
// fields
18+
// SSE fields
1819
const DATA = `data`;
1920
const ID = `id`;
2021
const EVENT = `event`;
22+
const RETRY = `retry`;
2123

2224
const RECONNECT = Symbol();
2325
const CONNECT = Symbol();
@@ -70,14 +72,16 @@ const isValidRequestForServerSentEvents = (request) => {
7072

7173

7274
const createEventStream = (options) => {
73-
const { path, server, asMiddleWare } = options;
75+
const { condition, server, asMiddleWare, reconnectionTime = 10 ** 4 } = options;
7476
const eventStream = Emitter({ lastEventId: 0 });
7577

7678
let responses = [];
7779
let onRequest;
7880

7981
const requestHandler = (request, response) => {
8082
if (!isValidRequestForServerSentEvents(request)) {
83+
response.writeHead(HTTP_PROBLEM);
84+
response.end();
8185
return false;
8286
}
8387

@@ -98,6 +102,8 @@ const createEventStream = (options) => {
98102
lastId,
99103
response,
100104
});
105+
} else {
106+
response.write(`${RETRY}: ${reconnectionTime}\n\n`)
101107
}
102108

103109
socket.once(`close`, () => {
@@ -145,11 +151,9 @@ const createEventStream = (options) => {
145151
};
146152
} else {
147153
onRequest = (request, response) => {
148-
const requestPath = request.url;
149-
if (requestPath !== path) {
150-
return;
154+
if (condition(request)) {
155+
requestHandler(request, response, responses);
151156
}
152-
requestHandler(request, response, responses);
153157
};
154158
server.on(`request`, onRequest);
155159
}

0 commit comments

Comments
 (0)