Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement EventSource #2608

Merged
merged 63 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
bace6e2
feat: implement eventsource
Uzlopak Jan 11, 2024
cb1db29
add wpts
KhafraDev Jan 11, 2024
926438e
make partially work wpts
Uzlopak Jan 11, 2024
1c7fcb2
fix some
Uzlopak Jan 12, 2024
2e69663
restructure, use ErrorEvent
Uzlopak Jan 12, 2024
79e7419
fix
Uzlopak Jan 12, 2024
54b234a
restructure, create distinct OpenEvent
Uzlopak Jan 12, 2024
3337d7d
add experimental warning, transform inputs
Uzlopak Jan 12, 2024
82214e0
add-types
Uzlopak Jan 15, 2024
56ec56f
restructure
Uzlopak Jan 15, 2024
8c76a8a
add TODO for comment
Uzlopak Jan 15, 2024
755b2d6
use mainFetch
Uzlopak Jan 15, 2024
4e7eeb5
k
Uzlopak Jan 15, 2024
a46bb20
make it terminatable
Uzlopak Jan 15, 2024
bb416e6
fix
Uzlopak Jan 15, 2024
36d9747
fix
Uzlopak Jan 15, 2024
bdc793c
remove OpenEvent
Uzlopak Jan 15, 2024
14e503b
fix wpt runner
Uzlopak Jan 15, 2024
c73c67d
fix
Uzlopak Jan 15, 2024
11ee17d
Merge branch 'main' into eventsource
Uzlopak Jan 16, 2024
2c65c75
Apply suggestions from code review
Uzlopak Jan 16, 2024
7c38511
Update lib/eventsource/index.js
Uzlopak Jan 16, 2024
d7a7dab
fetching
KhafraDev Jan 16, 2024
110fc04
Merge branch 'main' into eventsource
Uzlopak Jan 16, 2024
1fa548b
improve BOM check
Uzlopak Jan 17, 2024
03dc926
improve parseLine
Uzlopak Jan 17, 2024
e04194c
rename back index.js to eventsource.js
Uzlopak Jan 17, 2024
041c648
improve
Uzlopak Jan 17, 2024
55e2729
rename eventSourceState
Uzlopak Jan 17, 2024
fc32639
fix
Uzlopak Jan 17, 2024
11f5814
fix
Uzlopak Jan 17, 2024
6ab5dea
fix
Uzlopak Jan 17, 2024
ba941ea
fix
Uzlopak Jan 17, 2024
f8dbc1a
add settings environment
Uzlopak Jan 17, 2024
b9fed12
fix isNetworkError
Uzlopak Jan 17, 2024
7a834fb
add route, fix 2 tests
KhafraDev Jan 17, 2024
ffaac56
fixup
KhafraDev Jan 17, 2024
46360dd
improve CRLF processing, add tests
Uzlopak Jan 17, 2024
05b0fa7
more tests
Uzlopak Jan 17, 2024
ef19996
remove constants.js
Uzlopak Jan 17, 2024
9ea9156
improve parseLine logic
Uzlopak Jan 18, 2024
1a2d508
rename
Uzlopak Jan 18, 2024
6d8a2e9
fixup
KhafraDev Jan 18, 2024
1531c8d
add ignored tests of wpt
Uzlopak Jan 18, 2024
7b32778
better
Uzlopak Jan 18, 2024
4b7ad3a
fix more
Uzlopak Jan 18, 2024
2e74bb3
Merge branch 'main' into eventsource
Uzlopak Jan 18, 2024
67fef73
add docs
Uzlopak Jan 18, 2024
104da43
fix setting origin on message event
Uzlopak Jan 19, 2024
2ca2594
add more tests
Uzlopak Jan 19, 2024
98eae69
fix wpt tests
Uzlopak Jan 19, 2024
49d40cc
add EventSource documentation to website sidebar
Uzlopak Jan 19, 2024
6d3f48a
activate skipped wpt test
Uzlopak Jan 19, 2024
8841ef1
fix some remarks
Uzlopak Jan 20, 2024
15fea93
simplify
Uzlopak Jan 20, 2024
cb392ef
remove newline
Uzlopak Jan 20, 2024
920885e
remove usage of ErrorEvent
Uzlopak Jan 20, 2024
b643dde
harden
Uzlopak Jan 20, 2024
7fd4a64
more tests
Uzlopak Jan 20, 2024
9592c47
dont check for strings in isValidLastEventId
Uzlopak Jan 20, 2024
3e912ec
add TODOs
Uzlopak Jan 20, 2024
4b18785
improve example for eventsource
Uzlopak Jan 20, 2024
42fbe93
trigger CI because node 21.6.1 got released
Uzlopak Jan 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/api/EventSource.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Loading
Loading