npm package: @mimik/address-helper
Description: IP Address helper for mimik microservices
Author: mimik technology inc
Latest version: 2.0.9

<a name="module_address-helper"></a>

## address-helper
**Example**  
```js
import addressHelper from '@mimik/address-helper';
// or
import { getAddressByType, hrefId, addressTypeMaker, addressFormat, addressFromNode, getAddress, updateAddresses, publicIpFromRequest, publicIpFromNode, } from '@mimik/address-helper';
```

* [address-helper](#module_address-helper)
    * [~getAddressByType(type, addresses)](#module_address-helper..getAddressByType) ⇒ <code>object</code>
    * [~hrefId(serverSettings, basePath, resource, id)](#module_address-helper..hrefId) ⇒ <code>URL.&lt;string&gt;</code>
    * [~addressTypeMaker(type, href, routingPort, hostname)](#module_address-helper..addressTypeMaker) ⇒ <code>object</code>
    * [~addressFormat(type, protocol, ip, port, pathname, routingPort)](#module_address-helper..addressFormat) ⇒ <code>object</code>
    * [~addressFromNode(type, ip, node, routingPort)](#module_address-helper..addressFromNode) ⇒ <code>object</code>
    * [~getAddress(node, localLinkNetworkId)](#module_address-helper..getAddress) ⇒ <code>object</code>
    * [~updateAddresses(type, newAddress, addresses)](#module_address-helper..updateAddresses) ⇒ <code>Array.&lt;object&gt;</code>
    * [~publicIpFromNode(node)](#module_address-helper..publicIpFromNode) ⇒ <code>IP.&lt;string&gt;</code>
    * [~publicIpFromRequest(req)](#module_address-helper..publicIpFromRequest) ⇒ <code>IP.&lt;string&gt;</code>

<a name="module_address-helper..getAddressByType"></a>

### address-helper~getAddressByType(type, addresses) ⇒ <code>object</code>
Get the address by type from an array of addresses.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Returns**: <code>object</code> - Address by type.  
**Category**: sync  
**Warning**: This function mutates the matched address object if `hostname` is missing, deriving it from `href`.  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | Type of address to return. |
| addresses | <code>Array.&lt;object&gt;</code> | Addresses from which the specific address is retrieved. |

<a name="module_address-helper..hrefId"></a>

### address-helper~hrefId(serverSettings, basePath, resource, id) ⇒ <code>URL.&lt;string&gt;</code>
Generate a href based on the given parameters.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Returns**: <code>URL.&lt;string&gt;</code> - Generated href.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| serverSettings | <code>object</code> | configuration to use to generate the href. |
| basePath | <code>PATH.&lt;string&gt;</code> | Base path to use to generate the href. |
| resource | <code>PATH.&lt;string&gt;</code> | Resource to use to generate the href. |
| id | <code>UUID.&lt;string&gt;</code> | id to use to generate the href. |

<a name="module_address-helper..addressTypeMaker"></a>

### address-helper~addressTypeMaker(type, href, routingPort, hostname) ⇒ <code>object</code>
Generate an address using the given parameters.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Returns**: <code>object</code> - Generated address.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | type to use to generate the address to return. |
| href | <code>URL.&lt;string&gt;</code> | URL to use to generate the address to return. |
| routingPort | <code>number</code> | Routing port to generate the address to return. |
| hostname | <code>string</code> | hostname to be used for the URL. |

<a name="module_address-helper..addressFormat"></a>

### address-helper~addressFormat(type, protocol, ip, port, pathname, routingPort) ⇒ <code>object</code>
Return an address using the given parameters.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Returns**: <code>object</code> - Generated address.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | type to use to generate the address to return. |
| protocol | <code>string</code> | protocol to use to generate the address to return. |
| ip | <code>IP.&lt;string&gt;</code> | IP to use to generate the address to return. |
| port | <code>number</code> | Port to use to generate the address to return. |
| pathname | <code>PATH.&lt;string&gt;</code> | Path to add to generate the address to return. |
| routingPort | <code>number</code> | Port to use to set the address to return. |

<a name="module_address-helper..addressFromNode"></a>

### address-helper~addressFromNode(type, ip, node, routingPort) ⇒ <code>object</code>
Return an address corresponding to the given parameters.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Summary**: If the type is `proxy`, an inversion using `routingPort` is done.  
**Returns**: <code>object</code> - Generated address corresponding to the given parameters.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | Type of address to return |
| ip | <code>IP.&lt;string&gt;</code> | IP address used to generate the address to return. |
| node | <code>object</code> | Node to use to generate the address to return. |
| routingPort | <code>number</code> | Routing port to use to generate the address to return. |

<a name="module_address-helper..getAddress"></a>

### address-helper~getAddress(node, localLinkNetworkId) ⇒ <code>object</code>
Return the address to be used to reach the node.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Summary**: If `localLinkNetworkId` is present and if the node's ID matches the given one, return the local address.
Otherwise return the proxy address, if it exists, or the public address.  
**Returns**: <code>object</code> - Address to use to reach the node.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| node | <code>object</code> | Node from which to get the address. |
| localLinkNetworkId | <code>string</code> | Id of the link local network to get the address from. |

<a name="module_address-helper..updateAddresses"></a>

### address-helper~updateAddresses(type, newAddress, addresses) ⇒ <code>Array.&lt;object&gt;</code>
Add a new address of a specific type in the array of addresses.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Summary**: If `newAddress` is falsy, the existing address of the given type is removed.  
**Returns**: <code>Array.&lt;object&gt;</code> - Updated array of addresses.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| type | <code>string</code> | Type of address to add. |
| newAddress | <code>object</code> | New address to add to the array of addresses. |
| addresses | <code>Array.&lt;object&gt;</code> | Initial array of addresses to which the new address will be added. |

<a name="module_address-helper..publicIpFromNode"></a>

### address-helper~publicIpFromNode(node) ⇒ <code>IP.&lt;string&gt;</code>
Get the public IP address from the registering node.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Summary**: If node is not defined or if there's no public address return `null`.  
**Returns**: <code>IP.&lt;string&gt;</code> - Public IP address of the node.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| node | <code>object</code> | The node containing the public IP address. |

<a name="module_address-helper..publicIpFromRequest"></a>

### address-helper~publicIpFromRequest(req) ⇒ <code>IP.&lt;string&gt;</code>
Get the public IP address from the http request.

**Kind**: inner method of [<code>address-helper</code>](#module_address-helper)  
**Returns**: <code>IP.&lt;string&gt;</code> - Public IP address of the component making the request.

| Env variable name | Description | Default | Comments |
| ----------------- | ----------- | ------- | -------- |
| IPV4 | when set and non-null, will translate an IPV6 address into IPV4 address when possible | | `::1` will be translated to `127.0.0.1`

If the environment variable IPV4 is set to true, the ip address will be translated to IPV4 address when possible, and `::1` will be translated to `127.0.0.1`.  
**Category**: sync  

| Param | Type | Description |
| --- | --- | --- |
| req | <code>object</code> | The http request to get the public IP address. |