From c70200e0f870022db2ec3ec4f07a31aed30b24e6 Mon Sep 17 00:00:00 2001 From: chimurai <655241+chimurai@users.noreply.github.com> Date: Fri, 12 Jul 2019 21:16:54 +0200 Subject: [PATCH] fix(ws): fix concurrent ws requests (#344) --- dist/http-proxy-middleware.js | 23 +++++----- examples/websocket/index.html | 81 +++++++++++++++++++++-------------- package.json | 9 ++-- src/http-proxy-middleware.ts | 25 +++++------ 4 files changed, 74 insertions(+), 64 deletions(-) diff --git a/dist/http-proxy-middleware.js b/dist/http-proxy-middleware.js index 477d6867..5f942029 100644 --- a/dist/http-proxy-middleware.js +++ b/dist/http-proxy-middleware.js @@ -19,7 +19,7 @@ const Router = require("./router"); class HttpProxyMiddleware { constructor(context, opts) { this.logger = logger_1.getInstance(); - this.wsInitialized = false; + this.wsInternalSubscribed = false; // https://github.com/Microsoft/TypeScript/wiki/'this'-in-TypeScript#red-flags-for-this this.middleware = (req, res, next) => __awaiter(this, void 0, void 0, function* () { if (this.shouldProxy(this.config.context, req)) { @@ -35,16 +35,12 @@ class HttpProxyMiddleware { } }); this.catchUpgradeRequest = server => { - // subscribe once; don't subscribe on every request... - // https://github.com/chimurai/http-proxy-middleware/issues/113 - if (!this.wsInitialized) { - server.on('upgrade', this.wsUpgradeDebounced); - this.wsInitialized = true; - } + server.on('upgrade', this.handleUpgrade); + // prevent duplicate upgrade handling; + // in case external upgrade is also configured + this.wsInternalSubscribed = true; }; this.handleUpgrade = (req, socket, head) => { - // set to initialized when used externally - this.wsInitialized = true; if (this.shouldProxy(this.config.context, req)) { const activeProxyOptions = this.prepareProxyRequest(req); this.proxy.ws(req, socket, head, activeProxyOptions); @@ -120,8 +116,6 @@ class HttpProxyMiddleware { const errReference = 'https://nodejs.org/api/errors.html#errors_common_system_errors'; // link to Node Common Systems Errors page this.logger.error(errorMessage, req.url, hostname, target, err.code || err, errReference); }; - // https://github.com/chimurai/http-proxy-middleware/issues/57 - this.wsUpgradeDebounced = _.debounce(this.handleUpgrade); this.config = config_factory_1.createConfig(context, opts); this.proxyOptions = this.config.options; // create proxy @@ -134,8 +128,11 @@ class HttpProxyMiddleware { this.proxy.on('error', this.logError); // https://github.com/chimurai/http-proxy-middleware/issues/19 // expose function to upgrade externally - // middleware.upgrade = wsUpgradeDebounced - this.middleware.upgrade = this.wsUpgradeDebounced; + this.middleware.upgrade = (req, socket, head) => { + if (!this.wsInternalSubscribed) { + this.handleUpgrade(req, socket, head); + } + }; } } exports.HttpProxyMiddleware = HttpProxyMiddleware; diff --git a/examples/websocket/index.html b/examples/websocket/index.html index 001dc931..27519122 100644 --- a/examples/websocket/index.html +++ b/examples/websocket/index.html @@ -30,15 +30,15 @@

WebSocket demo

- - + +

- +

@@ -49,44 +49,61 @@

WebSocket demo