Skip to content

Commit

Permalink
feat: Implement EventSource (#2608)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Uzlopak and KhafraDev committed Jan 24, 2024
1 parent 90675d6 commit e2652b7
Show file tree
Hide file tree
Showing 125 changed files with 4,891 additions and 4 deletions.
21 changes: 21 additions & 0 deletions docs/api/EventSource.md
@@ -0,0 +1,21 @@
# EventSource

Undici exposes a WHATWG spec-compliant implementation of [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource)
for [Server-Sent Events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events).

## Instantiating EventSource

Undici exports a EventSource class. You can instantiate the EventSource as
follows:

```mjs
import { EventSource } from 'undici'

const evenSource = new EventSource('http://localhost:3000')
evenSource.onmessage = (event) => {
console.log(event.data)
}
```

More information about the EventSource API can be found on
[MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventSource).
1 change: 1 addition & 0 deletions docsify/sidebar.md
Expand Up @@ -10,6 +10,7 @@
* [ProxyAgent](/docs/api/ProxyAgent.md "Undici API - ProxyAgent")
* [Connector](/docs/api/Connector.md "Custom connector")
* [Errors](/docs/api/Errors.md "Undici API - Errors")
* [EventSource](/docs/api/EventSource.md "Undici API - EventSource")
* [Fetch](/docs/api/Fetch.md "Undici API - Fetch")
* [Cookies](/docs/api/Cookies.md "Undici API - Cookies")
* [MockClient](/docs/api/MockClient.md "Undici API - MockClient")
Expand Down
20 changes: 20 additions & 0 deletions examples/eventsource.js
@@ -0,0 +1,20 @@
'use strict'

const { randomBytes } = require('crypto')
const { EventSource } = require('../')

async function main () {
const url = `https://smee.io/${randomBytes(8).toString('base64url')}`
console.log(`Connecting to event source server ${url}`)
const ev = new EventSource(url)
ev.onmessage = console.log
ev.onerror = console.log
ev.onopen = console.log

// Special event of smee.io
ev.addEventListener('ready', console.log)

// Ping event is sent every 30 seconds by smee.io
ev.addEventListener('ping', console.log)
}
main()
4 changes: 4 additions & 0 deletions index.js
Expand Up @@ -149,3 +149,7 @@ module.exports.MockClient = MockClient
module.exports.MockPool = MockPool
module.exports.MockAgent = MockAgent
module.exports.mockErrors = mockErrors

const { EventSource } = require('./lib/eventsource/eventsource')

module.exports.EventSource = EventSource

0 comments on commit e2652b7

Please sign in to comment.