Skip to content

Commit

Permalink
fix(socket): Throw an error if trying to write to a closed socket
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Jan 15, 2025
1 parent b53fc3e commit 1d8e941
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/imap-flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit 1d8e941

Please sign in to comment.