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

enable proxy if https_proxy taken by env #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 20 additions & 1 deletion lib/wit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
const fetch = require('node-fetch');
const readline = require('readline');
const uuid = require('node-uuid');
const Url = require('url');
const Logger = require('./logger').Logger;
const logLevels = require('./logger').logLevels;
const HttpsProxyAgent = require('https-proxy-agent');

const DEFAULT_API_VERSION = '20160516';
const DEFAULT_MAX_STEPS = 5;
Expand Down Expand Up @@ -59,7 +61,7 @@ const validateActions = (actions) => {
if (!actions.error) {
throw new Error('The \'error\' action is missing. ' + learnMore);
}

Object.keys(actions).forEach(key => {
if (typeof actions[key] !== 'function') {
l.warn('The \'' + key + '\' action should be a function.');
Expand Down Expand Up @@ -109,9 +111,24 @@ const clone = (obj) => {
}
};

const getProxyAgent = (wit_url) => {
const url = Url.parse(wit_url);
const proxy = url.protocol === "http:" ?
process.env.http_proxy || process.env.HTTP_PROXY :
process.env.https_proxy || process.env.HTTPS_PROXY;
const noProxy = process.env.no_proxy || process.env.NO_PROXY;

const shouldIgnore = noProxy && noProxy.indexOf(url.hostname) > -1;
if (proxy && !shouldIgnore) {
return new HttpsProxyAgent(proxy);
}
if(!proxy) return null;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe we can rewrite like so ->

const shouldIgnore = noProxy && noProxy.indexOf(url.hostname > -1

if (proxy && !shouldIgnore) {
  return new HttpsProxyAgent(proxy)
}

};

const Wit = function(token, actions, logger) {
const baseURL = process.env.WIT_URL || 'https://api.wit.ai';
const version = process.env.WIT_API_VERSION || DEFAULT_API_VERSION;
const proxyAgent = getProxyAgent(baseURL);
const headers = {
'Authorization': 'Bearer ' + token,
'Accept': 'application/vnd.wit.' + version + '+json',
Expand All @@ -135,6 +152,7 @@ const Wit = function(token, actions, logger) {
fetch(baseURL + '/message?' + qs, {
method: 'GET',
headers: headers,
agent: proxyAgent
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
Expand All @@ -152,6 +170,7 @@ const Wit = function(token, actions, logger) {
method: 'POST',
headers: headers,
body: JSON.stringify(context),
agent: proxyAgent
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"repository": "https://github.com/wit-ai/node-wit",
"author": "Julien Odent <[email protected]>",
"dependencies": {
"https-proxy-agent": "^1.0.0",
"node-fetch": "^1.5.1",
"node-uuid": "^1.4.7"
}
Expand Down