{
  "type": "module",
  "source": "doc/api/api-agent.md",
  "modules": [
    {
      "textRaw": "Agent",
      "name": "agent",
      "introduced_in": "v3.2.0",
      "type": "module",
      "stability": 2,
      "stabilityText": "Stable",
      "desc": "<p>An <code>Agent</code> dispatches requests against multiple different origins, creating and\nreusing a per-origin <a href=\"Pool.html#class-pool\"><code>Pool</code></a> (or <a href=\"Client.html#class-client\"><code>Client</code></a>) on demand. It is the default\ndispatcher used by <a href=\"Dispatcher.html#dispatcherrequestoptions-callback\"><code>request</code></a>, <a href=\"Dispatcher.html#dispatcherstreamoptions-factory-callback\"><code>stream</code></a>, and <a href=\"Fetch.html\"><code>fetch</code></a> when no explicit\ndispatcher is supplied.</p>\n<p>Requests are not guaranteed to be dispatched in their order of invocation.</p>\n<pre><code class=\"language-mjs\">import { Agent, setGlobalDispatcher } from 'undici'\n\nconst agent = new Agent({ connections: 10 })\nsetGlobalDispatcher(agent)\n</code></pre>",
      "classes": [
        {
          "textRaw": "Class: `Agent`",
          "name": "Agent",
          "type": "class",
          "meta": {
            "added": [
              "v3.2.0"
            ],
            "changes": []
          },
          "desc": "<ul>\n<li>Extends: <a href=\"Dispatcher.md#class-dispatcher\"><code>&#x3C;Dispatcher></code></a></li>\n</ul>\n<p><code>Agent</code> keeps an internal map of origins to dispatchers. The first request to a\ngiven origin lazily creates a dispatcher through the configured <code>factory</code>, and\nsubsequent requests to the same origin reuse it. Idle dispatchers are closed\nautomatically once they have no open connections and are no longer busy.</p>",
          "signatures": [
            {
              "textRaw": "`new Agent([options])`",
              "name": "Agent",
              "type": "ctor",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": [
                  {
                    "version": "v7.16.0",
                    "pr-url": "https://github.com/nodejs/undici/pull/4365",
                    "description": "Added the `maxOrigins` option to cap the number of origins."
                  }
                ]
              },
              "params": [
                {
                  "textRaw": "`options` {AgentOptions} (optional) Extends {PoolOptions}.",
                  "name": "options",
                  "type": "AgentOptions",
                  "desc": "(optional) Extends {PoolOptions}.",
                  "options": [
                    {
                      "textRaw": "`factory` {Function} Builds the dispatcher used for a given origin. When the resolved options contain `connections: 1`, the default factory creates a {Client}; otherwise it creates a {Pool}. **Default:** `(origin, opts) => new Pool(origin, opts)`.",
                      "name": "factory",
                      "type": "Function",
                      "default": "`(origin, opts) => new Pool(origin, opts)`",
                      "desc": "Builds the dispatcher used for a given origin. When the resolved options contain `connections: 1`, the default factory creates a {Client}; otherwise it creates a {Pool}.",
                      "options": [
                        {
                          "textRaw": "`origin` {URL|string} The origin to create a dispatcher for.",
                          "name": "origin",
                          "type": "URL|string",
                          "desc": "The origin to create a dispatcher for."
                        },
                        {
                          "textRaw": "`opts` {Object} The resolved options for the new dispatcher.",
                          "name": "opts",
                          "type": "Object",
                          "desc": "The resolved options for the new dispatcher."
                        },
                        {
                          "textRaw": "Returns: {Dispatcher}",
                          "name": "return",
                          "type": "Dispatcher"
                        }
                      ]
                    },
                    {
                      "textRaw": "`maxOrigins` {number} Limits the total number of distinct origins that may receive requests at the same time. A {MaxOriginsReachedError} is thrown when a request targets a new origin once the limit is reached. When set to `Infinity`, no limit is enforced. Must be a number greater than `0`. **Default:** `Infinity`.",
                      "name": "maxOrigins",
                      "type": "number",
                      "default": "`Infinity`",
                      "desc": "Limits the total number of distinct origins that may receive requests at the same time. A {MaxOriginsReachedError} is thrown when a request targets a new origin once the limit is reached. When set to `Infinity`, no limit is enforced. Must be a number greater than `0`."
                    }
                  ],
                  "optional": true
                }
              ],
              "desc": "<p><code>Agent</code> inherits all <a href=\"Pool.md#new-poolurl-options\"><code>&#x3C;PoolOptions></code></a> (and therefore all <a href=\"Client.md#new-clienturl-options\"><code>&#x3C;ClientOptions></code></a>). The\nper-origin <a href=\"Pool.md#class-pool\"><code>&#x3C;Pool></code></a> it creates uses the default unlimited <code>connections</code>, so\nconcurrent requests to the same origin are spread across separate <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a>\ninstances on separate sockets.</p>\n<blockquote>\n<p>[!NOTE]\nBecause each concurrent request to an origin may use a different <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a>,\nHTTP/2 multiplexing on a shared session does not apply unless <code>connections</code> is\nset to a small value (for example <code>connections: 1</code>). See <a href=\"Pool.md#new-poolurl-options\"><code>&#x3C;PoolOptions></code></a> and\n<a href=\"Client.md#new-clienturl-options\"><code>&#x3C;ClientOptions></code></a> for the full set of inherited options such as <code>allowH2</code>\n(default <code>true</code>) and <code>maxConcurrentStreams</code> (default <code>100</code>).</p>\n</blockquote>"
            }
          ],
          "properties": [
            {
              "textRaw": "Type: {boolean}",
              "name": "closed",
              "type": "boolean",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "desc": "<p><code>true</code> after <a href=\"#agentclosecallback\"><code>agent.close()</code></a> has been called.</p>"
            },
            {
              "textRaw": "Type: {boolean}",
              "name": "destroyed",
              "type": "boolean",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "desc": "<p><code>true</code> after <a href=\"#agentdestroyerror-callback\"><code>agent.destroy()</code></a> has been called, or after <a href=\"#agentclosecallback\"><code>agent.close()</code></a>\nhas been called and the shutdown has completed.</p>"
            },
            {
              "textRaw": "Type: {Record}<{string}, {ClientStats|PoolStats}>",
              "name": "stats",
              "type": "Record",
              "meta": {
                "added": [
                  "v7.9.0"
                ],
                "changes": []
              },
              "desc": "<p>Aggregate statistics for the agent, keyed by origin. Each value is the\n<a href=\"ClientStats.html\"><code>ClientStats</code></a> or <a href=\"PoolStats.html\"><code>PoolStats</code></a> object reported by the dispatcher currently\nserving that origin. Origins whose dispatcher does not expose <code>stats</code> are\nomitted.</p>\n<pre><code class=\"language-mjs\">import { Agent } from 'undici'\n\nconst agent = new Agent()\nawait agent.request({ origin: 'http://localhost:3000', path: '/', method: 'GET' })\nconsole.log(agent.stats)\n// { 'http://localhost:3000': PoolStats { ... } }\n</code></pre>",
              "shortDesc": "<{string}, {ClientStats|PoolStats}>"
            }
          ],
          "methods": [
            {
              "textRaw": "`agent.dispatch(options, handler)`",
              "name": "dispatch",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`options` {AgentDispatchOptions} Extends {DispatchOptions}.",
                      "name": "options",
                      "type": "AgentDispatchOptions",
                      "desc": "Extends {DispatchOptions}.",
                      "options": [
                        {
                          "textRaw": "`origin` {string|URL} The origin to dispatch the request against. Required.",
                          "name": "origin",
                          "type": "string|URL",
                          "desc": "The origin to dispatch the request against. Required."
                        }
                      ]
                    },
                    {
                      "textRaw": "`handler` {DispatchHandler}",
                      "name": "handler",
                      "type": "DispatchHandler"
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {boolean} `false` if the dispatcher is busy and the caller should wait for the `'drain'` event before dispatching again.",
                    "name": "return",
                    "type": "boolean",
                    "desc": "`false` if the dispatcher is busy and the caller should wait for the `'drain'` event before dispatching again."
                  }
                }
              ],
              "desc": "<p>Routes the request to the dispatcher for <code>options.origin</code>, creating one through\nthe <code>factory</code> if needed. Throws an <a href=\"Errors.md#class-invalidargumenterror\"><code>&#x3C;InvalidArgumentError></code></a> when <code>options.origin</code> is not a non-empty string or <a href=\"https://developer.mozilla.org/docs/Web/API/URL\"><code>&#x3C;URL></code></a>, and a <a href=\"Errors.md#class-maxoriginsreachederror\"><code>&#x3C;MaxOriginsReachedError></code></a> when a new\norigin would exceed <code>maxOrigins</code>. Implements <a href=\"Dispatcher.html#dispatcherdispatchoptions-handler\"><code>Dispatcher.dispatch()</code></a>.</p>"
            },
            {
              "textRaw": "`agent.close([callback])`",
              "name": "close",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`callback` {Function} (optional) Invoked once every per-origin dispatcher has closed. When omitted, a {Promise} is returned.",
                      "name": "callback",
                      "type": "Function",
                      "desc": "(optional) Invoked once every per-origin dispatcher has closed. When omitted, a {Promise} is returned.",
                      "optional": true
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {Promise|void}",
                    "name": "return",
                    "type": "Promise|void"
                  }
                }
              ],
              "desc": "<p>Gracefully closes the agent and every dispatcher it owns, waiting for in-flight\nrequests to complete. Implements <a href=\"Dispatcher.html#dispatcherclosecallback-promise\"><code>Dispatcher.close()</code></a>.</p>"
            },
            {
              "textRaw": "`agent.destroy([error[, callback]])`",
              "name": "destroy",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "textRaw": "`error` {Error} (optional) The error to abort pending requests with.",
                      "name": "error",
                      "type": "Error",
                      "desc": "(optional) The error to abort pending requests with.",
                      "optional": true
                    },
                    {
                      "textRaw": "`callback` {Function} (optional) Invoked once every per-origin dispatcher has been destroyed. When omitted, a {Promise} is returned.",
                      "name": "callback",
                      "type": "Function",
                      "desc": "(optional) Invoked once every per-origin dispatcher has been destroyed. When omitted, a {Promise} is returned.",
                      "optional": true
                    }
                  ],
                  "return": {
                    "textRaw": "Returns: {Promise|void}",
                    "name": "return",
                    "type": "Promise|void"
                  }
                }
              ],
              "desc": "<p>Forcefully destroys the agent and every dispatcher it owns, aborting all pending\nrequests. Implements <a href=\"Dispatcher.html#dispatcherdestroyerror-callback-promise\"><code>Dispatcher.destroy()</code></a>.</p>"
            },
            {
              "textRaw": "`agent.connect(options[, callback])`",
              "name": "connect",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>See <a href=\"Dispatcher.html#dispatcherconnectoptions-callback\"><code>Dispatcher.connect()</code></a>. The request is routed to the dispatcher for\n<code>options.origin</code>.</p>"
            },
            {
              "textRaw": "`agent.pipeline(options, handler)`",
              "name": "pipeline",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "handler"
                    }
                  ]
                }
              ],
              "desc": "<p>See <a href=\"Dispatcher.html#dispatcherpipelineoptions-handler\"><code>Dispatcher.pipeline()</code></a>. The request is routed to the dispatcher for\n<code>options.origin</code>.</p>"
            },
            {
              "textRaw": "`agent.request(options[, callback])`",
              "name": "request",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>See <a href=\"Dispatcher.html#dispatcherrequestoptions-callback\"><code>Dispatcher.request()</code></a>. The request is routed to the dispatcher for\n<code>options.origin</code>.</p>\n<pre><code class=\"language-mjs\">import { Agent } from 'undici'\n\nconst agent = new Agent()\nconst { statusCode, body } = await agent.request({\n  origin: 'http://localhost:3000',\n  path: '/',\n  method: 'GET',\n})\n\nconsole.log('response received', statusCode)\nconsole.log('data', await body.text())\n</code></pre>"
            },
            {
              "textRaw": "`agent.stream(options, factory[, callback])`",
              "name": "stream",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "factory"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>See <a href=\"Dispatcher.html#dispatcherstreamoptions-factory-callback\"><code>Dispatcher.stream()</code></a>. The request is routed to the dispatcher for\n<code>options.origin</code>.</p>"
            },
            {
              "textRaw": "`agent.upgrade(options[, callback])`",
              "name": "upgrade",
              "type": "method",
              "meta": {
                "added": [
                  "v3.2.0"
                ],
                "changes": []
              },
              "signatures": [
                {
                  "params": [
                    {
                      "name": "options"
                    },
                    {
                      "name": "callback",
                      "optional": true
                    }
                  ]
                }
              ],
              "desc": "<p>See <a href=\"Dispatcher.html#dispatcherupgradeoptions-callback\"><code>Dispatcher.upgrade()</code></a>. The request is routed to the dispatcher for\n<code>options.origin</code>.</p>"
            }
          ],
          "events": [
            {
              "textRaw": "Event: `'connect'`",
              "name": "connect",
              "type": "event",
              "meta": {
                "added": [
                  "v6.6.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`origin` {URL}",
                  "name": "origin",
                  "type": "URL"
                },
                {
                  "textRaw": "`targets` {Array<Dispatcher>} The dispatchers involved, starting with this `Agent` followed by the per-origin dispatcher chain.",
                  "name": "targets",
                  "type": "Array<Dispatcher>",
                  "desc": "The dispatchers involved, starting with this `Agent` followed by the per-origin dispatcher chain."
                }
              ],
              "desc": "<p>Emitted when a socket has been created for an origin and is ready to receive\nrequests. Forwarded from the underlying dispatcher's <a href=\"Dispatcher.html#event-connect\"><code>'connect'</code></a> event.</p>"
            },
            {
              "textRaw": "Event: `'disconnect'`",
              "name": "disconnect",
              "type": "event",
              "meta": {
                "added": [
                  "v6.6.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`origin` {URL}",
                  "name": "origin",
                  "type": "URL"
                },
                {
                  "textRaw": "`targets` {Array<Dispatcher>} The dispatchers involved, starting with this `Agent`.",
                  "name": "targets",
                  "type": "Array<Dispatcher>",
                  "desc": "The dispatchers involved, starting with this `Agent`."
                },
                {
                  "textRaw": "`error` {Error}",
                  "name": "error",
                  "type": "Error"
                }
              ],
              "desc": "<p>Emitted when a socket for an origin has disconnected. Forwarded from the\nunderlying dispatcher's <a href=\"Dispatcher.html#event-disconnect\"><code>'disconnect'</code></a> event.</p>"
            },
            {
              "textRaw": "Event: `'connectionError'`",
              "name": "connectionError",
              "type": "event",
              "meta": {
                "added": [
                  "v6.6.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`origin` {URL}",
                  "name": "origin",
                  "type": "URL"
                },
                {
                  "textRaw": "`targets` {Array<Dispatcher>} The dispatchers involved, starting with this `Agent`.",
                  "name": "targets",
                  "type": "Array<Dispatcher>",
                  "desc": "The dispatchers involved, starting with this `Agent`."
                },
                {
                  "textRaw": "`error` {Error}",
                  "name": "error",
                  "type": "Error"
                }
              ],
              "desc": "<p>Emitted when a connection attempt for an origin fails. Forwarded from the\nunderlying dispatcher's <a href=\"Dispatcher.html#event-connectionerror\"><code>'connectionError'</code></a> event.</p>"
            },
            {
              "textRaw": "Event: `'drain'`",
              "name": "drain",
              "type": "event",
              "meta": {
                "added": [
                  "v6.6.0"
                ],
                "changes": []
              },
              "params": [
                {
                  "textRaw": "`origin` {URL}",
                  "name": "origin",
                  "type": "URL"
                },
                {
                  "textRaw": "`targets` {Array<Dispatcher>} The dispatchers involved, starting with this `Agent`.",
                  "name": "targets",
                  "type": "Array<Dispatcher>",
                  "desc": "The dispatchers involved, starting with this `Agent`."
                }
              ],
              "desc": "<p>Emitted when an origin's dispatcher is no longer busy and can accept more\nrequests. Forwarded from the underlying dispatcher's <a href=\"Dispatcher.html#event-drain\"><code>'drain'</code></a> event.</p>"
            }
          ]
        }
      ]
    }
  ]
}