File tree Expand file tree Collapse file tree 1 file changed +16
-10
lines changed Expand file tree Collapse file tree 1 file changed +16
-10
lines changed Original file line number Diff line number Diff line change @@ -291,27 +291,33 @@ export default class Socket extends EventEmitter {
291
291
}
292
292
293
293
/**
294
- * @param {string | Buffer | Uint8Array } data
294
+ * Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.
295
+ *
296
+ * @param {string | Buffer | Uint8Array } [data]
295
297
* @param {BufferEncoding } [encoding]
296
298
*/
297
299
end ( data , encoding ) {
298
300
if ( data ) {
299
301
this . write ( data , encoding , ( ) => {
300
302
Sockets . end ( this . _id ) ;
301
303
} ) ;
302
- } else {
303
- this . _clearTimeout ( ) ;
304
- Sockets . end ( this . _id ) ;
304
+ return this ;
305
305
}
306
+ if ( this . _pending || this . _destroyed ) return this ;
307
+
308
+ this . _clearTimeout ( ) ;
309
+ Sockets . end ( this . _id ) ;
306
310
return this ;
307
311
}
308
312
313
+ /**
314
+ * Ensures that no more I/O activity happens on this socket. Destroys the stream and closes the connection.
315
+ */
309
316
destroy ( ) {
310
- if ( ! this . _destroyed ) {
311
- this . _destroyed = true ;
312
- this . _clearTimeout ( ) ;
313
- Sockets . destroy ( this . _id ) ;
314
- }
317
+ if ( this . _pending || this . _destroyed ) return this ;
318
+ this . _destroyed = true ;
319
+ this . _clearTimeout ( ) ;
320
+ Sockets . destroy ( this . _id ) ;
315
321
return this ;
316
322
}
317
323
@@ -331,7 +337,7 @@ export default class Socket extends EventEmitter {
331
337
*/
332
338
write ( buffer , encoding , cb ) {
333
339
const self = this ;
334
- if ( this . _pending || this . _destroyed ) throw new Error ( 'Socket is not connected .' ) ;
340
+ if ( this . _pending || this . _destroyed ) throw new Error ( 'Socket is closed .' ) ;
335
341
336
342
const generatedBuffer = this . _generateSendBuffer ( buffer , encoding ) ;
337
343
this . _writeBufferSize += generatedBuffer . byteLength ;
You can’t perform that action at this time.
0 commit comments