Skip to content

Commit

Permalink
Updating turtle.io, syntax & eslint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Nov 19, 2016
1 parent d56c41f commit 0f4ade4
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 91 deletions.
10 changes: 7 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
"amd": false
},
"ecmaFeatures": {
"jsx": true,
"jsx": false,
"superInFunctions": false,
"classes": false,
"modules": [2]
},
"rules": {
"arrow-parens": [2, "as-needed"],
"arrow-spacing": [2, {"before": true, "after": true}],
"block-scoped-var": [0],
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
"camelcase": [0],
Expand All @@ -31,6 +33,7 @@
"generator-star-spacing": [2, "after"],
"guard-for-in": [0],
"handle-callback-err": [0],
"indent": ["error", "tab", {"VariableDeclarator": {"var": 1, "let": 1, "const": 1}, "SwitchCase": 1}],
"key-spacing": [2, { "beforeColon": false, "afterColon": true }],
"quotes": [2, "double", "avoid-escape"],
"max-depth": [0, 4],
Expand All @@ -41,6 +44,7 @@
"new-parens": [2],
"new-cap": [2, { "capIsNewExceptions": ["ToInteger", "ToObject", "ToPrimitive", "ToUint32"] }],
"newline-after-var": [0],
"newline-before-return": [2],
"no-alert": [2],
"no-array-constructor": [2],
"no-bitwise": [0],
Expand Down Expand Up @@ -125,7 +129,7 @@
"no-underscore-dangle": [0],
"no-unreachable": [2],
"no-unused-expressions": [2],
"no-unused-vars": [1, { "vars": "all", "args": "after-used" }],
"no-unused-vars": [2, { "vars": "all", "args": "after-used" }],
"no-use-before-define": [2],
"no-void": [0],
"no-warning-comments": [0, { "terms": ["todo", "fixme", "xxx"], "location": "start" }],
Expand All @@ -142,7 +146,7 @@
"sort-vars": [0],
"keyword-spacing": [2],
"space-before-function-paren": [2, { "anonymous": "always", "named": "always" }],
"space-before-blocks": [0, "always"],
"space-before-blocks": [2, "always"],
"space-in-brackets": [0, "never", {
"singleValue": true,
"arraysInArrays": false,
Expand Down
4 changes: 3 additions & 1 deletion lib/iterate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const array = require("retsu");

function iterate (obj, fn) {
if (obj instanceof Object) {
Object.keys(obj).forEach(function (i) {
array.each(Object.keys(obj), i => {
fn.call(obj, obj[i], i);
});
} else {
Expand Down
18 changes: 8 additions & 10 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ function asyncFlag (req, res, next) {
}

function bypass (req, res, next) {
let pass = req.server.config.auth.unprotect.filter(function (i) {
return i.test(req.url);
}).length > 0;
let pass = req.server.config.auth.unprotect.filter(i => i.test(req.url)).length > 0;

if (pass) {
req.unprotect = true;
Expand All @@ -74,7 +72,7 @@ function parse (req, res, next) {
args = req.body ? array.chunk(req.body.split(regex.body_split), 2) : [];
req.body = {};

array.each(args, function (i) {
array.each(args, i => {
req.body[i[0]] = coerce(i[1]);
});
}
Expand All @@ -83,7 +81,7 @@ function parse (req, res, next) {
try {
req.body = JSON.parse(req.body);
} catch (e) {
console.warn(e.message);
void 0;
}
}
}
Expand Down Expand Up @@ -111,7 +109,7 @@ function keymaster (req, res, next) {
res.send(result);
}
} else {
iterate(routes, function (value, key) {
iterate(routes, (value, key) => {
if (regex.has_param.test(key) && key.indexOf("(") === -1) {
key = key.replace(/\/:(\w*)/g, "/(.*)");
}
Expand Down Expand Up @@ -150,7 +148,7 @@ function rate (req, res, next) {
results = server.tenso.rate(req, config.override);
good = results.shift();

rateHeaders.forEach(function (i, idx) {
rateHeaders.forEach((i, idx) => {
res.setHeader(i, results[idx]);
});

Expand All @@ -166,8 +164,8 @@ function zuul (req, res, next) {
let uri = req.parsed.path,
protectd = false;

array.each(req.server.config.auth.protect, function (r) {
if (r.test(uri)) {
array.each(req.server.config.auth.protect, i => {
if (i.test(uri)) {
return !(protectd = true);
}

Expand All @@ -178,7 +176,7 @@ function zuul (req, res, next) {
req.protect = protectd;
req.protectAsync = false;

rate(req, res, function (e) {
rate(req, res, e => {
if (e) {
next(e);
} else if (protectd) {
Expand Down
30 changes: 10 additions & 20 deletions lib/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,53 @@ function sanitize (arg) {
let output = arg;

if (typeof arg === "string") {
[["<", "&lt;"], [">", "&gt;"]].forEach(function (i) {
array.each([["<", "&lt;"], [">", "&gt;"]], i => {
output = output.replace(new RegExp(i[0], "g"), i[1]);
});
}

return output;
}

renderers.set("text/csv", function (arg, req, headers) {
renderers.set("text/csv", (arg, req, headers) => {
req.headers.accept = "text/csv";
headers["content-type"] = "text/csv";
headers["content-disposition"] = "attachment; filename=\"" + req.parsed.pathname.replace(/.*\//g, "").replace(/\..*/, "_") + req.parsed.search.replace("?", "").replace(/\&|=/g, "_") + ".csv\"";

return csv.encode(arg);
});

renderers.set("text/html", function (arg, req, headers, tpl) {
renderers.set("text/html", (arg, req, headers, tpl) => {
let protocol = req.headers["x-forwarded-proto"] ? req.headers["x-forwarded-proto"] + ":" : req.parsed.protocol;

return (tpl || "")
.replace(/\{\{title\}\}/g, req.server.config.title)
.replace("{{url}}", req.parsed.href.replace(req.parsed.protocol, protocol))
.replace("{{headers}}", Object.keys(headers).sort(array.sort).map(function (i) {
.replace("{{headers}}", Object.keys(headers).sort(array.sort).map(i => {
return "<tr><td>" + i + "</td><td>" + sanitize(headers[i]) + "</td></tr>";
}).join("\n"))
.replace("{{formats}}", "<option value=''></option>" + Array.from(renderers.keys()).filter(function (i) { return i.indexOf("html") === -1; }).map(function (i) {
.replace("{{formats}}", "<option value=''></option>" + Array.from(renderers.keys()).filter(i => i.indexOf("html") === -1).map(i => {
return "<option value='" + i + "'>" + i.replace(/^.*\//, "").toUpperCase() + "</option>";
}).join("\n"))
.replace("{{body}}", JSON.stringify(arg, null, 2))
.replace("{{year}}", new Date().getFullYear())
.replace("{{version}}", "4.0.0")
.replace("{{allow}}", headers.allow)
.replace("{{methods}}", utility.explode(headers.allow.replace("GET, HEAD, OPTIONS", "")).filter(function (i) {
return i !== "";
}).map(function (i) {
.replace("{{methods}}", utility.explode(headers.allow.replace("GET, HEAD, OPTIONS", "")).filter(i => i !== "").map(i => {
return "<option value='" + i + "'>" + i + "</option>";
}).join("\n"))
.replace("{{csrf}}", headers["x-csrf-token"] || "");
});

renderers.set("application/javascript", function (arg, req, headers) {
renderers.set("application/javascript", (arg, req, headers) => {
req.headers.accept = "application/javascript";
headers["content-type"] = "application/javascript";

return (req.parsed.query.callback || "callback") + "(" + JSON.stringify(arg, null, 0) + ");";
});

renderers.set("application/json", function (arg) {
return arg;
});

renderers.set("application/yaml", function (arg) {
return yaml.stringify(arg, 4);
});

renderers.set("application/xml", function (arg) {
return xml.serialize(arg);
});
renderers.set("application/json", arg => arg);
renderers.set("application/yaml", arg => yaml.stringify(arg, 4));
renderers.set("application/xml", arg => xml.serialize(arg));

module.exports = renderers;
8 changes: 3 additions & 5 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class Tenso {
accepts = utility.explode(req.parsed.query.format || req.headers.accept || format, ","),
renderer;

array.each(accepts, function (i) {
array.each(accepts, i => {
let mimetype = i.replace(regex.mimetype, ""),
found = false;

Expand Down Expand Up @@ -154,7 +154,7 @@ class Tenso {
errz = arg instanceof Error,
result, serializer;

array.each(accepts, function (i) {
array.each(accepts, i => {
let mimetype = i.replace(regex.mimetype, ""),
found = false;

Expand Down Expand Up @@ -184,6 +184,4 @@ class Tenso {
}
}

module.exports = function (config) {
return new Tenso(config);
};
module.exports = config => new Tenso(config);
Loading

0 comments on commit 0f4ade4

Please sign in to comment.