Skip to content

Commit

Permalink
net: make readyState to be closed if socket is not opened
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayase-252 committed Apr 15, 2021
1 parent c3a5e15 commit 89bcaad
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions doc/api/net.md
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,10 @@ information.
### `socket.readyState`
<!-- YAML
added: v0.5.0
changes:
- version: REPLACEME
pr-url: REPLACEME
description: When stream is not opened, reading it will be `closed`.
-->

* {string}
Expand All @@ -1125,6 +1129,8 @@ This property represents the state of the connection as a string.
* If the stream is readable and writable, it is `open`.
* If the stream is readable and not writable, it is `readOnly`.
* If the stream is not readable and writable, it is `writeOnly`.
* If the stream is not opened or is neither readable nor writable, it is
`closed`.

## `net.connect()`

Expand Down
2 changes: 2 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ ObjectDefineProperty(Socket.prototype, 'readyState', {
get: function() {
if (this.connecting) {
return 'opening';
} else if(this.pending) {
return "closed";
} else if (this.readable && this.writable) {
return 'open';
} else if (this.readable && !this.writable) {
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-net-connect-buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tcp.listen(0, common.mustCall(function() {

let connected = false;
assert.strictEqual(socket.pending, true);
assert.strictEqual(socket.readyState, 'closed');
socket.connect(this.address().port, common.mustCall(() => connected = true));

assert.strictEqual(socket.pending, true);
Expand Down

0 comments on commit 89bcaad

Please sign in to comment.