Skip to content

Commit e8539f3

Browse files
feat: report event source headers on open (#33)
1 parent 213e056 commit e8539f3

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ jobs:
3333
set -o pipefail
3434
make run-contract-tests | tee test-harness.log
3535
- name: Upload Test Service Logs
36-
uses: actions/upload-artifact@v3
36+
uses: actions/upload-artifact@v4
3737
if: always()
3838
with:
3939
name: test-service-${{ matrix.node }}
4040
path: test-service.log
4141
- name: Upload Test Harness Logs
42-
uses: actions/upload-artifact@v3
42+
uses: actions/upload-artifact@v4
4343
if: always()
4444
with:
4545
name: test-harness-${{ matrix.node }}

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ In a browser that is using native `EventSource` the extra argument would simply
5656

5757
Like the standard `EventSource`, this implementation emits the event `open` when a stream has been started and `error` when a stream has failed for any reason. All events have a single parameter which is an instance of the `Event` class.
5858

59+
The `open` event has the following extended behavior: the event object will have a `headers` property which is an object representing the HTTP response headers received when opening the stream. For example:
60+
61+
```javascript
62+
{
63+
'content-type': 'text/event-stream; charset=utf-8',
64+
'transfer-encoding': 'chunked',
65+
'connection': 'close',
66+
'accept-ranges': 'bytes',
67+
'cache-control': 'no-cache, no-store, must-revalidate',
68+
}
69+
```
70+
5971
The `error` event has the following extended behavior: for an HTTP error response, the event object will have a `status` property (such as `401`) and optionally a `message` property (such as `"Unauthorized"`).
6072

6173
```javascript

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)