Skip to content

Commit

Permalink
bug(websocket): fix memory leak when option 'ws:true' is used.
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Sep 26, 2016
1 parent 4db1bfa commit e4f8da7
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = HttpProxyMiddleware;
function HttpProxyMiddleware(context, opts) {
// https://github.com/chimurai/http-proxy-middleware/issues/57
var wsUpgradeDebounced = _.debounce(handleUpgrade);
var wsInitialized = false;
var config = configFactory.createConfig(context, opts);
var proxyOptions = config.options;

Expand Down Expand Up @@ -43,15 +44,24 @@ function HttpProxyMiddleware(context, opts) {
}

if (proxyOptions.ws === true) {
// use initial request to access the server object to subscribe to http upgrade event
catchUpgradeRequest(req.connection.server);
}
}

function catchUpgradeRequest(server) {
server.on('upgrade', wsUpgradeDebounced);
// subscribe once; don't subscribe on every request...
// https://github.com/chimurai/http-proxy-middleware/issues/113
if (!wsInitialized) {
server.on('upgrade', wsUpgradeDebounced);
wsInitialized = true;
}
}

function handleUpgrade(req, socket, head) {
// set to initialized when used externally
wsInitialized = true;

if (shouldProxy(config.context, req)) {
var activeProxyOptions = prepareProxyRequest(req);
proxy.ws(req, socket, head, activeProxyOptions);
Expand Down

0 comments on commit e4f8da7

Please sign in to comment.