From 1fa52249178dc83e6d24c0789fd50c6e4a99dc82 Mon Sep 17 00:00:00 2001 From: muddydixon Date: Fri, 20 May 2016 20:35:50 +0900 Subject: [PATCH 1/3] added proxy if https_proxy taken by env --- lib/wit.js | 3 +++ package.json | 1 + 2 files changed, 4 insertions(+) diff --git a/lib/wit.js b/lib/wit.js index 1c1bc3b..a777bdf 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -5,6 +5,7 @@ const readline = require('readline'); const uuid = require('node-uuid'); 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; @@ -135,6 +136,7 @@ const Wit = function(token, actions, logger) { fetch(baseURL + '/message?' + qs, { method: 'GET', headers: headers, + agent: process.env.https_proxy ? new HttpsProxyAgent(process.env.https_proxy) : undefined }) .then(response => Promise.all([response.json(), response.status])) .then(handler) @@ -152,6 +154,7 @@ const Wit = function(token, actions, logger) { method: 'POST', headers: headers, body: JSON.stringify(context), + agent: process.env.https_proxy ? new HttpsProxyAgent(process.env.https_proxy) : undefined }) .then(response => Promise.all([response.json(), response.status])) .then(handler) diff --git a/package.json b/package.json index d5bbf08..2c70be0 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "repository": "https://github.com/wit-ai/node-wit", "author": "Julien Odent ", "dependencies": { + "https-proxy-agent": "^1.0.0", "node-fetch": "^1.5.1", "node-uuid": "^1.4.7" } From df3e1c728e20af2b584872df7699e0d5ee66ea12 Mon Sep 17 00:00:00 2001 From: muddydixon Date: Tue, 14 Jun 2016 16:52:37 +0900 Subject: [PATCH 2/3] supported http/s proxy * ignore cases of http/s_proxy * not apply if host include no_proxy --- lib/wit.js | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/wit.js b/lib/wit.js index a777bdf..b2207ee 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -3,6 +3,7 @@ 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'); @@ -60,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.'); @@ -110,9 +111,21 @@ 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 || null : + process.env.https_proxy || process.env.HTTPS_PROXY || null; + const noProxy = process.env.no_proxy || process.env.NO_PROXY; + if(!proxy) return null; + if(noProxy.indexOf(url.hostname) > -1) return null; + 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', @@ -136,7 +149,7 @@ const Wit = function(token, actions, logger) { fetch(baseURL + '/message?' + qs, { method: 'GET', headers: headers, - agent: process.env.https_proxy ? new HttpsProxyAgent(process.env.https_proxy) : undefined + agent: proxyAgent }) .then(response => Promise.all([response.json(), response.status])) .then(handler) @@ -154,7 +167,7 @@ const Wit = function(token, actions, logger) { method: 'POST', headers: headers, body: JSON.stringify(context), - agent: process.env.https_proxy ? new HttpsProxyAgent(process.env.https_proxy) : undefined + agent: proxyAgent }) .then(response => Promise.all([response.json(), response.status])) .then(handler) From 99be0b5552a6de090619f16684a8f806fb9003e2 Mon Sep 17 00:00:00 2001 From: muddydixon Date: Fri, 18 Nov 2016 11:01:32 +0900 Subject: [PATCH 3/3] fix: according to comment --- lib/wit.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/wit.js b/lib/wit.js index b2207ee..eafd327 100644 --- a/lib/wit.js +++ b/lib/wit.js @@ -114,12 +114,15 @@ 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 || null : - process.env.https_proxy || process.env.HTTPS_PROXY || null; + 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; - if(noProxy.indexOf(url.hostname) > -1) return null; - return new HttpsProxyAgent(proxy); }; const Wit = function(token, actions, logger) {