Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

request: add proxy support #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
const assert = require('bsert');
const {Stream} = require('stream');
const mime = require('./mime');
const tryRequire = require('try-require');
const ProxyAgent = tryRequire('proxy-agent');

/*
* Lazily Loaded
Expand Down Expand Up @@ -50,6 +52,7 @@ class RequestOptions {
this.timeout = 5000;
this.buffer = buffer || false;
this.headers = Object.create(null);
this.proxy = process.env.socks_proxy || process.env.all_proxy;

// Hack
ensureRequires();
Expand Down Expand Up @@ -185,6 +188,10 @@ class RequestOptions {
this.lookup = options.lookup;
}

if (options.proxy != null) {
assert(typeof options.proxy === 'string');
}

return this;
}

Expand Down Expand Up @@ -270,6 +277,11 @@ class RequestOptions {
return this.ssl ? https : http;
}

getProxyAgent() {
if (typeof ProxyAgent !== undefined)
return new ProxyAgent(this.proxy);
}

getHeaders() {
const headers = Object.create(null);

Expand Down Expand Up @@ -330,7 +342,7 @@ class RequestOptions {
port: this.port,
path: this.path + query,
headers: this.getHeaders(),
agent: this.pool ? null : false,
agent: this.pool ? null : this.proxy ? this.getProxyAgent() : false,
lookup: this.lookup || undefined,
rejectUnauthorized: this.strictSSL
};
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"test": "bmocha --reporter spec test/*-test.js"
},
"dependencies": {
"bsert": "~0.0.10"
"bsert": "~0.0.10",
"try-require": "^1.2.1"
},
"devDependencies": {
"bmocha": "^2.1.0"
Expand All @@ -29,5 +30,8 @@
},
"browser": {
"./lib/request": "./lib/request-browser.js"
},
"optionalDependencies": {
"proxy-agent": "^3.1.1"
}
}