From d743ec9d62e7df20b05ee7f03aa464525e81e634 Mon Sep 17 00:00:00 2001 From: Harrison Powers Date: Thu, 11 May 2017 16:20:48 -0400 Subject: [PATCH] Use flushHeaders method As `flush` was deprecated https://github.com/nodejs/node/blob/master/lib/_http_outgoing.js#L892 https://github.com/nodejs/node/pull/1156 --- lib/sse-channel.js | 10 ++++------ test/sse-channel.test.js | 2 +- test/util/server-init.js | 4 ++-- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/sse-channel.js b/lib/sse-channel.js index 8f44bb4..d4979d6 100644 --- a/lib/sse-channel.js +++ b/lib/sse-channel.js @@ -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(); } } diff --git a/test/sse-channel.test.js b/test/sse-channel.test.js index ad446e3..8eef0f1 100644 --- a/test/sse-channel.test.js +++ b/test/sse-channel.test.js @@ -662,7 +662,7 @@ describe('sse-channel', function() { // - After each broadcast var flushes = 0; initServer({ - flush: function() { + flushHeaders: function() { flushes++; }, history: [ diff --git a/test/util/server-init.js b/test/util/server-init.js index ed486b4..29ce31f 100644 --- a/test/util/server-init.js +++ b/test/util/server-init.js @@ -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);