Skip to content

Commit 58e177c

Browse files
sam-githubaddaleax
authored andcommitted
doc: document and test that methods return this
Also, add tests to ensure they will always return this, and to confirm they return this when these doc changes are back-ported to earlier release lines. PR-URL: #13531 Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent f5f2a0e commit 58e177c

5 files changed

+51
-24
lines changed

doc/api/net.md

+40-16
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ This class is used to create a TCP or [IPC][] server.
5252

5353
## new net.Server([options][, connectionListener])
5454

55+
* Returns: {net.Server}
56+
5557
See [`net.createServer([options][, connectionListener])`][`net.createServer()`].
5658

5759
`net.Server` is an [`EventEmitter`][] with the following events:
@@ -130,13 +132,17 @@ Don't call `server.address()` until the `'listening'` event has been emitted.
130132
added: v0.1.90
131133
-->
132134

135+
* Returns: {net.Server}
136+
133137
Stops the server from accepting new connections and keeps existing
134138
connections. This function is asynchronous, the server is finally
135139
closed when all connections are ended and the server emits a [`'close'`][] event.
136140
The optional `callback` will be called once the `'close'` event occurs. Unlike
137141
that event, it will be called with an Error as its only argument if the server
138142
was not open when it was closed.
139143

144+
Returns `server`.
145+
140146
### server.connections
141147
<!-- YAML
142148
added: v0.2.0
@@ -218,6 +224,7 @@ added: v0.5.10
218224
* `handle` {Object}
219225
* `backlog` {number} Common parameter of [`server.listen()`][] functions
220226
* `callback` {Function} Common parameter of [`server.listen()`][] functions
227+
* Returns: {net.Server}
221228

222229
Start a server listening for connections on a given `handle` that has
223230
already been bound to a port, a UNIX domain socket, or a Windows named pipe.
@@ -243,6 +250,7 @@ added: v0.11.14
243250
* `exclusive` {boolean} Default to `false`
244251
* `callback` {Function} Common parameter of [`server.listen()`][]
245252
functions
253+
* Returns: {net.Server}
246254

247255
If `port` is specified, it behaves the same as
248256
[`server.listen([port][, hostname][, backlog][, callback])`][`server.listen(port, host)`].
@@ -273,6 +281,7 @@ added: v0.1.90
273281
[Identifying paths for IPC connections][].
274282
* `backlog` {number} Common parameter of [`server.listen()`][] functions
275283
* `callback` {Function} Common parameter of [`server.listen()`][] functions
284+
* Returns: {net.Server}
276285

277286
Start a [IPC][] server listening for connections on the given `path`.
278287

@@ -284,6 +293,7 @@ added: v0.1.90
284293
* `host` {string}
285294
* `backlog` {number} Common parameter of [`server.listen()`][] functions
286295
* `callback` {Function} Common parameter of [`server.listen()`][] functions
296+
* Returns: {net.Server}
287297

288298
Start a TCP server listening for connections on the given `port` and `host`.
289299

@@ -323,23 +333,23 @@ with [`child_process.fork()`][].
323333
added: v0.9.1
324334
-->
325335

336+
* Returns: {net.Server}
337+
326338
Opposite of `unref`, calling `ref` on a previously `unref`d server will *not*
327339
let the program exit if it's the only server left (the default behavior). If
328340
the server is `ref`d calling `ref` again will have no effect.
329341

330-
Returns `server`.
331-
332342
### server.unref()
333343
<!-- YAML
334344
added: v0.9.1
335345
-->
336346

347+
* Returns: {net.Server}
348+
337349
Calling `unref` on a server will allow the program to exit if this is the only
338350
active server in the event system. If the server is already `unref`d calling
339351
`unref` again will have no effect.
340352

341-
Returns `server`.
342-
343353
## Class: net.Socket
344354
<!-- YAML
345355
added: v0.3.4
@@ -532,6 +542,7 @@ Possible signatures:
532542
for [IPC][] connections.
533543
* [socket.connect(port[, host][, connectListener])][`socket.connect(port, host)`]
534544
for TCP connections.
545+
* Returns: {net.Socket} The socket itself.
535546

536547
This function is asynchronous. When the connection is established, the
537548
[`'connect'`][] event will be emitted. If there is a problem connecting,
@@ -578,6 +589,8 @@ For [IPC][] connections, available `options` are:
578589
* `path` {string} Required. Path the client should connect to.
579590
See [Identifying paths for IPC connections][].
580591

592+
Returns `socket`.
593+
581594
#### socket.connect(path[, connectListener])
582595

583596
* `path` {string} Path the client should connect to. See
@@ -592,6 +605,8 @@ Alias to
592605
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
593606
called with `{ path: path }` as `options`.
594607

608+
Returns `socket`.
609+
595610
#### socket.connect(port[, host][, connectListener])
596611
<!-- YAML
597612
added: v0.1.90
@@ -609,6 +624,8 @@ Alias to
609624
[`socket.connect(options[, connectListener])`][`socket.connect(options)`]
610625
called with `{port: port, host: host}` as `options`.
611626

627+
Returns `socket`.
628+
612629
### socket.connecting
613630
<!-- YAML
614631
added: v6.1.0
@@ -642,14 +659,14 @@ connection is destroyed no further data can be transferred using it.
642659
added: v0.1.90
643660
-->
644661

662+
* Returns: {net.Socket} The socket itself.
663+
645664
Half-closes the socket. i.e., it sends a FIN packet. It is possible the
646665
server will still send some data.
647666

648667
If `data` is specified, it is equivalent to calling
649668
`socket.write(data, encoding)` followed by [`socket.end()`][].
650669

651-
Returns `socket`.
652-
653670
### socket.localAddress
654671
<!-- YAML
655672
added: v0.9.6
@@ -670,6 +687,8 @@ The numeric representation of the local port. For example,
670687

671688
### socket.pause()
672689

690+
* Returns: {net.Socket} The socket itself.
691+
673692
Pauses the reading of data. That is, [`'data'`][] events will not be emitted.
674693
Useful to throttle back an upload.
675694

@@ -678,12 +697,12 @@ Useful to throttle back an upload.
678697
added: v0.9.1
679698
-->
680699

700+
* Returns: {net.Socket} The socket itself.
701+
681702
Opposite of `unref`, calling `ref` on a previously `unref`d socket will *not*
682703
let the program exit if it's the only socket left (the default behavior). If
683704
the socket is `ref`d calling `ref` again will have no effect.
684705

685-
Returns `socket`.
686-
687706
### socket.remoteAddress
688707
<!-- YAML
689708
added: v0.5.10
@@ -710,13 +729,17 @@ The numeric representation of the remote port. For example,
710729

711730
### socket.resume()
712731

732+
* Returns: {net.Socket} The socket itself.
733+
713734
Resumes reading after a call to [`socket.pause()`][].
714735

715736
### socket.setEncoding([encoding])
716737
<!-- YAML
717738
added: v0.1.90
718739
-->
719740

741+
* Returns: {net.Socket} The socket itself.
742+
720743
Set the encoding for the socket as a [Readable Stream][]. See
721744
[`stream.setEncoding()`][] for more information.
722745

@@ -725,6 +748,8 @@ Set the encoding for the socket as a [Readable Stream][]. See
725748
added: v0.1.92
726749
-->
727750

751+
* Returns: {net.Socket} The socket itself.
752+
728753
Enable/disable keep-alive functionality, and optionally set the initial
729754
delay before the first keepalive probe is sent on an idle socket.
730755
`enable` defaults to `false`.
@@ -734,25 +759,25 @@ data packet received and the first keepalive probe. Setting 0 for
734759
initialDelay will leave the value unchanged from the default
735760
(or previous) setting. Defaults to `0`.
736761

737-
Returns `socket`.
738-
739762
### socket.setNoDelay([noDelay])
740763
<!-- YAML
741764
added: v0.1.90
742765
-->
743766

767+
* Returns: {net.Socket} The socket itself.
768+
744769
Disables the Nagle algorithm. By default TCP connections use the Nagle
745770
algorithm, they buffer data before sending it off. Setting `true` for
746771
`noDelay` will immediately fire off data each time `socket.write()` is called.
747772
`noDelay` defaults to `true`.
748773

749-
Returns `socket`.
750-
751774
### socket.setTimeout(timeout[, callback])
752775
<!-- YAML
753776
added: v0.1.90
754777
-->
755778

779+
* Returns: {net.Socket} The socket itself.
780+
756781
Sets the socket to timeout after `timeout` milliseconds of inactivity on
757782
the socket. By default `net.Socket` do not have a timeout.
758783

@@ -773,19 +798,17 @@ If `timeout` is 0, then the existing idle timeout is disabled.
773798
The optional `callback` parameter will be added as a one time listener for the
774799
[`'timeout'`][] event.
775800

776-
Returns `socket`.
777-
778801
### socket.unref()
779802
<!-- YAML
780803
added: v0.9.1
781804
-->
782805

806+
* Returns: {net.Socket} The socket itself.
807+
783808
Calling `unref` on a socket will allow the program to exit if this is the only
784809
active socket in the event system. If the socket is already `unref`d calling
785810
`unref` again will have no effect.
786811

787-
Returns `socket`.
788-
789812
### socket.write(data[, encoding][, callback])
790813
<!-- YAML
791814
added: v0.1.90
@@ -965,6 +988,7 @@ Creates a new TCP or [IPC][] server.
965988
should be paused on incoming connections.
966989
* `connectionListener` {Function} Automatically set as a listener for the
967990
[`'connection'`][] event
991+
* Returns: {net.Server}
968992

969993
If `allowHalfOpen` is set to `true`, when the other end of the socket
970994
sends a FIN packet, the server will only send a FIN packet back when

test/parallel/test-net-end-close.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const s = new net.Socket({
1414
},
1515
writable: false
1616
});
17-
s.resume();
17+
assert.strictEqual(s, s.resume());
1818

1919
const events = [];
2020

test/parallel/test-net-server-close.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const server = net.createServer(function(c) {
4444
sockets.push(c);
4545

4646
if (sockets.length === 2) {
47-
server.close();
47+
assert.strictEqual(server.close(), server);
4848
sockets.forEach(function(c) { c.destroy(); });
4949
}
5050
});
@@ -53,7 +53,7 @@ server.on('close', function() {
5353
events.push('server');
5454
});
5555

56-
server.listen(0, function() {
56+
assert.strictEqual(server, server.listen(0, function() {
5757
net.createConnection(this.address().port);
5858
net.createConnection(this.address().port);
59-
});
59+
}));

test/parallel/test-net-socket-local-address.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ function connect() {
3434

3535
conns++;
3636
client.once('close', connect);
37-
client.connect(server.address().port, common.localhostIPv4, () => {
38-
clientLocalPorts.push(client.localPort);
39-
});
37+
assert.strictEqual(
38+
client,
39+
client.connect(server.address().port, common.localhostIPv4, () => {
40+
clientLocalPorts.push(client.localPort);
41+
})
42+
);
4043
}

test/parallel/test-net-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const server = net.createServer(function(socket) {
6161
}).listen(0, function() {
6262
const conn = net.connect(this.address().port);
6363
conn.on('data', function(buf) {
64-
conn.pause();
64+
assert.strictEqual(conn, conn.pause());
6565
setTimeout(function() {
6666
conn.destroy();
6767
}, 20);

0 commit comments

Comments
 (0)