Skip to content
jshttpPublic

About

Create simple HTTP ETags

Topics

Resources

Code of conduct

Security policy

Stars

265 stars

Watchers

10 watching

Forks

Repository files navigation

etag

NPM Version NPM Downloads Node.js Version Build Status Coverage Status

Create simple HTTP ETags.

This module generates HTTP ETags for use in HTTP responses. See RFC 9110, Section 8.8.3.

Installation

This is a Node.js module available through the npm registry. Installation is done using the npm install command:

$ npm install etag

API

import { etag } from "etag";

etag(entity, [options])

Generate an ETag for a string, ArrayBuffer view (a typed array such as Buffer, or a DataView), or file stats object. Only the bytes within the view are hashed. File stats can come from fs.stat or fs.statSync, with or without { bigint: true }. Strings and views generate strong ETags by default. File stats generate weak ETags by default. Use options.weak to override the default.

res.setHeader("ETag", etag(body));

Note: Use the same bigint setting when using fs.stat. Node.js can round modification times differently in the two modes, so the same file can produce different ETags.

Which bytes to hash

Hash the full selected representation as it would be sent in a 200 OK response. Use distinct strong ETags for different content codings, such as gzip and identity. HTTP chunk framing does not affect the ETag. A 206 Partial Content response uses the ETag of the full representation. See RFC 9110, Section 8.8.3.3 and Section 15.3.7.

Strings are hashed as UTF-8. If you send a string using another encoding, encode it once and use the same buffer for hashing and sending.

const body = Buffer.from("café", "latin1");
res.setHeader("ETag", etag(body));
res.end(body);

Options

etag accepts these properties in the options object.

weak

Specifies whether the generated ETag includes the weak validator mark (the leading W/). The actual entity tag is the same. The default is true for file stats and false for strings and ArrayBuffer views.

License

MIT

About

Create simple HTTP ETags

Topics

Resources

Code of conduct

Security policy

Stars

265 stars

Watchers

10 watching

Forks

Releases

Packages

Used by

Contributors

Languages