Skip to content

Commit

Permalink
Removing some RegExp
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Jan 4, 2019
1 parent b02181b commit 144d11a
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 21 deletions.
12 changes: 6 additions & 6 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ function parse (req, res, next) {
exception;

if (req.body !== "") {
const type = req.headers["content-type"];
const type = req.headers["content-type"] || "";

if (regex.encodeForm.test(type) === true) {
if (type.includes("application/x-www-form-urlencoded") === true) {
const args = req.body ? retsu.chunk(req.body.split(regex.bodySplit), 2) : [];

req.body = {};
each(args, i => {
req.body[i[0]] = coerce(decodeURIComponent(i[1]));
});
} else if (regex.encodeJson.test(type) === true || regex.jsonWrap.test(req.body) === true) {
} else if (type.includes("application/json") === true || regex.jsonWrap.test(req.body) === true) {
try {
req.body = JSON.parse(req.body);
} catch (e) {
Expand Down Expand Up @@ -138,7 +138,7 @@ function payload (req, res, next) {
function keymaster (req, res) {
// No authentication, or it's already happened
if (req.protect === false || req.protectAsync === false || (req.session !== void 0 && req.isAuthenticated() === true) === true) {
const method = regex.head.test(req.method) ? "GET" : req.method,
const method = req.method === "HEAD" ? "GET" : req.method,
routes = req.server.router.middleware.get(method) || new Map(),
uri = req.parsed.pathname;
let result;
Expand Down Expand Up @@ -170,7 +170,7 @@ function keymaster (req, res) {
} else {
res.send(result);
}
} else if (regex.options.test(method) && req.allow.includes("GET") === true) {
} else if (method === "OPTIONS" && req.allow.includes("GET") === true) {
res.send(STATUS_CODES[204], 204);
} else {
res.error(404);
Expand Down Expand Up @@ -212,7 +212,7 @@ function stream (req, res) {
letag = headers.etag = req.server.etag(req.parsed.pathname, req.file.stats.size, req.file.stats.mtime);
}

if (regex.getOnly.test(req.method) === true) {
if (req.method === "GET") {
if (letag !== void 0 && req.headers["if-none-match"] === letag) {
delete headers["content-length"];
res.send("", 304, headers);
Expand Down
9 changes: 1 addition & 8 deletions lib/regex.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ module.exports = {
collection: /(.*)(\/.*)$/,
def: /deflate/,
dir: /\/$/,
encodeForm: /application\/x-www-form-urlencoded/,
encodeJson: /application\/json/,
head: /^HEAD$/,
"get": /^(GET|HEAD|OPTIONS)$/,
getOnly: /^GET$/,
hasParam: /\/:\w*/,
hasOrderByDesc: /(\?|&)order_by=desc/,
hypermedia: /(([a-z]+(_)?)?id|url|uri)$/i,
Expand All @@ -22,13 +18,10 @@ module.exports = {
leftBrace: /\(/,
mimetype: /;.*/,
modify: /DELETE|PATCH|POST|PUT/,
options: /^OPTIONS$/,
orderBy: /^order_by\=/,
private: /private/,
scheme: /^(\w+\:\/\/)|\//,
trailing: /_.*$/,
trailingS: /s$/,
trailingSlash: /\/$/,
trailingY: /y$/,
unsortable: /boolean|number|string|void 0/
trailingY: /y$/
};
6 changes: 3 additions & 3 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class Tenso extends Base {
}

headers (req, res, status, body, headers) {
if (req.protect === true && regex.private.test(headers["cache-control"]) === false) {
if (req.protect === true && (headers["cache-control"] || "").includes("private") === false) {
headers["cache-control"] = (headers["cache-control"] || res.getHeader("cache-control") || "").replace(/(private|public)(,\s)?/g, "");
headers["cache-control"] = `private${(headers["cache-control"].length > 0 ? ", " : "")}${(headers["cache-control"] || "")}`;
}
Expand All @@ -274,7 +274,7 @@ class Tenso extends Base {
delete headers["last-modified"];
}

if (regex.head.test(req.method) === true) {
if (req.method === "HEAD") {
headers.connection = "close";
}

Expand Down Expand Up @@ -426,7 +426,7 @@ class Tenso extends Base {
result = this.render(req, res, utility.hypermedia(this, req, utility.serialize(req, body, status), headers), headers);
headers["content-length"] = Buffer.byteLength(result);

if (regex.head.test(req.method)) {
if (req.method === "HEAD") {
result = "";
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ function hypermedia (server, req, rep, headers) {

if (exists) {
if (Array.isArray(rep.data)) {
if (regex.getOnly.test(req.method) === true && (rep.status >= 200 && rep.status <= 206)) {
if (req.method === "GET" && (rep.status >= 200 && rep.status <= 206)) {
if (isNaN(page) || page <= 0) {
page = 1;
}
Expand Down Expand Up @@ -623,7 +623,9 @@ function sort (arg, req) {
let output;

if (typeof req.parsed.search === "string" && req.parsed.searchParams.has("order_by") && Array.isArray(arg)) {
if (regex.unsortable.test(typeof arg[0]) === false && arg[0] !== null) {
const type = typeof arg[0];

if (type !== "boolean" && type !== "number" && type !== "string" && type !== "undefined" && arg[0] !== null) {
output = keysort(clone(arg), req.parsed.search.replace("?", "").split("&").filter(i => regex.orderBy.test(i)).reduce((a, b) => [...a, decodeURIComponent(b.replace(regex.orderBy, ""))], []).join(", "));
} else {
// Primitives, regular Array.sort()
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,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is an HTTP/HTTP2 REST API framework",
"version": "10.0.23",
"version": "10.0.24",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down

0 comments on commit 144d11a

Please sign in to comment.