From 233fb263c710b718f488fcd56ccca76032ddd4c2 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Wed, 5 Jul 2023 21:23:15 +0200 Subject: [PATCH] fix: don't allow message sending before both sides are ready Signed-off-by: Tilman Vatteroth --- .../message-transporter.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/commons/src/message-transporters/message-transporter.ts b/commons/src/message-transporters/message-transporter.ts index b3112c985..5b3c4d371 100644 --- a/commons/src/message-transporters/message-transporter.ts +++ b/commons/src/message-transporters/message-transporter.ts @@ -42,6 +42,14 @@ export class MessageTransporter extends EventEmitter2 { * @throws Error if no transport adapter has been set */ public sendMessage(content: Message): void { + if (this.transportAdapter === undefined) { + console.debug( + "Can't send message without transport adapter. Message that couldn't be sent was", + content + ) + return + } + if (!this.isConnected()) { this.onDisconnecting() console.debug( @@ -51,6 +59,22 @@ export class MessageTransporter extends EventEmitter2 { return } + if ( + !this.thisSideReady && + content.type !== MessageType.READY_REQUEST && + content.type !== MessageType.READY_ANSWER + ) { + throw new Error("Can't send message. This side isn't ready") + } + + if ( + !this.otherSideReady && + content.type !== MessageType.READY_REQUEST && + content.type !== MessageType.READY_ANSWER + ) { + throw new Error("Can't send message. Other side isn't ready") + } + try { this.transportAdapter.send(content) } catch (error: unknown) {