{
  "version": 3,
  "sources": ["../../../../src/packages/network.proxy-agent/proxy-agent.ts"],
  "sourcesContent": ["import { PnpmError } from '../error/index.ts';\nimport { HttpsProxyAgent } from 'https-proxy-agent';\nimport { HttpProxyAgent, type HttpProxyAgentOptions } from 'http-proxy-agent';\nimport { SocksProxyAgent } from 'socks-proxy-agent';\nimport * as LRU from 'lru-cache';\nimport type { ClientRequest } from 'node:http';\nimport type { RequestOptions } from 'node:https';\n\nconst DEFAULT_MAX_SOCKETS = 50;\n\nconst AGENT_CACHE = new LRU.LRUCache<\n  string,\n  HttpProxyAgent<string> | PatchedHttpsProxyAgent | SocksProxyAgent\n>({ max: 50 });\n\nexport type ProxyAgentOptions = {\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 getProxyAgent(\n  uri: string,\n  opts: ProxyAgentOptions\n):\n  | HttpProxyAgent<string>\n  | PatchedHttpsProxyAgent\n  | SocksProxyAgent\n  | undefined {\n  const parsedUri = new URL(uri);\n\n  const pxuri = getProxyUri(parsedUri, opts);\n\n  if (!pxuri) {\n    return;\n  }\n\n  const isHttps = parsedUri.protocol === 'https:';\n\n  const key = [\n    `https:${isHttps.toString()}`,\n    `proxy:${pxuri.protocol}//${pxuri.username}:${pxuri.password}@${pxuri.host}:${pxuri.port}`,\n    `local-address:${opts.localAddress ?? '>no-local-address<'}`,\n    `strict-ssl:${\n      isHttps ? Boolean(opts.strictSsl).toString() : '>no-strict-ssl<'\n    }`,\n    `ca:${(isHttps && (opts.ca?.toString() ?? '>noca<')) || '>no-ca<'}`,\n    `cert:${(isHttps && (opts.cert?.toString() ?? '>no-cert<')) || '>no-cert<'}`,\n    `key:${(isHttps && (opts.key ?? '>no-key<')) || '>no-key<'}`,\n  ].join(':');\n\n  if (typeof AGENT_CACHE.peek(key) !== 'undefined') {\n    return AGENT_CACHE.get(key);\n  }\n\n  const proxy = getProxy(pxuri, opts, isHttps);\n\n  AGENT_CACHE.set(key, proxy);\n\n  return proxy;\n}\n\nfunction getProxyUri(\n  uri: URL,\n  opts: {\n    httpProxy?: string | undefined;\n    httpsProxy?: string | undefined;\n  }\n): URL | undefined {\n  const { protocol } = uri;\n\n  let proxy: string | undefined;\n  switch (protocol) {\n    case 'http:': {\n      proxy = opts.httpProxy;\n      break;\n    }\n    case 'https:': {\n      proxy = opts.httpsProxy;\n      break;\n    }\n  }\n\n  if (typeof proxy === 'undefined' || proxy === '') {\n    return undefined;\n  }\n\n  if (!proxy.includes('://')) {\n    proxy = `${protocol}//${proxy}`;\n  }\n\n  if (typeof proxy !== 'string') {\n    return proxy;\n  }\n\n  try {\n    return new URL(proxy);\n    // eslint-disable-next-line @typescript-eslint/no-unused-vars\n  } catch (_err: unknown) {\n    throw new PnpmError('INVALID_PROXY', \"Couldn't parse proxy URL\", {\n      hint: 'If your proxy URL contains a username and password, make sure to URL-encode them (you may use the encodeURIComponent function). For instance, https-proxy=https://use%21r:pas%2As@my.proxy:1234/foo. Do not encode the colon (:) between the username and password.',\n    });\n  }\n}\n\nfunction getProxy(\n  proxyUrl: URL,\n  opts: {\n    ca?: string | string[] | undefined;\n    cert?: string | string[] | undefined;\n    key?: string | undefined;\n    timeout?: number | undefined;\n    localAddress?: string | undefined;\n    maxSockets?: number | undefined;\n    strictSsl?: boolean | undefined;\n  },\n  isHttps: boolean\n):\n  | HttpProxyAgent<string>\n  | PatchedHttpsProxyAgent\n  | SocksProxyAgent\n  | undefined {\n  const proxyOptions = {\n    auth: getAuth(proxyUrl),\n    ca: opts.ca,\n    cert: opts.cert,\n    host: proxyUrl.hostname,\n    key: opts.key,\n    localAddress: opts.localAddress,\n    maxSockets: opts.maxSockets ?? DEFAULT_MAX_SOCKETS,\n    path: proxyUrl.pathname,\n    port: Number.parseInt(proxyUrl.port, 10),\n    protocol: proxyUrl.protocol,\n    rejectUnauthorized: opts.strictSsl,\n    timeout:\n      typeof opts.timeout !== 'number' || opts.timeout === 0\n        ? 0\n        : opts.timeout + 1,\n  };\n\n  if (proxyUrl.protocol === 'http:' || proxyUrl.protocol === 'https:') {\n    if (!isHttps) {\n      return new HttpProxyAgent(proxyUrl, proxyOptions);\n    }\n\n    return new PatchedHttpsProxyAgent(proxyUrl, proxyOptions);\n  }\n\n  if (proxyUrl.protocol.startsWith('socks')) {\n    return new SocksProxyAgent(proxyUrl, proxyOptions);\n  }\n\n  return undefined;\n}\n\nfunction getAuth(user: {\n  username?: string | undefined;\n  password?: string | undefined;\n}): string | undefined {\n  if (typeof user.username === 'undefined' || user.username === '') {\n    return undefined;\n  }\n\n  let auth = user.username;\n\n  if (typeof user.password === 'string' && user.password !== '') {\n    auth += `:${user.password}`;\n  }\n\n  return decodeURIComponent(auth);\n}\n\nconst extraOpts = Symbol('extra agent opts');\n\n// This is a workaround for this issue: https://github.com/TooTallNate/node-https-proxy-agent/issues/89\nexport class PatchedHttpsProxyAgent extends HttpsProxyAgent<string> {\n  constructor(uri: string | URL, opts: HttpProxyAgentOptions<string>) {\n    super(uri, opts);\n\n    // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n    // @ts-ignore\n    this[extraOpts] = opts;\n  }\n\n  callback(req: ClientRequest, opts: RequestOptions): unknown {\n    // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n    // @ts-ignore\n    return super.callback(req, { ...this[extraOpts], ...opts });\n  }\n}\n"],
  "mappings": "AAAA,SAAS,iBAAiB;AAC1B,SAAS,uBAAuB;AAChC,SAAS,sBAAkD;AAC3D,SAAS,uBAAuB;AAChC,YAAY,SAAS;AAIrB,MAAM,sBAAsB;AAE5B,MAAM,cAAc,IAAI,IAAI,SAG1B,EAAE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