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

Fix issue #3: HTTPS proxy support through custom agent #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions lib/ovh.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var https = require('https'),
this.timeout = params.timeout;
this.apiTimeDiff = params.apiTimeDiff || null;
this.endpoint = params.endpoint || null;
this.httpsAgent = params.httpsAgent || null;

// Preconfigured API endpoints
if (this.endpoint) {
Expand Down Expand Up @@ -103,6 +104,10 @@ var https = require('https'),
path: this.basePath + path
};

if (this.httpsAgent) {
request.agent = this.httpsAgent;
}

// Fetch only selected APIs
if (path === '/') {
return async.each(
Expand Down Expand Up @@ -519,6 +524,10 @@ var https = require('https'),
'X-Ovh-Application': this.appKey,
};

if (this.httpsAgent) {
options.agent = this.httpsAgent;
}

// Remove undefined values
for (var k in params) {
if (params.hasOwnProperty(k) && typeof(params[k]) === 'undefined') {
Expand Down Expand Up @@ -654,6 +663,17 @@ var https = require('https'),
return '$1$' + crypto.createHash('sha1').update(s.join('+')).digest('hex');
};

/**
* Change the HTTPS agent used for reaching the OVH servers
*
* @param {Object} httpsAgent: the custom HTTPS agent. Pass undefined to unset a previously defined agent.
*/
Ovh.prototype.setHTTPSAgent = function (httpsAgent) {
if (typeof(httpsAgent) === 'undefined' || typeof(httpsAgent) === 'object') {
this.httpsAgent = httpsAgent;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do you unset an existing agent ? This said, I can't find a use-case where a setter is needed at all. When would one want to switch proxies at runtime ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • the typeof(httpsAgent) === 'undefined' part of the setter was meant to allow unsetting an existing agent. I've indicated it in the documentation. Perhaps there's a better way ?
  • I used a setter in my application, but I could easily do without it, indeed, by injecting the agent inside the config before passing it to the constructor of the ovh module.

};

module.exports = function (params) {
return new Ovh(params || {});
};
Expand Down