Skip to content

feat: report event source headers on open #33

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

Merged
merged 3 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ jobs:
set -o pipefail
make run-contract-tests | tee test-harness.log
- name: Upload Test Service Logs
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: always()
with:
name: test-service-${{ matrix.node }}
path: test-service.log
- name: Upload Test Harness Logs
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
if: always()
with:
name: test-harness-${{ matrix.node }}
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ In a browser that is using native `EventSource` the extra argument would simply

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.

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:

```javascript
{
'content-type': 'text/event-stream; charset=utf-8',
'transfer-encoding': 'chunked',
'connection': 'close',
'accept-ranges': 'bytes',
'cache-control': 'no-cache, no-store, must-revalidate',
}
```

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"`).

```javascript
Expand Down
2 changes: 1 addition & 1 deletion lib/eventsource.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ function EventSource (url, eventSourceInitDict) {
res.removeAllListeners('end')
failOnce()
})
_emit(new Event('open'))
_emit(new Event('open', { headers: res.headers }))

// text/event-stream parser adapted from webkit's
// Source/WebCore/page/EventSource.cpp
Expand Down
11 changes: 6 additions & 5 deletions test/eventsource_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ async function expectNothingReceived (q) {
assert.ok(q.isEmpty())
}

function writeEvents (chunks) {
function writeEvents (chunks, headers = {}) {
const resHeaders = Object.assign({}, headers, { 'Content-Type': 'text/event-stream' })
const q = new AsyncQueue()
chunks.forEach(chunk => q.add(chunk))
return TestHttpHandlers.chunkedStream(200, {'Content-Type': 'text/event-stream'}, q)
return TestHttpHandlers.chunkedStream(200, resHeaders, q)
}

function assertRange (min, max, value) {
Expand Down Expand Up @@ -901,13 +902,13 @@ describe('Events', function () {
})
})

it('emits open event when connection is established', async () => {
it('emits open event with headers when connection is established', async () => {
await withServer(async server => {
server.byDefault(writeEvents([]))

server.byDefault(writeEvents([], { 'X-LD-EnvId': '12345' }))
await withEventSource(server, async es => {
const e = await waitForOpenEvent(es)
assert.equal(e.type, 'open')
assert.equal(e.headers['x-ld-envid'], '12345')
})
})
})
Expand Down