From 5d3048ce3840b1321e1c8b6b1facf359b3fc5c9b Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 28 Oct 2020 22:44:23 +0530 Subject: [PATCH] Moved message parsing logic outside try/catch --- src/WAConnection/3.Connect.ts | 84 ++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 40 deletions(-) diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index fca191f..7b53204 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -299,53 +299,57 @@ export class WAConnection extends Base { this.lastSeen = new Date(parseInt(timestamp)) this.emit ('received-pong') } else { + let messageTag: string + let json: any try { - const [messageTag, json] = Utils.decryptWA (message, this.authInfo?.macKey, this.authInfo?.encKey, new Decoder()) - if (this.shouldLogMessages) this.messageLog.push ({ tag: messageTag, json: JSON.stringify(json), fromMe: false }) - - if (!json) { return } - - if (this.logger.level === 'trace') { - this.logger.trace(messageTag + ', ' + JSON.stringify(json)) - } - /* Check if this is a response to a message we sent */ - if (this.callbacks[messageTag]) { - const q = this.callbacks[messageTag] - q.callback(json) - delete this.callbacks[messageTag] - return - } - /* - Check if this is a response to a message we are expecting - */ - let anyTriggered = false - const l0 = json[0] || '' - const l1 = typeof json[1] !== 'object' ? {} : json[1] - const l2 = ((json[2] || [])[0] || [])[0] || '' - Object.keys(l1).forEach(key => { - anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]},${l2}`, json) - anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]}`, json) - }) - anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0},,${l2}`, json) - anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0}`, json) - if (anyTriggered) return - - if (this.state === 'open' && json[0] === 'Pong') { - if (this.phoneConnected !== json[1]) { - this.phoneConnected = json[1] - this.emit ('connection-phone-change', { connected: this.phoneConnected }) - return - } - } - if (this.logger.level === 'debug') { - this.logger.debug({ unhandled: true }, messageTag + ',' + JSON.stringify(json)) - } + const dec = Utils.decryptWA (message, this.authInfo?.macKey, this.authInfo?.encKey, new Decoder()) + messageTag = dec[0] + json = dec[1] } catch (error) { this.logger.error ({ error }, `encountered error in decrypting message, closing: ${error}`) if (this.state === 'open') this.unexpectedDisconnect (DisconnectReason.badSession) else this.rejectPendingConnection (new Error(DisconnectReason.badSession)) } + + if (this.shouldLogMessages) this.messageLog.push ({ tag: messageTag, json: JSON.stringify(json), fromMe: false }) + if (!json) return + + if (this.logger.level === 'trace') { + this.logger.trace(messageTag + ', ' + JSON.stringify(json)) + } + /* Check if this is a response to a message we sent */ + if (this.callbacks[messageTag]) { + const q = this.callbacks[messageTag] + q.callback(json) + delete this.callbacks[messageTag] + return + } + /* + Check if this is a response to a message we are expecting + */ + let anyTriggered = false + const l0 = json[0] || '' + const l1 = typeof json[1] !== 'object' ? {} : json[1] + const l2 = ((json[2] || [])[0] || [])[0] || '' + Object.keys(l1).forEach(key => { + anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]},${l2}`, json) + anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0},${key}:${l1[key]}`, json) + }) + anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0},,${l2}`, json) + anyTriggered = anyTriggered || this.emit (`${DEF_CALLBACK_PREFIX}${l0}`, json) + if (anyTriggered) return + + if (this.state === 'open' && json[0] === 'Pong') { + if (this.phoneConnected !== json[1]) { + this.phoneConnected = json[1] + this.emit ('connection-phone-change', { connected: this.phoneConnected }) + return + } + } + if (this.logger.level === 'debug') { + this.logger.debug({ unhandled: true }, messageTag + ',' + JSON.stringify(json)) + } } } /** Send a keep alive request every X seconds, server updates & responds with last seen */