From ace37042fa5357b81dcc0173b9e405d266e1f2cd Mon Sep 17 00:00:00 2001 From: HanishKVC Date: Wed, 29 May 2024 12:46:40 +0530 Subject: [PATCH] SimpleChat:MultiPart/Stream flow cleanup Dont try utf8-decode and newlines-add_append if no data to work on. If there is no more data to get (ie done is set), then let NewLines instance return line without newline at end, So that we dont miss out on any last-data-line without newline kind of scenario. Pass stream flag wrt utf-8 decode, so that if any multi-byte char is only partly present in the passed buffer, it can be accounted for along with subsequent buffer. At sametime, bcas of utf-8's characteristics there shouldnt be any unaccounted bytes at end, for valid block of utf8 data split across chunks, so not bothering calling with stream set to false at end. LATER: Look at TextDecoder's implementation, for any over intelligence, it may be doing.. If needed, one can use done flag to account wrt both cases. --- examples/server/public_simplechat/datautils.mjs | 12 ++++++------ examples/server/public_simplechat/simplechat.js | 10 ++++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/server/public_simplechat/datautils.mjs b/examples/server/public_simplechat/datautils.mjs index 9ac1b76e8..75159d6b1 100644 --- a/examples/server/public_simplechat/datautils.mjs +++ b/examples/server/public_simplechat/datautils.mjs @@ -247,17 +247,17 @@ export class NewLines { } /** - * Shift the oldest/0th line in the array. - * Optionally control whether only full lines will be returned - * or will a partial line (last line) be returned. - * @param {boolean} bFullOnly + * Shift the oldest/earliest/0th line in the array. [Old-New|Earliest-Latest] + * Optionally control whether only full lines (ie those with newline at end) will be returned + * or will a partial line without a newline at end (can only be the last line) be returned. + * @param {boolean} bFullWithNewLineOnly */ - shift(bFullOnly=true) { + shift(bFullWithNewLineOnly=true) { let line = this.lines[0]; if (line == undefined) { return undefined; } - if ((line[line.length-1] != "\n") && bFullOnly){ + if ((line[line.length-1] != "\n") && bFullWithNewLineOnly){ return undefined; } return this.lines.shift(); diff --git a/examples/server/public_simplechat/simplechat.js b/examples/server/public_simplechat/simplechat.js index bcbb8f5c0..c9ff2898d 100644 --- a/examples/server/public_simplechat/simplechat.js +++ b/examples/server/public_simplechat/simplechat.js @@ -330,11 +330,13 @@ class SimpleChat { 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); - xLines.add_append(curBody); + if (cur) { + let curBody = tdUtf8.decode(cur, {stream: true}); + console.debug("DBUG:SC:PART:Str:", curBody); + xLines.add_append(curBody); + } while(true) { - let curLine = xLines.shift(); + let curLine = xLines.shift(!done); if (curLine == undefined) { break; }