Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
The 'fresh' npm package is a HTTP response freshness testing library. It is used to determine whether the response is still 'fresh' in the context of HTTP caching. It checks the request and response headers to decide if the response should be re-used or if a new one needs to be generated.
Freshness Checking
This feature allows you to check if the HTTP response is still fresh by comparing the request and response headers. If the function returns true, the response is considered fresh.
const fresh = require('fresh');
const reqHeaders = { 'if-none-match': 'some-etag' };
const resHeaders = { 'etag': 'some-etag' };
const isFresh = fresh(reqHeaders, resHeaders);
console.log(isFresh); // true or false
The 'etag' package is used to generate HTTP ETags, which are typically used in HTTP headers to determine change in content at a given URL. While 'fresh' is used to check the freshness of responses, 'etag' helps in generating the identifiers that 'fresh' would use for its comparisons.
The 'cache-control' package is used for parsing and formatting HTTP 'Cache-Control' headers. Unlike 'fresh', which checks for response freshness, 'cache-control' is more focused on providing utilities for working with the 'Cache-Control' header directly.
HTTP response freshness testing
This is a Node.js module available through the
npm registry. Installation is done using the
npm install
command:
$ npm install fresh
var fresh = require('fresh')
Check freshness of the response using request and response headers.
When the response is still "fresh" in the client's cache true
is
returned, otherwise false
is returned to indicate that the client
cache is now stale and the full response should be sent.
When a client sends the Cache-Control: no-cache
request header to
indicate an end-to-end reload request, this module will return false
to make handling these requests transparent.
This module is designed to only follow the HTTP specifications, not to work-around all kinda of client bugs (especially since this module typically does not recieve enough information to understand what the client actually is).
There is a known issue that in certain versions of Safari, Safari will incorrectly make a request that allows this module to validate freshness of the resource even when Safari does not have a representation of the resource in the cache. The module jumanji can be used in an Express application to work-around this issue and also provides links to further reading on this Safari bug.
var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { 'etag': '"bar"' }
fresh(reqHeaders, resHeaders)
// => false
var reqHeaders = { 'if-none-match': '"foo"' }
var resHeaders = { 'etag': '"foo"' }
fresh(reqHeaders, resHeaders)
// => true
var fresh = require('fresh')
var http = require('http')
var server = http.createServer(function (req, res) {
// perform server logic
// ... including adding ETag / Last-Modified response headers
if (isFresh(req, res)) {
// client has a fresh copy of resource
res.statusCode = 304
res.end()
return
}
// send the resource
res.statusCode = 200
res.end('hello, world!')
})
function isFresh (req, res) {
return fresh(req.headers, {
'etag': res.getHeader('ETag'),
'last-modified': res.getHeader('Last-Modified')
})
}
server.listen(3000)
FAQs
HTTP response freshness testing
The npm package fresh receives a total of 22,697,681 weekly downloads. As such, fresh popularity was classified as popular.
We found that fresh 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.