Skip to content

Commit 6ab2715

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 7f09d98 commit 6ab2715

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

doc/api/net.md

+33-33
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,19 @@ changes:
660660
`false`.
661661
* `fd` {number} If specified, wrap around an existing socket with
662662
the given file descriptor, otherwise a new socket will be created.
663+
* `onread` {Object} If specified, incoming data is stored in a single `buffer`
664+
and passed to the supplied `callback` when data arrives on the socket.
665+
This will cause the streaming functionality to not provide any data.
666+
The socket will emit events like `'error'`, `'end'`, and `'close'`
667+
as usual. Methods like `pause()` and `resume()` will also behave as
668+
expected.
669+
* `buffer` {Buffer|Uint8Array|Function} Either a reusable chunk of memory to
670+
use for storing incoming data or a function that returns such.
671+
* `callback` {Function} This function is called for every chunk of incoming
672+
data. Two arguments are passed to it: the number of bytes written to
673+
`buffer` and a reference to `buffer`. Return `false` from this function to
674+
implicitly `pause()` the socket. This function will be executed in the
675+
global context.
663676
* `readable` {boolean} Allow reads on the socket when an `fd` is passed,
664677
otherwise ignored. **Default:** `false`.
665678
* `signal` {AbortSignal} An Abort signal that may be used to destroy the
@@ -1021,39 +1034,6 @@ For [IPC][] connections, available `options` are:
10211034
See [Identifying paths for IPC connections][]. If provided, the TCP-specific
10221035
options above are ignored.
10231036

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

10591039
* `path` {string} Path the client should connect to. See
@@ -1556,6 +1536,26 @@ To connect on the socket `/tmp/echo.sock`:
15561536
const client = net.createConnection({ path: '/tmp/echo.sock' });
15571537
```
15581538

1539+
Following is an example of a client using the `port` and `onread`
1540+
option. In this case, the `onread` option will be only used to call
1541+
`new net.Socket([options])` and the `port` option will be used to
1542+
call `socket.connect(options[, connectListener])`.
1543+
1544+
```js
1545+
const net = require('node:net');
1546+
net.createConnection({
1547+
port: 80,
1548+
onread: {
1549+
// Reuses a 4KiB Buffer for every read from the socket.
1550+
buffer: Buffer.alloc(4 * 1024),
1551+
callback: function(nread, buf) {
1552+
// Received data is available in `buf` from 0 to `nread`.
1553+
console.log(buf.toString('utf8', 0, nread));
1554+
},
1555+
},
1556+
});
1557+
```
1558+
15591559
### `net.createConnection(path[, connectListener])`
15601560

15611561
<!-- YAML

0 commit comments

Comments
 (0)