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 string or fs.Stats
will generate a weak ETag while a Buffer
will generate a strong 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 464,089 ops/sec ±1.38% (185 runs sampled)
buffer - weak x 1,154,033 ops/sec ±0.24% (195 runs sampled)
string - strong x 222,036 ops/sec ±0.98% (188 runs sampled)
string - weak x 374,874 ops/sec ±0.52% (193 runs sampled)
> node benchmark/body1-1kb.js
1KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 293,836 ops/sec ±1.01% (190 runs sampled)
buffer - weak x 300,585 ops/sec ±0.21% (195 runs sampled)
string - strong x 156,011 ops/sec ±2.01% (187 runs sampled)
string - weak x 167,002 ops/sec ±1.28% (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 102,942 ops/sec ±0.76% (193 runs sampled)
buffer - weak x 69,593 ops/sec ±0.39% (193 runs sampled)
string - strong x 58,533 ops/sec ±3.97% (185 runs sampled)
string - weak x 46,991 ops/sec ±3.05% (189 runs sampled)
> node benchmark/body3-10kb.js
10KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 57,475 ops/sec ±0.61% (193 runs sampled)
buffer - weak x 36,124 ops/sec ±0.21% (194 runs sampled)
string - strong x 38,994 ops/sec ±3.31% (187 runs sampled)
string - weak x 27,801 ops/sec ±2.56% (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,593 ops/sec ±0.21% (197 runs sampled)
buffer - weak x 3,719 ops/sec ±0.18% (195 runs sampled)
string - strong x 5,125 ops/sec ±2.24% (191 runs sampled)
string - weak x 3,181 ops/sec ±1.90% (192 runs sampled)
License
MIT