HTTP ETag Generator

1.8.1 · maintenance · verified Tue Apr 21

The `etag` package is a minimalist, RFC 7232-compliant utility for generating HTTP ETags in Node.js applications. It currently maintains a stable version 1.8.1 and has a low release cadence, reflecting its maturity and focused scope. The library supports generating strong ETags for strings and `Buffer`s, and weak ETags by default for `fs.Stats` objects, with an option to override this behavior. Key differentiators include its strict adherence to the HTTP ETag specification, performance optimization for various entity sizes, and a history of robust integration within the `jshttp` ecosystem, often used by popular HTTP frameworks like Express. It prioritizes correctness and efficiency in ETag calculation, having evolved through several hashing algorithm updates to improve security and performance.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates generating strong and weak ETags for string and Buffer entities, illustrating typical usage patterns in Node.js.

import etag from 'etag';

// Scenario 1: Generating a strong ETag for a string entity
const stringBody = '<h1>Hello World!</h1>';
const strongStringEtag = etag(stringBody);
console.log(`Strong ETag for string: ${strongStringEtag}`);

// Scenario 2: Generating a weak ETag for a string entity
const weakStringEtag = etag(stringBody, { weak: true });
console.log(`Weak ETag for string: ${weakStringEtag}`);

// Scenario 3: Generating a strong ETag for a Buffer entity
const bufferBody = Buffer.from('<p>This is a buffered response.</p>');
const strongBufferEtag = etag(bufferBody);
console.log(`Strong ETag for buffer: ${strongBufferEtag}`);

// Scenario 4: Generating a weak ETag for a Buffer entity
const weakBufferEtag = etag(bufferBody, { weak: true });
console.log(`Weak ETag for buffer: ${weakBufferEtag}`);

// Scenario 5: How it's typically used in an HTTP response (conceptual)
// (Imagine `res` is an HTTP response object in a web framework)
// const res = {
//   setHeader: (name, value) => console.log(`Setting Header: ${name}: ${value}`)
// };
// const responseBody = 'Some dynamic content';
// res.setHeader('ETag', etag(responseBody));
// console.log("Header set for responseBody.");

view raw JSON →