CorsProxy

CorsProxy

new CorsProxy(overrideLoadJson)

Source:

Rewrites URLs so that they're resolved via the TerriaJS-Server proxy rather than going direct. This is most useful for getting around CORS restrictions on services that don't have CORS set up or when using pre-CORS browsers like IE9. Going via the proxy is also useful if you want to change the caching headers on map requests (for instance many map tile providers set cache headers to no-cache even for maps that rarely change, resulting in a much slower experience particularly on time-series data).

Parameters:
Name Type Description
overrideLoadJson

A method for getting JSON from a URL that matches the signature of Core/loadJson module - this is overridable mainly for testing.

Members

alwaysUseProxy :boolean

Source:

Whether the proxy should be used regardless of whether the domain supports CORS or not. This defaults to true on IE<10.

Type:
  • boolean

baseProxyUrl :String

Source:

The base URL of the TerriaJS server proxy, to which requests will be appended. In most cases this is the server's host + '/proxy'.

Type:
  • String

corsDomains :Array.<String>

Source:

Domains that are known to support CORS, as set by config files.

Type:
  • Array.<String>

isOpenProxy :boolean

Source:

True if we expect that the proxy will proxy any URL - note that if the server isn't set up to do this, having this set to true will just result in a lot of failed AJAX calls

Type:
  • boolean

pageIsHttps :boolean

Source:

Whether the page that Terria is running on is HTTPS. This is relevant because calling an HTTP domain from HTTPS results in mixed content warnings and going through the proxy is required to get around this.

Type:
  • boolean

proxyDomains :Array.<String>

Source:

Domains that should be proxied for, as set by config files. Stored as an array of hosts - if a TLD is specified, subdomains will also be proxied.

Type:
  • Array.<String>

Methods

getURL(resource, proxyFlag) → {String}

Source:

Proxies a URL by appending it to CorsProxy#baseProxyUrl. Optionally inserts a proxyFlag that will override the cache headers of the response, allowing for caching to be added where it wouldn't otherwise.

Parameters:
Name Type Description
resource String

the URL to potentially proxy

proxyFlag String

the proxy flag to pass - generally this is the length of time that you want to override the cache headers with. E.g. '2d' for 2 days.

Returns:

The proxied URL

Type
String

getURLProxyIfNecessary(resource, proxyFlag) → {String}

Source:

Convenience method that combines CorsProxy#shouldUseProxy and getURL - if the URL passed needs to be proxied according to the rules/config of the proxy, this will return a proxied URL, otherwise it will return the original URL.

{@see CorsProxy#shouldUseProxy} {@see CorsProxy#getURL}

Parameters:
Name Type Description
resource String

the URL to potentially proxy

proxyFlag String

the proxy flag to pass - generally this is the length of time that you want to override the cache headers with. E.g. '2d' for 2 days.

Returns:

Either the URL passed in or a proxied URL if it should be proxied.

Type
String

init(serverConfig, baseProxyUrl, proxyDomains) → {Promise}

Source:

Initialises values with config previously loaded from server. This is the recommended way to use this object as it ensures the options will be correct for the proxy server it's configured to call, but this can be skipped and the values it initialises set manually if desired.

Parameters:
Name Type Description
serverConfig Object

Configuration options retrieved from a ServerConfig object.

baseProxyUrl String

The base URL to proxy with - this will default to 'proxy/'

proxyDomains Array.<String>

Initial value for proxyDomains to which proxyable domains from the server will be appended - defaults to an empty array.

Returns:

A promise that resolves when initialisation is complete.

Type
Promise

shouldUseProxy(url) → {Boolean}

Source:

Determines if the proxying service should be used to access the given URL, based on our list of domains we're willing to proxy for and hosts that are known to support CORS.

Parameters:
Name Type Description
url String

The url to examine.

Returns:

true if the proxy should be used, false if not.

Type
Boolean