Repository: cypress-io/cypress
File: packages/network/lib/agent.ts (TypeScript)

Code before:
  return _.get(matches, 1) === '200'
}

export const regenerateRequestHead = (req: http.ClientRequest) => {
  delete req._header
  req._implicitHeader()
  if (req.output && req.output.length > 0) {
    // the _header has already been queued to be written to the socket
    const first = req.output[0]
    const endOfHeaders = first.indexOf(_.repeat(CRLF, 2)) + 4

    req.output[0] = req._header + first.substring(endOfHeaders)
  }
}

export const getFirstWorkingFamily = (
  { port, host }: Pick<http.RequestOptions, 'port' | 'host'>,
  familyCache: FamilyCache,
  cb: (family?: net.family) => void,
) => {
  // this is a workaround for localhost (and potentially others) having invalid
  // A records but valid AAAA records. here, we just cache the family of the first
  // returned A/AAAA record for a host that we can establish a connection to.
  // https://github.com/cypress-io/cypress/issues/112

  const isIP = net.isIP(host) as net.family

  if (isIP) {
    // isIP conveniently returns the family of the address
    return cb(isIP)
  }

  if (process.env.HTTP_PROXY) {
    // can't make direct connections through the proxy, this won't work
    return cb()
  }

  const cacheKey = `${host}:${port}`

  if (familyCache[cacheKey]) {
    return cb(familyCache[cacheKey])
  }

  return getAddress(port, host)
  .then((firstWorkingAddress: net.Address) => {
    familyCache[cacheKey] = firstWorkingAddress.family

    return cb(firstWorkingAddress.family)
  })
  .catch(() => {
    return cb()

Review comment (suggestion):
Reading the docs for `net.isIP` this can return 4 | 6 | 0. `net.family` is just 4 | 6. I think this provides clarity and explains why we have the check on 163.

```suggestion
  const isIP = net.isIP(host) as net.family | 0
```

Code after:
  return _.get(matches, 1) === '200'
}

export const regenerateRequestHead = (req: http.ClientRequest) => {
  delete req._header
  req._implicitHeader()
  if (req.output && req.output.length > 0) {
    // the _header has already been queued to be written to the socket
    const first = req.output[0]
    const endOfHeaders = first.indexOf(_.repeat(CRLF, 2)) + 4

    req.output[0] = req._header + first.substring(endOfHeaders)
  }
}

export const getFirstWorkingFamily = (
  { port, host }: Pick<http.RequestOptions, 'port' | 'host'>,
  familyCache: FamilyCache,
  cb: (family?: net.family) => void,
) => {
  // this is a workaround for localhost (and potentially others) having invalid
  // A records but valid AAAA records. here, we just cache the family of the first
  // returned A/AAAA record for a host that we can establish a connection to.
  // https://github.com/cypress-io/cypress/issues/112

  const isIP = net.isIP(host) as net.family | 0

  if (isIP) {
    // isIP conveniently returns the family of the address
    return cb(isIP)
  }

  if (process.env.HTTP_PROXY) {
    // can't make direct connections through the proxy, this won't work
    return cb()
  }

  const cacheKey = `${host}:${port}`

  if (familyCache[cacheKey]) {
    return cb(familyCache[cacheKey])
  }

  return getAddress(port, host)
  .then((firstWorkingAddress: net.Address) => {
    familyCache[cacheKey] = firstWorkingAddress.family

    return cb(firstWorkingAddress.family)
  })
  .catch(() => {
    return cb()