Skip to content

Commit

Permalink
Revert "server, launcher, ts: typescript"
Browse files Browse the repository at this point in the history
This reverts commit d3f8b8b.
  • Loading branch information
flotwig committed Mar 26, 2019
1 parent d3f8b8b commit 818dfdf
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 204 deletions.
10 changes: 0 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,6 @@
"@cypress/env-or-json-file": "2.0.0",
"@cypress/npm-run-all": "4.0.5",
"@cypress/questions-remain": "1.0.1",
"@types/bluebird": "3.5.21",
"@types/chai": "3.5.2",
"@types/debug": "0.0.31",
"@types/execa": "0.7.2",
"@types/fs-extra": "3.0.0",
"@types/lodash": "4.14.122",
"@types/mocha": "2.2.48",
"@types/node": "7.10.3",
"@types/ramda": "0.25.47",
"@types/request-promise": "4.1.42",
"ansi-styles": "3.2.1",
"ascii-table": "0.0.9",
"babel-eslint": "10.0.1",
Expand Down
9 changes: 9 additions & 0 deletions packages/launcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
"lib"
],
"devDependencies": {
"@types/bluebird": "3.5.21",
"@types/chai": "3.5.2",
"@types/debug": "0.0.31",
"@types/execa": "0.7.2",
"@types/fs-extra": "3.0.0",
"@types/lodash": "4.14.122",
"@types/mocha": "2.2.48",
"@types/node": "7.10.3",
"@types/ramda": "0.25.47",
"bin-up": "1.1.0",
"chai": "3.5.0",
"prettier": "1.16.4",
Expand Down
63 changes: 30 additions & 33 deletions packages/server/lib/util/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ import * as url from 'url'
import * as debugModule from 'debug'
import { getProxyForUrl } from 'proxy-from-env'
import * as Promise from 'bluebird'
import { getAddress } from './connect'
import * as connect from './connect'

const debug = debugModule('cypress:server:agent')

interface RequestOptionsWithProxy extends http.RequestOptions {
proxy: string
}

function createProxySock (proxy: url.Url) {
if (proxy.protocol === 'http:') {
return net.connect(Number(proxy.port || 80), proxy.hostname)
Expand All @@ -28,21 +24,21 @@ function createProxySock (proxy: url.Url) {
throw new Error(`Unsupported proxy protocol: ${proxy.protocol}`)
}

function regenerateRequestHead(req: http.ClientRequest) {
delete req._header
req._implicitHeader()
function regenerateRequestHead(req) {
req._header = null;
req._implicitHeader();
if (req.output && req.output.length > 0) {
// the _header has already been queued to be written to the socket
var first = req.output[0]
var endOfHeaders = first.indexOf('\r\n\r\n') + 4
req.output[0] = req._header + first.substring(endOfHeaders)
var first = req.output[0];
var endOfHeaders = first.indexOf('\r\n\r\n') + 4;
req.output[0] = req._header + first.substring(endOfHeaders);
}
}

export class CombinedAgent {
httpAgent: HttpAgent
httpsAgent: HttpsAgent
familyCache: { [host: string] : 4 | 6 } = {}
familyCache = {}

constructor(httpOpts: http.AgentOptions = {}, httpsOpts: https.AgentOptions = {}) {
this.httpAgent = new HttpAgent(httpOpts)
Expand Down Expand Up @@ -73,7 +69,7 @@ export class CombinedAgent {
debug(`addRequest called for ${options.href}`)

this._getFirstWorkingFamily(options)
.then((family: Optional<Number>) => {
.then(family => {
options.family = family

if (isHttps) {
Expand All @@ -84,7 +80,7 @@ export class CombinedAgent {
})
}

_getFirstWorkingFamily({ port, host }: http.RequestOptions) {
_getFirstWorkingFamily({ port, host }) {
// 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.
Expand All @@ -105,8 +101,8 @@ export class CombinedAgent {
return Promise.resolve(this.familyCache[host])
}

return getAddress(port, host)
.then((firstWorkingAddress: net.Address) => {
return connect.getAddress(port, host)
.then(firstWorkingAddress => {
this.familyCache[host] = firstWorkingAddress.family
return firstWorkingAddress.family
})
Expand All @@ -124,21 +120,21 @@ class HttpAgent extends http.Agent {
this.httpsAgent = new https.Agent({ keepAlive: true })
}

createSocket (req: http.ClientRequest, options: http.RequestOptions, cb: http.SocketCallback) {
createSocket (req, options, cb) {
if (process.env.HTTP_PROXY) {
const proxy = getProxyForUrl(options.href)

if (proxy) {
options.proxy = proxy

return this._createProxiedSocket(req, <RequestOptionsWithProxy>options, cb)
return this._createProxiedSocket(req, options, cb)
}
}

super.createSocket(req, options, cb)
}

_createProxiedSocket (req: http.ClientRequest, options: RequestOptionsWithProxy, cb: http.SocketCallback) {
_createProxiedSocket (req, options, cb) {
debug(`Creating proxied socket for ${options.href} through ${options.proxy}`)

const proxy = url.parse(options.proxy)
Expand All @@ -158,8 +154,8 @@ class HttpAgent extends http.Agent {
// https://github.com/TooTallNate/node-http-proxy-agent/blob/master/index.js#L93
regenerateRequestHead(req)

options.port = Number(proxy.port || 80)
options.host = proxy.hostname || 'localhost'
options.port = proxy.port
options.host = proxy.hostname
delete options.path // so the underlying net.connect doesn't default to IPC

if (proxy.protocol === 'https:') {
Expand All @@ -179,22 +175,22 @@ class HttpsAgent extends https.Agent {
super(opts)
}

createConnection (options: http.RequestOptions, cb: http.SocketCallback) {
createConnection (options, cb) {
if (process.env.HTTPS_PROXY) {
const proxy = getProxyForUrl(options.href)

if (typeof proxy === "string") {
options.proxy = <string>proxy
if (proxy) {
options.proxy = proxy

return this.createProxiedConnection(<RequestOptionsWithProxy>options, cb)
return this.createProxiedConnection(options, cb)
}
}

// @ts-ignore
cb(null, super.createConnection(options))
}

createProxiedConnection (options: RequestOptionsWithProxy, cb: http.SocketCallback) {
createProxiedConnection (options, cb) {
// heavily inspired by
// https://github.com/mknj/node-keepalive-proxy-agent/blob/master/index.js
debug(`Creating proxied socket for ${options.href} through ${options.proxy}`)
Expand All @@ -205,14 +201,14 @@ class HttpsAgent extends https.Agent {

const proxySocket = createProxySock(proxy)

const onError = (err: Error) => {
const onError = (err) => {
proxySocket.destroy()
cb(err, undefined)
cb(err)
}

let buffer = ''

const onData = (data: Buffer) => {
const onData = (data) => {
debug(`Proxy socket for ${options.href} established`)

buffer += data.toString()
Expand All @@ -227,18 +223,19 @@ class HttpsAgent extends https.Agent {
// read status code from proxy's response
const matches = buffer.match(/^HTTP\/1.1 (\d*)/)

if (!matches || matches[1] !== '200') {
return onError(new Error(`Error establishing proxy connection: ${matches ? matches[0] : buffer}`))
if (matches[1] !== '200') {
return onError(new Error(`Error establishing proxy connection: ${matches[0]}`))
}

if (options._agentKey) {
// https.Agent will upgrade and reuse this socket now
options.socket = proxySocket
options.servername = hostname
return cb(undefined, super.createConnection(options, undefined))
// @ts-ignore
return cb(null, super.createConnection(options))
}

cb(undefined, proxySocket)
cb(null, proxySocket)
}

proxySocket.once('error', onError)
Expand Down
62 changes: 62 additions & 0 deletions packages/server/lib/util/connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const net = require('net')
const dns = require('dns')
const url = require('url')
const rp = require('request-promise')
const Promise = require('bluebird')

module.exports = {
byPortAndAddress (port, address) {
// https://nodejs.org/api/net.html#net_net_connect_port_host_connectlistener
return new Promise((resolve, reject) => {
const client = net.connect(port, address.address)

client.on('connect', () => {
client.end()

resolve(address)
})

client.on('error', reject)
})
},

getAddress (port, hostname) {
const fn = this.byPortAndAddress.bind(this, port)

// promisify at the very last second which enables us to
// modify dns lookup function (via hosts overrides)
const lookupAsync = Promise.promisify(dns.lookup, { context: dns })

// this does not go out to the network to figure
// out the addresess. in fact it respects the /etc/hosts file
// https://github.com/nodejs/node/blob/dbdbdd4998e163deecefbb1d34cda84f749844a4/lib/dns.js#L108
// https://nodejs.org/api/dns.html#dns_dns_lookup_hostname_options_callback
return lookupAsync(hostname, { all: true })
.then((addresses) => {
// convert to an array if string
return [].concat(addresses).map(fn)
})
.any()
},

ensureUrl (urlStr) {
// takes a urlStr and verifies the hostname + port
let { hostname, protocol, port } = url.parse(urlStr)

if (port == null) {
port = protocol === 'https:' ? '443' : '80'
}

if (process.env.HTTP_PROXY) {
// cannot make arbitrary connections behind a proxy, attempt HTTP/HTTPS
return rp({
url: urlStr,
agent: require('./agent'),
proxy: null,
})
.catch({ name: 'StatusCodeError' }, () => {}) // we just care if it can connect, not if it's a valid resource
}

return this.getAddress(port, hostname)
},
}
58 changes: 0 additions & 58 deletions packages/server/lib/util/connect.ts

This file was deleted.

42 changes: 42 additions & 0 deletions packages/server/lib/util/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const os = require('os')
const getWindowsProxy = require('@cypress/get-windows-proxy')

module.exports = {
_getWindowsProxy () {
return getWindowsProxy()
},

_normalizeEnvironmentProxy () {
if (!process.env.HTTPS_PROXY) {
// request library will use HTTP_PROXY as a fallback for HTTPS urls, but
// proxy-from-env will not, so let's just force it to fall back like this
process.env.HTTPS_PROXY = process.env.HTTP_PROXY
}

if (!process.env.NO_PROXY) {
// don't proxy localhost, to match Chrome's default behavior and user expectation
process.env.NO_PROXY = 'localhost'
}
},

loadSystemProxySettings () {
if (process.env.HTTP_PROXY !== undefined) {
this._normalizeEnvironmentProxy()

return
}

if (os.platform() === 'win32') {
const windowsProxy = this._getWindowsProxy()

if (windowsProxy) {
process.env.HTTP_PROXY = process.env.HTTPS_PROXY = windowsProxy.httpProxy
process.env.NO_PROXY = process.env.NO_PROXY || windowsProxy.noProxy
}

this._normalizeEnvironmentProxy()

return 'win32'
}
},
}
Loading

0 comments on commit 818dfdf

Please sign in to comment.