Skip to content

Commit 5175903

Browse files
sendorutargos
authored andcommitted
doc: move onread option from socket.connect() to new net.socket()
Fixes: #53792 PR-URL: #54194 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Tim Perry <[email protected]>
1 parent 8daeccf commit 5175903

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

doc/api/net.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,19 @@ changes:
667667
`false`.
668668
* `fd` {number} If specified, wrap around an existing socket with
669669
the given file descriptor, otherwise a new socket will be created.
670+
* `onread` {Object} If specified, incoming data is stored in a single `buffer`
671+
and passed to the supplied `callback` when data arrives on the socket.
672+
This will cause the streaming functionality to not provide any data.
673+
The socket will emit events like `'error'`, `'end'`, and `'close'`
674+
as usual. Methods like `pause()` and `resume()` will also behave as
675+
expected.
676+
* `buffer` {Buffer|Uint8Array|Function} Either a reusable chunk of memory to
677+
use for storing incoming data or a function that returns such.
678+
* `callback` {Function} This function is called for every chunk of incoming
679+
data. Two arguments are passed to it: the number of bytes written to
680+
`buffer` and a reference to `buffer`. Return `false` from this function to
681+
implicitly `pause()` the socket. This function will be executed in the
682+
global context.
670683
* `readable` {boolean} Allow reads on the socket when an `fd` is passed,
671684
otherwise ignored. **Default:** `false`.
672685
* `signal` {AbortSignal} An Abort signal that may be used to destroy the
@@ -1038,39 +1051,6 @@ For [IPC][] connections, available `options` are:
10381051
See [Identifying paths for IPC connections][]. If provided, the TCP-specific
10391052
options above are ignored.
10401053

1041-
For both types, available `options` include:
1042-
1043-
* `onread` {Object} If specified, incoming data is stored in a single `buffer`
1044-
and passed to the supplied `callback` when data arrives on the socket.
1045-
This will cause the streaming functionality to not provide any data.
1046-
The socket will emit events like `'error'`, `'end'`, and `'close'`
1047-
as usual. Methods like `pause()` and `resume()` will also behave as
1048-
expected.
1049-
* `buffer` {Buffer|Uint8Array|Function} Either a reusable chunk of memory to
1050-
use for storing incoming data or a function that returns such.
1051-
* `callback` {Function} This function is called for every chunk of incoming
1052-
data. Two arguments are passed to it: the number of bytes written to
1053-
`buffer` and a reference to `buffer`. Return `false` from this function to
1054-
implicitly `pause()` the socket. This function will be executed in the
1055-
global context.
1056-
1057-
Following is an example of a client using the `onread` option:
1058-
1059-
```js
1060-
const net = require('node:net');
1061-
net.connect({
1062-
port: 80,
1063-
onread: {
1064-
// Reuses a 4KiB Buffer for every read from the socket.
1065-
buffer: Buffer.alloc(4 * 1024),
1066-
callback: function(nread, buf) {
1067-
// Received data is available in `buf` from 0 to `nread`.
1068-
console.log(buf.toString('utf8', 0, nread));
1069-
},
1070-
},
1071-
});
1072-
```
1073-
10741054
#### `socket.connect(path[, connectListener])`
10751055

10761056
* `path` {string} Path the client should connect to. See
@@ -1573,6 +1553,26 @@ To connect on the socket `/tmp/echo.sock`:
15731553
const client = net.createConnection({ path: '/tmp/echo.sock' });
15741554
```
15751555

1556+
Following is an example of a client using the `port` and `onread`
1557+
option. In this case, the `onread` option will be only used to call
1558+
`new net.Socket([options])` and the `port` option will be used to
1559+
call `socket.connect(options[, connectListener])`.
1560+
1561+
```js
1562+
const net = require('node:net');
1563+
net.createConnection({
1564+
port: 80,
1565+
onread: {
1566+
// Reuses a 4KiB Buffer for every read from the socket.
1567+
buffer: Buffer.alloc(4 * 1024),
1568+
callback: function(nread, buf) {
1569+
// Received data is available in `buf` from 0 to `nread`.
1570+
console.log(buf.toString('utf8', 0, nread));
1571+
},
1572+
},
1573+
});
1574+
```
1575+
15761576
### `net.createConnection(path[, connectListener])`
15771577

15781578
<!-- YAML

0 commit comments

Comments
 (0)