Security News
JavaScript Leaders Demand Oracle Release the JavaScript Trademark
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
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.
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);
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.
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.
Create simple ETags
$ npm install etag
var etag = require('etag')
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
).
res.setHeader('ETag', etag(body))
etag
accepts these properties in the options object.
Specifies if a "strong" or a "weak" ETag will be generated. The ETag can only really be a strong as the given input.
$ npm test
$ npm run-script bench
> etag@1.6.0 bench nodejs-etag
> node benchmark/index.js
http_parser@1.0
node@0.10.33
v8@3.14.5.9
ares@1.9.0-DEV
uv@0.10.29
zlib@1.2.3
modules@11
openssl@1.0.1j
> node benchmark/body0-100b.js
100B body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 425,007 ops/sec ±1.47% (184 runs sampled)
* buffer - weak x 1,009,859 ops/sec ±0.18% (197 runs sampled)
string - strong x 442,096 ops/sec ±1.20% (181 runs sampled)
string - weak x 325,063 ops/sec ±0.31% (192 runs sampled)
> node benchmark/body1-1kb.js
1KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 263,069 ops/sec ±1.60% (190 runs sampled)
* buffer - weak x 295,732 ops/sec ±0.43% (199 runs sampled)
string - strong x 274,822 ops/sec ±1.15% (191 runs sampled)
string - weak x 169,473 ops/sec ±1.59% (194 runs sampled)
> node benchmark/body2-5kb.js
5KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 104,299 ops/sec ±0.60% (193 runs sampled)
* buffer - weak x 108,126 ops/sec ±0.65% (196 runs sampled)
string - strong x 101,736 ops/sec ±0.78% (194 runs sampled)
string - weak x 101,266 ops/sec ±0.85% (192 runs sampled)
> node benchmark/body3-10kb.js
10KB body
1 test completed.
2 tests completed.
3 tests completed.
4 tests completed.
buffer - strong x 59,007 ops/sec ±0.29% (198 runs sampled)
* buffer - weak x 60,968 ops/sec ±0.48% (197 runs sampled)
string - strong x 51,873 ops/sec ±1.78% (178 runs sampled)
string - weak x 52,307 ops/sec ±2.63% (193 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,712 ops/sec ±0.11% (198 runs sampled)
* buffer - weak x 6,716 ops/sec ±0.50% (196 runs sampled)
string - strong x 6,397 ops/sec ±0.36% (196 runs sampled)
string - weak x 6,635 ops/sec ±0.15% (198 runs sampled)
FAQs
Create simple HTTP ETags
We found that etag demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
In an open letter, JavaScript community leaders urge Oracle to give up the JavaScript trademark, arguing that it has been effectively abandoned through nonuse.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.