Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

etag

jshttp98.1mMIT1.8.1TypeScript support: definitely-typed

Create simple HTTP ETags

etag, http, res

readme

etag

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

Create simple HTTP ETags

This module generates HTTP ETags (as defined in RFC 7232) for use in HTTP responses.

Installation

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

$ 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, Buffers, 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 the generated ETag will include the weak validator mark (that is, the leading W/). The actual entity tag is the same. The default value is false, unless the entity is fs.Stats, in which case it is true.

Testing

$ npm test

Benchmark

$ npm run-script bench

> etag@1.8.1 bench nodejs-etag
> node benchmark/index.js

  http_parser@2.7.0
  node@6.11.1
  v8@5.1.281.103
  uv@1.11.0
  zlib@1.2.11
  ares@1.10.1-DEV
  icu@58.2
  modules@48
  openssl@1.0.2k

> node benchmark/body0-100b.js

  100B body

  4 tests completed.

  buffer - strong x 258,647 ops/sec ±1.07% (180 runs sampled)
  buffer - weak   x 263,812 ops/sec ±0.61% (184 runs sampled)
  string - strong x 259,955 ops/sec ±1.19% (185 runs sampled)
  string - weak   x 264,356 ops/sec ±1.09% (184 runs sampled)

> node benchmark/body1-1kb.js

  1KB body

  4 tests completed.

  buffer - strong x 189,018 ops/sec ±1.12% (182 runs sampled)
  buffer - weak   x 190,586 ops/sec ±0.81% (186 runs sampled)
  string - strong x 144,272 ops/sec ±0.96% (188 runs sampled)
  string - weak   x 145,380 ops/sec ±1.43% (187 runs sampled)

> node benchmark/body2-5kb.js

  5KB body

  4 tests completed.

  buffer - strong x 92,435 ops/sec ±0.42% (188 runs sampled)
  buffer - weak   x 92,373 ops/sec ±0.58% (189 runs sampled)
  string - strong x 48,850 ops/sec ±0.56% (186 runs sampled)
  string - weak   x 49,380 ops/sec ±0.56% (190 runs sampled)

> node benchmark/body3-10kb.js

  10KB body

  4 tests completed.

  buffer - strong x 55,989 ops/sec ±0.93% (188 runs sampled)
  buffer - weak   x 56,148 ops/sec ±0.55% (190 runs sampled)
  string - strong x 27,345 ops/sec ±0.43% (188 runs sampled)
  string - weak   x 27,496 ops/sec ±0.45% (190 runs sampled)

> node benchmark/body4-100kb.js

  100KB body

  4 tests completed.

  buffer - strong x 7,083 ops/sec ±0.22% (190 runs sampled)
  buffer - weak   x 7,115 ops/sec ±0.26% (191 runs sampled)
  string - strong x 3,068 ops/sec ±0.34% (190 runs sampled)
  string - weak   x 3,096 ops/sec ±0.35% (190 runs sampled)

> node benchmark/stats.js

  stat

  4 tests completed.

  real - strong x 871,642 ops/sec ±0.34% (189 runs sampled)
  real - weak   x 867,613 ops/sec ±0.39% (190 runs sampled)
  fake - strong x 401,051 ops/sec ±0.40% (189 runs sampled)
  fake - weak   x 400,100 ops/sec ±0.47% (188 runs sampled)

License

MIT

changelog

1.8.1 / 2017-09-12

  • perf: replace regular expression with substring

1.8.0 / 2017-02-18

  • Use SHA1 instead of MD5 for ETag hashing
    • Improves performance for larger entities
    • Works with FIPS 140-2 OpenSSL configuration

1.7.0 / 2015-06-08

  • Always include entity length in ETags for hash length extensions
  • Generate non-Stats ETags using MD5 only (no longer CRC32)
  • Improve stat performance by removing hashing
  • Remove base64 padding in ETags to shorten
  • Use MD5 instead of MD4 in weak ETags over 1KB

1.6.0 / 2015-05-10

  • Improve support for JXcore
  • Remove requirement of atime in the stats object
  • Support "fake" stats objects in environments without fs

1.5.1 / 2014-11-19

1.5.0 / 2014-10-14

  • Improve string performance
  • Slightly improve speed for weak ETags over 1KB

1.4.0 / 2014-09-21

  • Support "fake" stats objects
  • Support Node.js 0.6

1.3.1 / 2014-09-14

  • Use the (new and improved) crc for crc32

1.3.0 / 2014-08-29

  • Default strings to strong ETags
  • Improve speed for weak ETags over 1KB

1.2.1 / 2014-08-29

  • Use the (much faster) buffer-crc32 for crc32

1.2.0 / 2014-08-24

  • Add support for file stat objects

1.1.0 / 2014-08-24

  • Add fast-path for empty entity
  • Add weak ETag generation
  • Shrink size of generated ETags

1.0.1 / 2014-08-24

  • Fix behavior of string containing Unicode

1.0.0 / 2014-05-18

  • Initial release