Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
connect-dyncache
Advanced tools
Connect middleware to enable intelligent cache headers for dynamic content
NodeJS is connect middleware to make it easy to add ETag or Last-Modified headers to your dyanmic resources to allow browsers to cache responses.
var app = require('express').createServer();
app.use(require('connect-dyncache')());
app.get('/page', function(req, res) {
res.autoEtag();
res.send("Some content that is generated based on state");
});
app.get('/page2', function(req, res) {
res.setEtag("Some well-known value");
res.send("Some large content that we have a pre-defined etag we can use for (such as a revision id or something)");
});
app.get('/page3', function(req, res) {
res.setLastModifiedDate(new Date("Thu Feb 21 2013 21:46:00"));
res.send("Some large content generated by something that we can track with a modified date, such as a db record");
});
... and on the other side of the TCP connection ...
HTTP/1.0 304 Not Modified
Content-Length: 0
ETag headers basically give the client something to identify this version of the document with. Using it with dynamic data like this all the processing still happens on the server, but you can save a lot of bandwidth (and speed up the client experience) by supporting ETag or Last-Modified date caching.
This middeware adds three methods for caching; you can use res.autoEtag()
anywhere before the first data is sent to the client to
have it generate an etag using an md5 hash of your page. If you have some identifier that can be used to uniquely identify this version
of the page you can set that using res.setEtag(etag)
, and if you have a modified date that you can use you can set that with
res.setLastModifiedDate()
.
res.autoEtag()
Call this function to have the response object automatically calculate the md5 sum of your response and use that as the ETag. This is probably the easiest method to use.
res.setEtag()
If you already have something that is a valid etag (something that will absolutely change if the document ever changes)
then you can use it here in this call to setEtag. If the ETag provided by the page matches this one then this method will return false.
If it does, you can stop processing and just call res.end()
.
res.setLastModifiedDate(date)
If you know when the last time the current page was modified, such as if the core database table the page is generated from
has a modified date on it, you can use that here either in place of or in addition to the ETag. If the client already has
a cached copy of the page based on the date they provided, this method will return false. If it does, you can stop processing
and just call res.end()
.
...cachify - middleware to help with caching static resources. ...etagify - a different approach to etag caching for pages that don't change during the lifetime of the process.
I created this because I needed it; feel free to extend it with examples, fix bugs, and add functionality through pull requests!
Copyright (c) 2013, Richard Bateman <taxilian@gmail.com>
You can use this however you want. I disavow responsibility for
*anything* that occurs as a result of using this, whether good
or bad, including but not limited to: Correct caching behavior,
incorrect caching behavior, promotion and/or positive change of
employment, downsizing and/or negative change of employment,
terrorism, philanthropism, sleep deprivation, sleep apnia,
excessive spam, marital problems, weddings, funerals, unexpected
lottery winnings, uncomfortable social situations, new facebook
friends, increase in website speed, decrease in website speed,
groupies, guppies, puppies, or loss of sales.
Use at your own risk.
FAQs
Connect middleware to enable intelligent cache headers for dynamic content
We found that connect-dyncache 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.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
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.