Wrap native HTTP requests with RFC compliant cache support

Overview

cacheable-request

Wrap native HTTP requests with RFC compliant cache support

Build Status Coverage Status npm npm

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.

Features

  • Only stores cacheable responses as defined by RFC 7234
  • Fresh cache entries are served directly from cache
  • Stale cache entries are revalidated with If-None-Match/If-Modified-Since headers
  • 304 responses from revalidation requests use cached body
  • Updates Age header on cached responses
  • Can completely bypass cache on a per request basis
  • In memory cache by default
  • Official support for Redis, MongoDB, SQLite, PostgreSQL and MySQL storage adapters
  • Easily plug in your own or third-party storage adapters
  • If DB connection fails, cache is automatically bypassed (disabled by default)
  • Adds cache support to any existing HTTP code with minimal changes
  • Uses http-cache-semantics internally for HTTP RFC 7234 compliance

Install

npm install cacheable-request

Usage

const http = require('http');
const CacheableRequest = require('cacheable-request');

// Then instead of
const req = http.request('http://example.com', cb);
req.end();

// You can do
const cacheableRequest = new CacheableRequest(http.request);
const cacheReq = cacheableRequest('http://example.com', cb);
cacheReq.on('request', req => req.end());
// Future requests to 'example.com' will be returned from cache if still valid

// You pass in any other http.request API compatible method to be wrapped with cache support:
const cacheableRequest = new CacheableRequest(https.request);
const cacheableRequest = new CacheableRequest(electron.net);

Storage Adapters

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:

const cacheableRequest = new CacheableRequest(http.request, 'redis://user:pass@localhost:6379');

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

const storageAdapter = new Map();
// or
const storageAdapter = require('./my-storage-adapter');
// or
const QuickLRU = require('quick-lru');
const storageAdapter = new QuickLRU({ maxSize: 1000 });

const cacheableRequest = new CacheableRequest(http.request, storageAdapter);

View the Keyv docs for more information on how to use storage adapters.

API

new cacheableRequest(request, [storageAdapter])

Returns the provided request function wrapped with cache support.

request

Type: function

Request function to wrap with cache support. Should be http.request or a similar API compatible request function.

storageAdapter

Type: Keyv storage adapter
Default: new Map()

A Keyv storage adapter instance, or connection string if using with an official Keyv storage adapter.

Instance

cacheableRequest(opts, [cb])

Returns an event emitter.

opts

Type: object, string

  • Any of the default request functions options.
  • Any http-cache-semantics options.
  • Any of the following:
opts.cache

Type: boolean
Default: true

If the cache should be used. Setting this to false will completely bypass the cache for the current request.

opts.strictTtl

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.

opts.maxTtl

Type: number
Default: undefined

Limits TTL. The number represents milliseconds.

opts.automaticFailover

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.

opts.forceRefresh

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.

cb

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.

.on('request', request)

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.

.on('response', response)

response event to get the response object from the HTTP request or cache.

.on('error', error)

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:

cacheableRequest('example.com', cb)
  .on('error', err => {
    if (err instanceof CacheableRequest.CacheError) {
      handleCacheError(err); // Cache error
    } else if (err instanceof CacheableRequest.RequestError) {
      handleRequestError(err); // Request function thrown
    }
  })
  .on('request', req => {
    req.on('error', handleRequestError); // Request error emitted
    req.end();
  });

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.

License

MIT © Luke Childs

Comments
  • Got Overwriting cache with 304 response

    Got Overwriting cache with 304 response

    with got 8.2.0 got.stream(imgUrl, { cache: keyv }).pipe(res); records in mongodb: { "_id": ObjectID("5ab084465a793ecc436a7849"), "key": "cacheable-request:GET:https://farm2.staticflickr.com/1521/26180297656_675e75a2ce_h.jpg", "value": "{\"value\":{\"cachePolicy\":{\"v\":1,\"t\":1521635364661,\"sh\":true,\"ch\":0.1,\"imm\":86400000,\"st\":200,\"resh\":{\"date\":\"Wed, 21 Mar 2018 12:29:24 GMT\",\"connection\":\"close\",\"expires\":\"Wed, 28 Mar 2018 12:29:24 UTC\",\"cache-control\":\"max-age=604800,public\",\"via\":\"http/1.1 pc-pool116.flickr.gq1.yahoo.com (ApacheTrafficServer [cMsSfW])\",\"server\":\"ATS\",\"x-photo-farm\":\"2\",\"x-photo-farm-guess\":\"2\",\"access-control-allow-origin\":\"*\",\"access-control-allow-methods\":\"POST, GET, OPTIONS\"},\"rescc\":{\"max-age\":\"604800\",\"public\":true},\"m\":\"GET\",\"a\":true,\"reqh\":null,\"reqcc\":{}},\"url\":\"\",\"statusCode\":304,\"body\":\":base64:\"},\"expires\":null}", "expiresAt": null }

    bug 
    opened by reggiezhang 55
  • Cannot use namespace 'ResponseLike' as a type.

    Cannot use namespace 'ResponseLike' as a type.

    Describe the bug When I'm running the project facing the error " Cannot find namespace 'CacheableRequest' "

    node_modules/got/dist/source/core/options.d.ts:870:27 - error TS2503: Cannot find namespace 'CacheableRequest'.

    870 get cache(): string | CacheableRequest.StorageAdapter | boolean | undefined; ~~~~~~~~~~~~~~~~

    node_modules/got/dist/source/core/options.d.ts:871:31 - error TS2503: Cannot find namespace 'CacheableRequest'.

    871 set cache(value: string | CacheableRequest.StorageAdapter | boolean | undefined); ~~~~~~~~~~~~~~~~

    node_modules/got/dist/source/core/options.d.ts:1163:35 - error TS2503: Cannot find namespace 'CacheableRequest'.

    1163 cache: string | boolean | CacheableRequest.StorageAdapter | undefined;

    bug investigating 
    opened by Rehan-05 18
  • Possible event listener memory leak

    Possible event listener memory leak

    Issuehunt badges

    Disclaimer: I'm fairly new to Node :)

    The following event listener in CacheableRequest.js is never removed:

    this.cache.on('error', err => ee.emit('error', new CacheableRequest.CacheError(err)));

    These listeners will keep accumulating in the scenario where you are reusing a single CacheableRequest to service multiple requests (which I believe is the intention?).

    Moreover, the error produced by the cache is not necessarily associated with the current request - it's a generic error that is broadcast to all listeners.


    IssueHunt Summary

    clement-buchart clement-buchart has been rewarded.

    Backers (Total: $80.00)

    Submitted pull Requests


    Tips


    IssueHunt has been backed by the following sponsors. Become a sponsor

    opened by antonmarsden 14
  • Cache remoteAddress

    Cache remoteAddress

    For https://github.com/sindresorhus/got/pull/827 we need the remoteAddress in the cache. This would require (as I understand correctly) connection: { remoteAddress: response.connection.remoteAddress } in the following lines: https://github.com/lukechilds/cacheable-request/blob/d50e6af350b6f52814c71ce3908e704a3d5f428e/src/index.js#L119-L124

    And a connection parameter in https://github.com/lukechilds/responselike/blob/093a21bc25ecc82daf17f436c13fd807e5d8eee0/src/index.js#L7

    Is this a conceivable change?

    enhancement in progress 
    opened by Zauberbutter 9
  • fix(cacheableRequest): prevent collisions on POST, PUT, and PATCH

    fix(cacheableRequest): prevent collisions on POST, PUT, and PATCH

    If a server responds with valid cache-control headers POST, PUT, and PATCH requests may be cached. This may lead to cache collisions if the URI is the same between requests. In order to avoid this, this change will hash the body of the request and add that to the cache key. One note: the cache is completely bypassed in the event that the request body is a stream. This is because the key could not be generated without waiting for the stream to end and the contents of the stream may or may not themselves be easily hashed.

    opened by elliotttf 9
  • Update ava to the latest version 🚀

    Update ava to the latest version 🚀

    The devDependency ava was updated from 0.25.0 to 1.1.0.

    This version is not covered by your current version range.

    If you don’t accept this pull request, your project will work just like it did before. However, you might be missing out on a bunch of new features, fixes and/or performance improvements from the dependency update.


    Release Notes for 1.1.0

    1.1.0

    New features

    AVA now exports a meta object. Currently, you can retrieve the path of the test file being run:

    import test from 'ava';
    

    console.log('Test currently being run: ', test.meta.file);

    import {meta} from 'ava';
    

    console.log('Test currently being run: ', meta.file);

    This is useful in helpers that need to know the test file. bccd297

    Bug fixes and other improvements

    • t.log() now works in hooks d187712

    • Error output for improper usage of t.throws() once again links to the correct documentation dc552bc

    • We've added a section on webpack aliases to the Babel recipe c3bcbf2

    • We've updated the Vue recipe for Babel 7, and added a section on webpack aliases c3bcbf2

    All changes 📚

    v1.0.1...v1.1.0

    Thanks 💌

    💖 Huge thanks to @fitztrev, @forresst, @astrob0t, @pearofducks, @coreyfarrell and @dflupu for helping us with this release. We couldn’t have done it without you!

    Get involved ✌️

    We welcome new contributors. AVA is a friendly place to get started in open source. We have a great article on getting started contributing and a comprehensive contributing guide.

    Commits

    The new version differs by 227 commits.

    • a28094a 1.1.0
    • 7262a78 Bump dependencies
    • d187712 Fix t.log() in hooks
    • bccd297 Expose test file path within worker process
    • c3bcbf2 Improve Babel and Vue recipes
    • dc552bc Update link to t.throws() assertion in AVA output
    • f4b2d19 Fix links to French translation of docs (#2005)
    • 2e72fe7 Fix npm install command in readme (#2001)
    • 7b6e578 Use newer ES syntax where possible
    • 783944f 1.0.1
    • 024bab7 1.0.0
    • aac41dc Reword introduction
    • 84d8fff Bump dependencies & update documentation
    • c1364a1 Update the tagline and readme intro (#1983)
    • eed2e7a Clarify that not all configuration options can be overridden by CLI flags

    There are 227 commits in total.

    See the full diff

    FAQ and help

    There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


    Your Greenkeeper bot :palm_tree:

    opened by greenkeeper[bot] 8
  • fix: request args treatment should be consistent with http.request

    fix: request args treatment should be consistent with http.request

    • adds support for URL instances being passed to request
    • when request argument is an object, normalize it according to the options accepted by http.request instead of url.format

    fixes:

    • https://github.com/sindresorhus/got/pull/519
    • https://github.com/sindresorhus/got/pull/487

    cc @sindresorhus

    opened by jstewmon 8
  • Uses old cache policy when response is not modified

    Uses old cache policy when response is not modified

    Old cache policy should be used when revalidated response is not modified. It does not make sense to use revalidated policy as the response is not modified at all.

    opened by adityapatadia 6
  • feat: optional keyv instance injection

    feat: optional keyv instance injection

    This feature allows external libraries that wraps cacheable-request to have control on the keyv cache instance while still defaulting to current behavior.

    ref: https://github.com/sindresorhus/got/issues/1523

    opened by simonecorsi 5
  • Bundle cacheable request types with the library

    Bundle cacheable request types with the library

    I bundled @types/cacheable-request definition with the library primarily to solve the problem where dependent libraries (ex. got) fail to compile with @types/nodejs v13. This effectively removes dependency on @types/keyv (which is included in @types/cacheable-request) and bonus is that it would be much easier to update the definitions in the future should things change.

    opened by normano 5
  • Cache decompressed response body

    Cache decompressed response body

    Feature request from https://github.com/sindresorhus/got/issues/1158.

    I noticed that the request cache is storing the compressed body. This adds extra computational load when using the cache - we're decompressing it every time.

    Is there any way that the cache could store the decompressed body to avoid this?

    enhancement in progress 
    opened by simontabor 5
  • Move @types/http-cache-semantics from dev to deps

    Move @types/http-cache-semantics from dev to deps

    Please check if the PR fulfills these requirements

    • [x] Followed the Contributing guidelines.
    • [x] Tests for the changes have been added (for bug fixes/features) with 100% code coverage.
    • [x] Docs have been added / updated (for bug fixes / features)

    What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

    Moves @types/http-cache-semantics from dev-deps to deps, and adds a test to prevent regressions.

    Fixes https://github.com/jaredwray/cacheable-request/issues/218

    opened by Maxim-Mazurok 1
  • [regression] Could not find a declaration file for module 'http-cache-semantics'

    [regression] Could not find a declaration file for module 'http-cache-semantics'

    Describe the bug Regression of https://github.com/jaredwray/cacheable-request/issues/194 It was previously fixed in https://github.com/jaredwray/cacheable-request/pull/195 And then broken again in https://github.com/jaredwray/cacheable-request/pull/217 Perhaps we need to add a test to prevent this in the future?

    How To Reproduce (best to provide workable code or tests!) See https://github.com/jaredwray/cacheable-request/issues/194

    bug 
    opened by Maxim-Mazurok 0
Releases(v10.2.4)
  • v10.2.4(Dec 23, 2022)

    v10.2.4

    Minor updates with one exception is that we removed @types/http-cache-semantics from the main dependencies as it does not look to be needed.

    What's Changed

    • upgrading typescript to 4.9.4 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/214
    • upgrading jest types and eslint for jest to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/215
    • upgrading sqlite3 to 5.1.4 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/216
    • removing @types/http-cache-semantics from the dependencies and moving… by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/217

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.2.3...v10.2.4

    Source code(tar.gz)
    Source code(zip)
  • v10.2.3(Nov 22, 2022)

    v10.2.3 Maintenance Release

    Upgrading core modules in the system such as keyv and also a minor fix to an uncaught exception that we were seeing referenced here: https://github.com/sindresorhus/got/issues/1925

    Additional update is moving normalize-url to 8.0.0 which after testing it looks to not affect anything but will post the release notes here: https://github.com/sindresorhus/normalize-url/releases/tag/v8.0.0

    What's Changed

    • fixed uncaught exception issue by @alphmth in https://github.com/jaredwray/cacheable-request/pull/206
    • upgrading xo to 0.53.1 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/208
    • upgrading typescript to 4.9.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/209
    • upgrading jest (29.3.1) and components to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/210
    • upgrading keyv to 4.5.2 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/211
    • upgrading normalize-url to 8.0.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/212

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.2.2...v10.2.3

    Source code(tar.gz)
    Source code(zip)
  • v10.2.2(Oct 19, 2022)

    Maintenance Release 10.2.2 🛠️

    This is a monthly maintenance release which focused mostly on development toolset and should not have any big changes. The only package that was updated is normalize-url with a version bump to 7.2.0.

    What's Changed

    • upgrading xo to 0.52.4 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/197
    • upgrading typescript to 4.8.4 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/198
    • upgrading sqlite3 to 5.1.2 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/199
    • upgrading jest to 29.2.1 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/200
    • upgrading ts-jest to 29.0.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/201
    • upgrading eslint-plugin-jest to 27.1.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/202
    • upgrading jest and node types to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/203
    • upgrading normalize-url to 7.2.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/204
    • upgrading @keyv/sqlite to 3.6.2 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/205

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.2.1...v10.2.2

    Source code(tar.gz)
    Source code(zip)
  • v10.2.1(Sep 26, 2022)

    10.2.1 Bug fixes and Changes

    Biggest change has been that when an agent is KeepAlive: true we are now sending a response end which will close the socket instead of an error.

    What's Changed

    • Moved @types/http-cache-semantics to dependencies by @Maxim-Mazurok in https://github.com/jaredwray/cacheable-request/pull/195
    • fix dangling response on cache revalidation by @zkx5xkt in https://github.com/jaredwray/cacheable-request/pull/196

    New Contributors

    • @Maxim-Mazurok made their first contribution in https://github.com/jaredwray/cacheable-request/pull/195
    • @zkx5xkt made their first contribution in https://github.com/jaredwray/cacheable-request/pull/196

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.2.0...v10.2.1

    Source code(tar.gz)
    Source code(zip)
  • v10.2.0(Sep 23, 2022)

    Hooks onResponse replacing response hook

    response will still work but will be deprecated as we expand this functionality. onResponse will be the name moving forward. Here are examples of how to use it.

    How to decompress

    import http from 'http';
    import CacheableRequest from 'cacheable-request';
    
    const cacheableRequest = new CacheableRequest(request, cache).request();
    
    // adding a hook to decompress response
    cacheableRequest.addHook('onResponse', async (value: CacheValue, response: any) => {
      const buffer = await pm(gunzip)(value.body);
      value.body = buffer.toString();
      return value;
    });
    

    how to add a remote address

    import CacheableRequest, {CacheValue} from 'cacheable-request';
    
    const cacheableRequest = new CacheableRequest(request, cache).request();
    cacheableRequest.addHook('onResponse', (value: CacheValue, response: any) => {
      if (response.connection) {
        value.remoteAddress = response.connection.remoteAddress;
      }
    
      return value;
    });
    

    In addition to that we have also updated some of the packages to their lates which you can read below 👇

    What's Changed

    • upgrading xo to 0.52.3 and updating types.ts with type definitions by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/190
    • upgrading ts-jest to 29.0.1 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/191
    • upgrading sqlite3 to 5.1.1 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/192
    • added remote address by @alphmth in https://github.com/jaredwray/cacheable-request/pull/189
    • moving to onResponse for hooks by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/193

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.1.2...v10.2.0

    Source code(tar.gz)
    Source code(zip)
  • v10.1.2(Sep 16, 2022)

    ⚠️ Breaking Change

    Based on feedback (thanks @szmarczak!) we have renamed the function createCacheableRequest to just request.

    - const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
    + const cacheableRequest = new CacheableRequest(https.request).request(); 
    

    v10 code with createCacheableRequest

    import CacheableRequest from 'cacheable-request';
    
    // Now You can do
    const cacheableRequest = new CacheableRequest(http.request).request();
    const cacheReq = cacheableRequest('http://example.com', cb);
    cacheReq.on('request', req => req.end());
    // Future requests to 'example.com' will be returned from cache if still valid
    
    // You pass in any other http.request API compatible method to be wrapped with cache support:
    const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
    const cacheableRequest = new CacheableRequest(electron.net).createCacheableRequest();
    

    v10.1.2 code with request

    import CacheableRequest from 'cacheable-request';
    
    // Now You can do
    const cacheableRequest = new CacheableRequest(http.request).request();
    const cacheReq = cacheableRequest('http://example.com', cb);
    cacheReq.on('request', req => req.end());
    // Future requests to 'example.com' will be returned from cache if still valid
    
    // You pass in any other http.request API compatible method to be wrapped with cache support:
    const cacheableRequest = new CacheableRequest(https.request).request();
    const cacheableRequest = new CacheableRequest(electron.net).request();
    

    What's Changed

    • refactor code by @alphmth in https://github.com/jaredwray/cacheable-request/pull/188

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.0.2...v10.1.2

    Source code(tar.gz)
    Source code(zip)
  • v10.0.2(Sep 14, 2022)

    Change to support got by exporting types enabling better integration with upstream packages.

    What's Changed

    • fixed types by @alphmth in https://github.com/jaredwray/cacheable-request/pull/187

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.0.1...v10.0.2

    Source code(tar.gz)
    Source code(zip)
  • v10.0.1(Sep 11, 2022)

    Minor maintenance release with some primary packages updated such as:

    • @types/node to 18.7.16
    • keyv to 4.5.0
    • normalize-url to 7.10

    Changelog

    • upgrading jest and ts-jest to version 29 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/177
    • upgrading @types/node to 18.7.16 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/178
    • upgrading eslint-plugin-jest to 27.0.4 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/179
    • updating documentation to on contribution by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/180
    • adding in table of contents by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/181
    • upgrading keyv to 4.5.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/182
    • adding a warning to security document on v8 support by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/183
    • upgrading typescript to 4.8.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/184
    • upgrading normalize-url to 7.1.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/185
    • updating docs and prepare when publishing by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/186

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v10.0.0...v10.0.1

    Source code(tar.gz)
    Source code(zip)
  • v10.0.0(Sep 10, 2022)

    Breaking Change with v10.0.0

    This release contains breaking changes as we are now using class to handle instances and hooks better. This is the new way to use this package.

    Usage Before v10

    import http from 'http';
    import CacheableRequest from 'cacheable-request';
    
    // Then instead of
    const req = http.request('http://example.com', cb);
    req.end();
    
    // You can do
    const cacheableRequest = new CacheableRequest(http.request);
    const cacheReq = cacheableRequest('http://example.com', cb);
    cacheReq.on('request', req => req.end());
    // Future requests to 'example.com' will be returned from cache if still valid
    
    // You pass in any other http.request API compatible method to be wrapped with cache support:
    const cacheableRequest = new CacheableRequest(https.request);
    const cacheableRequest = new CacheableRequest(electron.net);
    

    Usage After v10

    import CacheableRequest from 'cacheable-request';
    
    // Now You can do
    const cacheableRequest = new CacheableRequest(http.request).createCacheableRequest();
    const cacheReq = cacheableRequest('http://example.com', cb);
    cacheReq.on('request', req => req.end());
    // Future requests to 'example.com' will be returned from cache if still valid
    
    // You pass in any other http.request API compatible method to be wrapped with cache support:
    const cacheableRequest = new CacheableRequest(https.request).createCacheableRequest();
    const cacheableRequest = new CacheableRequest(electron.net).createCacheableRequest();
    

    The biggest change is that when you do a new CacheableRequest you now want to call createCacheableRequest method will give you the instance to use.

    - const cacheableRequest = new CacheableRequest(http.request);
    + const cacheableRequest = new CacheableRequest(http.request).createCacheableRequest();
    

    What's Changed

    • updating security readme on support for v8.x.x by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/166
    • upgrading keyv to 4.4.1 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/168
    • upgrading ts-jest to 28.0.8 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/169
    • fixed hooks instance issue by @alphmth in https://github.com/jaredwray/cacheable-request/pull/167
    • fixed issue by @alphmth in https://github.com/jaredwray/cacheable-request/pull/172
    • fixed type issues by @alphmth in https://github.com/jaredwray/cacheable-request/pull/173
    • Issue #174 by @slwhitman in https://github.com/jaredwray/cacheable-request/pull/176
    • Issue 171 by @alphmth in https://github.com/jaredwray/cacheable-request/pull/175

    New Contributors

    • @slwhitman made their first contribution in https://github.com/jaredwray/cacheable-request/pull/176

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v9.0.0...v10.0.0

    Source code(tar.gz)
    Source code(zip)
  • v9.0.0(Aug 13, 2022)

    v9 is now pure ESM 🎉

    cacheable-request is now pure ESM with version v9.0.0 and up. Version v.8.x.x is the supported commonjs version which will get only major security fixes moving forward until end of 2022. If you would like to learn about uprgrading / using ESM @sindresorhus has an amazing reference guide here: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c

    Major thank you to @alphmth, @sindresorhus, @szmarczak, @jasonbaik, and everybody for help on this. 🙌

    Special call out to @CyberShadow as in the previous release we forgot to mention the work done on issue #28 as without their guidance and code we wouldn't have fixed it. ❤️

    Typescript

    In addition to pure ESM we are now fulling using Typescript moving forward on this project which means all type definitions are now native with the service. 🤩

    Hooks!

    Hooks have been introduced for the response object that will run a processing function like so if you wanted to do compression:

    CacheableRequest.addHook('response', async (response: any) => {
      const buffer = await pm(gunzip)(response);
      return buffer.toString();
    });
    
    const cacheableRequest = CacheableRequest(request, cache);
    

    How to Add a Hook: https://github.com/jaredwray/cacheable-request#add-hooks How to Remove a Hook: https://github.com/jaredwray/cacheable-request#remove-hooks

    We would love some feedback on this feature!

    Change Log

    • support esm by @alphmth in https://github.com/jaredwray/cacheable-request/pull/145
    • Moving to pure ESM by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/147
    • upgrading jest and ts-node to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/148
    • fixed for 304 by @alphmth in https://github.com/jaredwray/cacheable-request/pull/149
    • upgrading ts-node to 10.9.1 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/151
    • upgrading xo to 0.51.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/152
    • upgrading jest and modules to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/153
    • upgrading @keyv/sqlite to 3.5.3 and sqlite3 to 5.0.10 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/154
    • upgrading keyv to 4.3.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/155
    • upgrading responseLink to 3.0.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/156
    • Issue 95 by @alphmth in https://github.com/jaredwray/cacheable-request/pull/157
    • upgrading sqlite3 to 5.0.11 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/158
    • upgrading eslint-plugin-jest to 26.7.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/159
    • Create PULL_REQUEST_TEMPLATE.md for use on pull requests by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/160
    • upgrading @types/node and eslint-plugin-jest to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/161
    • removing console.log from tests by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/162
    • added hook for pre computation on response by @alphmth in https://github.com/jaredwray/cacheable-request/pull/163
    • upgrading @types/node to 18.7.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/164

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v8.3.1...v9.0.0

    Source code(tar.gz)
    Source code(zip)
  • v8.3.1(Jun 27, 2022)

    What's Changed

    • Typescript by @alphmth in https://github.com/jaredwray/cacheable-request/pull/140
    • support jest by @alphmth in https://github.com/jaredwray/cacheable-request/pull/141
    • Create SECURITY.md by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/142
    • fixed line coverages by @alphmth in https://github.com/jaredwray/cacheable-request/pull/143
    • Typescript by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/144

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v8.0.1...v8.3.1

    Source code(tar.gz)
    Source code(zip)
  • v8.0.1(Jun 11, 2022)

    What's Changed

    • upgrading xo to version 0.50.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/135
    • updating readme for ESM support and removing this package by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/136

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v8.0.0...v8.0.1

    Source code(tar.gz)
    Source code(zip)
  • v8.0.0(Jun 11, 2022)

    What's Changed

    • removing funding as no longer needed by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/110
    • Moving to GitHub workflows for testing by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/111
    • upgrading eslint xo config to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/112
    • upgrading this module to version 1.1.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/113
    • upgrading sqlite3 to version 5.0.2 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/114
    • upgrading sqlite3 to version 5.0.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/115
    • upgrading nyc to version 15.1.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/116
    • upgrading delay to version 5.0.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/117
    • upgrading http-cache-semantics to version 4.1.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/118
    • upgrading pify to version 5.0.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/119
    • upgrading create-test-server to version 3.0.1 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/120
    • feat: optional keyv instance injection by @simonecorsi in https://github.com/jaredwray/cacheable-request/pull/101
    • fix(cacheableRequest): prevent collisions on POST, PUT, and PATCH by @elliotttf in https://github.com/jaredwray/cacheable-request/pull/31
    • test(100%): add case to reach coverage by @simonecorsi in https://github.com/jaredwray/cacheable-request/pull/121
    • upgrading kevy and @keyv/sqlite to latest by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/122
    • license and package clean up by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/125
    • fixed for node 14, 16, and 18 by @alphmth in https://github.com/jaredwray/cacheable-request/pull/126
    • upgrading ava to version 4.3.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/128
    • xo upgrade by @alphmth in https://github.com/jaredwray/cacheable-request/pull/130
    • removing lowercasekeys as no longer needed by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/131
    • upgrading pify to version 6.0.0 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/132
    • Convert package to esm by @jasonbaik in https://github.com/jaredwray/cacheable-request/pull/133
    • upgrading normalize-url to version 7.0.3 by @jaredwray in https://github.com/jaredwray/cacheable-request/pull/134

    New Contributors

    • @jaredwray made their first contribution in https://github.com/jaredwray/cacheable-request/pull/110
    • @simonecorsi made their first contribution in https://github.com/jaredwray/cacheable-request/pull/101
    • @elliotttf made their first contribution in https://github.com/jaredwray/cacheable-request/pull/31
    • @alphmth made their first contribution in https://github.com/jaredwray/cacheable-request/pull/126
    • @jasonbaik made their first contribution in https://github.com/jaredwray/cacheable-request/pull/133

    Full Changelog: https://github.com/jaredwray/cacheable-request/compare/v7.0.2...v8.0.0

    Source code(tar.gz)
    Source code(zip)
Owner
Luke Childs
Building @getumbrel
Luke Childs
make streaming http requests

hyperquest treat http requests as a streaming transport The hyperquest api is a subset of request. This module works in the browser with browserify. r

James Halliday 711 Sep 8, 2022
Library agnostic in-process recording of http(s) requests and responses

@gr2m/http-recorder Library agnostic in-process recording of http(s) requests and responses Install npm install @gr2m/http-recorder Usage import http

Gregor Martynus 4 May 12, 2022
Node.js web server framework for Http/1.1 or Http/2

Node.js web server framework for Http/1.1 or Http/2 Description: This is http framework, you can use it to create Http/1.1 or Http/2 service。 Now let'

Jeremy Yu 10 Mar 24, 2022
GitHub Action that configures Nix to read/write to a cache

Set up Nix Cache Action This is a GitHub Action that configures the Nix package manager to read from (and optionally write to) a remote cache. Usage U

Ross Light 5 Nov 22, 2022
The official proxy of Titanium Network with enhanced support for a large majority of sites with hCAPTCHA support. Successor to Alloy Proxy.

Corrosion Titanium Networks main web proxy. Successor to Alloy Installation: npm i corrosion Example: const Corrosion = require('corrosion'); const p

Titanium Network 79 Dec 21, 2022
Promise based HTTP client for the browser and node.js

axios Promise based HTTP client for the browser and node.js New axios docs website: click here Table of Contents Features Browser Support Installing E

axios 98k Dec 31, 2022
🏊🏾 Simplified HTTP request client.

Deprecated! As of Feb 11th 2020, request is fully deprecated. No new changes are expected to land. In fact, none have landed for some time. For more i

request 25.6k Jan 4, 2023
Ajax for Node.js and browsers (JS HTTP client)

superagent Small progressive client-side HTTP request library, and Node.js module with the same API, supporting many high-level HTTP client features T

Sloth 16.2k Jan 1, 2023
A full-featured http proxy for node.js

node-http-proxy node-http-proxy is an HTTP programmable proxying library that supports websockets. It is suitable for implementing components such as

http ... PARTY! 13.1k Jan 3, 2023
HTTP server mocking and expectations library for Node.js

Nock HTTP server mocking and expectations library for Node.js Nock can be used to test modules that perform HTTP requests in isolation. For instance,

Nock 11.9k Jan 3, 2023
🌐 Human-friendly and powerful HTTP request library for Node.js

Sindre's open source work is supported by the community. Special thanks to: Human-friendly and powerful HTTP request library for Node.js Moving from R

Sindre Sorhus 12.5k Jan 9, 2023
HTTP Client Utilities

@hapi/wreck HTTP client utilities. wreck is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other c

hapi.js 383 Nov 1, 2022
Full-featured, middleware-oriented, programmatic HTTP and WebSocket proxy for node.js

rocky A multipurpose, full-featured, middleware-oriented and hackable HTTP/S and WebSocket proxy with powerful built-in features such as versatile rou

Tom 370 Nov 24, 2022
Simplifies node HTTP request making.

Requestify - Simplifies node HTTP request making. Requestify is a super easy to use and extendable HTTP client for nodeJS + it supports cache (-:. Ins

Ran Mizrahi 222 Nov 28, 2022
Run HTTP over UDP with Node.js

nodejs-httpp - Run HTTP over UDP based transport and Bring Web in Peer or P2P styles main js modules: udt.js, httpp.js, udts.js and httpps.js, that's

AppNet.Link 142 Aug 2, 2022
Global HTTP/HTTPS proxy agent configurable using environment variables.

global-agent Global HTTP/HTTPS proxy configurable using environment variables. Usage Setup proxy using global-agent/bootstrap Setup proxy using bootst

Gajus Kuizinas 267 Dec 20, 2022
An HTTP Web Server for Chrome (chrome.sockets API)

An HTTP Web Server for Chrome (chrome.sockets API)

Kyle Graehl 1.2k Dec 31, 2022
HTTP Client for Visual Studio Code to POST JSON, XML, image, ... files to REST APIs

friflo POST Goal Main goal of this extension is storing all HTTP request & response data automatically as files in a VSCode workspace. This ensures th

Ullrich Praetz 2 Nov 18, 2021
Very very very powerful, extensible http client for both node.js and browser.

ES-Fetch-API 中文 | English Very very very powerful, extensible http client for both node.js and browser. Why should you use ES-Fetch API? Still using a

null 17 Dec 12, 2022