Skip to content
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

Use flushHeaders method #18

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 4 additions & 6 deletions lib/sse-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,17 @@ function broadcast(connections, packet) {
}

/**
* Calls `flush()` on the response if it exists. This is necessary in the case
* Calls `flushHeaders()` on the response if it exists. This is necessary in the case
* when you are using the `compression` middleware for express.js, where the
* middleware will wait for a certain amount of data before sending it to the
* client in order to compress in a more efficient manner.
*
* Unfortunately, node has a native (but deprecated) flush() method which will trigger
* deprecation warnings if called. Thus, we're checking the name of the function.
* Please open a pull request if you can come up with a cleaner approach to this.
*
* @param {Response} response Response object to flush
*/
function flush(response) {
if (response.flush && response.flush.name !== 'deprecated') {
if (typeof response.flushHeaders === 'function') {
response.flushHeaders();
} else if (response.flush && response.flush.name !== 'deprecated') {
response.flush();
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/sse-channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ describe('sse-channel', function() {
// - After each broadcast
var flushes = 0;
initServer({
flush: function() {
flushHeaders: function() {
flushes++;
},
history: [
Expand Down
4 changes: 2 additions & 2 deletions test/util/server-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = function(opts) {
var channel = new Channel(opts);

var server = http.createServer(function(req, res) {
if (opts.flush) {
res.flush = opts.flush;
if (opts.flushHeaders) {
res.flushHeaders = opts.flushHeaders;
}

channel.addClient(req, res, opts.addClientCallback);
Expand Down