Skip to content

Commit

Permalink
Merge pull request #322 from cainrus/support_http2
Browse files Browse the repository at this point in the history
Support HTTP/2
  • Loading branch information
glenjamin committed Jul 13, 2018
2 parents 8d46df6 + 22e922b commit 84c3f54
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 12 additions & 4 deletions middleware.js
Expand Up @@ -57,16 +57,24 @@ function createEventStream(heartbeat) {
}, heartbeat).unref();
return {
handler: function(req, res) {
req.socket.setKeepAlive(true);
res.writeHead(200, {
var headers = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/event-stream;charset=utf-8',
'Cache-Control': 'no-cache, no-transform',
'Connection': 'keep-alive',
// While behind nginx, event stream should not be buffered:
// http://nginx.org/docs/http/ngx_http_proxy_module.html#proxy_buffering
'X-Accel-Buffering': 'no'
});
};

var isHttp1 = !(parseInt(req.httpVersion) >= 2);
if (isHttp1) {
req.socket.setKeepAlive(true);
Object.assign(headers, {
'Connection': 'keep-alive',
});
}

res.writeHead(200, headers);
res.write('\n');
var id = clientId++;
clients[id] = res;
Expand Down
10 changes: 10 additions & 0 deletions test/middleware-test.js
Expand Up @@ -183,6 +183,16 @@ describe("middleware", function() {
}
});
});
// Express HTTP/2 support is in progress: https://github.com/expressjs/express/pull/3390
it("should not contain `conntection: keep-alive` header for HTTP/2 request");
it("should contain `conntection: keep-alive` header for HTTP/1 request", function(done) {
request('/__webpack_hmr')
.end(function(err, res) {
if (err) return done(err);
assert.equal(res.headers['connection'], 'keep-alive');
done();
});
});
});

beforeEach(function() {
Expand Down

0 comments on commit 84c3f54

Please sign in to comment.