From d955cc4bac6695ed053840431475ac056871bd81 Mon Sep 17 00:00:00 2001 From: Taha Yassine Kraiem Date: Fri, 4 Mar 2022 21:49:08 +0100 Subject: [PATCH] feat(utilities): EE support different responses --- ee/utilities/servers/websocket-cluster.js | 45 ++++++++++++++++------- ee/utilities/servers/websocket.js | 45 ++++++++++++++++------- 2 files changed, 64 insertions(+), 26 deletions(-) diff --git a/ee/utilities/servers/websocket-cluster.js b/ee/utilities/servers/websocket-cluster.js index f9255905f..605879922 100644 --- a/ee/utilities/servers/websocket-cluster.js +++ b/ee/utilities/servers/websocket-cluster.js @@ -47,9 +47,14 @@ const socketsList = async function (req, res) { liveSessions[projectKey].push(sessionId); } } - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions})); + let result = {"data": liveSessions}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(result)); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-list`, socketsList); @@ -64,9 +69,14 @@ const socketsListByProject = async function (req, res) { liveSessions[projectKey].push(sessionId); } } - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions[req.params.projectKey] || []})); + let result = {"data": liveSessions[req.params.projectKey] || []}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(result)); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-list/:projectKey`, socketsListByProject); @@ -87,10 +97,14 @@ const socketsLive = async function (req, res) { liveSessions[projectKey] = uniqueSessions(liveSessions[projectKey]); } } - - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions})); + let result = {"data": liveSessions}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(result)); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-live`, socketsLive); @@ -111,9 +125,14 @@ const socketsLiveByProject = async function (req, res) { liveSessions[projectKey] = uniqueSessions(liveSessions[projectKey]); } } - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions[req.params.projectKey] || []})); + let result = {"data": liveSessions[req.params.projectKey] || []}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(result)); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-live/:projectKey`, socketsLiveByProject); diff --git a/ee/utilities/servers/websocket.js b/ee/utilities/servers/websocket.js index 2fb5c3499..37331e7a5 100644 --- a/ee/utilities/servers/websocket.js +++ b/ee/utilities/servers/websocket.js @@ -27,9 +27,14 @@ const socketsList = function (req, res) { liveSessions[projectKey].push(sessionId); } } - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions})); + let result = {"data": liveSessions}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(result)); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-list`, socketsList); @@ -43,9 +48,14 @@ const socketsListByProject = function (req, res) { liveSessions[projectKey].push(sessionId); } } - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions[req.params.projectKey] || []})); + let result = {"data": liveSessions[req.params.projectKey] || []}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify()); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-list/:projectKey`, socketsListByProject); @@ -64,10 +74,14 @@ const socketsLive = async function (req, res) { } } } - - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions})); + let result = {"data": liveSessions}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(result)); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-live`, socketsLive); @@ -86,9 +100,14 @@ const socketsLiveByProject = async function (req, res) { } } } - res.statusCode = 200; - res.setHeader('Content-Type', 'application/json'); - res.end(JSON.stringify({"data": liveSessions[req.params.projectKey] || []})); + let result = {"data": liveSessions[req.params.projectKey] || []}; + if (process.env.uws !== "true") { + res.statusCode = 200; + res.setHeader('Content-Type', 'application/json'); + res.end(JSON.stringify(result)); + } else { + res.writeStatus('200 OK').writeHeader('Content-Type', 'application/json').end(JSON.stringify(result)); + } } wsRouter.get(`/${process.env.S3_KEY}/sockets-live/:projectKey`, socketsLiveByProject);