unexpired
Simple API for keeping your expiring resources fresh!
usage
Set up an expiring resource by providing a fetch
function.
var unexpired = ; var freshCertificate = ;
The generated function will lazily call your fetch
function as necessary to provide fresh resources.
;
promises
Your fetch
function may also allowed to return a promise instead of calling the supplied callback.
unexpired
will check for a then
method on your return value, and use that. If you are using promises,
you will likely want to use your promise library to "promisify" the generated function:
var resource = Promise; ;
options
You can customize the behavior by passing an options object instead.
;
Only fetch
is required, everything else is optional.
-
fetch
: Function
The fetch function. It must accept a node style callback (i.e.cb(err, result)
). By default, the callback should be called with an object that has anexpires
property. Theexpires
property should be an integer, representing the time the resource expires (in milliseconds since epoch). -
buffer
: Number or String
The safety buffer in milliseconds. Forcibly refresh resources a little earlier than necessary. This is useful for resources (like authentication tokens) that you want to use over the network. It mitigates problems arising from network latency and slightly off system clocks. Callbacks will never receive results that expire withinbuffer
milliseconds. String values will be parsed using duration-parser. Defaults to 0. -
prefetch
: Number or String
Prefetch time in milliseconds. Proactively fetch a new resource even before it is expired. Any requests for the resource withinbuffer + prefetch
ms of the expiration will trigger a fetch. Unlikebuffer
, this value does not prevent incoming requests from being fulfilled by an existing unexpired resource. Incoming requests will continue to be served by the unexpired resource until the newly fetched resource is available. This helps avoid latency on incoming requests by ensuring there is always an unexpired resource ready to go. String values will be parsed using duration-parser. Defaults to 0. -
retry
: Number or String
If a prefetch attempt fails, how long to wait before trying to prefetch again (in milliseconds). String values will be parsed using duration-parser. Defaults to 0. -
expires
: Function or String
Alternate method for extracting the expiration from the fetch result. It will be called with the fetch results, and must return a number representing the expiration (in milliseconds since epoch). By default, it just returns theexpires
property of the fetch result. Possible use would be parsing thenotAfter
result of anX509
certificate. If you provide a string, it will use that named property of the fetch result. -
transform
: Function
Transform the result before passing to callbacks. -
copy
: Function
Create a defensive copy of the result before passing to each callback. -
now
: Function
Alternate method of fetching the current time.