pax_global_header00006660000000000000000000000064131561141610014511gustar00rootroot0000000000000052 comment=9b1e3e41df31cda4080833c187120b91a7ce8327 etag-1.8.1/000077500000000000000000000000001315611416100124405ustar00rootroot00000000000000etag-1.8.1/.eslintignore000066400000000000000000000000261315611416100151410ustar00rootroot00000000000000coverage node_modules etag-1.8.1/.eslintrc000066400000000000000000000000341315611416100142610ustar00rootroot00000000000000{ "extends": "standard" } etag-1.8.1/.gitignore000066400000000000000000000000701315611416100144250ustar00rootroot00000000000000coverage/ node_modules/ npm-debug.log package-lock.json etag-1.8.1/.travis.yml000066400000000000000000000023051315611416100145510ustar00rootroot00000000000000language: node_js node_js: - "0.6" - "0.8" - "0.10" - "0.12" - "1.8" - "2.5" - "3.3" - "4.8" - "5.12" - "6.11" - "7.10" - "8.4" sudo: false dist: precise cache: directories: - node_modules before_install: # Skip updating shrinkwrap / lock - "npm config set shrinkwrap false" # Remove all non-test dependencies - npm rm --save-dev benchmark - npm rm --save-dev beautify-benchmark # Setup Node.js version-specific dependencies - "test $TRAVIS_NODE_VERSION != '0.6' || npm rm --save-dev istanbul" - "test $TRAVIS_NODE_VERSION != '0.8' || npm rm --save-dev istanbul" - "test $(echo $TRAVIS_NODE_VERSION | cut -d. -f1) -ge 4 || npm rm --save-dev $(grep -E '\"eslint\\S*\"' package.json | cut -d'\"' -f2)" # Update Node.js modules - "test ! -d node_modules || npm prune" - "test ! -d node_modules || npm rebuild" script: # Run test script, depending on istanbul install - "test ! -z $(npm -ps ls istanbul) || npm test" - "test -z $(npm -ps ls istanbul) || npm run-script test-travis" - "test -z $(npm -ps ls eslint ) || npm run-script lint" after_script: - "test -e ./coverage/lcov.info && npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" etag-1.8.1/HISTORY.md000066400000000000000000000033041315611416100141230ustar00rootroot000000000000001.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 ================== * deps: crc@3.2.1 - Minor fixes 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 etag-1.8.1/LICENSE000066400000000000000000000021061315611416100134440ustar00rootroot00000000000000(The MIT License) Copyright (c) 2014-2016 Douglas Christopher Wilson Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. etag-1.8.1/README.md000066400000000000000000000101461315611416100137210ustar00rootroot00000000000000# etag [![NPM Version][npm-image]][npm-url] [![NPM Downloads][downloads-image]][downloads-url] [![Node.js Version][node-version-image]][node-version-url] [![Build Status][travis-image]][travis-url] [![Test Coverage][coveralls-image]][coveralls-url] 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](https://nodejs.org/en/) module available through the [npm registry](https://www.npmjs.com/). Installation is done using the [`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally): ```sh $ npm install etag ``` ## API ```js 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`). ```js 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 ```sh $ npm test ``` ## Benchmark ```bash $ 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](LICENSE) [npm-image]: https://img.shields.io/npm/v/etag.svg [npm-url]: https://npmjs.org/package/etag [node-version-image]: https://img.shields.io/node/v/etag.svg [node-version-url]: https://nodejs.org/en/download/ [travis-image]: https://img.shields.io/travis/jshttp/etag/master.svg [travis-url]: https://travis-ci.org/jshttp/etag [coveralls-image]: https://img.shields.io/coveralls/jshttp/etag/master.svg [coveralls-url]: https://coveralls.io/r/jshttp/etag?branch=master [downloads-image]: https://img.shields.io/npm/dm/etag.svg [downloads-url]: https://npmjs.org/package/etag etag-1.8.1/benchmark/000077500000000000000000000000001315611416100143725ustar00rootroot00000000000000etag-1.8.1/benchmark/body0-100b.js000066400000000000000000000023731315611416100164120ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var Buffer = require('safe-buffer').Buffer var seedrandom = require('seedrandom') /** * Globals for benchmark.js */ global.buffer = getbuffer(100) global.etag = require('..') global.string = getbuffer(100).toString() var suite = new benchmark.Suite() suite.add({ name: 'buffer - strong', minSamples: 100, fn: 'var val = etag(buffer, {weak: false})' }) suite.add({ name: 'buffer - weak', minSamples: 100, fn: 'var val = etag(buffer, {weak: true})' }) suite.add({ name: 'string - strong', minSamples: 100, fn: 'var val = etag(string, {weak: false})' }) suite.add({ name: 'string - weak', minSamples: 100, fn: 'var val = etag(string, {weak: true})' }) suite.on('start', function onCycle (event) { process.stdout.write(' 100B body\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function getbuffer (size) { var buffer = Buffer.alloc(size) var rng = seedrandom('body ' + size) for (var i = 0; i < buffer.length; i++) { buffer[i] = (rng() * 94 + 32) | 0 } return buffer } etag-1.8.1/benchmark/body1-1kb.js000066400000000000000000000024041315611416100164210ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var Buffer = require('safe-buffer').Buffer var seedrandom = require('seedrandom') /** * Globals for benchmark.js */ global.buffer = getbuffer(1 * 1000) global.etag = require('..') global.string = getbuffer(1 * 1000).toString() var suite = new benchmark.Suite() suite.add({ name: 'buffer - strong', minSamples: 100, fn: 'var val = etag(buffer, {weak: false})' }) suite.add({ name: 'buffer - weak', minSamples: 100, fn: 'var val = etag(buffer, {weak: true})' }) suite.add({ name: 'string - strong', minSamples: 100, fn: 'var val = etag(string, {weak: false})' }) suite.add({ name: 'string - weak', minSamples: 100, fn: 'var val = etag(string, {weak: true})' }) suite.on('start', function onCycle (event) { process.stdout.write(' 1KB body\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function getbuffer (size) { var buffer = Buffer.alloc(size) var rng = seedrandom('body ' + size) for (var i = 0; i < buffer.length; i++) { buffer[i] = (rng() * 94 + 32) | 0 } return buffer } etag-1.8.1/benchmark/body2-5kb.js000066400000000000000000000024041315611416100164260ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var Buffer = require('safe-buffer').Buffer var seedrandom = require('seedrandom') /** * Globals for benchmark.js */ global.buffer = getbuffer(5 * 1000) global.etag = require('..') global.string = getbuffer(5 * 1000).toString() var suite = new benchmark.Suite() suite.add({ name: 'buffer - strong', minSamples: 100, fn: 'var val = etag(buffer, {weak: false})' }) suite.add({ name: 'buffer - weak', minSamples: 100, fn: 'var val = etag(buffer, {weak: true})' }) suite.add({ name: 'string - strong', minSamples: 100, fn: 'var val = etag(string, {weak: false})' }) suite.add({ name: 'string - weak', minSamples: 100, fn: 'var val = etag(string, {weak: true})' }) suite.on('start', function onCycle (event) { process.stdout.write(' 5KB body\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function getbuffer (size) { var buffer = Buffer.alloc(size) var rng = seedrandom('body ' + size) for (var i = 0; i < buffer.length; i++) { buffer[i] = (rng() * 94 + 32) | 0 } return buffer } etag-1.8.1/benchmark/body3-10kb.js000066400000000000000000000024071315611416100165060ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var Buffer = require('safe-buffer').Buffer var seedrandom = require('seedrandom') /** * Globals for benchmark.js */ global.buffer = getbuffer(10 * 1000) global.etag = require('..') global.string = getbuffer(10 * 1000).toString() var suite = new benchmark.Suite() suite.add({ name: 'buffer - strong', minSamples: 100, fn: 'var val = etag(buffer, {weak: false})' }) suite.add({ name: 'buffer - weak', minSamples: 100, fn: 'var val = etag(buffer, {weak: true})' }) suite.add({ name: 'string - strong', minSamples: 100, fn: 'var val = etag(string, {weak: false})' }) suite.add({ name: 'string - weak', minSamples: 100, fn: 'var val = etag(string, {weak: true})' }) suite.on('start', function onCycle (event) { process.stdout.write(' 10KB body\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function getbuffer (size) { var buffer = Buffer.alloc(size) var rng = seedrandom('body ' + size) for (var i = 0; i < buffer.length; i++) { buffer[i] = (rng() * 94 + 32) | 0 } return buffer } etag-1.8.1/benchmark/body4-100kb.js000066400000000000000000000024121315611416100165630ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var Buffer = require('safe-buffer').Buffer var seedrandom = require('seedrandom') /** * Globals for benchmark.js */ global.buffer = getbuffer(100 * 1000) global.etag = require('..') global.string = getbuffer(100 * 1000).toString() var suite = new benchmark.Suite() suite.add({ name: 'buffer - strong', minSamples: 100, fn: 'var val = etag(buffer, {weak: false})' }) suite.add({ name: 'buffer - weak', minSamples: 100, fn: 'var val = etag(buffer, {weak: true})' }) suite.add({ name: 'string - strong', minSamples: 100, fn: 'var val = etag(string, {weak: false})' }) suite.add({ name: 'string - weak', minSamples: 100, fn: 'var val = etag(string, {weak: true})' }) suite.on('start', function onCycle (event) { process.stdout.write(' 100KB body\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function getbuffer (size) { var buffer = Buffer.alloc(size) var rng = seedrandom('body ' + size) for (var i = 0; i < buffer.length; i++) { buffer[i] = (rng() * 94 + 32) | 0 } return buffer } etag-1.8.1/benchmark/index.js000066400000000000000000000013751315611416100160450ustar00rootroot00000000000000var fs = require('fs') var path = require('path') var spawn = require('child_process').spawn var exe = process.argv[0] var cwd = process.cwd() for (var dep in process.versions) { console.log(' %s@%s', dep, process.versions[dep]) } console.log('') runScripts(fs.readdirSync(__dirname)) function runScripts (fileNames) { var fileName = fileNames.shift() if (!fileName) return if (!/\.js$/i.test(fileName)) return runScripts(fileNames) if (fileName.toLowerCase() === 'index.js') return runScripts(fileNames) var fullPath = path.join(__dirname, fileName) console.log('> %s %s', exe, path.relative(cwd, fullPath)) var proc = spawn(exe, [fullPath], { 'stdio': 'inherit' }) proc.on('exit', function () { runScripts(fileNames) }) } etag-1.8.1/benchmark/stats.js000066400000000000000000000022721315611416100160710ustar00rootroot00000000000000 /** * Module dependencies. */ var benchmark = require('benchmark') var benchmarks = require('beautify-benchmark') var fs = require('fs') /** * Globals for benchmark.js */ global.etag = require('..') global.fakestat = getstat(false) global.realstat = getstat(true) var suite = new benchmark.Suite() suite.add({ name: 'real - strong', minSamples: 100, fn: 'var val = etag(realstat, {weak: false})' }) suite.add({ name: 'real - weak', minSamples: 100, fn: 'var val = etag(realstat, {weak: true})' }) suite.add({ name: 'fake - strong', minSamples: 100, fn: 'var val = etag(fakestat, {weak: false})' }) suite.add({ name: 'fake - weak', minSamples: 100, fn: 'var val = etag(fakestat, {weak: true})' }) suite.on('start', function onCycle (event) { process.stdout.write(' stat\n\n') }) suite.on('cycle', function onCycle (event) { benchmarks.add(event.target) }) suite.on('complete', function onComplete () { benchmarks.log() }) suite.run({async: false}) function getstat (real) { if (real) { return fs.statSync(__filename) } return { ctime: new Date('2014-09-01T14:52:07Z'), mtime: new Date('2014-09-01T14:52:07Z'), ino: 0, size: 3027 } } etag-1.8.1/index.js000066400000000000000000000046571315611416100141210ustar00rootroot00000000000000/*! * etag * Copyright(c) 2014-2016 Douglas Christopher Wilson * MIT Licensed */ 'use strict' /** * Module exports. * @public */ module.exports = etag /** * Module dependencies. * @private */ var crypto = require('crypto') var Stats = require('fs').Stats /** * Module variables. * @private */ var toString = Object.prototype.toString /** * Generate an entity tag. * * @param {Buffer|string} entity * @return {string} * @private */ function entitytag (entity) { if (entity.length === 0) { // fast-path empty return '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"' } // compute hash of entity var hash = crypto .createHash('sha1') .update(entity, 'utf8') .digest('base64') .substring(0, 27) // compute length of entity var len = typeof entity === 'string' ? Buffer.byteLength(entity, 'utf8') : entity.length return '"' + len.toString(16) + '-' + hash + '"' } /** * Create a simple ETag. * * @param {string|Buffer|Stats} entity * @param {object} [options] * @param {boolean} [options.weak] * @return {String} * @public */ function etag (entity, options) { if (entity == null) { throw new TypeError('argument entity is required') } // support fs.Stats object var isStats = isstats(entity) var weak = options && typeof options.weak === 'boolean' ? options.weak : isStats // validate argument if (!isStats && typeof entity !== 'string' && !Buffer.isBuffer(entity)) { throw new TypeError('argument entity must be string, Buffer, or fs.Stats') } // generate entity tag var tag = isStats ? stattag(entity) : entitytag(entity) return weak ? 'W/' + tag : tag } /** * Determine if object is a Stats object. * * @param {object} obj * @return {boolean} * @api private */ function isstats (obj) { // genuine fs.Stats if (typeof Stats === 'function' && obj instanceof Stats) { return true } // quack quack return obj && typeof obj === 'object' && 'ctime' in obj && toString.call(obj.ctime) === '[object Date]' && 'mtime' in obj && toString.call(obj.mtime) === '[object Date]' && 'ino' in obj && typeof obj.ino === 'number' && 'size' in obj && typeof obj.size === 'number' } /** * Generate a tag for a stat. * * @param {object} stat * @return {string} * @private */ function stattag (stat) { var mtime = stat.mtime.getTime().toString(16) var size = stat.size.toString(16) return '"' + size + '-' + mtime + '"' } etag-1.8.1/package.json000066400000000000000000000024321315611416100147270ustar00rootroot00000000000000{ "name": "etag", "description": "Create simple HTTP ETags", "version": "1.8.1", "contributors": [ "Douglas Christopher Wilson ", "David Björklund " ], "license": "MIT", "keywords": [ "etag", "http", "res" ], "repository": "jshttp/etag", "devDependencies": { "beautify-benchmark": "0.2.4", "benchmark": "2.1.4", "eslint": "3.19.0", "eslint-config-standard": "10.2.1", "eslint-plugin-import": "2.7.0", "eslint-plugin-markdown": "1.0.0-beta.6", "eslint-plugin-node": "5.1.1", "eslint-plugin-promise": "3.5.0", "eslint-plugin-standard": "3.0.1", "istanbul": "0.4.5", "mocha": "1.21.5", "safe-buffer": "5.1.1", "seedrandom": "2.4.3" }, "files": [ "LICENSE", "HISTORY.md", "README.md", "index.js" ], "engines": { "node": ">= 0.6" }, "scripts": { "bench": "node benchmark/index.js", "lint": "eslint --plugin markdown --ext js,md .", "test": "mocha --reporter spec --bail --check-leaks test/", "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot --check-leaks test/", "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks test/" } } etag-1.8.1/test/000077500000000000000000000000001315611416100134175ustar00rootroot00000000000000etag-1.8.1/test/.eslintrc000066400000000000000000000000451315611416100152420ustar00rootroot00000000000000{ "env": { "mocha": true } } etag-1.8.1/test/test.js000066400000000000000000000105731315611416100147420ustar00rootroot00000000000000 var assert = require('assert') var Buffer = require('safe-buffer').Buffer var etag = require('..') var fs = require('fs') var seedrandom = require('seedrandom') var buf5kb = getbuffer(5 * 1024) var str5kb = getbuffer(5 * 1024).toString() describe('etag(entity)', function () { it('should require an entity', function () { assert.throws(etag.bind(), /argument entity is required/) }) it('should reject number entities', function () { assert.throws(etag.bind(null, 4), /argument entity must be/) }) describe('when "entity" is a string', function () { it('should generate a strong ETag', function () { assert.equal(etag('beep boop'), '"9-fINXV39R1PCo05OqGqr7KIY9lCE"') }) it('should work containing Unicode', function () { assert.equal(etag('论'), '"3-QkSKq8sXBjHL2tFAZknA2n6LYzM"') assert.equal(etag('论', {weak: true}), 'W/"3-QkSKq8sXBjHL2tFAZknA2n6LYzM"') }) it('should work for empty string', function () { assert.equal(etag(''), '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"') }) }) describe('when "entity" is a Buffer', function () { it('should generate a strong ETag', function () { assert.equal(etag(Buffer.from([1, 2, 3])), '"3-cDeAcZjCKn0rCAc3HXY3eahP388"') }) it('should work for empty Buffer', function () { assert.equal(etag(Buffer.alloc(0)), '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"') }) }) describe('when "entity" is a fs.Stats', function () { it('should generate a weak ETag', function () { assert.ok(isweak(etag(fs.statSync(__filename)))) }) it('should generate consistently', function () { assert.equal(etag(fs.statSync(__filename)), etag(fs.statSync(__filename))) }) }) describe('when "entity" looks like a stats object', function () { it('should generate a weak ETag', function () { var fakeStat = { ctime: new Date('2014-09-01T14:52:07Z'), mtime: new Date('2014-09-01T14:52:07Z'), ino: 0, size: 3027 } assert.equal(etag(fakeStat), 'W/"bd3-14831b399d8"') }) }) describe('with "weak" option', function () { describe('when "false"', function () { it('should generate a strong ETag for a string', function () { assert.equal(etag('', {weak: false}), '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"') assert.equal(etag('beep boop', {weak: false}), '"9-fINXV39R1PCo05OqGqr7KIY9lCE"') assert.equal(etag(str5kb, {weak: false}), '"1400-CH0oWYLQGHe/yDhUrMkMg3fIdVU"') }) it('should generate a strong ETag for a Buffer', function () { assert.equal(etag(Buffer.alloc(0), {weak: false}), '"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"') assert.equal(etag(Buffer.from([1, 2, 3]), {weak: false}), '"3-cDeAcZjCKn0rCAc3HXY3eahP388"') assert.equal(etag(buf5kb, {weak: false}), '"1400-CH0oWYLQGHe/yDhUrMkMg3fIdVU"') }) it('should generate a strong ETag for fs.Stats', function () { assert.ok(!isweak(etag(fs.statSync(__filename), {weak: false}))) }) }) describe('when "true"', function () { it('should generate a weak ETag for a string', function () { assert.equal(etag('', {weak: true}), 'W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"') assert.equal(etag('beep boop', {weak: true}), 'W/"9-fINXV39R1PCo05OqGqr7KIY9lCE"') assert.equal(etag(str5kb, {weak: true}), 'W/"1400-CH0oWYLQGHe/yDhUrMkMg3fIdVU"') }) it('should generate a weak ETag for a Buffer', function () { assert.equal(etag(Buffer.alloc(0), {weak: true}), 'W/"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk"') assert.equal(etag(Buffer.from([1, 2, 3]), {weak: true}), 'W/"3-cDeAcZjCKn0rCAc3HXY3eahP388"') assert.equal(etag(buf5kb, {weak: true}), 'W/"1400-CH0oWYLQGHe/yDhUrMkMg3fIdVU"') }) it('should generate a weak ETag for fs.Stats', function () { assert.ok(isweak(etag(fs.statSync(__filename), {weak: true}))) }) it('should generate different ETags for different strings', function () { assert.notEqual(etag('plumless', {weak: true}), etag('buckeroo', {weak: true})) }) }) }) }) function getbuffer (size) { var buffer = Buffer.alloc(size) var rng = seedrandom('etag test') for (var i = 0; i < buffer.length; i++) { buffer[i] = (rng() * 94 + 32) | 0 } return buffer } function isweak (etag) { var weak = /^(W\/|)"([^"]+)"/.exec(etag) if (weak === null) { throw new Error('invalid ETag: ' + etag) } return weak[1] === 'W/' }