{
  "version": 3,
  "sources": ["../../../../src/packages/fetch/fetchFromRegistry.ts"],
  "sourcesContent": ["import { URL } from 'node:url';\nimport type { SslConfig } from '../types/index.ts';\nimport type { FetchFromRegistry } from '../fetching-types/index.ts';\nimport { getAgent, type AgentOptions } from '../network.agent/index.ts';\nimport {\n  fetch,\n  isRedirect,\n  type Response,\n  type RequestInfo,\n  type RequestInit,\n} from './fetch.ts';\nimport type { HeadersInit } from 'node-fetch';\n\nconst USER_AGENT = 'pnpm'; // or maybe make it `${pkg.name}/${pkg.version} (+https://npm.im/${pkg.name})`\n\nconst ABBREVIATED_DOC =\n  'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*';\nconst JSON_DOC = 'application/json';\nconst MAX_FOLLOWED_REDIRECTS = 20;\n\nexport type FetchWithAgentOptions = RequestInit & {\n  agentOptions: AgentOptions;\n};\n\nexport function fetchWithAgent(\n  url: RequestInfo,\n  opts: FetchWithAgentOptions\n): Promise<Response> {\n  const agent = getAgent(\n    typeof url === 'string' ? url : 'href' in url ? url.href : url.url,\n    {\n      ...opts.agentOptions,\n      strictSsl: opts.agentOptions.strictSsl ?? true,\n    }\n  );\n\n  const headers = opts.headers ?? {};\n\n  // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n  // @ts-ignore Element implicitly has an 'any' type because expression of type '\"connection\"' can't be used to index type 'HeadersInit'.\n  //Property 'connection' does not exist on type 'HeadersInit'.ts(7053)\n  headers['connection'] = agent ? 'keep-alive' : 'close';\n\n  return fetch(url, {\n    ...opts,\n    agent,\n  });\n}\n\nexport type { AgentOptions };\n\nexport function createFetchFromRegistry(\n  defaultOpts: {\n    fullMetadata?: boolean | undefined;\n    userAgent?: string | undefined;\n    sslConfigs?: Record<string, SslConfig> | undefined;\n  } & AgentOptions\n): FetchFromRegistry {\n  return async (url, opts): Promise<Response> => {\n    const headers: Headers = {\n      'user-agent': USER_AGENT,\n      ...getHeaders({\n        auth: opts?.authHeaderValue,\n        fullMetadata: defaultOpts.fullMetadata,\n        userAgent: defaultOpts.userAgent,\n      }),\n    };\n\n    let redirects = 0;\n    let urlObject = new URL(url);\n    const originalHost = urlObject.host;\n\n    // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n    while (true) {\n      const agentOptions = {\n        ...defaultOpts,\n        ...opts,\n        strictSsl: defaultOpts.strictSsl ?? true,\n      } as any; // eslint-disable-line\n\n      // We should pass a URL object to node-fetch till this is not resolved:\n      // https://github.com/bitinn/node-fetch/issues/245\n      const response = await fetchWithAgent(urlObject, {\n        agentOptions: {\n          ...agentOptions,\n          clientCertificates: defaultOpts.sslConfigs,\n        },\n        // if verifying integrity, node-fetch must not decompress\n        compress: opts?.compress ?? false,\n        method: opts?.method ?? '',\n        headers: headers as HeadersInit,\n        redirect: 'manual',\n        retry: opts?.retry,\n        timeout: opts?.timeout ?? 60_000,\n      });\n\n      if (!isRedirect(response.status) || redirects >= MAX_FOLLOWED_REDIRECTS) {\n        return response;\n      }\n\n      // This is a workaround to remove authorization headers on redirect.\n      // Related pnpm issue: https://github.com/pnpm/pnpm/issues/1815\n      redirects++;\n\n      urlObject = new URL(response.headers.get('location') ?? '');\n\n      if (\n        typeof headers['authorization'] !== 'string' ||\n        originalHost === urlObject.host\n      ) {\n        continue;\n      }\n\n      headers.authorization = '';\n    }\n  };\n}\n\ntype Headers = {\n  accept: string;\n  authorization?: string | undefined;\n  'user-agent'?: string | undefined;\n};\n\nfunction getHeaders(opts: {\n  auth?: string | undefined;\n  fullMetadata?: boolean | undefined;\n  userAgent?: string | undefined;\n}): Headers {\n  const headers: Headers = {\n    accept: opts.fullMetadata === true ? JSON_DOC : ABBREVIATED_DOC,\n  };\n\n  if (typeof opts.auth === 'string') {\n    headers.authorization = opts.auth;\n  }\n\n  if (typeof opts.userAgent === 'string') {\n    headers['user-agent'] = opts.userAgent;\n  }\n\n  return headers;\n}\n"],
  "mappings": "AAAA,SAAS,WAAW;AAGpB,SAAS,gBAAmC;AAC5C;AAAA,EACE;AAAA,EACA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