Skip to content

Commit 4955cb2

Browse files
mag123caduh95
authored andcommitted
doc: improve agent.createConnection docs for http and https agents
PR-URL: #58205 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 6283bb5 commit 4955cb2

File tree

2 files changed

+31
-11
lines changed

2 files changed

+31
-11
lines changed

doc/api/http.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,37 @@ added: v0.11.4
236236
-->
237237

238238
* `options` {Object} Options containing connection details. Check
239-
[`net.createConnection()`][] for the format of the options
240-
* `callback` {Function} Callback function that receives the created socket
241-
* Returns: {stream.Duplex}
239+
[`net.createConnection()`][] for the format of the options. For custom agents,
240+
this object is passed to the custom `createConnection` function.
241+
* `callback` {Function} (Optional, primarily for custom agents) A function to be
242+
called by a custom `createConnection` implementation when the socket is
243+
created, especially for asynchronous operations.
244+
* `err` {Error | null} An error object if socket creation failed.
245+
* `socket` {stream.Duplex} The created socket.
246+
* Returns: {stream.Duplex} The created socket. This is returned by the default
247+
implementation or by a custom synchronous `createConnection` implementation.
248+
If a custom `createConnection` uses the `callback` for asynchronous
249+
operation, this return value might not be the primary way to obtain the socket.
242250

243251
Produces a socket/stream to be used for HTTP requests.
244252

245-
By default, this function is the same as [`net.createConnection()`][]. However,
246-
custom agents may override this method in case greater flexibility is desired.
253+
By default, this function behaves identically to [`net.createConnection(options)`][],
254+
synchronously returning the created socket. The optional `callback` parameter in the
255+
signature is **not** used by this default implementation.
247256

248-
A socket/stream can be supplied in one of two ways: by returning the
249-
socket/stream from this function, or by passing the socket/stream to `callback`.
257+
However, custom agents may override this method to provide greater flexibility,
258+
for example, to create sockets asynchronously. When overriding `createConnection`:
250259

251-
This method is guaranteed to return an instance of the {net.Socket} class,
252-
a subclass of {stream.Duplex}, unless the user specifies a socket
253-
type other than {net.Socket}.
260+
1. **Synchronous socket creation**: The overriding method can return the
261+
socket/stream directly.
262+
2. **Asynchronous socket creation**: The overriding method can accept the `callback`
263+
and pass the created socket/stream to it (e.g., `callback(null, newSocket)`).
264+
If an error occurs during socket creation, it should be passed as the first
265+
argument to the `callback` (e.g., `callback(err)`).
254266

255-
`callback` has a signature of `(err, stream)`.
267+
The agent will call the provided `createConnection` function with `options` and
268+
this internal `callback`. The `callback` provided by the agent has a signature
269+
of `(err, stream)`.
256270

257271
### `agent.keepSocketAlive(socket)`
258272

doc/api/https.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ changes:
6161
An [`Agent`][] object for HTTPS similar to [`http.Agent`][]. See
6262
[`https.request()`][] for more information.
6363

64+
Like `http.Agent`, the `createConnection(options[, callback])` method can be overridden
65+
to customize how TLS connections are established.
66+
67+
> See [`http.Agent#createConnection()`][] for details on overriding this method,
68+
> including asynchronous socket creation with a callback.
69+
6470
### `new Agent([options])`
6571

6672
<!-- YAML

0 commit comments

Comments
 (0)