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 35 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
17 changes: 17 additions & 0 deletions examples/eventsource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

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

async function main () {
const ev = new EventSource('https://smee.io/wcGp009TievZCLT')
ev.onmessage = (event) => {
console.log(event)
}
ev.onerror = event => {
console.log(event)
}
ev.onopen = event => {
console.log(event)
}
}
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
93 changes: 93 additions & 0 deletions lib/eventsource/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict'

/**
* A reconnection time, in milliseconds. This must initially be an implementation-defined value,
* probably in the region of a few seconds.
*
* In Comparison:
* - Chrome uses 3000ms.
* - Deno uses 5000ms.
*/
const defaultReconnectionTime = 3000

/**
* The readyState attribute represents the state of the connection.
* @enum
* @readonly
* @see https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate-dev
*/

/**
* The connection has not yet been established, or it was closed and the user
* agent is reconnecting.
* @type {0}
*/
const CONNECTING = 0

/**
* The user agent has an open connection and is dispatching events as it
* receives them.
* @type {1}
*/
const OPEN = 1

/**
* The connection is not open, and the user agent is not trying to reconnect.
* @type {2}
*/
const CLOSED = 2

/**
* Requests for the element will have their mode set to "cors" and their credentials mode set to "same-origin".
* @type {'anonymous'}
*/
const ANONYMOUS = 'anonymous'

/**
* Requests for the element will have their mode set to "cors" and their credentials mode set to "include".
* @type {'use-credentials'}
*/
const USE_CREDENTIALS = 'use-credentials'

/**
* @type {number[]} BOM
*/
const BOM = [0xEF, 0xBB, 0xBF]
/**
* @type {10} LF
*/
const LF = 0x0A
/**
* @type {13} CR
*/
const CR = 0x0D
/**
* @type {58} COLON
*/
const COLON = 0x3A
/**
* @type {32} SPACE
*/
const SPACE = 0x20

const validMessageEventFieldNames = [
'data',
'event',
'id',
'retry'
]

module.exports = {
defaultReconnectionTime,
CONNECTING,
OPEN,
CLOSED,
BOM,
LF,
CR,
COLON,
SPACE,
validMessageEventFieldNames,
ANONYMOUS,
USE_CREDENTIALS
}
Loading
Loading