@mimik/address-helper
address-helper
Example
import addressHelper from '@mimik/address-helper';
// or
import { getAddressByType, hrefId, addressTypeMaker, addressFormat, addressFromNode, getAddress, updateAddresses, publicIpFromRequest, publicIpFromNode } from '@mimik/address-helper';
- address-helper
- ~getAddressByType(type, addresses) ⇒
object - ~hrefId(serverSettings, basePath, resource, id) ⇒
URL.<string> - ~addressTypeMaker(type, href, routingPort, hostname) ⇒
object - ~addressFormat(type, protocol, ip, port, pathname, routingPort) ⇒
object - ~addressFromNode(type, ip, node, routingPort) ⇒
object - ~getAddress(node, localLinkNetworkId) ⇒
object - ~updateAddresses(type, newAddress, addresses) ⇒
Array.<object> - ~publicIpFromNode(node) ⇒
IP.<string> - ~publicIpFromRequest(req) ⇒
IP.<string>
- ~getAddressByType(type, addresses) ⇒
address-helper~getAddressByType(type, addresses) ⇒ object
Get the address by type from an array of addresses.
Kind: inner method of address-helper
Returns: object - 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 | string |
Type of address to return. |
| addresses | Array.<object> |
Addresses from which the specific address is retrieved. |
address-helper~hrefId(serverSettings, basePath, resource, id) ⇒ URL.<string>
Generate a href based on the given parameters.
Kind: inner method of address-helper
Returns: URL.<string> - Generated href.
Category: sync
| Param | Type | Description |
|---|---|---|
| serverSettings | object |
Configuration to use to generate the href. Requires public.protocol; optionally public.domainName. When public.domainName is absent, ip.public (or ip.local) and port are required to build the host. |
| basePath | PATH.<string> |
Base path to use to generate the href. |
| resource | PATH.<string> |
Resource to use to generate the href. |
| id | UUID.<string> |
Id to use to generate the href. |
address-helper~addressTypeMaker(type, href, routingPort, hostname) ⇒ object
Generate an address using the given parameters.
Kind: inner method of address-helper
Returns: object - Generated address.
Category: sync
| Param | Type | Description |
|---|---|---|
| type | string |
Type to use to generate the address to return. |
| href | URL.<string> |
URL to use to generate the address to return. |
| routingPort | string |
Routing port to generate the address to return. |
| hostname | string |
Hostname to be used for the URL. |
address-helper~addressFormat(type, protocol, ip, port, pathname, routingPort) ⇒ object
Return an address using the given parameters.
Kind: inner method of address-helper
Returns: object - Generated address.
Category: sync
| Param | Type | Description |
|---|---|---|
| type | string |
Type to use to generate the address to return. |
| protocol | string |
Protocol to use to generate the address to return. |
| ip | IP.<string> |
IP to use to generate the address to return. A raw IPv6 literal is automatically bracketed (e.g. ::1 becomes [::1]). |
| port | string |
Port to use to generate the address to return. |
| pathname | PATH.<string> |
Path to add to generate the address to return. |
| routingPort | string |
Port to use to set the address to return. |
address-helper~addressFromNode(type, ip, node, routingPort) ⇒ object
Return an address corresponding to the given parameters.
Kind: inner method of address-helper
Summary: If the type is proxy, an inversion using routingPort is done.
Returns: object - Generated address corresponding to the given parameters.
Category: sync
| Param | Type | Description |
|---|---|---|
| type | string |
Type of address to return. |
| ip | IP.<string> |
IP address used to generate the address to return. |
| node | object |
Node to use to generate the address to return. |
| routingPort | string |
Routing port to use to generate the address to return. |
address-helper~getAddress(node, localLinkNetworkId) ⇒ object
Return the address to be used to reach the node.
Kind: inner method of 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: object - Address to use to reach the node.
Category: sync
| Param | Type | Description |
|---|---|---|
| node | object |
Node from which to get the address. |
| localLinkNetworkId | string |
Id of the link local network to get the address from. |
address-helper~updateAddresses(type, newAddress, addresses) ⇒ Array.<object>
Add a new address of a specific type in the array of addresses.
Kind: inner method of address-helper
Summary: If newAddress is falsy, the existing address of the given type is removed.
Returns: Array.<object> - Updated array of addresses.
Category: sync
| Param | Type | Description |
|---|---|---|
| type | string |
Type of address to add. |
| newAddress | object |
New address to add to the array of addresses. |
| addresses | Array.<object> |
Initial array of addresses to which the new address will be added. |
address-helper~publicIpFromNode(node) ⇒ IP.<string>
Get the public IP address from the registering node.
Kind: inner method of address-helper
Summary: If node is not defined or if there's no public address return null.
Returns: IP.<string> - Public IP address of the node.
Category: sync
| Param | Type | Description |
|---|---|---|
| node | object |
The node containing the public IP address. |
address-helper~publicIpFromRequest(req) ⇒ IP.<string>
Get the public IP address from the http request.
Kind: inner method of address-helper
Summary: Uses the leftmost x-forwarded-for entry when present. That value is client-supplied and can be spoofed
unless a trusted edge proxy sanitizes the header, so it should not be relied upon for rate limiting or allowlisting.
Returns: IP.<string> - 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 any non-null value), 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 | object |
The http request to get the public IP address. |