diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6fb96e85..ace00d18 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 }} diff --git a/README.md b/README.md index a6845253..4473ae93 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/eventsource.js b/lib/eventsource.js index 1a4b0405..feb6ee8b 100644 --- a/lib/eventsource.js +++ b/lib/eventsource.js @@ -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 diff --git a/test/eventsource_test.js b/test/eventsource_test.js index 9c60ae83..e866acee 100644 --- a/test/eventsource_test.js +++ b/test/eventsource_test.js @@ -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) { @@ -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') }) }) })