{
  "type": "module",
  "source": "doc/api/api-api-lifecycle.md",
  "miscs": [
    {
      "textRaw": "Client Lifecycle",
      "name": "Client Lifecycle",
      "type": "misc",
      "desc": "<p>A <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> can be understood as a state machine. As requests are dispatched,\nprocessed, and drained, the client moves through a well-defined set of states\nuntil it is eventually destroyed. This document describes those states and the\ntransitions between them.</p>\n<p>The <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> class is not literally implemented as a state machine, so the\nactual execution may deviate slightly from what is described here. Treat this\nguide as a conceptual model for reasoning about a client's behavior rather than\nas a formal specification.</p>",
      "miscs": [
        {
          "textRaw": "State transition overview",
          "name": "state_transition_overview",
          "type": "misc",
          "desc": "<ul>\n<li>A <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> begins in the <strong>idle</strong> state with no socket connection and no\nqueued requests.\n<ul>\n<li>The <em>connect</em> transition moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> to the <strong>pending</strong> state, where\nrequests can be queued before they are processed.</li>\n<li>Calling <a href=\"Client.html#clientclosecallback\"><code>client.close()</code></a> or <a href=\"Client.html#clientdestroyerror-callback\"><code>client.destroy()</code></a> moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a>\nto the <strong>destroyed</strong> state. Because there are no queued requests in this\nstate, <em>close</em> transitions straight to <strong>destroyed</strong>.</li>\n</ul>\n</li>\n<li>The <strong>pending</strong> state indicates that the underlying socket connection has been\nestablished and that requests are queueing.\n<ul>\n<li>The <em>process</em> transition moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> to the <strong>processing</strong> state,\nwhere requests are processed.</li>\n<li>If requests are queued, <em>close</em> transitions to <strong>processing</strong>; otherwise it\ntransitions to <strong>destroyed</strong>.</li>\n<li>The <em>destroy</em> transition moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> to <strong>destroyed</strong>.</li>\n</ul>\n</li>\n<li>The <strong>processing</strong> state initializes to the <strong>processing.running</strong> sub-state.\n<ul>\n<li>If the current request body requires draining, <em>needDrain</em> moves the\n<a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> into the <strong>processing.busy</strong> sub-state, which returns to\n<strong>processing.running</strong> once <em>drainComplete</em> fires.</li>\n<li>After all queued requests complete, <em>keepalive</em> moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> back to\nthe <strong>pending</strong> state. If no requests are queued before the socket times\nout, the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> transitions to <strong>idle</strong>.</li>\n<li>If <em>close</em> is fired while the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> still has queued requests, the\n<a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> transitions to <strong>processing.closing</strong>, where it completes all\noutstanding requests before firing <em>done</em>.</li>\n<li>The <em>done</em> transition moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> gracefully to <strong>destroyed</strong>.</li>\n<li>At any time, <em>destroy</em> moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> from <strong>processing</strong> to\n<strong>destroyed</strong>, aborting any queued requests.</li>\n</ul>\n</li>\n<li>The <strong>destroyed</strong> state is final; the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> is no longer functional.</li>\n</ul>\n<p>A state diagram representing a <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> instance:</p>\n<pre><code class=\"language-mermaid\">stateDiagram-v2\n  [*] --> idle\n  idle --> pending : connect\n  idle --> destroyed : destroy/close\n\n  pending --> idle : timeout\n  pending --> destroyed : destroy\n\n  state close_fork &#x3C;&#x3C;fork>>\n  pending --> close_fork : close\n  close_fork --> processing\n  close_fork --> destroyed\n\n  pending --> processing : process\n\n  processing --> pending : keepalive\n  processing --> destroyed : done\n  processing --> destroyed : destroy\n\n  destroyed --> [*]\n\n  state processing {\n      [*] --> running\n      running --> closing : close\n      running --> busy : needDrain\n      busy --> running : drainComplete\n      running --> [*] : keepalive\n      closing --> [*] : done\n  }\n</code></pre>",
          "displayName": "State transition overview"
        },
        {
          "textRaw": "State details",
          "name": "state_details",
          "type": "misc",
          "modules": [
            {
              "textRaw": "idle",
              "name": "idle",
              "type": "module",
              "desc": "<p><strong>idle</strong> is the initial state of a <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> instance. Although an <code>origin</code> is\nrequired to construct a <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a>, the underlying socket connection is not\nestablished until a request is queued through <a href=\"Client.html#clientdispatchoptions-handlers\"><code>client.dispatch()</code></a>. Calling\n<code>client.dispatch()</code> directly, or through one of its higher-level wrappers\n(<a href=\"Client.html#clientconnectoptions-callback\"><code>client.connect()</code></a>, <a href=\"Client.html#clientpipelineoptions-handler\"><code>client.pipeline()</code></a>, <a href=\"Client.html#clientrequestoptions-callback\"><code>client.request()</code></a>,\n<a href=\"Client.html#clientstreamoptions-factory-callback\"><code>client.stream()</code></a>, or <a href=\"Client.html#clientupgradeoptions-callback\"><code>client.upgrade()</code></a>), moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> from <strong>idle</strong> to <strong>pending</strong>, and then usually directly on to <strong>processing</strong>.</p>\n<p>Calling <a href=\"Client.html#clientclosecallback\"><code>client.close()</code></a> or <a href=\"Client.html#clientdestroyerror-callback\"><code>client.destroy()</code></a> in this state moves the\n<a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> straight to <strong>destroyed</strong>, since there are no queued requests.</p>",
              "displayName": "idle"
            },
            {
              "textRaw": "pending",
              "name": "pending",
              "type": "module",
              "desc": "<p><strong>pending</strong> signifies a connected but non-processing <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a>. On entering this\nstate, the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> establishes a socket connection and emits the <a href=\"Client.html#event-connect\"><code>'connect'</code></a> event, signalling that a connection to the <code>origin</code> supplied at\nconstruction time has been established. The internal queue starts empty, and\nrequests can begin queueing.</p>\n<p>Calling <a href=\"Client.html#clientclosecallback\"><code>client.close()</code></a> with queued requests moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> to the <strong>processing</strong> state; without queued requests it moves to <strong>destroyed</strong>.</p>\n<p>Calling <a href=\"Client.html#clientdestroyerror-callback\"><code>client.destroy()</code></a> moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> directly to <strong>destroyed</strong>,\nregardless of any queued requests.</p>",
              "displayName": "pending"
            },
            {
              "textRaw": "processing",
              "name": "processing",
              "type": "module",
              "desc": "<p><strong>processing</strong> is itself a sub-state machine that initializes to the\n<strong>processing.running</strong> sub-state. <a href=\"Client.html#clientdispatchoptions-handlers\"><code>client.dispatch()</code></a>, <a href=\"Client.html#clientclosecallback\"><code>client.close()</code></a>,\nand <a href=\"Client.html#clientdestroyerror-callback\"><code>client.destroy()</code></a> can all be called while the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> is in this\nstate. <code>client.dispatch()</code> queues additional requests while existing ones\ncontinue to be processed, <code>client.close()</code> moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> to the <strong>processing.closing</strong> sub-state, and <code>client.destroy()</code> moves it to\n<strong>destroyed</strong>.</p>",
              "modules": [
                {
                  "textRaw": "running",
                  "name": "running",
                  "type": "module",
                  "desc": "<p>In <strong>processing.running</strong>, queued requests are processed in FIFO order. If a\nrequest body requires draining, <em>needDrain</em> moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> to the <strong>processing.busy</strong> sub-state. The <em>close</em> transition moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> to the <strong>processing.closing</strong> sub-state. When all queued requests have completed and\nneither <a href=\"Client.html#clientclosecallback\"><code>client.close()</code></a> nor <a href=\"Client.html#clientdestroyerror-callback\"><code>client.destroy()</code></a> has been called, the\n<em>keepalive</em> transition moves the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> back to the <strong>pending</strong> state. There\nthe <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> waits for the socket connection to time out; once it does, the\n<a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> returns to the <strong>idle</strong> state.</p>",
                  "displayName": "running"
                },
                {
                  "textRaw": "busy",
                  "name": "busy",
                  "type": "module",
                  "desc": "<p>This sub-state is entered only when a request body is a <a href=\"https://nodejs.org/api/stream.html#class-streamreadable\"><code>&#x3C;stream.Readable></code></a> that\nrequires draining. The <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> cannot process additional requests while in\nthis state and waits until the current request body has been fully drained\nbefore returning to <strong>processing.running</strong>.</p>",
                  "displayName": "busy"
                },
                {
                  "textRaw": "closing",
                  "name": "closing",
                  "type": "module",
                  "desc": "<p>This sub-state is entered only when a <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> has queued requests and <a href=\"Client.html#clientclosecallback\"><code>client.close()</code></a> is called. The <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> continues to process requests as\nusual, except that no additional requests may be queued. Once all queued\nrequests have been processed, <em>done</em> fires and the <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> moves gracefully to <strong>destroyed</strong> without an error.</p>",
                  "displayName": "closing"
                }
              ],
              "displayName": "processing"
            },
            {
              "textRaw": "destroyed",
              "name": "destroyed",
              "type": "module",
              "desc": "<p><strong>destroyed</strong> is the final state of a <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> instance. Once in this state, the\n<a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> is no longer functional, and calling any further <a href=\"https://developer.mozilla.org/docs/Web/API/Client\"><code>&#x3C;Client></code></a> method\nrejects or fails with a <a href=\"Errors.md#class-clientdestroyederror\"><code>&#x3C;ClientDestroyedError></code></a>.</p>",
              "displayName": "destroyed"
            }
          ],
          "displayName": "State details"
        }
      ]
    }
  ]
}