Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

request-etag

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

request-etag

Small, ETag-based, HTTP-response-caching module

  • 2.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

request-etag Build Status

Small, in-memory, ETag-based, HTTP-response-caching module. It is based on the two following popular NPM packages,

  • lru-cache: a cache object that deletes the least-recently-used items.
  • request: a simplified HTTP request client. This is used by default, but can be overridden in the request-etag constructor.

Usage

Configuration,

var cacheConfig = {
	max: 10 * 1024 * 1024
};

A default length function is injected into the cache object by request-etag. It returns the sum of the length of the key, and the length of the value as JSON. More details on the cache configuration options can be found on the lru-cache webpage.

var ETagRequest = require('request-etag');
var eTagRequest = new ETagRequest(cacheConfig);

The ETagRequest constructor takes a second optional argument which specifies the underlying HTTP request client to use. It defaults to request, so the above constructor call is equivalent to,

var eTagRequest = new ETagRequest(cacheConfig, require('request'));

Note that the signature of the underlying HTTP request client function MUST be the same as that of request (e.g. requestretry).

The first GET request will be sent without an If-None-Match header, and its response will contain a body.

eTagRequest('www.immutablepage.com', function (error, response, body) {
	if (!error && response.statusCode === 200) {
		console.log('Received 200 - body retrieved from response.');
		console.log(body);
	}
	if (!error && response.statusCode === 304) {
		console.log('Received 304 - body retrieved from cache.')
		console.log(body);
	}
});

Subsequent GET requests to the same URL will be sent with an If-None-Match header. The response code will be 304, and the response will not contain a body. However a body will be passed by the cache to the body parameter of the callback function.

Why is my request not cached?

Situations where the request response is not cached include,

  • the request is not a GET request,
  • The response has no entity tag in the header,
  • the request is sent with a non-empty cookie header,
  • the response body is bigger than the cache.

Contributing

Please run the following commands before submitting a pull-request,

npm run lint
npm run code-style
npm run test

Keywords

FAQs

Package last updated on 18 Jan 2016

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc