Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ out/
**/*.xml
dist/**
!dist/core/
!dist/core/package.json
!dist/core/package.json
/core/
/bundles/
10 changes: 5 additions & 5 deletions src/core/TerminalDataProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { type IDataProcessor } from './Interfaces'
/** class to process serial over lan data **/
export class TerminalDataProcessor implements IDataProcessor {
terminal: AmtTerminal
private readonly decoder = new TextDecoder('utf-8')

constructor(terminal) {
this.terminal = terminal
}
Expand All @@ -19,17 +21,15 @@ export class TerminalDataProcessor implements IDataProcessor {
/** processing data received from serial port**/
processData = (str: string): any => {
if (this.terminal.capture != null) this.terminal.capture = String(this.terminal.capture) + str
let c = ''
const bytes: number[] = []
for (let i = 0; i < str.length; i++) {
const ch = str.charCodeAt(i)
if (str[i] === 'J') {
this.clearTerminal()
} else if ((ch & 0x80) !== 0) {
c += String.fromCharCode(this.terminal.AsciiToUnicode[ch & 0x7f])
} else {
c += `${str[i]}`
bytes.push(ch & 0xff)
}
}
this.processDataToXterm(c)
this.processDataToXterm(this.decoder.decode(new Uint8Array(bytes), { stream: true }))
}
}
26 changes: 16 additions & 10 deletions src/test/terminaldataprocessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import { TerminalDataProcessor } from '../core/TerminalDataProcessor'
import { AmtTerminal } from '../core/AMTTerminal'
import { AmtTerminal2 } from './helper/amtTerminal2'
import { TextDecoder, TextEncoder } from 'util'

globalThis.TextDecoder = TextDecoder as typeof globalThis.TextDecoder
globalThis.TextEncoder = TextEncoder as typeof globalThis.TextEncoder

describe('Test TerminalDataProcessor class', () => {
let result = ''
it('Test TerminalDataProcessor for processData', () => {
it('decodes UTF-8 data received in one chunk', () => {
// callback function for Unit testing
function callback(value: string): void {
result = value
Expand All @@ -21,34 +25,36 @@ describe('Test TerminalDataProcessor class', () => {
tdataprocessor.processDataToXterm = callback

// Test input
const s = 'abcD123?!=*“€'
const s = String.fromCharCode(...new TextEncoder().encode('abcD123?!=*“€'))

// call processdata
tdataprocessor.processData(s)

// Test output
expect(result).toBe('abcD123?!=*“¼')
expect(result).toBe('abcD123?!=*“')
})

it('Test TerminalDataProcessor for processData', () => {
it('decodes UTF-8 data split across chunks', () => {
// callback function for Unit testing
function callback(value: string): void {
result = value
result += value
}

// create object and set callback
result = ''
const term = new AmtTerminal2(1)
const tdataprocessor = new TerminalDataProcessor(term)
tdataprocessor.processDataToXterm = callback

// Test input
const s = "123Z?“€'"
const encoded = new TextEncoder().encode("123Z?“€'")
const split = encoded.indexOf(0xe2) + 1

// call processdata
tdataprocessor.processData(s)
tdataprocessor.processData(String.fromCharCode(...encoded.slice(0, split)))
tdataprocessor.processData(String.fromCharCode(...encoded.slice(split)))

// Test output
expect(result).toBe("123Z?“¼'")
expect(term.capture).toBe("123Z?“€'")
expect(result).toBe("123Z?“'")
expect(term.capture).toBe(String.fromCharCode(...encoded))
})
})
Loading