From 8d9faa56306808d1a072cfbe3120ef0daec5b4ff Mon Sep 17 00:00:00 2001 From: Elin Angelow Date: Thu, 8 Feb 2024 13:11:52 +0200 Subject: [PATCH] fix: connection were created when write is sent --- lib/wss.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/wss.js b/lib/wss.js index 2701d48..efd2974 100644 --- a/lib/wss.js +++ b/lib/wss.js @@ -79,7 +79,7 @@ const Wss = ({ for (const [id, conn] of this.connections) { if (message.params.connectionIds.indexOf(id) > -1) { this.dumper && this.dumper(encoded, connectionId); - this.connection(id).send(encoded); + this.connection(id)?.send(encoded); } } } else { @@ -106,7 +106,7 @@ const Wss = ({ }); this.dumper && this.dumper(encoded, message.params.connectionId); this.connection(message.params.connectionId) - .send(encoded); + ?.send(encoded); const response = await rq.wait; return response; } catch (e) { @@ -220,7 +220,7 @@ const Wss = ({ id: decoded.id }, decoded); this.dumper && this.dumper(encoded, connectionId); - this.connection(connectionId).send(encoded); + this.connection(connectionId)?.send(encoded); // metrics const ct = process.hrtime(); const f = ct[0] - response.meta.passTrough.time[0]; @@ -236,6 +236,9 @@ const Wss = ({ } refreshConnection(id, connection) { const existing = this.connections.get(id); + if (!existing && !connection) { + return; + } if (existing?.timeOut) { clearTimeout(existing.timeOut); if (!connection) { // update only timeout @@ -261,7 +264,7 @@ const Wss = ({ } connection(id) { this.refreshConnection(id); - return this.connections.get(id).connection; + return this.connections.get(id)?.connection; } async connect() { return await (new Promise((resolve, reject) => {