Skip to content

Commit

Permalink
Simplifying middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jul 4, 2016
1 parent b5bc637 commit 802010d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 13 deletions.
19 changes: 7 additions & 12 deletions lib/etag.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class ETag {
const parsed = parse(req),
implicit = regex.implicitGet.test(req.method);

let cached, headers;
let cached = obj.cache.get(obj.hash(parsed.href)),
headers;

res.on("finish", () => {
if (implicit && !cached && res.statusCode < 400 && regex.etag.test(res._header)) {
Expand All @@ -57,17 +58,11 @@ class ETag {
}
});

if (implicit && !req.headers.range && req.headers["if-none-match"] !== undefined) {
cached = obj.cache.get(obj.hash(parsed.href));

if (regex.explicitGet.test(req.method) && cached && (req.headers["if-none-match"] || "") === cached.etag) {
headers = clone(cached.headers);
headers.age = parseInt(new Date().getTime() / 1000 - cached.timestamp, 10);
res.writeHead(304, headers);
res.end("");
} else {
next();
}
if (regex.explicitGet.test(req.method) && cached && !req.headers.range && (req.headers["if-none-match"] || "") === cached.etag) {
headers = clone(cached.headers);
headers.age = parseInt(new Date().getTime() / 1000 - cached.timestamp, 10);
res.writeHead(304, headers);
res.end("");
} else {
next();
}
Expand Down
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.7",
"version": "1.0.8",
"description": "ETag middleware",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 802010d

Please sign in to comment.