http-cache

Caching HTTP client.

Caches HTTP requests to disc so that plugins that make network requests are faster on consecutive builds.

new HttpCache(context, options)

Create an HttpCache plugin.

Configure this plugin for the build phase.

Assigns this plugin instance to context.agent and does nothing else until the public functions are invoked by other plugins.

If no directory is given files are cached in the cache folder within this package.

This implementation will only cache responses for the 200 status code, you can cache responses for other status codes using the codes option.

To disable caching you can set the expires option to zero. If the context option cache has been set to false the expires value is forced to zero and the cache is bypassed.

This implementation respects the ETag header and will only use a cached resource when the ETag has not changed.

If a server sends a Cache-Control header containing no-store the response is not cached.

If a server sends a Cache-Control header with a max-age value it is respected and overrides the expires option.

If a Cache-Control header does not contain the no-store value and the server response has not expired the result is served from the cache with no network request (optimal path).

For each request a directory is created corresponding to the request URL in the cache directory. Within each directory a meta.json file is written containing information about the last request, for methods other than HEAD a body file is also written to the directory containing the response body buffer.

HttpCache.prototype.request(url, options[, cb])

Perform a request and cache the result.

If a result already exists in the cache and is not stale the cached result is returned.

Options are passed to the http.request() or https.request() functions depending upon the URL, see the relevant nodejs documentation for more information. Options related to the URL are automatically set by calling require('url').parse(url) so you do not need to assign those options.

If the buffer option is set to false then the response body data is not read which is useful if you either just want the headers for a method other than HEAD or if you are reading in the body data when consuming the response (for example to calculate checksums).

The response object that the promise resolves to will contain meta information about the last request and a body field which is the buffer content from the last request.

The field hit indicates whether the result is coming from the cache or from a network request. It will be false when coming from the network.

When the cb callback function is specified it is invoked with the server response object.

Returns a promise that resolves to the downloaded or cached content.

HttpCache.prototype.head(url[, options][, cb])

Perform a HEAD request and cache the result meta data only.

Returns a promise that resolves to the downloaded or cached content.

HttpCache.prototype.get(url[, options][, cb])

Perform a GET request and cache the result meta data and response buffer.

Returns a promise that resolves to the downloaded or cached content.

HttpCache.prototype.post(url[, options][, cb])

Perform a POST request and cache the result meta data and response buffer.

If you add a data field to the options it is sent as the POST data.

Returns a promise that resolves to the downloaded or cached content.