openwhisk-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jamestho...@apache.org
Subject [incubator-openwhisk-client-js] branch master updated: Openwhisk proxy (#147)
Date Fri, 22 Feb 2019 10:21:13 GMT
This is an automated email from the ASF dual-hosted git repository.

jamesthomas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-client-js.git


The following commit(s) were added to refs/heads/master by this push:
     new c2de73b  Openwhisk proxy (#147)
c2de73b is described below

commit c2de73b69db3c95fa874c009c5b9b92302eecbd8
Author: karanrk <karankamatgi@gmail.com>
AuthorDate: Fri Feb 22 02:21:09 2019 -0800

    Openwhisk proxy (#147)
    
    * implement proxy for needle to pass-thru openwhisk sdk
    * Added unit test to verify the proxy options using proxy-agent
---
 README.md                | 10 ++++++++++
 lib/client.js            | 14 +++++++++++++-
 package.json             |  3 ++-
 test/unit/client.test.js | 17 +++++++++++++++++
 4 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md
index c0cdf52..29f8c5d 100644
--- a/README.md
+++ b/README.md
@@ -131,6 +131,16 @@ in your options structure, e.g.
 ow.actions.invoke({ noUserAgent: true, name, params })
 ```
 
+### Working with a Proxy
+
+ If you are working behind a firewall, you could use the following environment variables
to proxy your HTTP/HTTPS requests
+
+ - *http_proxy/HTTP_PROXY*
+- *https_proxy/HTTPS_proxy*
+
+ The openwhisk-client-js SDK supports the use of above mentioned proxies through third-party

+ HTTP agent such as [proxy-agent](https://github.com/TooTallNate/node-proxy-agent "proxy-agent
Github page")
+
 ## Examples
 
 ### invoke action, blocking for result
diff --git a/lib/client.js b/lib/client.js
index 488e363..ee797fc 100644
--- a/lib/client.js
+++ b/lib/client.js
@@ -8,7 +8,7 @@ const OpenWhiskError = require('./openwhisk_error')
 const needle = require('needle')
 const url = require('url')
 const http = require('http')
-
+const ProxyAgent = require('proxy-agent')
 /**
  * This implements a request-promise-like facade over the needle
  * library. There are two gaps between needle and rp that need to be
@@ -36,6 +36,18 @@ const rp = opts => {
   // this situation than needle
   opts.json = true
 
+  // gather proxy settings if behind a firewall
+  var proxyUri = process.env.proxy ||
+      process.env.HTTP_PROXY ||
+      process.env.http_proxy ||
+      process.env.HTTPS_PROXY ||
+      process.env.https_proxy
+
+  if (proxyUri !== undefined) {
+    // set the agent with corresponding proxy
+    opts.agent = new ProxyAgent(proxyUri)
+  }
+
   return needle(opts.method.toLowerCase(), // needle takes e.g. 'put' not 'PUT'
     opts.url,
     opts.body || opts.params,
diff --git a/package.json b/package.json
index c382673..e7b596c 100644
--- a/package.json
+++ b/package.json
@@ -54,7 +54,8 @@
     "standard": "^11.0.1"
   },
   "dependencies": {
-    "needle": "^2.1.0"
+    "needle": "^2.1.0",
+    "proxy-agent": "^3.0.3"
   },
   "babel": {
     "presets": [
diff --git a/test/unit/client.test.js b/test/unit/client.test.js
index c79cfb5..00ba2f0 100644
--- a/test/unit/client.test.js
+++ b/test/unit/client.test.js
@@ -6,6 +6,7 @@
 const test = require('ava')
 const Client = require('../../lib/client')
 const http = require('http')
+const ProxyAgent = require('proxy-agent')
 
 test('should use default constructor options', t => {
   const client = new Client({api_key: 'aaa', apihost: 'my_host'})
@@ -137,6 +138,22 @@ test('should return request parameters with merged options', async t
=> {
   t.deepEqual(params.b, {bar: 'foo'})
 })
 
+test('should be able to use proxy options leveraging the proxy agent.', async t => {
+  process.env['proxy'] = 'http://some_proxy'
+  const client = new Client({api_key: 'username:password', apihost: 'blah'})
+  const METHOD = 'get'
+  const PATH = 'some/path/to/resource'
+  const OPTIONS = {agent: new ProxyAgent(process.env['proxy'])}
+
+  const params = await client.params(METHOD, PATH, OPTIONS)
+  t.is(params.method, METHOD)
+  t.true(params.json)
+  t.true(params.rejectUnauthorized)
+  t.true(params.headers.hasOwnProperty('Authorization'))
+  t.deepEqual(params.agent.proxyUri, 'http://some_proxy')
+  delete process.env['proxy']
+})
+
 test('should return request parameters with explicit api option', async t => {
   const client = new Client({api_key: 'username:password', api: 'https://api.com/api/v1'})
   const METHOD = 'get'


Mime
View raw message