This commit is contained in:
nguyentrungthat 2025-11-11 09:57:07 +07:00
parent 32873ad388
commit 3e720691fb
2 changed files with 20 additions and 7 deletions

View File

@ -94,6 +94,7 @@ export default class LineConnection {
this.client.on('data', (data) => {
if (this.connecting) return
let message = data.toString()
let rawData = ''
if (this.isRunningScript) {
this.waitingScenario = true
this.outputBuffer += message
@ -111,9 +112,10 @@ export default class LineConnection {
this.config.output = this.config.output.slice(0, -1)
// message = message.slice(0, -1)
} else {
this.config.output += cleanData(char)
rawData += char
}
}
this.config.output += cleanData(rawData)
this.config.output = this.config.output.slice(-15000)
this.socketIO.emit('line_output', {
stationId,

View File

@ -7,12 +7,23 @@ import path from 'node:path'
* @returns {string} - The cleaned data.
*/
export const cleanData = (data: string) => {
return data
.replace(/--More--\s*BS\s*BS\s*BS\s*BS\s*BS\s*BS/g, '')
.replace(/\s*--More--\s*/g, '')
.replace(/\x1b\[[0-9;]*m/g, '') // Remove ANSI escape codes
.replace(/\x08/g, '')
.replace(/[^\x20-\x7E\r\n]/g, '') // Remove non-printable characters
return (
data
// 1⃣ Xóa chuỗi "--More--" (Cisco/Unix pager)
.replace(/--More--[\s\x08\x1b\[K]*/g, '')
// 2⃣ Xóa toàn bộ chuỗi ANSI escape sequences
// Ví dụ: ESC[2J, ESC[K, ESC[?25h, ESC[0m, ...
.replace(/\x1B\[[0-9;?]*[A-Za-z]/g, '')
// 3⃣ Xóa ký tự Backspace (BS) hoặc Delete (DEL)
.replace(/[\x08\x7F]/g, '')
// 4⃣ Xóa ký tự NUL và các control char khác (trừ \r, \n, \t)
.replace(/[^\x09\x0A\x0D\x20-\x7E]/g, '')
)
// 5⃣ Chuẩn hóa xuống dòng nếu cần
// .replace(/\r\n/g, '\n')
}