New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

exp-fetch

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

exp-fetch

A small pluggable fetch lib

0.0.3
Source
npm
Version published
Weekly downloads
335
-22.63%
Maintainers
1
Weekly downloads
 
Created
Source

fetch

A small and pluggable lib to fetch a resource and cache the result.

Caching

Fetch will parse the cache-control header. If fetch encounters private, no-cache, max-age=0 or must-revalidate it wont cache. Otherwise it will respect the max-age header.

Callback usage:
var behavior = {};
var fetch = fetchBuilder(behavior);
fetch("http://example.com/resource.json", function (err, content) {
    // Do something with the result
});
Promise usage:
var behavior = {};
var fetch = fetchBuilder(behavior);
fetch("http://example.com/resource.json").then(function (content) {
    // Do something with the result
});

Allowed behavior options

  • freeze: (default:false). When this option is set it will freeze the response so it can't be modified.
  • cache: (default: an instance of AsyncCache) (https://github.com/ExpressenAB/exp-asynccache). To disable caching set {cache: null}
  • cacheKeyFn: (default: caches on the url) An optional formatting function for finding the cache-key. One might, for example, want to cache on an url with the get params stripped.
  • maxAgeFn: (default: respects the cache-control header)
  • onNotFound: If given a function, it will be called each time fetch encounters a 404
  • onError: If given a function, it will be called each time fetch encounters a non 200 nor 404 response
  • onSuccess: If given a function, it will be called each time fetch encounters a 200
  • logger: A logger object implementing error, warning, info, debug for example https://github.com/tj/log.js
  • cacheNotFound: (default: false). If set it will cache 404s, if given a number it will cache the 404 for that time. If the maxAgeFn is given, it will get this time as the first parameter.
CacheKeyFn
var keyFinder = function (url) {
    return url.replace(/\//g, "");
}
var fetch = fetchBuilder({cacheKeyFn: keyFinder});
Promise.all([
   fetch("http://example.com/foo/bar")
   fetch("http://example.com/foobar")
]).then(function (result) {
   result[0] === result[1];
});
maxAgeFn
function cacheNothing(maxAge, key, res, content) {
    return -1;
}
var fetch = fetchBuilder({maxAgeFn: cacheNothing});

Init cache function

The fetch lib provides a convenient initCache-method which sets up a cache purging it's expired content.


var initCache = require("exp-fetch").initCache;
var cache = new AsyncCache(initCache({ age: 60, size: 2000});

Allowed params:

  • size or max: the max allowed size, the unit is set by the length method. Default is value.length. Default: 2000000
  • length: the length function, default is v && v.length || 1
  • age or maxAge: the maximum number of seconds a key will be kept in the cache. Default 60

Keywords

fetch

FAQs

Package last updated on 29 May 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts