Skip to content

Commit

Permalink
Removing baked in functions for modules
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Dec 28, 2015
1 parent 7072e8c commit 6d12632
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 125 deletions.
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var fs = require("fs"),
path = require("path"),
merge = require("tiny-merge"),
root = path.join(__dirname, ".."),
cfg = require(path.join(root, "config.json")),
tenso = require(path.join(__dirname, "tenso.js")),
Expand All @@ -10,7 +11,7 @@ var fs = require("fs"),
function factory(arg) {
var hostname = arg ? arg.hostname || "localhost" : "localhost",
vhosts = {},
config = arg ? utility.merge(utility.clone(cfg), arg) : utility.clone(cfg),
config = arg ? merge(utility.clone(cfg), arg) : utility.clone(cfg),
obj = undefined;

if (!config.port) {
Expand Down
13 changes: 13 additions & 0 deletions lib/iterate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

function iterate(obj, fn) {
if (obj instanceof Object) {
Object.keys(obj).forEach(function (i) {
fn.call(obj, obj[i], i);
});
} else {
obj.forEach(fn);
}
}

module.exports = iterate;
7 changes: 4 additions & 3 deletions lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

var path = require("path"),
array = require("retsu"),
coerce = require("tiny-coerce"),
regex = require(path.join(__dirname, "regex.js")),
shared = require(path.join(__dirname, "shared.js"));
iterate = require(path.join(__dirname, "iterate.js"));

var rateHeaders = ["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset"];

Expand Down Expand Up @@ -72,7 +73,7 @@ function parse(req, res, next) {
req.body = {};

array.each(args, function (i) {
req.body[i[0]] = shared.coerce(i[1]);
req.body[i[0]] = coerce(i[1]);
});
}

Expand Down Expand Up @@ -111,7 +112,7 @@ function keymaster(req, res, next) {
res.send(result);
}
} else {
shared.iterate(routes, function (value, key) {
iterate(routes, function (value, key) {
if (new RegExp("^" + key + "$", "i").test(uri)) {
return !(result = value);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/renderers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ renderers.set("text/html", function (arg, req, headers, tpl) {
return i.indexOf("html") === -1;
}).map(function (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}}", "3.0.2").replace("{{allow}}", headers.allow).replace("{{methods}}", utility.explode(headers.allow.replace("GET, HEAD, OPTIONS", "")).filter(function (i) {
}).join("\n")).replace("{{body}}", JSON.stringify(arg, null, 2)).replace("{{year}}", new Date().getFullYear()).replace("{{version}}", "3.0.3").replace("{{allow}}", headers.allow).replace("{{methods}}", utility.explode(headers.allow.replace("GET, HEAD, OPTIONS", "")).filter(function (i) {
return i !== "";
}).map(function (i) {
return "<option value='" + i + "'>" + i + "</option>";
Expand Down
5 changes: 3 additions & 2 deletions lib/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var path = require("path"),
array = require("retsu"),
turtleio = require("turtle.io"),
deferred = require("tiny-defer"),
merge = require("tiny-merge"),
regex = require(path.join(__dirname, "regex")),
utility = require(path.join(__dirname, "utility"));

Expand All @@ -23,7 +24,7 @@ var Tenso = (function () {
this.rates = {};
this.server = turtleio(config);
this.server.tenso = this;
this.version = "3.0.2";
this.version = "3.0.3";
}

_createClass(Tenso, [{
Expand Down Expand Up @@ -124,7 +125,7 @@ var Tenso = (function () {
ref = [headers || {}];

if (res._headers) {
utility.merge(ref[0], res._headers);
merge(ref[0], res._headers);
}

if (req.protect) {
Expand Down
38 changes: 9 additions & 29 deletions lib/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require("path"),
array = require("retsu"),
coerce = require("tiny-coerce"),
keysort = require("keysort"),
url = require("url"),
session = require("express-session"),
Expand All @@ -10,7 +11,7 @@ var path = require("path"),
uuid = require("tiny-uuid4"),
middleware = require(path.join(__dirname, "middleware.js")),
regex = require(path.join(__dirname, "regex.js")),
shared = require(path.join(__dirname, "shared.js")),
iterate = require(path.join(__dirname, "iterate.js")),
passport = require("passport"),
BasicStrategy = require("passport-http").BasicStrategy,
BearerStrategy = require("passport-http-bearer").Strategy,
Expand Down Expand Up @@ -59,26 +60,6 @@ function isEmpty(obj) {
return trim(obj) === "";
}

function merge(a, b) {
if (a instanceof Object && b instanceof Object) {
Object.keys(b).forEach(function (i) {
if (a[i] instanceof Object && b[i] instanceof Object) {
a[i] = merge(a[i], b[i]);
} else if (a[i] instanceof Array && b[i] instanceof Array) {
a[i] = a[i].concat(b[i]);
} else {
a[i] = b[i];
}
});
} else if (a instanceof Array && b instanceof Array) {
a = a.concat(b);
} else {
a = b;
}

return a;
}

function auth(obj, config) {
var ssl = config.ssl.cert && config.ssl.key,
proto = "http" + (ssl ? "s" : ""),
Expand Down Expand Up @@ -138,7 +119,7 @@ function auth(obj, config) {
});

if (async) {
shared.iterate(config.auth, function (v, k) {
iterate(config.auth, function (v, k) {
if (v.enabled) {
authMap[k + "_uri"] = "/auth/" + k;
config.auth.protect.push(new RegExp("^/auth/" + k));
Expand Down Expand Up @@ -530,16 +511,16 @@ function bootstrap(obj, config) {

// Setting headers
config.headers = config.headers || {};
config.headers.server = "tenso/3.0.2";
config.headers.server = "tenso/3.0.3";

// Creating status > message map
shared.iterate(obj.server.codes, function (value, key) {
iterate(obj.server.codes, function (value, key) {
obj.messages[value] = obj.server.messages[key];
});

// Setting routes
shared.iterate(config.routes, function (routes, method) {
shared.iterate(routes, function (arg, route) {
iterate(config.routes, function (routes, method) {
iterate(routes, function (arg, route) {
if (typeof arg === "function") {
obj.server[method](route, function () {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
Expand Down Expand Up @@ -608,7 +589,7 @@ function queryString() {
if (item[1] === undefined) {
item[1] = "";
} else {
item[1] = shared.coerce(decodeURIComponent(item[1]));
item[1] = coerce(decodeURIComponent(item[1]));
}

if (obj[item[0]] === undefined) {
Expand Down Expand Up @@ -649,7 +630,7 @@ function parse(uri) {
parsed = url.parse(luri);
parsed.query = parsed.search ? queryString(parsed.search) : {};

shared.iterate(parsed, function (v, k) {
iterate(parsed, function (v, k) {
if (v === null) {
parsed[k] = "";
}
Expand Down Expand Up @@ -813,7 +794,6 @@ module.exports = {
escape: escape,
hypermedia: hypermedia,
isEmpty: isEmpty,
merge: merge,
queryString: queryString,
parse: parse,
trim: trim
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "tenso",
"description": "Tensō is a REST API gateway for node.js, designed to simplify the implementation of APIs.",
"version": "3.0.2",
"version": "3.0.3",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down Expand Up @@ -36,7 +36,9 @@
"passport-saml": "^0.14.0",
"passport-twitter": "^1.0.3",
"retsu": "^2.0.0",
"tiny-coerce": "^1.0.1",
"tiny-defer": "^1.0.4",
"tiny-merge": "^1.0.0",
"tiny-uuid4": "^1.0.0",
"tiny-xml": "^1.0.6",
"turtle.io": "^5.0.3",
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require("fs"),
path = require("path"),
merge = require("tiny-merge"),
root = path.join(__dirname, ".."),
cfg = require(path.join(root, "config.json")),
tenso = require(path.join(__dirname, "tenso.js")),
Expand All @@ -8,7 +9,7 @@ const fs = require("fs"),
function factory (arg) {
let hostname = arg ? arg.hostname || "localhost" : "localhost",
vhosts = {},
config = arg ? utility.merge(utility.clone(cfg), arg) : utility.clone(cfg),
config = arg ? merge(utility.clone(cfg), arg) : utility.clone(cfg),
obj;

if (!config.port) {
Expand Down
11 changes: 11 additions & 0 deletions src/iterate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function iterate (obj, fn) {
if (obj instanceof Object) {
Object.keys(obj).forEach(function (i) {
fn.call(obj, obj[i], i);
});
} else {
obj.forEach(fn);
}
}

module.exports = iterate;
7 changes: 4 additions & 3 deletions src/middleware.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const path = require("path"),
array = require("retsu"),
coerce = require("tiny-coerce"),
regex = require(path.join(__dirname, "regex.js")),
shared = require(path.join(__dirname, "shared.js"));
iterate = require(path.join(__dirname, "iterate.js"));

const rateHeaders = [
"x-ratelimit-limit",
Expand Down Expand Up @@ -71,7 +72,7 @@ function parse (req, res, next) {
req.body = {};

array.each(args, function (i) {
req.body[i[0]] = shared.coerce(i[1]);
req.body[i[0]] = coerce(i[1]);
});
}

Expand Down Expand Up @@ -107,7 +108,7 @@ function keymaster (req, res, next) {
res.send(result);
}
} else {
shared.iterate(routes, function (value, key) {
iterate(routes, function (value, key) {
if (new RegExp("^" + key + "$", "i").test(uri)) {
return !(result = value);
}
Expand Down
41 changes: 0 additions & 41 deletions src/shared.js

This file was deleted.

3 changes: 2 additions & 1 deletion src/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require("path"),
array = require("retsu"),
turtleio = require("turtle.io"),
deferred = require("tiny-defer"),
merge = require("tiny-merge"),
regex = require(path.join(__dirname, "regex")),
utility = require(path.join(__dirname, "utility"));

Expand Down Expand Up @@ -104,7 +105,7 @@ class Tenso {
ref = [headers || {}];

if (res._headers) {
utility.merge(ref[0], res._headers);
merge(ref[0], res._headers);
}

if (req.protect) {
Expand Down
Loading

0 comments on commit 6d12632

Please sign in to comment.