{
  "version": 3,
  "sources": ["../../../../src/packages/network.agent/agent.ts"],
  "sourcesContent": ["import { URL } from 'node:url';\nimport { HttpsAgent, HttpAgent } from 'agentkeepalive';\nimport * as LRU from 'lru-cache';\nimport { getProxyAgent } from '../network.proxy-agent/index.ts';\nimport { pickSettingByUrl } from '../network.config/index.ts';\nimport type { HttpProxyAgent } from 'http-proxy-agent';\nimport type { PatchedHttpsProxyAgent } from '../network.proxy-agent/proxy-agent.ts';\nimport type { SocksProxyAgent } from 'socks-proxy-agent';\n\nconst DEFAULT_MAX_SOCKETS = 50;\n\nconst AGENT_CACHE = new LRU.LRUCache<string, HttpsAgent | HttpAgent>({\n  max: 50,\n});\n\nexport type AgentOptions = {\n  ca?: string | string[] | undefined;\n  cert?: string | string[] | undefined;\n  httpProxy?: string | undefined;\n  httpsProxy?: string | undefined;\n  key?: string | undefined;\n  localAddress?: string | undefined;\n  maxSockets?: number | undefined;\n  noProxy?: boolean | string | undefined;\n  strictSsl?: boolean | undefined;\n  timeout?: number | undefined;\n  clientCertificates?:\n    | {\n        [registryUrl: string]: {\n          cert: string;\n          key: string;\n          ca?: string | undefined;\n        };\n      }\n    | undefined;\n};\n\nexport function getAgent(\n  uri: string,\n  opts: AgentOptions\n):\n  | HttpProxyAgent<string>\n  | PatchedHttpsProxyAgent\n  | SocksProxyAgent\n  | HttpsAgent\n  | HttpAgent\n  | undefined {\n  if (\n    (typeof opts.httpProxy === 'string' ||\n      typeof opts.httpsProxy === 'string') &&\n    !checkNoProxy(uri, opts)\n  ) {\n    const proxyAgent = getProxyAgent(uri, opts);\n\n    if (proxyAgent) {\n      return proxyAgent;\n    }\n  }\n\n  return getNonProxyAgent(uri, opts);\n}\n\nfunction getNonProxyAgent(\n  uri: string,\n  opts: AgentOptions\n): HttpsAgent | HttpAgent | undefined {\n  const parsedUri = new URL(uri);\n\n  const isHttps = parsedUri.protocol === 'https:';\n\n  const {\n    ca,\n    cert,\n    key: certKey,\n  } = {\n    ...opts,\n    ...pickSettingByUrl(opts.clientCertificates, uri),\n  };\n\n  const key = [\n    `https:${isHttps.toString()}`,\n    `local-address:${opts.localAddress ?? '>no-local-address<'}`,\n    `strict-ssl:${\n      isHttps ? Boolean(opts.strictSsl).toString() : '>no-strict-ssl<'\n    }`,\n    `ca:${(isHttps && ca?.toString()) ?? '>no-ca<'}`,\n    `cert:${(isHttps && cert?.toString()) ?? '>no-cert<'}`,\n    `key:${(isHttps && certKey?.toString()) ?? '>no-key<'}`,\n  ].join(':');\n\n  if (typeof AGENT_CACHE.peek(key) !== 'undefined') {\n    return AGENT_CACHE.get(key);\n  }\n\n  // If opts.timeout is zero, set the agentTimeout to zero as well. A timeout\n  // of zero disables the timeout behavior (OS limits still apply). Else, if\n  // opts.timeout is a non-zero value, set it to timeout + 1, to ensure that\n  // the node-fetch-npm timeout will always fire first, giving us more\n  // consistent errors.\n  const agentTimeout =\n    typeof opts.timeout !== 'number' || opts.timeout === 0\n      ? 0\n      : opts.timeout + 1;\n\n  // NOTE: localAddress is passed to the agent here even though it is an\n  // undocumented option of the agent's constructor.\n  //\n  // This works because all options of the agent are merged with\n  // all options of the request:\n  // https://github.com/nodejs/node/blob/350a95b89faab526de852d417bbb8a3ac823c325/lib/_http_agent.js#L254\n  const agent = isHttps\n    ? new HttpsAgent({\n        ca,\n        cert,\n        key: certKey,\n        localAddress: opts.localAddress,\n        maxSockets: opts.maxSockets ?? DEFAULT_MAX_SOCKETS,\n        rejectUnauthorized: opts.strictSsl,\n        timeout: agentTimeout,\n      } as any) // eslint-disable-line @typescript-eslint/no-explicit-any\n    : new HttpAgent({\n        localAddress: opts.localAddress,\n        maxSockets: opts.maxSockets ?? DEFAULT_MAX_SOCKETS,\n        timeout: agentTimeout,\n      } as any); // eslint-disable-line @typescript-eslint/no-explicit-any\n  AGENT_CACHE.set(key, agent);\n\n  return agent;\n}\n\nfunction checkNoProxy(\n  uri: string,\n  opts: { noProxy?: boolean | string | undefined }\n): boolean {\n  const host = new URL(uri).hostname\n    .split('.')\n    .filter((x) => x)\n    .reverse();\n  if (typeof opts.noProxy === 'string') {\n    const noproxyArr = opts.noProxy.split(/\\s*,\\s*/g);\n    return noproxyArr.some((no) => {\n      const noParts = no\n        .split('.')\n        .filter((x) => x)\n        .reverse();\n      if (noParts.length === 0) {\n        return false;\n      }\n      for (let i = 0; i < noParts.length; i++) {\n        if (host[i] !== noParts[i]) {\n          return false;\n        }\n      }\n      return true;\n    });\n  }\n  return opts.noProxy ?? false;\n}\n"],
  "mappings": "AAAA,SAAS,WAAW;AACpB,SAAS,YAAY,iBAAiB;AACtC,YAAY,SAAS;AACrB,SAAS,qBAAqB;AAC9B,SAAS,wBAAwB;AAKjC,MAAM,sBAAsB;AAE5B,MAAM,cAAc,IAAI,IAAI,SAAyC;AAAA,EACnE,KAAK;AACP,CAAC;AAwBM,SAAS,SACd,KACA,MAOY;AACZ,OACG,OAAO,KAAK,cAAc,YACzB,OAAO,KAAK,eAAe,aAC7B,CAAC,aAAa,KAAK,IAAI,GACvB;AACA,UAAM,aAAa,cAAc,KAAK,IAAI;AAE1C,QAAI,YAAY;AACd,aAAO;AAAA,IACT;AAAA,EACF;AAEA,SAAO,iBAAiB,KAAK,IAAI;AACnC;AAEA,SAAS,iBACP,KACA,MACoC;AACpC,QAAM,YAAY,IAAI,IAAI,GAAG;AAE7B,QAAM,UAAU,UAAU,aAAa;AAEvC,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA,KAAK;AAAA,EACP,IAAI;AAAA,IACF,GAAG;AAAA,IACH,GAAG,iBAAiB,KAAK,oBAAoB,GAAG;AAAA,EAClD;AAEA,QAAM,MAAM;AAAA,IACV,SAAS,QAAQ,SAAS,CAAC;AAAA,IAC3B,iBAAiB,KAAK,gBAAgB,oBAAoB;AAAA,IAC1D,cACE,UAAU,QAAQ,KAAK,SAAS,EAAE,SAAS,IAAI,iBACjD;AAAA,IACA,OAAO,WAAW,IAAI,SAAS,MAAM,SAAS;AAAA,IAC9C,SAAS,WAAW,MAA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