Skip to content

Commit e2652b7

Browse files
UzlopakKhafraDev
andauthored
feat: Implement EventSource (#2608)
* feat: implement eventsource * add wpts * make partially work wpts * fix some * restructure, use ErrorEvent * fix * restructure, create distinct OpenEvent * add experimental warning, transform inputs * add-types * restructure * add TODO for comment * use mainFetch * k * make it terminatable * fix * fix * remove OpenEvent * fix wpt runner * fix * Apply suggestions from code review * Update lib/eventsource/index.js Co-authored-by: Khafra <maitken033380023@gmail.com> * fetching * improve BOM check * improve parseLine * rename back index.js to eventsource.js * improve * rename eventSourceState * fix * fix * fix * fix * add settings environment * fix isNetworkError * add route, fix 2 tests * fixup * improve CRLF processing, add tests * more tests * remove constants.js * improve parseLine logic * rename * fixup * add ignored tests of wpt * better * fix more * add docs * fix setting origin on message event * add more tests * fix wpt tests * add EventSource documentation to website sidebar * activate skipped wpt test * fix some remarks * simplify * remove newline * remove usage of ErrorEvent * harden * more tests * dont check for strings in isValidLastEventId * add TODOs * improve example for eventsource * trigger CI because node 21.6.1 got released --------- Co-authored-by: Khafra <maitken033380023@gmail.com>
1 parent 90675d6 commit e2652b7

File tree

125 files changed

+4891
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+4891
-4
lines changed

docs/api/EventSource.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# EventSource
2+
3+
Undici exposes a WHATWG spec-compliant implementation of [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource)
4+
for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).
5+
6+
## Instantiating EventSource
7+
8+
Undici exports a EventSource class. You can instantiate the EventSource as
9+
follows:
10+
11+
```mjs
12+
import { EventSource } from 'undici'
13+
14+
const evenSource = new EventSource('http://localhost:3000')
15+
evenSource.onmessage = (event) => {
16+
console.log(event.data)
17+
}
18+
```
19+
20+
More information about the EventSource API can be found on
21+
[MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventSource).

docsify/sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* [ProxyAgent](/docs/api/ProxyAgent.md "Undici API - ProxyAgent")
1111
* [Connector](/docs/api/Connector.md "Custom connector")
1212
* [Errors](/docs/api/Errors.md "Undici API - Errors")
13+
* [EventSource](/docs/api/EventSource.md "Undici API - EventSource")
1314
* [Fetch](/docs/api/Fetch.md "Undici API - Fetch")
1415
* [Cookies](/docs/api/Cookies.md "Undici API - Cookies")
1516
* [MockClient](/docs/api/MockClient.md "Undici API - MockClient")

examples/eventsource.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const { randomBytes } = require('crypto')
4+
const { EventSource } = require('../')
5+
6+
async function main () {
7+
const url = `https://smee.io/${randomBytes(8).toString('base64url')}`
8+
console.log(`Connecting to event source server ${url}`)
9+
const ev = new EventSource(url)
10+
ev.onmessage = console.log
11+
ev.onerror = console.log
12+
ev.onopen = console.log
13+
14+
// Special event of smee.io
15+
ev.addEventListener('ready', console.log)
16+
17+
// Ping event is sent every 30 seconds by smee.io
18+
ev.addEventListener('ping', console.log)
19+
}
20+
main()

index.js

+4
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,7 @@ module.exports.MockClient = MockClient
149149
module.exports.MockPool = MockPool
150150
module.exports.MockAgent = MockAgent
151151
module.exports.mockErrors = mockErrors
152+
153+
const { EventSource } = require('./lib/eventsource/eventsource')
154+
155+
module.exports.EventSource = EventSource

0 commit comments

Comments
 (0)