Skip to content

Commit

Permalink
Use flushHeaders method
Browse files Browse the repository at this point in the history
  • Loading branch information
fijimunkii committed May 11, 2017
1 parent 2647262 commit d743ec9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
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

0 comments on commit d743ec9

Please sign in to comment.