Skip to content

Commit

Permalink
Removing unneeded files, reverting Reflect.ownKeys() to `Object.key…
Browse files Browse the repository at this point in the history
…s()` because it doesn't need symbols included
  • Loading branch information
avoidwork committed Nov 6, 2017
1 parent e832c1d commit 8b9647e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 35 deletions.
13 changes: 0 additions & 13 deletions lib/regex.js

This file was deleted.

6 changes: 0 additions & 6 deletions lib/utility.js

This file was deleted.

39 changes: 25 additions & 14 deletions lib/woodland.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,29 @@

const tcoerce = require("tiny-coerce"),
lru = require("tiny-lru"),
path = require("path"),
http = require("http"),
parse = require("tiny-parse"),
mmh3 = require("murmurhash3js").x86.hash32,
regex = require(path.join(__dirname, "regex.js")),
utility = require(path.join(__dirname, "utility.js")),
all = "all",
delimiter = ":",
methods = http.METHODS;
methods = http.METHODS,
regex = {
braceLeft: /\(/,
hasGet: /GET/,
hasParam: /\/:(\w*)/,
isHead: /^HEAD$/,
isParam: /^:/,
isOptions: /^OPTIONS$/,
slashEnd: /\/$/,
slashStart: /^\//
};

class Woodland {
constructor (defaultHeaders, cacheSize, seed, coerce) {
this.blacklisted = new Set();
this.cache = lru(cacheSize);
this.coerce = coerce;
this.defaultHeaders = Reflect.ownKeys(defaultHeaders).map(key => [key.toLowerCase(), defaultHeaders[key]]);
this.defaultHeaders = Object.keys(defaultHeaders).map(key => [key.toLowerCase(), defaultHeaders[key]]);
this.permissions = lru(cacheSize);
this.middleware = new Map();
this.seed = seed;
Expand Down Expand Up @@ -70,7 +77,7 @@ class Woodland {
req.query = parsed.query;
req.host = req.parsed.hostname;
req.allow = this.allows(req.parsed.pathname);
req.cors = req.headers.origin !== void 0 && utility.schemeless(req.headers.host) !== utility.schemeless(req.headers.origin);
req.cors = req.headers.origin !== void 0 && this.schemeless(req.headers.host) !== this.schemeless(req.headers.origin);

// CORS handling
if (req.cors === true) {
Expand All @@ -80,7 +87,7 @@ class Woodland {
res.header("access-control-allow-credentials", "true");

if (headers !== void 0) {
res.header(regex.options.test(req.method) ? "access-control-allow-headers" : "access-control-expose-headers", headers);
res.header(regex.isOptions.test(req.method) ? "access-control-allow-headers" : "access-control-expose-headers", headers);
}

if (req.allow !== "") {
Expand Down Expand Up @@ -133,7 +140,7 @@ class Woodland {
onfinish () {}

params (req, pos = []) {
const uri = req.parsed.path.replace(regex.startSlash, "").replace(regex.endSlash, "").split("/");
const uri = req.parsed.path.replace(regex.slashEnd, "").replace(regex.slashEnd, "").split("/");

pos.forEach(i => {
req.params[i[1]] = tcoerce(uri[i[0]]);
Expand All @@ -142,7 +149,7 @@ class Woodland {

route (req, res) {
return new Promise((resolve, reject) => {
let method = regex.head.test(req.method) ? "GET" : req.method,
let method = regex.isHead.test(req.method) ? "GET" : req.method,
middleware, result;

const last = err => {
Expand Down Expand Up @@ -203,7 +210,7 @@ class Woodland {
this.decorate(req, res);
this.onconnect(req, res);

if (regex.options.test(method) === true && this.allowed(method, req.parsed.pathname) === false) {
if (regex.isOptions.test(method) === true && this.allowed(method, req.parsed.pathname) === false) {
method = "GET"; // Changing an OPTIONS request to GET due to absent route
}

Expand Down Expand Up @@ -243,11 +250,11 @@ class Woodland {
let now = false,
valid;

if (regex.hasParam.test(route) === true && regex.leftBrace.test(route) === false && params === false) {
if (regex.hasParam.test(route) === true && regex.braceLeft.test(route) === false && params === false) {
params = true;
now = true;

route.replace(regex.startSlash, "").replace(regex.endSlash, "").split("/").forEach((i, idx) => {
route.replace(regex.slashEnd, "").replace(regex.slashEnd, "").split("/").forEach((i, idx) => {
if (regex.isParam.test(i) === true) {
pos.push([idx, i.replace(regex.isParam, "")]);
}
Expand All @@ -259,7 +266,7 @@ class Woodland {
try {
valid = new RegExp("^" + route + "$", "i").test(uri);
} catch (e) {
valid = new RegExp("^" + utility.escape(route) + "$", "i").test(uri);
valid = new RegExp("^" + route.replace(/[-[\]\/{}()*+?.,\\^$|#]/g, "\\$&") + "$", "i").test(uri);
}

if (now === true && valid === false) {
Expand All @@ -281,6 +288,10 @@ class Woodland {
return result;
}

schemeless (arg) {
return arg.replace(/^.*:\/\//, "");
}

use (...args) {
let rpath = args[0],
fn = args[1],
Expand All @@ -303,7 +314,7 @@ class Woodland {
throw new TypeError("Invalid HTTP method");
}

if (regex.head.test(method) === true) {
if (regex.isHead.test(method) === true) {
throw new TypeError("Cannot set HEAD route, use GET");
}

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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": "3.0.3",
"version": "3.0.4",
"description": "Lightweight HTTP/HTTPS router with automatic `Allow` & `CORS` headers",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 8b9647e

Please sign in to comment.