From b129cf3b4b93e84bf79e36766ed296ffa8041bf2 Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Mon, 18 Apr 2022 14:40:09 +0200 Subject: [PATCH] fix: destroy & end work as no-op on closed streams Fixes #145 --- src/Socket.js | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Socket.js b/src/Socket.js index 92bab46..e17ee2d 100644 --- a/src/Socket.js +++ b/src/Socket.js @@ -291,7 +291,9 @@ export default class Socket extends EventEmitter { } /** - * @param {string | Buffer | Uint8Array} data + * Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data. + * + * @param {string | Buffer | Uint8Array} [data] * @param {BufferEncoding} [encoding] */ end(data, encoding) { @@ -299,19 +301,23 @@ export default class Socket extends EventEmitter { this.write(data, encoding, () => { Sockets.end(this._id); }); - } else { - this._clearTimeout(); - Sockets.end(this._id); + return this; } + if (this._pending || this._destroyed) return this; + + this._clearTimeout(); + Sockets.end(this._id); return this; } + /** + * Ensures that no more I/O activity happens on this socket. Destroys the stream and closes the connection. + */ destroy() { - if (!this._destroyed) { - this._destroyed = true; - this._clearTimeout(); - Sockets.destroy(this._id); - } + if (this._pending || this._destroyed) return this; + this._destroyed = true; + this._clearTimeout(); + Sockets.destroy(this._id); return this; } @@ -331,7 +337,7 @@ export default class Socket extends EventEmitter { */ write(buffer, encoding, cb) { const self = this; - if (this._pending || this._destroyed) throw new Error('Socket is not connected.'); + if (this._pending || this._destroyed) throw new Error('Socket is closed.'); const generatedBuffer = this._generateSendBuffer(buffer, encoding); this._writeBufferSize += generatedBuffer.byteLength;