What is etag?
The etag npm package is used to generate HTTP ETags, which are typically used for cache validation. It can generate ETags based on the contents of a file or a buffer, a string, or based on a specified entity and its attributes.
What are etag's main functionalities?
Generating ETags from a file
This code sample demonstrates how to generate an ETag for the contents of a file. It reads the file 'example.txt' and uses the etag function to create an ETag based on the file's contents.
const etag = require('etag');
const fs = require('fs');
fs.readFile('example.txt', function(err, data) {
if (err) throw err;
const eTagValue = etag(data);
console.log(eTagValue);
});
Generating ETags from a string
This code sample shows how to generate an ETag from a string. The etag function is called with a string 'Hello, World!' to produce an ETag for that specific string.
const etag = require('etag');
const entity = 'Hello, World!';
const eTagValue = etag(entity);
console.log(eTagValue);
Generating ETags with options
This example illustrates how to generate a weak ETag by passing an options object to the etag function. The options object specifies that the ETag should be weak, which is indicated by a 'W/' prefix in the ETag value.
const etag = require('etag');
const entity = Buffer.from('Hello, World!');
const options = { weak: true };
const eTagValue = etag(entity, options);
console.log(eTagValue);
Other packages similar to etag
fresh
The 'fresh' package is used to check if the HTTP response is still 'fresh' on the client side. It is similar to etag in that it deals with HTTP caching mechanisms, but it focuses on cache validation rather than ETag generation.
cacheable-response
The 'cacheable-response' package is a higher-level abstraction for creating cacheable responses in Node.js servers. It uses ETags among other headers to manage caching. It provides a more comprehensive solution compared to etag, which is focused solely on ETag generation.
etag
Create simple ETags
Installation
$ npm install etag
API
var etag = require('etag')
etag(entity, [options])
Generate a strong ETag for the given entity. This should be the complete
body of the entity. Strings, Buffer
s, and fs.Stats
are accepted. By
default, a strong ETag is generated except for fs.Stats
, which will
generate a weak ETag (this can be overwritten by options.weak
).
res.setHeader('ETag', etag(body))
Options
etag
accepts these properties in the options object.
weak
Specifies if a "strong" or a "weak" ETag will be generated. The ETag can only
really be a strong as the given input.
Testing
$ npm test
Benchmark
$ npm run-script bench
> etag@1.2.0 bench nodejs-etag
> node benchmark/index.js
> node benchmark/body0-100b.js
100B body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 518,895 ops/sec ±1.71% (185 runs sampled)
* buffer - weak x 1,917,975 ops/sec ±0.34% (195 runs sampled)
string - strong x 245,251 ops/sec ±0.90% (190 runs sampled)
string - weak x 442,232 ops/sec ±0.21% (196 runs sampled)
> node benchmark/body1-1kb.js
1KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 309,748 ops/sec ±0.99% (191 runs sampled)
* buffer - weak x 352,402 ops/sec ±0.20% (198 runs sampled)
string - strong x 159,058 ops/sec ±1.83% (191 runs sampled)
string - weak x 184,052 ops/sec ±1.30% (189 runs sampled)
> node benchmark/body2-5kb.js
5KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
* buffer - strong x 110,157 ops/sec ±0.60% (194 runs sampled)
* buffer - weak x 111,333 ops/sec ±0.67% (194 runs sampled)
string - strong x 62,091 ops/sec ±3.92% (186 runs sampled)
string - weak x 60,681 ops/sec ±3.98% (186 runs sampled)
> node benchmark/body3-10kb.js
10KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
* buffer - strong x 61,843 ops/sec ±0.44% (197 runs sampled)
* buffer - weak x 61,687 ops/sec ±0.52% (197 runs sampled)
string - strong x 41,377 ops/sec ±3.33% (189 runs sampled)
string - weak x 41,368 ops/sec ±3.29% (190 runs sampled)
> node benchmark/body4-100kb.js
100KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
* buffer - strong x 6,874 ops/sec ±0.17% (198 runs sampled)
* buffer - weak x 6,880 ops/sec ±0.15% (198 runs sampled)
string - strong x 5,382 ops/sec ±2.17% (192 runs sampled)
string - weak x 5,361 ops/sec ±2.23% (192 runs sampled)
License
MIT