Skip to content

Commit

Permalink
Initial changes to support turtle.io 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
avoidwork committed Feb 16, 2016
1 parent 6d12632 commit 5c7b4fa
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 70 deletions.
10 changes: 6 additions & 4 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
}
},
"compress": true,
"headers": {
"cache-control": "private max-age=60"
},
"hostname": "127.0.0.1",
"id": "tenso",
"json": 2,
"logs": {
"stdout": true,
"dtrace": false,
"logging": {
"level": "info",
"enabled": true,
"stack": false
},
"security": {
Expand Down
14 changes: 0 additions & 14 deletions dtrace.sh

This file was deleted.

8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var fs = require("fs"),

function factory(arg) {
var hostname = arg ? arg.hostname || "localhost" : "localhost",
vhosts = {},
hosts = {},
config = arg ? merge(utility.clone(cfg), arg) : utility.clone(cfg),
obj = undefined;

Expand All @@ -19,12 +19,12 @@ function factory(arg) {
process.exit(1);
}

vhosts[hostname] = "www";
hosts[hostname] = "www";
config.root = root;
config.vhosts = vhosts;
config.hosts = hosts;
config.default = hostname;
config.template = fs.readFileSync(path.join(config.root, "template.html"), { encoding: "utf8" });
obj = tenso();
obj = tenso(config);
obj.hostname = hostname;
utility.bootstrap(obj, config);
obj.server.start(config);
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.3").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.1.0").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
28 changes: 16 additions & 12 deletions lib/tenso.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";

var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

Expand All @@ -15,22 +15,26 @@ var path = require("path"),
var renderers = require(path.join(__dirname, "renderers")),
serializers = require(path.join(__dirname, "serializers"));

var Tenso = (function () {
var Tenso = function () {
function Tenso(config) {
var _this = this;

_classCallCheck(this, Tenso);

this.hostname = "";
this.messages = {};
this.rates = {};
this.server = turtleio(config);
this.server = turtleio(config, function (req, res, status, arg) {
_this.error(req, res, status, arg);
});
this.server.tenso = this;
this.version = "3.0.3";
this.version = "3.1.0";
}

_createClass(Tenso, [{
key: "error",
value: function error(req, res, status, arg) {
return this.server.error(req, res, status, arg);
return this.respond(req, res, arg instanceof Error ? arg : new Error(arg), status);
}
}, {
key: "rate",
Expand Down Expand Up @@ -79,7 +83,7 @@ var Tenso = (function () {
value: function redirect(req, res, uri) {
var perm = arguments.length <= 3 || arguments[3] === undefined ? false : arguments[3];

return this.server.respond(req, res, this.server.messages.NO_CONTENT, this.server.codes[!perm ? "FOUND" : "MOVED"], { location: uri });
return this.server.send(req, res, "", !perm ? 302 : 301, { location: uri });
}
}, {
key: "render",
Expand Down Expand Up @@ -119,7 +123,8 @@ var Tenso = (function () {
value: function respond(req, res, arg, status, headers) {
var resStatus = status || 200,
defer = deferred(),
ref = undefined;
ref = undefined,
output = undefined;

if (!res._header) {
ref = [headers || {}];
Expand All @@ -142,10 +147,9 @@ var Tenso = (function () {
ref[0][this.server.config.security.key] = res.locals[this.server.config.security.key];
}

ref[0] = this.server.headers(req, ref[0], resStatus);
this.server.respond(req, res, this.render(req, utility.hypermedia(this.server, req, this.serialize(req, arg, resStatus), ref[0]), ref[0]), resStatus, ref[0]).then(function () {
defer.resolve();
}, defer.reject);
output = this.render(req, utility.hypermedia(this.server, req, this.serialize(req, arg, resStatus), ref[0]), ref[0]);
ref[0] = this.server.headers(req, res, resStatus, output, ref[0], false);
this.server.send(req, res, output, resStatus, ref[0]).then(defer.resolve, defer.reject);
} else {
defer.resolve();
}
Expand Down Expand Up @@ -197,7 +201,7 @@ var Tenso = (function () {
}]);

return Tenso;
})();
}();

module.exports = function (config) {
return new Tenso(config);
Expand Down
13 changes: 7 additions & 6 deletions lib/utility.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var path = require("path"),
http = require("http"),
array = require("retsu"),
coerce = require("tiny-coerce"),
keysort = require("keysort"),
Expand Down Expand Up @@ -511,11 +512,11 @@ function bootstrap(obj, config) {

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

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

// Setting routes
Expand Down Expand Up @@ -678,7 +679,7 @@ function hypermedia(server, req, rep, headers) {
if (uri !== root && !seen[uri]) {
seen[uri] = 1;

if (server.allowed("GET", uri, req.vhost)) {
if (server.allowed("GET", uri, req.host)) {
rep.links.push({ uri: uri, rel: lrel });
}
}
Expand All @@ -699,7 +700,7 @@ function hypermedia(server, req, rep, headers) {
if (req.parsed.pathname !== "/") {
proot = root.replace(regex.trailing_slash, "").replace(regex.collection, "$1") || "/";

if (server.allows(proot, "GET", req.vhost)) {
if (server.allows(proot, "GET", req.host)) {
rep.links.push({
uri: proot,
rel: "collection"
Expand Down Expand Up @@ -754,7 +755,7 @@ function hypermedia(server, req, rep, headers) {
if (li !== collection) {
uri = li.indexOf("//") > -1 || li.indexOf("/") === 0 ? li : (collection + "/" + li).replace(/^\/\//, "/");

if (server.allowed("GET", uri, req.vhost)) {
if (server.allowed("GET", uri, req.host)) {
rep.links.push({ uri: uri, rel: "item" });
}
}
Expand Down
4 changes: 2 additions & 2 deletions 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.3",
"version": "3.1.0",
"homepage": "http://avoidwork.github.io/tenso",
"author": "Jason Mulligan <[email protected]>",
"repository": {
Expand Down Expand Up @@ -41,7 +41,7 @@
"tiny-merge": "^1.0.0",
"tiny-uuid4": "^1.0.0",
"tiny-xml": "^1.0.6",
"turtle.io": "^5.0.3",
"turtle.io": "^6.0.8",
"yamljs": "^0.2.4"
},
"devDependencies": {
Expand Down
8 changes: 4 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const fs = require("fs"),

function factory (arg) {
let hostname = arg ? arg.hostname || "localhost" : "localhost",
vhosts = {},
hosts = {},
config = arg ? merge(utility.clone(cfg), arg) : utility.clone(cfg),
obj;

Expand All @@ -17,12 +17,12 @@ function factory (arg) {
process.exit(1);
}

vhosts[hostname] = "www";
hosts[hostname] = "www";
config.root = root;
config.vhosts = vhosts;
config.hosts = hosts;
config.default = hostname;
config.template = fs.readFileSync(path.join(config.root, "template.html"), {encoding: "utf8"});
obj = tenso();
obj = tenso(config);
obj.hostname = hostname;
utility.bootstrap(obj, config);
obj.server.start(config);
Expand Down
17 changes: 9 additions & 8 deletions src/tenso.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ class Tenso {
this.hostname = "";
this.messages = {};
this.rates = {};
this.server = turtleio(config);
this.server = turtleio(config, (req, res, status, arg) => {
this.error(req, res, status, arg);
});
this.server.tenso = this;
this.version = "{{VERSION}}";
}

error (req, res, status, arg) {
return this.server.error(req, res, status, arg);
return this.respond(req, res, arg instanceof Error ? arg : new Error(arg), status);
}

rate (req, fn) {
Expand Down Expand Up @@ -62,7 +64,7 @@ class Tenso {
}

redirect (req, res, uri, perm = false) {
return this.server.respond(req, res, this.server.messages.NO_CONTENT, this.server.codes[!perm ? "FOUND" : "MOVED"], {location: uri});
return this.server.send(req, res, "", !perm ? 302 : 301, {location: uri});
}

render (req, arg, headers) {
Expand Down Expand Up @@ -99,7 +101,7 @@ class Tenso {
respond (req, res, arg, status, headers) {
let resStatus = status || 200,
defer = deferred(),
ref;
ref, output;

if (!res._header) {
ref = [headers || {}];
Expand All @@ -122,10 +124,9 @@ class Tenso {
ref[0][this.server.config.security.key] = res.locals[this.server.config.security.key];
}

ref[0] = this.server.headers(req, ref[0], resStatus);
this.server.respond(req, res, this.render(req, utility.hypermedia(this.server, req, this.serialize(req, arg, resStatus), ref[0]), ref[0]), resStatus, ref[0]).then(function () {
defer.resolve();
}, defer.reject);
output = this.render(req, utility.hypermedia(this.server, req, this.serialize(req, arg, resStatus), ref[0]), ref[0]);
ref[0] = this.server.headers(req, res, resStatus, output, ref[0], false);
this.server.send(req, res, output, resStatus, ref[0]).then(defer.resolve, defer.reject);
} else {
defer.resolve();
}
Expand Down
11 changes: 6 additions & 5 deletions src/utility.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require("path"),
http = require("http"),
array = require("retsu"),
coerce = require("tiny-coerce"),
keysort = require("keysort"),
Expand Down Expand Up @@ -497,8 +498,8 @@ function bootstrap (obj, config) {
config.headers.server = "tenso/{{VERSION}}";

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

// Setting routes
Expand Down Expand Up @@ -646,7 +647,7 @@ function hypermedia (server, req, rep, headers) {
if (uri !== root && !seen[uri]) {
seen[uri] = 1;

if (server.allowed("GET", uri, req.vhost)) {
if (server.allowed("GET", uri, req.host)) {
rep.links.push({uri: uri, rel: lrel});
}
}
Expand All @@ -667,7 +668,7 @@ function hypermedia (server, req, rep, headers) {
if (req.parsed.pathname !== "/") {
proot = root.replace(regex.trailing_slash, "").replace(regex.collection, "$1") || "/";

if (server.allows(proot, "GET", req.vhost)) {
if (server.allows(proot, "GET", req.host)) {
rep.links.push({
uri: proot,
rel: "collection"
Expand Down Expand Up @@ -722,7 +723,7 @@ function hypermedia (server, req, rep, headers) {
if (li !== collection) {
uri = li.indexOf("//") > -1 || li.indexOf("/") === 0 ? li : (collection + "/" + li).replace(/^\/\//, "/");

if (server.allowed("GET", uri, req.vhost)) {
if (server.allowed("GET", uri, req.host)) {
rep.links.push({uri: uri, rel: "item"});
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/auth_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function get_token (port, fn, url) {
describe("Permissions (CSRF disabled)", function () {
var port = 8001;

tenso({port: port, routes: routes, logs: {level: "error"}, security: {csrf: false}});
tenso({port: port, routes: routes, logging: {level: "error"}, security: {csrf: false}});

this.timeout(5000);

Expand Down Expand Up @@ -121,7 +121,7 @@ describe("Basic Auth", function () {
tenso({
port: port,
routes: routes,
logs: {level: "error"},
logging: {level: "error"},
auth: {basic: {enabled: true, list: ["test:123"]}, protect: ["/uuid"]}
});

Expand Down Expand Up @@ -177,7 +177,7 @@ describe("OAuth2 Token Bearer", function () {
tenso({
port: port,
routes: routes,
logs: {level: "error"},
logging: {level: "error"},
auth: {bearer: {enabled: true, tokens: ["abc-123"]}, protect: ["/"]}
});

Expand Down Expand Up @@ -220,7 +220,7 @@ describe("Local", function () {
tenso({
port: port,
routes: require("./routes.js"),
logs: {
logging: {
level: "error",
dtrace: true,
stderr: true
Expand Down
Loading

0 comments on commit 5c7b4fa

Please sign in to comment.