From 07923745cf20e2a82067bdef5bed506f18b00f47 Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Tue, 28 May 2024 21:54:45 +0530 Subject: [PATCH] SimpleChat:HandleResponseMultiPart using NewLines helper Make handle_response_multipart logic better and cleaner. Now it allows for working with the situation, where the delta data line got from server in stream mode, could be split up when recving, but still the logic will handle it appropriately. ALERT: Rather except (for now) for last data line wrt a request's response. --- .../server/public_simplechat/simplechat.js | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index f9418578a..551d9c519 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -322,29 +322,31 @@ class SimpleChat { async handle_response_multipart(resp, apiEP, elDiv) { let elP = ui.el_create_append_p("", elDiv); if (!resp.body) { - throw Error("ERRR:SimpleChat:SC:ReadJsonEarly:No body..."); + throw Error("ERRR:SimpleChat:SC:HandleResponseMultiPart:No body..."); } let tdUtf8 = new TextDecoder("utf-8"); let rr = resp.body.getReader(); let gotBody = ""; + let xLines = new du.NewLines(); while(true) { let { value: cur, done: done } = await rr.read(); let curBody = tdUtf8.decode(cur); console.debug("DBUG:SC:PART:Str:", curBody); - if (curBody.length > 0) { - let curArrays = curBody.split("\n"); - for(let curArray of curArrays) { - console.debug("DBUG:SC:PART:StrPart:", curArray); - if (curArray.length <= 0) { - continue; - } - if (curArray.startsWith("data:")) { - curArray = curArray.substring(5); - } - let curJson = JSON.parse(curArray); - console.debug("DBUG:SC:PART:Json:", curJson); - gotBody += this.response_extract_stream(curJson, apiEP); + xLines.add_append(curBody); + while(true) { + let curLine = xLines.shift(); + if (curLine == undefined) { + break; } + if (curLine.trim() == "") { + continue; + } + if (curLine.startsWith("data:")) { + curLine = curLine.substring(5); + } + let curJson = JSON.parse(curLine); + console.debug("DBUG:SC:PART:Json:", curJson); + gotBody += this.response_extract_stream(curJson, apiEP); } elP.innerText = gotBody; elP.scrollIntoView(false);