Skip to content

feat: replace/extend default HTTP headers with custom ones #2

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ In a `(req, res)` handler for a [`request`](https://nodejs.org/api/http.html#htt

```javascript
const SseStream = require('ssestream')
const customHeaders = {'Cache-Control': 'no-cache, no-transform'} // optional

function (req, res) {
const sse = new SseStream(req)
sse.pipe(res)
sse.pipe(res, undefined, customHeaders)

const message = {
data: 'hello\nworld',
Expand Down
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,25 @@ class SseStream extends Stream.Transform {
}
}

pipe(destination, options) {
/**
* Similar to {@link Stream.Transform.push} with the addition of specifying custom HTTP headers.
*/
pipe(destination, options, customHeaders) {
if (typeof destination.writeHead === 'function') {
destination.writeHead(200, {
var defaultHeaders = {
'Content-Type': 'text/event-stream; charset=utf-8',
'Transfer-Encoding': 'identity',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
})
}

// replace/extend default headers
if (customHeaders) {
for (const header of Object.keys(customHeaders)) {
defaultHeaders[header] = customHeaders[header]
}
}
destination.writeHead(200, defaultHeaders)
destination.flushHeaders()
}
// Some clients (Safari) don't trigger onopen until the first frame is received.
Expand Down
20 changes: 20 additions & 0 deletions test/ssestream_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,26 @@ data: hello
sse.pipe(sink)
})


it('extends and replaces default headers with custom ones', callback => {
sink.writeHead = (status, headers) => {
assert.deepEqual(headers, {
'Content-Type': 'text/event-stream; charset=utf-8',
'Transfer-Encoding': 'identity',
// 'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache, no-transform', // replace existing header
'Custom-Header': 'foo' // add custom header
})
callback()
}
const customHeaders = {
'Cache-Control': 'no-cache, no-transform', // replace existing header
'Custom-Header': 'foo' // add custom header
}
sse.pipe(sink, undefined, customHeaders)
})

it('allows an eventsource to connect', callback => {
const server = http.createServer((req, res) => {
sse = new SseStream(req)
Expand Down