/provider/getSrc

The getSrc module resolves src references through a provider determined from the src prefix, eg. file:, https:, cloudfront:, or a signer provider from a SIGN_* xyzEnv key.

All source responses are cached in a module scope source map regardless of their provider. Promises are stored in the map before provider requests are awaited so concurrent requests for the same src share one request. The source map must be cleared when the workspace cache is rebuilt.

Requires

Members

(inner, constant) srcMap

Module scope source promise map. The map must be cleared when the workspace cache is rebuilt.

Methods

(async, inner) Cloudfront(ref) → {Promise.<(String|JSON|Error)>}

The method will extract a cloudfront URL from the ref param string.

The fetch request will be created from the cloudfront provider module with the cloudfront url.

Parameters:
NameTypeDescription
refstring

Cloudfront resource reference.

Returns:

The fetch is resolved into either a string or JSON depending on the url ending.

Type: 
Promise.<(String|JSON|Error)>

(async, inner) File(ref) → {Promise.<(String|JSON|Error)>}

The method will extract a file path from the ref param string.

The fetch request will be created from the file provider module with the file path.

Parameters:
NameTypeDescription
refstring

File resource reference.

Returns:

The fetch is resolved into either a string or JSON depending on the url ending.

Type: 
Promise.<(String|JSON|Error)>

(async, inner) Https(ref) → {Promise.<(String|JSON|Error)>}

The method will extract a https URL from the ref param string.

The fetch request will be created with the https url.

Parameters:
NameTypeDescription
refstring

Https resource reference.

Returns:

The fetch is resolved into either a string or JSON depending on the url ending.

Type: 
Promise.<(String|JSON|Error)>

(async, inner) XYZ(ref) → {Promise.<(String|JSON|Error)>}

The method splits the reference string into a params object for the XYZ file signer.

Parameters:
NameTypeDescription
refstring

Reference for the XYZ signer.

Returns:

The fetch is resolved into either a string or JSON depending on the url ending.

Type: 
Promise.<(String|JSON|Error)>

(async, inner) cacheSources(workspace) → {Promise.<Array.<String>>}

Recursively discovers src properties in a workspace and in fetched responses. Every source promise in a breadth is inserted into the source map before responses are inspected, so duplicate and concurrent references share one request.

All sources are fetched and inspected regardless of their provider. Sources nested in a source response, eg. a template src in a template, are discovered by inspecting the response.

Objects with the srcLoaded flag have their source response assembled in the cached workspace and their src is not read.

Parameters:
NameTypeDescription
workspaceObject
Returns:

Array of error messages for failed source requests.

Type: 
Promise.<Array.<String>>

(inner) clearSrcMap()

Clears the module scope source map. The source map must be cleared when the workspace cache is rebuilt.

(inner) collectSrcs(value, sources, inspectedObjects) → {void}

Recursively collects src properties from the value object and adds them to the sources Set. Inspected objects are tracked in the inspectedObjects WeakSet to avoid infinite recursion.

The src of an object with the srcLoaded flag is not collected.

Parameters:
NameTypeDescription
valueObject
sourcesSet
inspectedObjectsWeakSet
Returns:
Type: 
void

(async, inner) getSrc(params) → {Promise.<(String|Object|Error)>}

The method will resolve a src reference through a provider determined from the src prefix, eg. file:, https:, cloudfront:, or a signer provider from a SIGN_* xyzEnv key.

An error is returned if the provider does not exist for the src prefix. The provider request is resolved into a string or JSON depending on the url ending.

Parameters:
NameTypeDescription
paramsString | Object

A src reference string or params object.

Properties
NameTypeAttributesDescription
params.srcString<optional>

Source reference.

params.cacheBoolean<optional>

The source map is bypassed for a fresh provider response with cache being false.

params.workspaceObject<optional>

Workspace to recursively discover and cache sources in.

Returns:

Cloned source response.

Type: 
Promise.<(String|Object|Error)>

(inner) getSrcPromise(src) → {Promise.<(Object|Error)>}

Retrieves the source promise for the given src. If the source promise does not exist, it is created with the provider determined from the resolved src prefix.

Environment variables in the src are only substituted on a map miss. A src with environment variables is stored as an alias for the resolved src promise so envReplace runs once per unique src string.

Parameters:
NameTypeDescription
srcString

Source identifier.

Returns:

Source response promise.

Type: 
Promise.<(Object|Error)>

(inner) providerPromise(resolvedSrc) → {Promise.<(String|Object|Error)>}

Creates a provider request promise for a resolved src. Provider errors are resolved rather than thrown.

Parameters:
NameTypeDescription
resolvedSrcString

Resolved source reference.

Returns:

Provider response promise.

Type: 
Promise.<(String|Object|Error)>