Gathering detailed insights and metrics for cacheable-request
Gathering detailed insights and metrics for cacheable-request
Gathering detailed insights and metrics for cacheable-request
Gathering detailed insights and metrics for cacheable-request
@types/cacheable-request
Stub TypeScript definitions entry for cacheable-request, which provides its own types definitions
@esm2cjs/cacheable-request
Wrap native HTTP requests with RFC compliant cache support. This is a fork of jaredwray/cacheable-request, but with CommonJS support.
cacheable-request-adaptable
Wrap native HTTP requests with RFC compliant cache support
improved-cacheable-request
Wrap native HTTP requests with RFC compliant cache support
a robust, scalable, and maintained set of caching packages
npm install cacheable-request
Typescript
Module System
Min. Node Version
Node Version
NPM Version
98.6
Supply Chain
99
Quality
91.1
Maintenance
100
Vulnerability
100
License
TypeScript (99.19%)
JavaScript (0.57%)
CSS (0.24%)
Total Downloads
4,011,485,899
Last Day
3,105,532
Last Week
19,818,630
Last Month
82,061,294
Last Year
898,939,998
MIT License
1,796 Stars
1,474 Commits
186 Forks
10 Watchers
1 Branches
90 Contributors
Updated on May 07, 2025
Latest Version
13.0.5
Package Id
cacheable-request@13.0.5
Unpacked Size
70.26 kB
Size
16.00 kB
File Count
11
NPM Version
10.9.0
Node Version
20.17.0
Published on
Apr 05, 2025
Cumulative downloads
Total Downloads
Last Day
12.6%
3,105,532
Compared to previous day
Last Week
8.7%
19,818,630
Compared to previous week
Last Month
-2.4%
82,061,294
Compared to previous month
Last Year
13.8%
898,939,998
Compared to previous year
Wrap native HTTP requests with RFC compliant cache support
RFC 7234 compliant HTTP caching for native Node.js HTTP/HTTPS requests. Caching works out of the box in memory or is easily pluggable with a wide range of storage adapters.
Note: This is a low level wrapper around the core HTTP modules, it's not a high level request library.
Keyv has been updated to version 5. With this update, you can no longer pass in a connection string directly to the CacheableRequest
constructor. Instead, you should pass in a Keyv or Keyv storage adapter instance.
This release contains breaking changes. This is the new way to use this package.
1import http from 'http';
2import CacheableRequest from 'cacheable-request';
3
4// Then instead of
5const req = http.request('http://example.com', cb);
6req.end();
7
8// You can do
9const cacheableRequest = new CacheableRequest(http.request);
10const cacheReq = cacheableRequest('http://example.com', cb);
11cacheReq.on('request', req => req.end());
12// Future requests to 'example.com' will be returned from cache if still valid
13
14// You pass in any other http.request API compatible method to be wrapped with cache support:
15const cacheableRequest = new CacheableRequest(https.request);
16const cacheableRequest = new CacheableRequest(electron.net);
1
2import CacheableRequest from 'cacheable-request';
3
4// Now You can do
5const cacheableRequest = new CacheableRequest(http.request).request();
6const cacheReq = cacheableRequest('http://example.com', cb);
7cacheReq.on('request', req => req.end());
8// Future requests to 'example.com' will be returned from cache if still valid
9
10// You pass in any other http.request API compatible method to be wrapped with cache support:
11const cacheableRequest = new CacheableRequest(https.request).request();
12const cacheableRequest = new CacheableRequest(electron.net).request();
The biggest change is that when you do a new
CacheableRequest you now want to call request
method will give you the instance to use.
1- const cacheableRequest = new CacheableRequest(http.request); 2+ const cacheableRequest = new CacheableRequest(http.request).request();
We are now using pure esm support in our package. If you need to use commonjs you can use v8 or lower. To learn more about using ESM please read this from sindresorhus
: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
If-None-Match
/If-Modified-Since
headersAge
header on cached responses1npm install cacheable-request
1import http from 'http';
2import CacheableRequest from 'cacheable-request';
3
4// Then instead of
5const req = http.request('http://example.com', cb);
6req.end();
7
8// You can do
9const cacheableRequest = new CacheableRequest(http.request).createCacheableRequest();
10const cacheReq = cacheableRequest('http://example.com', cb);
11cacheReq.on('request', req => req.end());
12// Future requests to 'example.com' will be returned from cache if still valid
13
14// You pass in any other http.request API compatible method to be wrapped with cache support:
15const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
16const cacheableRequest = new CacheableRequest(electron.net).createCacheableRequest();
cacheable-request
uses Keyv to support a wide range of storage adapters.
For example, to use Redis as a cache backend, you just need to install the official Redis Keyv storage adapter:
npm install @keyv/redis
And then you can pass CacheableRequest
your connection string:
1import KeyvRedis from '@keyv/redis'; 2import CacheableRequest from 'cacheable-request'; 3 4const keyvRedis = new KeyvRedis('redis://localhost:6379'); 5const cacheableRequest = new CacheableRequest(http.request, KeyvRedis).createCacheableRequest();
View all official Keyv storage adapters.
Keyv also supports anything that follows the Map API so it's easy to write your own storage adapter or use a third-party solution.
e.g The following are all valid storage adapters
1const storageAdapter = new Map(); 2// or 3const storageAdapter = require('./my-storage-adapter'); 4// or 5const QuickLRU = require('quick-lru'); 6const storageAdapter = new QuickLRU({ maxSize: 1000 }); 7 8const cacheableRequest = new CacheableRequest(http.request, storageAdapter).createCacheableRequest();
View the Keyv docs for more information on how to use storage adapters.
Returns the provided request function wrapped with cache support.
Type: function
Request function to wrap with cache support. Should be http.request
or a similar API compatible request function.
Type: Keyv storage adapter
Default: new Map()
A Keyv storage adapter instance, or connection string if using with an official Keyv storage adapter.
Returns an event emitter.
Type: object
, string
http-cache-semantics
options.Type: boolean
Default: true
If the cache should be used. Setting this to false will completely bypass the cache for the current request.
Type: boolean
Default: false
If set to true
once a cached resource has expired it is deleted and will have to be re-requested.
If set to false
(default), after a cached resource's TTL expires it is kept in the cache and will be revalidated on the next request with If-None-Match
/If-Modified-Since
headers.
Type: number
Default: undefined
Limits TTL. The number
represents milliseconds.
Type: boolean
Default: false
When set to true
, if the DB connection fails we will automatically fallback to a network request. DB errors will still be emitted to notify you of the problem even though the request callback may succeed.
Type: boolean
Default: false
Forces refreshing the cache. If the response could be retrieved from the cache, it will perform a new request and override the cache instead.
Type: function
The callback function which will receive the response as an argument.
The response can be either a Node.js HTTP response stream or a responselike object. The response will also have a fromCache
property set with a boolean value.
request
event to get the request object of the request.
Note: This event will only fire if an HTTP request is actually made, not when a response is retrieved from cache. However, you should always handle the request
event to end the request and handle any potential request errors.
response
event to get the response object from the HTTP request or cache.
error
event emitted in case of an error with the cache.
Errors emitted here will be an instance of CacheableRequest.RequestError
or CacheableRequest.CacheError
. You will only ever receive a RequestError
if the request function throws (normally caused by invalid user input). Normal request errors should be handled inside the request
event.
To properly handle all error scenarios you should use the following pattern:
1cacheableRequest('example.com', cb) 2 .on('error', err => { 3 if (err instanceof CacheableRequest.CacheError) { 4 handleCacheError(err); // Cache error 5 } else if (err instanceof CacheableRequest.RequestError) { 6 handleRequestError(err); // Request function thrown 7 } 8 }) 9 .on('request', req => { 10 req.on('error', handleRequestError); // Request error emitted 11 req.end(); 12 });
Note: Database connection errors are emitted here, however cacheable-request
will attempt to re-request the resource and bypass the cache on a connection error. Therefore a database connection error doesn't necessarily mean the request won't be fulfilled.
Hooks have been implemented since version v9
and are very useful to modify response before saving it in cache. You can use hooks to modify response before saving it in cache.
The hook will pre compute response right before saving it in cache. You can include many hooks and it will run in order you add hook on response object.
1import http from 'http'; 2import CacheableRequest from 'cacheable-request'; 3 4const cacheableRequest = new CacheableRequest(request, cache).request(); 5 6// adding a hook to decompress response 7cacheableRequest.addHook('onResponse', async (value: CacheValue, response: any) => { 8 const buffer = await pm(gunzip)(value.body); 9 value.body = buffer.toString(); 10 return value; 11});
here is also an example of how to add in the remote address
1import CacheableRequest, {CacheValue} from 'cacheable-request'; 2 3const cacheableRequest = new CacheableRequest(request, cache).request(); 4cacheableRequest.addHook('onResponse', (value: CacheValue, response: any) => { 5 if (response.connection) { 6 value.remoteAddress = response.connection.remoteAddress; 7 } 8 9 return value; 10});
You can also remove hook by using below
1CacheableRequest.removeHook('onResponse');
Cacheable-Request is an open source package and community driven that is maintained regularly. In addition we have a couple of other guidelines for review:
To post an issue, navigate to the "Issues" tab in the main repository, and then select "New Issue." Enter a clear title describing the issue, as well as a description containing additional relevant information. Also select the label that best describes your issue type. For a bug report, for example, create an issue with the label "bug." In the description field, Be sure to include replication steps, as well as any relevant error messages.
If you're reporting a security violation, be sure to check out the project's security policy.
Please also refer to our Code of Conduct for more information on how to report issues.
To ask a question, create an issue with the label "question." In the issue description, include the related code and any context that can help us answer your question.
7.5/10
Summary
Withdrawn: cacheable-request depends on http-cache-semantics, which is vulnerable to Regular Expression Denial of Service
Affected Versions
< 10.2.7
Patched Versions
10.2.7
No security vulnerabilities found.