Skip to content

doc: improve agent.createConnection docs for http and https agents #58205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,23 +204,37 @@ added: v0.11.4
-->

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

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

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

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

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

`callback` has a signature of `(err, stream)`.
The agent will call the provided `createConnection` function with `options` and
this internal `callback`. The `callback` provided by the agent has a signature
of `(err, stream)`.

### `agent.keepSocketAlive(socket)`

Expand Down
6 changes: 6 additions & 0 deletions doc/api/https.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ changes:
An [`Agent`][] object for HTTPS similar to [`http.Agent`][]. See
[`https.request()`][] for more information.

Like `http.Agent`, the `createConnection(options[, callback])` method can be overridden
to customize how TLS connections are established.

> See [`http.Agent#createConnection()`][] for details on overriding this method,
> including asynchronous socket creation with a callback.
### `new Agent([options])`

<!-- YAML
Expand Down