Skip to content

Commit 206723b

Browse files
authored
Merge pull request #103 from nrkno/fix/parse-everything-as-strings
Parse all XML as proper strings (SOFIE-3006)
2 parents 47ade0e + 4dd8071 commit 206723b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3436
-1565
lines changed

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"packages": [
33
"packages/*"
44
],
5-
"version": "4.1.1",
5+
"version": "4.2.0-alpha.0",
66
"npmClient": "yarn",
77
"useWorkspaces": true
88
}

packages/connector/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@mos-connection/connector",
3-
"version": "4.1.1",
3+
"version": "4.2.0-alpha.0",
44
"description": "MOS compliant TCP/IP Socket connection.",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -29,8 +29,9 @@
2929
"/LICENSE"
3030
],
3131
"dependencies": {
32-
"@mos-connection/helper": "4.1.1",
33-
"@mos-connection/model": "4.1.1",
32+
"@mos-connection/helper": "4.2.0-alpha.0",
33+
"@mos-connection/model": "4.2.0-alpha.0",
34+
"eventemitter3": "^5.0.1",
3435
"iconv-lite": "^0.6.3",
3536
"tslib": "^2.5.3",
3637
"xml-js": "^1.6.11",

packages/connector/src/MosConnection.ts

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@ import { MosDevice } from './MosDevice'
66
import { SocketServerEvent, SocketDescription, IncomingConnectionType } from './connection/socketConnection'
77
import { NCSServerConnection } from './connection/NCSServerConnection'
88
import { MosModel } from '@mos-connection/helper'
9-
import { EventEmitter } from 'events'
9+
import { EventEmitter } from 'eventemitter3'
1010
import * as iconv from 'iconv-lite'
11-
import { MosMessageParser } from './connection/mosMessageParser'
11+
import { ParsedMosMessage, MosMessageParser } from './connection/mosMessageParser'
1212
import { IConnectionConfig, IMosConnection, IMOSDeviceConnectionOptions } from './api'
1313
import { PROFILE_VALIDNESS_CHECK_WAIT_TIME } from './lib'
1414

15-
export class MosConnection extends EventEmitter implements IMosConnection {
15+
export interface MosConnectionEvents {
16+
// Note: These match the events defined in IMosConnection
17+
rawMessage: (source: string, type: string, message: string) => void
18+
info: (message: string, data?: any) => void
19+
warning: (message: string) => void
20+
error: (error: Error) => void
21+
}
22+
export class MosConnection extends EventEmitter<MosConnectionEvents> implements IMosConnection {
1623
static readonly CONNECTION_PORT_LOWER = 10540
1724
static readonly CONNECTION_PORT_UPPER = 10541
1825
static readonly CONNECTION_PORT_QUERY = 10542
@@ -95,7 +102,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
95102
this.emit('warning', 'primary: ' + str)
96103
})
97104
primary.on('error', (error: Error) => {
98-
this.emit('error', 'primary: ' + error + ' ' + (typeof error === 'object' ? error.stack : ''))
105+
error.message = `primary: ${error.message}`
106+
this.emit('error', error)
99107
})
100108
primary.on('info', (str: string) => {
101109
this.emit('info', 'primary: ' + str)
@@ -140,7 +148,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
140148
this.emit('warning', 'secondary: ' + str)
141149
})
142150
secondary.on('error', (error: Error) => {
143-
this.emit('error', 'secondary: ' + error + ' ' + (typeof error === 'object' ? error.stack : ''))
151+
error.message = `secondary: ${error.message}`
152+
this.emit('error', error)
144153
})
145154
secondary.on('info', (str: string) => {
146155
this.emit('info', 'secondary: ' + str)
@@ -359,9 +368,9 @@ export class MosConnection extends EventEmitter implements IMosConnection {
359368
socketServer.on(SocketServerEvent.CLIENT_CONNECTED, (e: SocketDescription) =>
360369
this._registerIncomingClient(e)
361370
)
362-
socketServer.on(SocketServerEvent.ERROR, (e) => {
371+
socketServer.on(SocketServerEvent.ERROR, (err) => {
363372
// handle error
364-
this.emit('error', e)
373+
this.emit('error', err)
365374
})
366375

367376
return socketServer
@@ -398,7 +407,7 @@ export class MosConnection extends EventEmitter implements IMosConnection {
398407
`${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}`
399408
)
400409
// messageParser.debug = this._debug
401-
messageParser.on('message', (message: any, messageString: string) => {
410+
messageParser.on('message', (message: ParsedMosMessage, messageString: string) => {
402411
// Handle incoming data
403412
handleMessage(message, messageString).catch((err) => this.emit('error', err))
404413
})
@@ -423,18 +432,18 @@ export class MosConnection extends EventEmitter implements IMosConnection {
423432
messageParser.debug = this._debug
424433
messageParser.parseMessage(messageString)
425434
} catch (err) {
426-
this.emit('error', err)
435+
this.emit('error', err instanceof Error ? err : new Error(`${err}`))
427436
}
428437
})
429-
const handleMessage = async (parsed: any, _messageString: string) => {
438+
const handleMessage = async (parsed: ParsedMosMessage, _messageString: string) => {
430439
const remoteAddressContent = client.socket.remoteAddress
431440
? client.socket.remoteAddress.split(':')
432441
: undefined
433442
const remoteAddress = remoteAddressContent ? remoteAddressContent[remoteAddressContent.length - 1] : ''
434443

435444
const ncsID = parsed.mos.ncsID
436445
const mosID = parsed.mos.mosID
437-
const mosMessageId: number = parsed.mos.messageID
446+
const mosMessageId = parsed.mos.messageID ? parseInt(parsed.mos.messageID, 10) : undefined
438447

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

@@ -472,8 +481,9 @@ export class MosConnection extends EventEmitter implements IMosConnection {
472481
primary.on('warning', (str: string) => {
473482
this.emit('warning', 'primary: ' + str)
474483
})
475-
primary.on('error', (str: string) => {
476-
this.emit('error', 'primary: ' + str)
484+
primary.on('error', (err: Error) => {
485+
err.message = `primary: ${err.message}`
486+
this.emit('error', err)
477487
})
478488
const openRelayOptions: IMOSDeviceConnectionOptions['primary'] | undefined =
479489
typeof this._conf.openRelay === 'object' ? this._conf.openRelay.options : undefined
@@ -503,24 +513,36 @@ export class MosConnection extends EventEmitter implements IMosConnection {
503513
}
504514
if (mosDevice) {
505515
mosDevice
506-
.routeData(parsed, client.portDescription)
516+
.routeData(parsed.mos, client.portDescription)
517+
.catch((err) => {
518+
if (MosModel.ParseError.isParseError(err)) {
519+
// Try to add the main mos message key to the error:
520+
const breadcrumbs = Object.keys(parsed.mos).filter(
521+
(key) =>
522+
!['messageID', 'mosID', 'ncsID'].includes(key) &&
523+
typeof parsed.mos[key] === 'object'
524+
)
525+
throw MosModel.ParseError.handleCaughtError(breadcrumbs.join('/'), err)
526+
} else throw err
527+
})
507528
.then((message: MosModel.MosMessage) => {
508529
sendReply(message)
509530
})
510-
.catch((err: Error | MosModel.MosMessage) => {
531+
.catch((err: Error | MosModel.MosMessage | MosModel.ParseError) => {
511532
// Something went wrong
512533
if (err instanceof MosModel.MosMessage) {
513534
sendReply(err)
514535
} else {
515-
// Unknown / internal error
536+
// Internal/parsing error
537+
516538
// Log error:
517539
this.emit('warning', `Error when handling incoming data: ${err} ${err?.stack}`)
518540
// reply with NACK:
519541
// TODO: implement ACK
520542
// https://mosprotocol.com/wp-content/MOS-Protocol-Documents/MOS_Protocol_Version_2.8.5_Final.htm#mosAck
521543
const msg = new MosModel.MOSAck(
522544
{
523-
ID: this.mosTypes.mosString128.create(0),
545+
ID: this.mosTypes.mosString128.create('0'),
524546
Revision: 0,
525547
Description: this.mosTypes.mosString128.create(
526548
`MosDevice "${ncsID + '_' + mosID}" not found`
@@ -538,7 +560,7 @@ export class MosConnection extends EventEmitter implements IMosConnection {
538560
// We can't handle the message, reply with a NACK:
539561
const msg = new MosModel.MOSAck(
540562
{
541-
ID: this.mosTypes.mosString128.create(0),
563+
ID: this.mosTypes.mosString128.create('0'),
542564
Revision: 0,
543565
Description: this.mosTypes.mosString128.create('MosDevice not found'),
544566
Status: IMOSAckStatus.NACK,
@@ -550,10 +572,8 @@ export class MosConnection extends EventEmitter implements IMosConnection {
550572
}
551573
}
552574
client.socket.on('error', (e: Error) => {
553-
this.emit(
554-
'error',
555-
`Socket had error (${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}): ${e}`
556-
)
575+
e.message = `Socket had error (${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}): ${e.message}`
576+
this.emit('error', e)
557577
this.debugTrace(
558578
`Socket had error (${socketID}, ${client.socket.remoteAddress}, ${client.portDescription}): ${e}`
559579
)

0 commit comments

Comments
 (0)