Skip to content

Commit

Permalink
Standardizing Map & LRU keys as hash() results of the original …
Browse files Browse the repository at this point in the history
…values now using the const `delimiter`
  • Loading branch information
avoidwork committed Jul 1, 2016
1 parent 2072001 commit 5f234d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions lib/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const defer = require("tiny-defer"),
utility = require(path.join(__dirname, "utility.js")),
all = require(path.join(__dirname, "all.js")),
allMethod = all.toUpperCase(),
delimiter = ":",
head = /^(HEAD|OPTIONS)$/,
space = /\s+/,
methods = ["DELETE", "GET", "POST", "PUT", "PATCH"];
Expand Down Expand Up @@ -39,30 +40,31 @@ class Woodland {
}

allows (uri, host, override = false) {
let result = !override ? this.permissions.get(host + "_" + uri) : undefined;
const key = this.hash(host + delimiter + uri);
let result = !override ? this.permissions.get(key) : undefined;

if (override || !result) {
result = methods.filter(i => this.allowed(i, uri, host, override)).join(", ").replace("GET", "GET, HEAD, OPTIONS");
this.permissions.set(host + "_" + uri, result);
this.permissions.set(key, result);
}

return result;
}

blacklist (fn) {
let hfn;
let key;

if (fn.hash) {
hfn = fn.hash;
key = fn.hash;
} else {
hfn = fn.hash = this.hash(fn.toString());
key = fn.hash = this.hash(fn.toString());
}

if (!this.blacklisted.has(hfn)) {
this.blacklisted.add(hfn);
if (!this.blacklisted.has(key)) {
this.blacklisted.add(key);
}

return hfn;
return this;
}

decorate (req, res) {
Expand Down Expand Up @@ -197,8 +199,8 @@ class Woodland {
}

routes (uri, host, method, override = false) {
let id = method + ":" + host + ":" + uri,
cached = !override ? this.cache.get(id) : undefined,
let key = this.hash(method + delimiter + host + delimiter + uri),
cached = !override ? this.cache.get(key) : undefined,
allMap, hostMap, result;

if (cached) {
Expand Down Expand Up @@ -226,7 +228,7 @@ class Woodland {
}
});

this.cache.set(id, result);
this.cache.set(key, result);
}

return result;
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": "woodland",
"version": "1.0.11",
"version": "1.0.12",
"description": "Lightweight HTTP/HTTPS router with virtual hosts",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 5f234d9

Please sign in to comment.