| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 7f09d98 commit 6ab2715
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -660,6 +660,19 @@ changes: | |||
| 660 | 660 | `false`. | |
| 661 | 661 | * `fd` {number} If specified, wrap around an existing socket with | |
| 662 | 662 | 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. | ||
| 663 | 676 | * `readable` {boolean} Allow reads on the socket when an `fd` is passed, | |
| 664 | 677 | otherwise ignored. **Default:** `false`. | |
| 665 | 678 | * `signal` {AbortSignal} An Abort signal that may be used to destroy the | |
@@ -1021,39 +1034,6 @@ For [IPC][] connections, available `options` are: | |||
| 1021 | 1034 | See [Identifying paths for IPC connections][]. If provided, the TCP-specific | |
| 1022 | 1035 | options above are ignored. | |
| 1023 | 1036 | ||
| 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 | - | ||
| 1057 | 1037 | #### `socket.connect(path[, connectListener])` | |
| 1058 | 1038 | ||
| 1059 | 1039 | * `path` {string} Path the client should connect to. See | |
@@ -1556,6 +1536,26 @@ To connect on the socket `/tmp/echo.sock`: | |||
| 1556 | 1536 | const client = net.createConnection({ path: '/tmp/echo.sock' }); | |
| 1557 | 1537 | ``` | |
| 1558 | 1538 | ||
| 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 | + | ||
| 1559 | 1559 | ### `net.createConnection(path[, connectListener])` | |
| 1560 | 1560 | ||
| 1561 | 1561 | <!-- YAML | |
| Back | FazBrowse Home | New Git URL |
0 commit comments