Skip to content

Commit

Permalink
fix: define protocol and defaultPort
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Apr 26, 2019
1 parent 398976b commit 0c83983
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/classes/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const log = Logger.child({
});

class Agent {
defaultPort: number;

protocol: ProtocolType;

fallbackAgent: AgentType;
Expand All @@ -22,8 +24,7 @@ class Agent {

getUrlProxy: GetUrlProxyMethodType;

constructor (protocol: ProtocolType, mustUrlUseProxy: MustUrlUseProxyMethodType, getUrlProxy: GetUrlProxyMethodType, fallbackAgent: AgentType) {
this.protocol = protocol;
constructor (mustUrlUseProxy: MustUrlUseProxyMethodType, getUrlProxy: GetUrlProxyMethodType, fallbackAgent: AgentType) {
this.fallbackAgent = fallbackAgent;
this.mustUrlUseProxy = mustUrlUseProxy;
this.getUrlProxy = getUrlProxy;
Expand All @@ -38,11 +39,13 @@ class Agent {
});

if (this.mustUrlUseProxy(requestUrl)) {
request.path = requestUrl;
if (this.protocol === 'http:') {
request.path = requestUrl;
}

const proxy = this.getUrlProxy(requestUrl);

log.trace('proxying request to %s use %s proxy', requestUrl, 'http://' + proxy.hostname + ':' + proxy.port);
log.trace('proxying request to %s using %s proxy', requestUrl, 'http://' + proxy.hostname + ':' + proxy.port);

request.shouldKeepAlive = false;

Expand Down
9 changes: 9 additions & 0 deletions src/classes/HttpProxyAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ import type {
import Agent from './Agent';

class HttpProxyAgent extends Agent {
// @see https://github.com/sindresorhus/eslint-plugin-unicorn/issues/169#issuecomment-486980290
// eslint-disable-next-line unicorn/prevent-abbreviations
constructor (...args: *) {
super(...args);

this.protocol = 'http:';
this.defaultPort = 80;
}

createConnection (configuration: ConnectionConfigurationType, callback: ConnectionCallbackType) {
const socket = net.connect(
configuration.proxy.port,
Expand Down
8 changes: 8 additions & 0 deletions src/classes/HttpsProxyAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ import type {
import Agent from './Agent';

class HttpsProxyAgent extends Agent {
// eslint-disable-next-line unicorn/prevent-abbreviations
constructor (...args: *) {
super(...args);

this.protocol = 'https:';
this.defaultPort = 443;
}

createConnection (configuration: ConnectionConfigurationType, callback: ConnectionCallbackType) {
const socket = net.connect(
configuration.proxy.port,
Expand Down
4 changes: 1 addition & 3 deletions src/routines/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,15 @@ export default () => {
// @see https://github.com/facebook/flow/issues/7670
// $FlowFixMe
http.globalAgent = new HttpProxyAgent(
'http:',
mustUrlUseProxy,
getUrlProxy,
http.globalAgent
);

// $FlowFixMe
https.globalAgent = new HttpsProxyAgent(
'https:',
mustUrlUseProxy,
getUrlProxy,
http.globalAgent
https.globalAgent
);
};

0 comments on commit 0c83983

Please sign in to comment.