Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
b908cfd
fix: modify export for the package
shazron Aug 12, 2021
38e10a1
feat: add NtlmFetch class
shazron Aug 12, 2021
8a73a78
tests: add http-proxy-server, api-server, ldap-server
shazron Aug 20, 2021
fa0f208
update tests
shazron Aug 20, 2021
18bed5c
fix proxy server cleanup
shazron Aug 20, 2021
516ec2d
better basic auth
shazron Aug 20, 2021
1ca3507
updates to exports
shazron Aug 30, 2021
e79bd1d
README update
shazron Aug 30, 2021
17c70f3
removed ntlm proxy impl and tests to ntlm-proxy branch
shazron Aug 30, 2021
035f295
100% code coverage, updated README
shazron Aug 30, 2021
e3e0d8c
fix port in use error
shazron Aug 30, 2021
8e025da
jest --runInBand
shazron Aug 30, 2021
d9a8369
fix unnecessary initErrors
shazron Sep 6, 2021
a7674d9
re-factor: move createFetch, getProxyOptionsFromConfig to utils
shazron Sep 6, 2021
384b3cc
fix: add proxy support for exponentialBackoff
shazron Sep 6, 2021
aaa79a6
added tests for Request, Response, Headers classes
shazron Sep 6, 2021
e1b9beb
Merge branch 'master' into proxy-support
shazron Sep 13, 2021
d8aafc9
added test https proxy server
shazron Sep 15, 2021
0adfed5
Merge branch 'proxy-support' of https://github.com/adobe/aio-lib-core…
shazron Sep 15, 2021
e949789
update tests for http api server
shazron Sep 17, 2021
d8e3bcc
update proxy test
shazron Sep 17, 2021
0510361
fix: add post test to api-server
shazron Sep 20, 2021
ed4e74f
use require('fs').promises for older node versions
shazron Sep 20, 2021
e0b84ac
remove test servers: use @adobe/aio-lib-test-proxy (main branch)
shazron Sep 21, 2021
111f77b
fix: pin node-fetch to 2.6.2 (bug)
shazron Sep 21, 2021
6b27724
set longer jest timeout for https proxy HttpExponentialBackoff tests
shazron Sep 21, 2021
c210067
try to use real timers
shazron Sep 21, 2021
fc94f3b
fetchRetry, ignore self-signed cert for test https proxy
shazron Sep 21, 2021
730c275
fix: update node-fetch to ^2.6.4 (bug fix release)
shazron Sep 22, 2021
5e77f19
fix: update @adobe/aio-lib-test-proxy to 1.0.0
shazron Sep 22, 2021
ba92efa
fix: update proxy use via HTTP_PROXY/HTTPS_PROXY environment variables
shazron Sep 28, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ launch.json
package-lock.json
/node_modules/
junit.xml
test-report.html
169 changes: 148 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2019 Adobe. All rights reserved.
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -10,12 +10,20 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
-->

<!--
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DO NOT update README.md, it is generated.
Modify 'docs/readme_template.md', then run `npm run generate-docs`.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-->

[![Version](https://img.shields.io/npm/v/@adobe/aio-lib-core-networking.svg)](https://npmjs.org/package/@adobe/aio-lib-core-networking)
[![Downloads/week](https://img.shields.io/npm/dw/@adobe/aio-lib-core-networking.svg)](https://npmjs.org/package/@adobe/aio-lib-core-networking)
[![Node.js CI](https://github.com/adobe/aio-lib-core-networking/actions/workflows/node.js.yml/badge.svg)](https://github.com/adobe/aio-lib-core-networking/actions/workflows/node.js.yml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Codecov Coverage](https://img.shields.io/codecov/c/github/adobe/aio-lib-core-networking/master.svg?style=flat-square)](https://codecov.io/gh/adobe/aio-lib-core-networking/)


# Adobe I/O Core Networking Lib

### Installing
Expand All @@ -28,15 +36,17 @@ $ npm install @adobe/aio-lib-core-networking
1) Initialize the SDK

```javascript
const fetchRetry = require('@adobe/aio-lib-core-networking')

const { HttpExponentialBackoff, createFetch } = require('@adobe/aio-lib-core-networking')
const fetchRetry = new HttpExponentialBackoff()
const proxyFetch = createFetch()
```

2) Call methods using the initialized SDK

```javascript

const fetchRetry = require('@adobe/aio-lib-core-networking')
const { HttpExponentialBackoff, createFetch } = require('@adobe/aio-lib-core-networking')
const fetchRetry = new HttpExponentialBackoff()
async function sdkTest() {

return new Promise((resolve, reject) => {
Expand All @@ -54,44 +64,161 @@ async function sdkTest() {
})
}

let proxyFetch
// this will get the proxy settings from the config, if available (proxy.url, proxy.username, proxy.password)
proxyFetch = createFetch()

// this will use the passed in proxy settings. only proxyUrl is required
proxyFetch = createFetch({ proxyUrl: 'http://my.proxy:8080', username: 'admin', password: 'secret' })

// if the proxy settings are not passed in, and not available in the config, it falls back to a simple fetch
const simpleFetch = createFetch()
```

<a name="module_@adobe/aio-lib-core-networking"></a>
## Modules

## @adobe/aio-lib-core-networking
<dl>
<dt><a href="#module_@adobe/aio-lib-core-networking">@adobe/aio-lib-core-networking</a></dt>
<dd></dd>
</dl>

* [@adobe/aio-lib-core-networking](#module_@adobe/aio-lib-core-networking)
* [~HttpExponentialBackoff](#module_@adobe/aio-lib-core-networking..HttpExponentialBackoff)
* [.exponentialBackoff(url, requestOptions, retryOptions, retryOn, retryDelay)](#module_@adobe/aio-lib-core-networking..HttpExponentialBackoff+exponentialBackoff) ⇒ <code>Promise.&lt;Response&gt;</code>
## Classes

<a name="module_@adobe/aio-lib-core-networking..HttpExponentialBackoff"></a>
<dl>
<dt><a href="#HttpExponentialBackoff">HttpExponentialBackoff</a></dt>
<dd><p>This class provides methods to implement fetch with retries.
The retries use exponential backoff strategy
with defaults set to max of 3 retries and initial Delay as 100ms</p>
</dd>
<dt><a href="#ProxyFetch">ProxyFetch</a></dt>
<dd><p>This provides a wrapper for fetch that facilitates proxy auth authorization.</p>
</dd>
</dl>

## Functions

<dl>
<dt><a href="#getProxyOptionsFromConfig">getProxyOptionsFromConfig()</a> ⇒ <code><a href="#ProxyAuthOptions">ProxyAuthOptions</a></code></dt>
<dd><p>Gets the proxy options from the config.</p>
</dd>
<dt><a href="#createFetch">createFetch([proxyOptions])</a> ⇒ <code>function</code></dt>
<dd><p>Return the appropriate Fetch function depending on proxy settings.</p>
</dd>
</dl>

## Typedefs

<dl>
<dt><a href="#ProxyAuthOptions">ProxyAuthOptions</a> : <code>object</code></dt>
<dd><p>Auth Options.</p>
</dd>
</dl>

### Working of HttpExponentialBackoff
<a name="module_@adobe/aio-lib-core-networking"></a>

![image not available](docs/sequenceDiagram.jpeg?s=50)
## @adobe/aio-lib-core-networking
<a name="HttpExponentialBackoff"></a>

### @adobe/aio-lib-core-networking~HttpExponentialBackoff
## HttpExponentialBackoff
This class provides methods to implement fetch with retries.
The retries use exponential backoff strategy
with defaults set to max of 3 retries and initial Delay as 100ms

**Kind**: inner class of [<code>@adobe/aio-lib-core-networking</code>](#module_@adobe/aio-lib-core-networking)
<a name="module_@adobe/aio-lib-core-networking..HttpExponentialBackoff+exponentialBackoff"></a>
**Kind**: global class
<a name="HttpExponentialBackoff+exponentialBackoff"></a>

#### httpExponentialBackoff.exponentialBackoff(url, requestOptions, retryOptions, retryOn, retryDelay) ⇒ <code>Promise.&lt;Response&gt;</code>
### httpExponentialBackoff.exponentialBackoff(url, requestOptions, retryOptions, [retryOn], [retryDelay]) ⇒ <code>Promise.&lt;Response&gt;</code>
This function will retry connecting to a url end-point, with
exponential backoff. Returns a Promise.

**Kind**: instance method of [<code>HttpExponentialBackoff</code>](#module_@adobe/aio-lib-core-networking..HttpExponentialBackoff)
**Kind**: instance method of [<code>HttpExponentialBackoff</code>](#HttpExponentialBackoff)
**Returns**: <code>Promise.&lt;Response&gt;</code> - Promise object representing the http response

| Param | Type | Description |
| --- | --- | --- |
| url | <code>string</code> | endpoint url |
| requestOptions | <code>object</code> | request options which includes the HTTP method, headers, timeout, etc. |
| retryOptions | <code>object</code> | retry options with options being maxRetries and initialDelayInMillis |
| retryOn | <code>function</code> \| <code>Array</code> | Optional Function or Array. If provided, will be used instead of the default |
| retryDelay | <code>function</code> \| <code>number</code> | Optional Function or number. If provided, will be used instead of the default |
| requestOptions | <code>object</code> | request options |
| retryOptions | <code>object</code> | retry options with keys being maxRetries and initialDelay in ms |
| [retryOn] | <code>function</code> \| <code>Array</code> | Optional Function or Array. If provided, will be used instead of the default |
| [retryDelay] | <code>function</code> \| <code>number</code> | Optional Function or number. If provided, will be used instead of the default |

<a name="ProxyFetch"></a>

## ProxyFetch
This provides a wrapper for fetch that facilitates proxy auth authorization.

**Kind**: global class

* [ProxyFetch](#ProxyFetch)
* [new ProxyFetch(authOptions)](#new_ProxyFetch_new)
* [.proxyAgent()](#ProxyFetch+proxyAgent) ⇒ <code>http.Agent</code>
* [.fetch(resource, options)](#ProxyFetch+fetch) ⇒ <code>Promise.&lt;Response&gt;</code>

<a name="new_ProxyFetch_new"></a>

### new ProxyFetch(authOptions)
Initialize this class with Proxy auth options


| Param | Type | Description |
| --- | --- | --- |
| authOptions | [<code>ProxyAuthOptions</code>](#ProxyAuthOptions) | the auth options to connect with |

<a name="ProxyFetch+proxyAgent"></a>

### proxyFetch.proxyAgent() ⇒ <code>http.Agent</code>
Returns the http.Agent used for this proxy

**Kind**: instance method of [<code>ProxyFetch</code>](#ProxyFetch)
**Returns**: <code>http.Agent</code> - a http.Agent for basic auth proxy
<a name="ProxyFetch+fetch"></a>

### proxyFetch.fetch(resource, options) ⇒ <code>Promise.&lt;Response&gt;</code>
Fetch function, using the configured NTLM Auth options.

**Kind**: instance method of [<code>ProxyFetch</code>](#ProxyFetch)
**Returns**: <code>Promise.&lt;Response&gt;</code> - Promise object representing the http response

| Param | Type | Description |
| --- | --- | --- |
| resource | <code>string</code> \| <code>Request</code> | the url or Request object to fetch from |
| options | <code>object</code> | the fetch options |

<a name="getProxyOptionsFromConfig"></a>

## getProxyOptionsFromConfig() ⇒ [<code>ProxyAuthOptions</code>](#ProxyAuthOptions)
Gets the proxy options from the config.

**Kind**: global function
**Returns**: [<code>ProxyAuthOptions</code>](#ProxyAuthOptions) - the proxy options
<a name="createFetch"></a>

## createFetch([proxyOptions]) ⇒ <code>function</code>
Return the appropriate Fetch function depending on proxy settings.

**Kind**: global function
**Returns**: <code>function</code> - the Fetch API function

| Param | Type | Description |
| --- | --- | --- |
| [proxyOptions] | [<code>ProxyAuthOptions</code>](#ProxyAuthOptions) | the options for the proxy |
| proxyOptions.proxyUrl | <code>string</code> | the url for the proxy |
| proxyOptions.username | <code>string</code> | the username for the proxy |
| proxyOptions.password | <code>string</code> | the password for the proxy |

<a name="ProxyAuthOptions"></a>

## ProxyAuthOptions : <code>object</code>
Auth Options.

**Kind**: global typedef
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| proxyUrl | <code>string</code> | the proxy's url |
| username | <code>string</code> | the username |
| password | <code>string</code> | the password |

### Debug Logs

Expand Down
71 changes: 44 additions & 27 deletions docs/readme_template.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
Copyright 2019 Adobe. All rights reserved.
Copyright 2021 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0
Expand All @@ -10,13 +10,21 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
-->

[![Version](https://img.shields.io/npm/v/@CNA/aio-lib-core-networking.svg)](https://npmjs.org/package/@CNA/aio-lib-core-networking)
[![Downloads/week](https://img.shields.io/npm/dw/@CNA/aio-lib-core-networking.svg)](https://npmjs.org/package/@CNA/aio-lib-core-networking)
[![Build Status](https://travis-ci.com/CNA/aio-lib-core-networking.svg?branch=master)](https://travis-ci.com/CNA/aio-lib-core-networking)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Greenkeeper badge](https://badges.greenkeeper.io/CNA/aio-lib-core-networking.svg)](https://greenkeeper.io/)
[![Codecov Coverage](https://img.shields.io/codecov/c/github/CNA/aio-lib-core-networking/master.svg?style=flat-square)](https://codecov.io/gh/CNA/aio-lib-core-networking/)
<!--
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
DO NOT update README.md, it is generated.
Modify 'docs/readme_template.md', then run `npm run generate-docs`.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-->

[![Version](https://img.shields.io/npm/v/@adobe/aio-lib-core-networking.svg)](https://npmjs.org/package/@adobe/aio-lib-core-networking)
[![Downloads/week](https://img.shields.io/npm/dw/@adobe/aio-lib-core-networking.svg)](https://npmjs.org/package/@adobe/aio-lib-core-networking)
[![Node.js CI](https://github.com/adobe/aio-lib-core-networking/actions/workflows/node.js.yml/badge.svg)](https://github.com/adobe/aio-lib-core-networking/actions/workflows/node.js.yml)
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
[![Codecov Coverage](https://img.shields.io/codecov/c/github/adobe/aio-lib-core-networking/master.svg?style=flat-square)](https://codecov.io/gh/adobe/aio-lib-core-networking/)

# Adobe I/O ??? Lib

# Adobe I/O Core Networking Lib

### Installing

Expand All @@ -28,39 +36,48 @@ $ npm install @adobe/aio-lib-core-networking
1) Initialize the SDK

```javascript
const sdk = require('@adobe/aio-lib-core-networking')

async function sdkTest() {
//initialize sdk
const client = await sdk.init('<tenant>', 'x-api-key', '<valid auth token>')
}
const { HttpExponentialBackoff, createFetch } = require('@adobe/aio-lib-core-networking')
const fetchRetry = new HttpExponentialBackoff()
const proxyFetch = createFetch()
```

2) Call methods using the initialized SDK

```javascript
const sdk = require('@adobe/aio-lib-core-networking')

const { HttpExponentialBackoff, createFetch } = require('@adobe/aio-lib-core-networking')
const fetchRetry = new HttpExponentialBackoff()
async function sdkTest() {
// initialize sdk
const client = await sdk.init('<tenant>', 'x-api-key', '<valid auth token>')

// call methods
try {
// get profiles by custom filters
const result = await client.getSomething({})
console.log(result)

} catch (e) {
console.error(e)
}

return new Promise((resolve, reject) => {
fetchRetry.exponentialBackoff(url, requestOptions, retryOptions, retryOn, retryDelay)
.then((response) => {
if (!response.ok) {
throw Error(reduceError(response))
}
resolve(response.json())
})
.catch(err => {
reject(
new codes.ERROR_GET_SOMETHING({ sdkDetails, messageValues: err }))
})
})
}

let proxyFetch
// this will get the proxy settings from the config, if available (proxy.url, proxy.username, proxy.password)
proxyFetch = createFetch()

// this will use the passed in proxy settings. only proxyUrl is required
proxyFetch = createFetch({ proxyUrl: 'http://my.proxy:8080', username: 'admin', password: 'secret' })

// if the proxy settings are not passed in, and not available in the config, it falls back to a simple fetch
const simpleFetch = createFetch()
```

{{>main-index~}}
{{>all-docs~}}


### Debug Logs

```bash
Expand Down
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
},
"bundleDependencies": false,
"dependencies": {
"node-fetch": "^2.6.0",
"@adobe/aio-lib-core-config": "^2.0.1",
"@adobe/aio-lib-core-errors": "^3.0.0",
"@adobe/aio-lib-core-logging": "1.1.0",
"fetch-retry": "^3.0.1"
"fetch-retry": "^3.0.1",
"node-fetch": "^2.6.0",
"proxy-agent": "^5.0.0"
},
"deprecated": false,
"description": "Adobe I/O Lib Core Networking",
"devDependencies": {
"@adobe/eslint-config-aio-lib-config": "^1.2.0",
"axios": "^0.21.1",
"babel-runtime": "^6.26.0",
"dotenv": "^8.1.0",
"eol": "^0.9.1",
Expand All @@ -24,16 +27,21 @@
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.0",
"express": "^4.17.1",
"express-basic-auth": "^1.2.0",
"fetch-mock": "^9.0.0",
"http-proxy": "^1.18.1",
"jest": "^25.1.0",
"jest-fetch-mock": "^3.0.1",
"jest-html-reporter": "^3.4.1",
"jest-junit": "^10.0.0",
"jest-plugin-fs": "^2.9.0",
"jsdoc": "^3.6.3",
"jsdoc-to-markdown": "^5.0.0",
"openapi-schema-validator": "^3.0.3",
"query-string": "^7.0.1",
"stdout-stderr": "^0.1.9",
"tsd-jsdoc": "^2.4.0",
"fetch-mock": "^9.0.0"
"tsd-jsdoc": "^2.4.0"
},
"homepage": "https://github.com/adobe/aio-lib-core-networking",
"license": "Apache-2.0",
Expand All @@ -47,7 +55,11 @@
"lint": "eslint src test",
"test": "npm run lint && npm run unit-tests",
"typings": "jsdoc -t node_modules/tsd-jsdoc/dist -r src/*.js -d .",
"unit-tests": "jest --config test/jest.config.js --maxWorkers=2"
"unit-tests": "jest --config test/jest.config.js --runInBand"
},
"version": "1.0.1"
"version": "1.0.1",
"engineStrict": true,
"engines": {
"node": ">=12.0.0"
}
}
Loading