Skip to content

Commit aeb8068

Browse files
feat: report event source headers on open
1 parent 213e056 commit aeb8068

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

lib/eventsource.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ function EventSource (url, eventSourceInitDict) {
252252
res.removeAllListeners('end')
253253
failOnce()
254254
})
255-
_emit(new Event('open'))
255+
_emit(new Event('open', { headers: res.headers }))
256256

257257
// text/event-stream parser adapted from webkit's
258258
// Source/WebCore/page/EventSource.cpp

test/eventsource_test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ async function expectNothingReceived (q) {
5959
assert.ok(q.isEmpty())
6060
}
6161

62-
function writeEvents (chunks) {
62+
function writeEvents (chunks, headers = {}) {
63+
const resHeaders = Object.assign({}, headers, { 'Content-Type': 'text/event-stream' })
6364
const q = new AsyncQueue()
6465
chunks.forEach(chunk => q.add(chunk))
65-
return TestHttpHandlers.chunkedStream(200, {'Content-Type': 'text/event-stream'}, q)
66+
return TestHttpHandlers.chunkedStream(200, resHeaders, q)
6667
}
6768

6869
function assertRange (min, max, value) {
@@ -901,13 +902,13 @@ describe('Events', function () {
901902
})
902903
})
903904

904-
it('emits open event when connection is established', async () => {
905+
it('emits open event with headers when connection is established', async () => {
905906
await withServer(async server => {
906-
server.byDefault(writeEvents([]))
907-
907+
server.byDefault(writeEvents([], { 'X-LD-EnvId': '12345' }))
908908
await withEventSource(server, async es => {
909909
const e = await waitForOpenEvent(es)
910910
assert.equal(e.type, 'open')
911+
assert.equal(e.headers['x-ld-envid'], '12345')
911912
})
912913
})
913914
})

0 commit comments

Comments
 (0)