From 1d8e941485223c76c4f22080e985587349c0e458 Mon Sep 17 00:00:00 2001 From: Andris Reinman Date: Wed, 15 Jan 2025 13:00:18 +0200 Subject: [PATCH] fix(socket): Throw an error if trying to write to a closed socket --- lib/imap-flow.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/imap-flow.js b/lib/imap-flow.js index 3fd5cd3..e4118c9 100644 --- a/lib/imap-flow.js +++ b/lib/imap-flow.js @@ -387,9 +387,18 @@ class ImapFlow extends EventEmitter { } write(chunk) { - if (this.socket.destroyed || this.state === this.states.LOGOUT) { + if (this.socket.destroyed) { // do not write after connection end or logout - return; + const error = new Error('Socket is already closed'); + error.code = 'NoConnection'; + throw error; + } + + if (this.state === this.states.LOGOUT) { + // should not happen + const error = new Error('Can not send data after logged out'); + error.code = 'StateLogout'; + throw error; } if (this.writeSocket.destroyed) { @@ -471,6 +480,7 @@ class ImapFlow extends EventEmitter { let options = data.options || {}; this.log.debug({ src: 's', msg: logCompiled.toString(), cid: this.id, comment: options.comment }); + this.write(this.commandParts.shift()); if (typeof options.onSend === 'function') {