Skip to content

Commit

Permalink
Fixing what triggers a cache update such that the middleware might mi…
Browse files Browse the repository at this point in the history
…ss the first time, fixing cached `timestamp` over the lifespan of the `cache` item
  • Loading branch information
avoidwork committed Jul 6, 2016
1 parent e8e024f commit 49fc4c7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ Creates a hash of `arg`, used by `create()`
Middleware to be used by an http framework

##### register (url, state)
Registers a state in the cache
Adds `url` to the `cache`

##### unregister (url)
Removes `url` from the `cache`

## License
Copyright (c) 2016 Jason Mulligan
Expand Down
11 changes: 8 additions & 3 deletions lib/etag.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const lru = require("tiny-lru"),
implicitGet: /^(HEAD|GET|OPTIONS)$/,
explicitGet: /^GET$/i,
etag: /ETag:\s/i,
invalid: /^(a|cache|connection|content-(d|e|l|m|r)|d|ex|l|p|r|s|t|u|v|w|x)/
invalid: /^(a|cache|connection|content-(d|e|l|m|r)|d|ex|l|p|r|s|t|u|v|w|x)/,
valid: /^(200|304)$/
};

function clone (arg) {
Expand Down Expand Up @@ -45,7 +46,7 @@ class ETag {
headers;

res.on("finish", () => {
if (implicit && res.statusCode === 200 && regex.etag.test(res._header)) {
if (implicit && regex.valid.test(res.statusCode) && regex.etag.test(res._header)) {
let lheaders = {};

trim(res._header).split(/\r\n/).forEach(row => {
Expand All @@ -59,7 +60,7 @@ class ETag {
obj.register(parsed.href, {
etag: lheaders.etag,
headers: lheaders,
timestamp: parseInt(new Date().getTime() / 1000, 10)
timestamp: cached ? cached.timestamp : parseInt(new Date().getTime() / 1000, 10)
});
}
});
Expand Down Expand Up @@ -95,6 +96,10 @@ class ETag {

return this;
}

unregister (uri) {
this.cache.remove(this.hash(uri));
}
}

module.exports = ETag;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tiny-etag",
"version": "1.0.10",
"version": "1.0.11",
"description": "ETag middleware",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 49fc4c7

Please sign in to comment.