From e2fd4c035c8c92dc81d2d1c250b3695b25b69160 Mon Sep 17 00:00:00 2001 From: Edvin Dulko Date: Tue, 14 May 2024 00:41:58 +0200 Subject: [PATCH 1/2] fix UTF-8 characters corruption --- lib/client.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/client.js b/lib/client.js index 014a8e7..4c18014 100644 --- a/lib/client.js +++ b/lib/client.js @@ -56,21 +56,22 @@ function invoke(socket, token, args, text) { args = ['--no-color'].concat(args); } - let buf = ''; + let buf = Buffer.alloc(0); socket.on('data', (chunk) => { - buf += chunk; - const p = buf.lastIndexOf('\n'); + buf = Buffer.concat([buf, chunk]); + const p = buf.lastIndexOf(Buffer.from('\n')); if (p !== -1) { - out.write(buf.substring(0, p + 1)); - buf = buf.substring(p + 1); + out.write(buf.subarray(0, p + 1).toString()); + buf = buf.subarray(p + 1); } }); socket.on('end', () => { - if (buf) { - if (buf.startsWith('# exit ')) { - process.exitCode = Number(buf.substring(7)); + const str = buf.toString(); + if (str) { + if (str.startsWith('# exit ')) { + process.exitCode = Number(str.substring(7)); } else { - out.write(buf); + out.write(str); } } }); From 918383d41b15462479e96ab8d4558a5c25bcd49f Mon Sep 17 00:00:00 2001 From: Edvin Dulko Date: Tue, 14 May 2024 01:25:01 +0200 Subject: [PATCH 2/2] create a variable for a buffer that represents \n --- lib/client.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/client.js b/lib/client.js index 4c18014..80e2cf4 100644 --- a/lib/client.js +++ b/lib/client.js @@ -56,10 +56,11 @@ function invoke(socket, token, args, text) { args = ['--no-color'].concat(args); } + const newLineBuf = Buffer.from('\n'); let buf = Buffer.alloc(0); socket.on('data', (chunk) => { buf = Buffer.concat([buf, chunk]); - const p = buf.lastIndexOf(Buffer.from('\n')); + const p = buf.lastIndexOf(newLineBuf); if (p !== -1) { out.write(buf.subarray(0, p + 1).toString()); buf = buf.subarray(p + 1);