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.
This commit is contained in:
HanishKVC 2024-05-29 12:46:40 +05:30
parent fcd385c36a
commit ace37042fa
2 changed files with 12 additions and 10 deletions

View file

@ -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();

View file

@ -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;
}