{
  "type": "module",
  "source": "doc/api/best-practices-proxy.md",
  "modules": [
    {
      "textRaw": "Connecting through a proxy",
      "name": "connecting_through_a_proxy",
      "type": "module",
      "desc": "<p>Connecting through a proxy is possible by:</p>\n<ul>\n<li>Using <a href=\"/docs/docs/api/ProxyAgent.html\">ProxyAgent</a>.</li>\n<li>Configuring <code>Client</code> or <code>Pool</code> constructor.</li>\n</ul>\n<p>The proxy url should be passed to the <code>Client</code> or <code>Pool</code> constructor, while the upstream server url\nshould be added to every request call in the <code>path</code>.\nFor instance, if you need to send a request to the <code>/hello</code> route of your upstream server,\nthe <code>path</code> should be <code>path: 'http://upstream.server:port/hello?foo=bar'</code>.</p>\n<p>If you proxy requires basic authentication, you can send it via the <code>proxy-authorization</code> header.</p>",
      "modules": [
        {
          "textRaw": "Connect without authentication",
          "name": "connect_without_authentication",
          "type": "module",
          "desc": "<pre><code class=\"language-js\">import { Client } from 'undici'\nimport { createServer } from 'http'\nimport { createProxy } from 'proxy'\n\nconst server = await buildServer()\nconst proxyServer = await buildProxy()\n\nconst serverUrl = `http://localhost:${server.address().port}`\nconst proxyUrl = `http://localhost:${proxyServer.address().port}`\n\nserver.on('request', (req, res) => {\n  console.log(req.url) // '/hello?foo=bar'\n  res.setHeader('content-type', 'application/json')\n  res.end(JSON.stringify({ hello: 'world' }))\n})\n\nconst client = new Client(proxyUrl)\n\nconst response = await client.request({\n  method: 'GET',\n  path: serverUrl + '/hello?foo=bar'\n})\n\nresponse.body.setEncoding('utf8')\nlet data = ''\nfor await (const chunk of response.body) {\n  data += chunk\n}\nconsole.log(response.statusCode) // 200\nconsole.log(JSON.parse(data)) // { hello: 'world' }\n\nserver.close()\nproxyServer.close()\nclient.close()\n\nfunction buildServer () {\n  return new Promise((resolve, reject) => {\n    const server = createServer()\n    server.listen(0, () => resolve(server))\n  })\n}\n\nfunction buildProxy () {\n  return new Promise((resolve, reject) => {\n    const server = createProxy(createServer())\n    server.listen(0, () => resolve(server))\n  })\n}\n</code></pre>",
          "displayName": "Connect without authentication"
        },
        {
          "textRaw": "Connect with authentication",
          "name": "connect_with_authentication",
          "type": "module",
          "desc": "<pre><code class=\"language-js\">import { Client } from 'undici'\nimport { createServer } from 'http'\nimport { createProxy } from 'proxy'\n\nconst server = await buildServer()\nconst proxyServer = await buildProxy()\n\nconst serverUrl = `http://localhost:${server.address().port}`\nconst proxyUrl = `http://localhost:${proxyServer.address().port}`\n\nproxyServer.authenticate = function (req) {\n  return req.headers['proxy-authorization'] === `Basic ${Buffer.from('user:pass').toString('base64')}`\n}\n\nserver.on('request', (req, res) => {\n  console.log(req.url) // '/hello?foo=bar'\n  res.setHeader('content-type', 'application/json')\n  res.end(JSON.stringify({ hello: 'world' }))\n})\n\nconst client = new Client(proxyUrl)\n\nconst response = await client.request({\n  method: 'GET',\n  path: serverUrl + '/hello?foo=bar',\n  headers: {\n    'proxy-authorization': `Basic ${Buffer.from('user:pass').toString('base64')}`\n  }\n})\n\nresponse.body.setEncoding('utf8')\nlet data = ''\nfor await (const chunk of response.body) {\n  data += chunk\n}\nconsole.log(response.statusCode) // 200\nconsole.log(JSON.parse(data)) // { hello: 'world' }\n\nserver.close()\nproxyServer.close()\nclient.close()\n\nfunction buildServer () {\n  return new Promise((resolve, reject) => {\n    const server = createServer()\n    server.listen(0, () => resolve(server))\n  })\n}\n\nfunction buildProxy () {\n  return new Promise((resolve, reject) => {\n    const server = createProxy(createServer())\n    server.listen(0, () => resolve(server))\n  })\n}\n</code></pre>",
          "displayName": "Connect with authentication"
        }
      ],
      "displayName": "Connecting through a proxy"
    }
  ]
}