Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse all XML as proper strings (SOFIE-3006) #103

Merged
merged 5 commits into from
Aug 27, 2024
Merged
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
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"packages": [
"packages/*"
],
"version": "4.1.1",
"version": "4.2.0-alpha.0",
"npmClient": "yarn",
"useWorkspaces": true
}
7 changes: 4 additions & 3 deletions packages/connector/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mos-connection/connector",
"version": "4.1.1",
"version": "4.2.0-alpha.0",
"description": "MOS compliant TCP/IP Socket connection.",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down Expand Up @@ -29,8 +29,9 @@
"/LICENSE"
],
"dependencies": {
"@mos-connection/helper": "4.1.1",
"@mos-connection/model": "4.1.1",
"@mos-connection/helper": "4.2.0-alpha.0",
"@mos-connection/model": "4.2.0-alpha.0",
"eventemitter3": "^5.0.1",
"iconv-lite": "^0.6.3",
"tslib": "^2.5.3",
"xml-js": "^1.6.11",
Expand Down
64 changes: 42 additions & 22 deletions packages/connector/src/MosConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import { MosDevice } from './MosDevice'
import { SocketServerEvent, SocketDescription, IncomingConnectionType } from './connection/socketConnection'
import { NCSServerConnection } from './connection/NCSServerConnection'
import { MosModel } from '@mos-connection/helper'
import { EventEmitter } from 'events'
import { EventEmitter } from 'eventemitter3'
import * as iconv from 'iconv-lite'
import { MosMessageParser } from './connection/mosMessageParser'
import { ParsedMosMessage, MosMessageParser } from './connection/mosMessageParser'
import { IConnectionConfig, IMosConnection, IMOSDeviceConnectionOptions } from './api'
import { PROFILE_VALIDNESS_CHECK_WAIT_TIME } from './lib'

export class MosConnection extends EventEmitter implements IMosConnection {
export interface MosConnectionEvents {
// Note: These match the events defined in IMosConnection
rawMessage: (source: string, type: string, message: string) => void
info: (message: string, data?: any) => void
warning: (message: string) => void
error: (error: Error) => void
}
export class MosConnection extends EventEmitter<MosConnectionEvents> implements IMosConnection {
static readonly CONNECTION_PORT_LOWER = 10540
static readonly CONNECTION_PORT_UPPER = 10541
static readonly CONNECTION_PORT_QUERY = 10542
Expand Down Expand Up @@ -95,7 +102,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
this.emit('warning', 'primary: ' + str)
})
primary.on('error', (error: Error) => {
this.emit('error', 'primary: ' + error + ' ' + (typeof error === 'object' ? error.stack : ''))
error.message = `primary: ${error.message}`
this.emit('error', error)
})
primary.on('info', (str: string) => {
this.emit('info', 'primary: ' + str)
Expand Down Expand Up @@ -140,7 +148,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
this.emit('warning', 'secondary: ' + str)
})
secondary.on('error', (error: Error) => {
this.emit('error', 'secondary: ' + error + ' ' + (typeof error === 'object' ? error.stack : ''))
error.message = `secondary: ${error.message}`
this.emit('error', error)
})
secondary.on('info', (str: string) => {
this.emit('info', 'secondary: ' + str)
Expand Down Expand Up @@ -359,9 +368,9 @@ export class MosConnection extends EventEmitter implements IMosConnection {
socketServer.on(SocketServerEvent.CLIENT_CONNECTED, (e: SocketDescription) =>
this._registerIncomingClient(e)
)
socketServer.on(SocketServerEvent.ERROR, (e) => {
socketServer.on(SocketServerEvent.ERROR, (err) => {
// handle error
this.emit('error', e)
this.emit('error', err)
})

return socketServer
Expand Down Expand Up @@ -398,7 +407,7 @@ export class MosConnection extends EventEmitter implements IMosConnection {
`${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}`
)
// messageParser.debug = this._debug
messageParser.on('message', (message: any, messageString: string) => {
messageParser.on('message', (message: ParsedMosMessage, messageString: string) => {
// Handle incoming data
handleMessage(message, messageString).catch((err) => this.emit('error', err))
})
Expand All @@ -423,18 +432,18 @@ export class MosConnection extends EventEmitter implements IMosConnection {
messageParser.debug = this._debug
messageParser.parseMessage(messageString)
} catch (err) {
this.emit('error', err)
this.emit('error', err instanceof Error ? err : new Error(`${err}`))
}
})
const handleMessage = async (parsed: any, _messageString: string) => {
const handleMessage = async (parsed: ParsedMosMessage, _messageString: string) => {
const remoteAddressContent = client.socket.remoteAddress
? client.socket.remoteAddress.split(':')
: undefined
const remoteAddress = remoteAddressContent ? remoteAddressContent[remoteAddressContent.length - 1] : ''

const ncsID = parsed.mos.ncsID
const mosID = parsed.mos.mosID
const mosMessageId: number = parsed.mos.messageID
const mosMessageId = parsed.mos.messageID ? parseInt(parsed.mos.messageID, 10) : undefined

let mosDevice = this._mosDevices[ncsID + '_' + mosID] || this._mosDevices[mosID + '_' + ncsID]

Expand Down Expand Up @@ -472,8 +481,9 @@ export class MosConnection extends EventEmitter implements IMosConnection {
primary.on('warning', (str: string) => {
this.emit('warning', 'primary: ' + str)
})
primary.on('error', (str: string) => {
this.emit('error', 'primary: ' + str)
primary.on('error', (err: Error) => {
err.message = `primary: ${err.message}`
this.emit('error', err)
})
const openRelayOptions: IMOSDeviceConnectionOptions['primary'] | undefined =
typeof this._conf.openRelay === 'object' ? this._conf.openRelay.options : undefined
Expand Down Expand Up @@ -503,24 +513,36 @@ export class MosConnection extends EventEmitter implements IMosConnection {
}
if (mosDevice) {
mosDevice
.routeData(parsed, client.portDescription)
.routeData(parsed.mos, client.portDescription)
.catch((err) => {
if (MosModel.ParseError.isParseError(err)) {
// Try to add the main mos message key to the error:
const breadcrumbs = Object.keys(parsed.mos).filter(
(key) =>
!['messageID', 'mosID', 'ncsID'].includes(key) &&
typeof parsed.mos[key] === 'object'
)
throw MosModel.ParseError.handleCaughtError(breadcrumbs.join('/'), err)
} else throw err
})
.then((message: MosModel.MosMessage) => {
sendReply(message)
})
.catch((err: Error | MosModel.MosMessage) => {
.catch((err: Error | MosModel.MosMessage | MosModel.ParseError) => {
// Something went wrong
if (err instanceof MosModel.MosMessage) {
sendReply(err)
} else {
// Unknown / internal error
// Internal/parsing error

// Log error:
this.emit('warning', `Error when handling incoming data: ${err} ${err?.stack}`)
// reply with NACK:
// TODO: implement ACK
// https://mosprotocol.com/wp-content/MOS-Protocol-Documents/MOS_Protocol_Version_2.8.5_Final.htm#mosAck
const msg = new MosModel.MOSAck(
{
ID: this.mosTypes.mosString128.create(0),
ID: this.mosTypes.mosString128.create('0'),
Revision: 0,
Description: this.mosTypes.mosString128.create(
`MosDevice "${ncsID + '_' + mosID}" not found`
Expand All @@ -538,7 +560,7 @@ export class MosConnection extends EventEmitter implements IMosConnection {
// We can't handle the message, reply with a NACK:
const msg = new MosModel.MOSAck(
{
ID: this.mosTypes.mosString128.create(0),
ID: this.mosTypes.mosString128.create('0'),
Revision: 0,
Description: this.mosTypes.mosString128.create('MosDevice not found'),
Status: IMOSAckStatus.NACK,
Expand All @@ -550,10 +572,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
}
}
client.socket.on('error', (e: Error) => {
this.emit(
'error',
`Socket had error (${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}): ${e}`
)
e.message = `Socket had error (${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}): ${e.message}`
this.emit('error', e)
this.debugTrace(
`Socket had error (${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}): ${e}`
)
Expand Down
Loading