Skip to content

Commit

Permalink
New release 0.5.0
Browse files Browse the repository at this point in the history
Node engine: LTS 6.9.5
New support to any classic Connect Middleware
New support to use req+res alternatively to Gangway on controllers
New support to basic Express binds for more compatibility to Connect based middleware (as Passport)
Now Route & Middleware calls support implicit and explicit "new", new Route(...) === Route(...)
New properties for Route and Middleware '.connectSupport' shows if handlers are classic or not
Middleware "Directory" have new config property 'hideExt', on 'true' hide extensions for supported template files (and redirect if use extensions)
  • Loading branch information
aitanadev committed Feb 16, 2017
1 parent eb66b4e commit 39129e4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 21 deletions.
21 changes: 19 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,21 @@ pillars.handler = function pillarsHandler(req,res){
var middlewareHandling = new Procedure();
for(var i=0,l=pillars.middleware.length;i<l;i++){
var middleware = pillars.middleware[i];
middlewareHandling.add(middleware.id,middleware.handler,gw);

if(middleware.handler.isConnect){
middlewareHandling.add(middleware.id,middleware.handler,gw.req,gw.res);
} else {
middlewareHandling.add(middleware.id,middleware.handler,gw);
}
}
// Express binds (TODO: Move this as formal Middleware)
middlewareHandling.add("ExpressBinds",function(gw,done){
gw.res.redirect = gw.redirect;
gw.req.body = gw.content.params;
gw.req.query = gw.query;
gw.req.session = gw.session;
done();
},gw);
middlewareHandling.launch(function(errors){
if(errors){
gw.error(500,errors[0]);
Expand All @@ -238,7 +251,11 @@ pillars.handler = function pillarsHandler(req,res){
var handler = gw.routing.handlers[i];
var handlerName = routeNames+".";
handlerName += handler.name?handler.name:i+1===l?"handler":"middleware["+i+"]";
routeHandling.add(handlerName,handler,gw);
if(handler.isConnect){
routeHandling.add(handlerName,handler,gw.req,gw.res);
} else {
routeHandling.add(handlerName,handler,gw);
}
}
routeHandling.launch(function(errors){
if(errors){
Expand Down
30 changes: 23 additions & 7 deletions lib/Middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ require('date.format');

module.exports = Middleware;
function Middleware(config,handler){
if(!(this instanceof Middleware)){return new Middleware(config);}
var plugin = this;
if(!(this instanceof Middleware)){return new Middleware(config,handler);}
var middleware = this;
handler = (typeof config === 'function')?config:handler;
config = (typeof config === 'object')?config:{};
plugin.id = (new Date()).format("{YYYY}{MM}{DD}{hh}{mm}{ss}{ms}")+Math.round(Math.random()*1000000000000000).toString(36);

plugin.handler = handler;
plugin.active = true;
middleware.id = (new Date()).format("{YYYY}{MM}{DD}{hh}{mm}{ss}{ms}")+Math.round(Math.random()*1000000000000000).toString(36);
middleware.active = true;

Object.defineProperty(middleware,"connectSupport",{
enumerable : true,
get : function(){
return handler.length === 3;
}
});

Object.defineProperty(middleware,"handler",{
enumerable : true,
get : function(){
handler.isConnect = handler.length === 3;
return handler;
},
set : function(set){
handler = set;
}
});

plugin.configure(config);
middleware.configure(config);
}
Middleware.prototype.configure = function configure(config){
for(var i=0,k=Object.keys(config),l=k.length;i<l;i++){
Expand Down
41 changes: 34 additions & 7 deletions lib/Route.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require('date.format');

module.exports = Route;
function Route(config){
if(!(this instanceof Route)){return new Route(config);}
if(!(this instanceof Route)){return new Route(...arguments);}
var route = this;
config = (config && typeof config !== 'function')?config:{};
route.id = (new Date()).format("{YYYY}{MM}{DD}{hh}{mm}{ss}{ms}")+Math.round(Math.random()*1000000000000000).toString(36);
Expand All @@ -18,7 +18,6 @@ function Route(config){
route.host = undefined;
route.port = undefined;
route.iPath = undefined;
route.handlers = [];

var pathComponentsCache = {};
Object.defineProperty(route,"pathComponentsCache",{
Expand All @@ -29,13 +28,40 @@ function Route(config){
}
});

var handlers = Array.prototype.slice.call(arguments);
for(var i=0,l=handlers.length;i<l;i++){
if(typeof handlers[i] === 'function'){
route.handlers.push(handlers[i]);
var args = Array.prototype.slice.call(arguments);
var handlers = [];
for(var i=0,l=args.length;i<l;i++){
if(typeof args[i] === 'function'){
handlers.push(args[i]);
}
}

Object.defineProperty(route,"connectSupport",{
enumerable : true,
get : function(){
var result = [];
handlers.forEach(function(e,i,a){
result.push(e.length == (i == a.length-1?2:3));
});
return result;
}
});

Object.defineProperty(route,"handlers",{
enumerable : true,
get : function(){
handlers.forEach(function(e,i,a){
var isLast =
e.isConnect = e.length == (i == a.length-1?2:3);
});
return handlers;
},
set : function(set){
set = Array.isArray(set)?set:[set];
handlers = set;
}
});

var path;
Object.defineProperty(route,"path",{
enumerable : true,
Expand Down Expand Up @@ -105,10 +131,11 @@ function Route(config){
compose.push("\\/"+parts[i]);
}
}
return this.pathComponentsCache[path] = {
this.pathComponentsCache[path] = {
path: path,
regexp: new RegExp('^'+compose.join(''),'i'),
params: params
};
return this.pathComponentsCache[path];
}
};
42 changes: 39 additions & 3 deletions middleware/directory.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,52 @@ var middleware = module.exports = new Middleware({
if (directory) {
directory.path = directory.path || '/';
directory.listing = directory.listing || false;
directory.hideExt = directory.hideExt || false;
directory.template = directory.template || pillars.config.directoryTemplate || directoryTemplate;

var path = paths.join(directory.path, (gw.params.path || ''));
var parent = path.replace(/[\/][^\/]*$/,'');
var ext = path.replace(/^.*\./,'');
var filename = path.replace(/^.*[\\\/]/,'');
var reidx = new RegExp('^index\\.('+templated.getEngines().concat(['htm','html']).join('|')+')$', 'i');
var nameonly = filename.replace(/\.[^\.]*$/,'');
var checkExt = new RegExp('\\.('+templated.getEngines().concat(['htm','html']).join('|')+')$', 'i');
var reidx = new RegExp('^index'+checkExt.source, 'i');

if (filename[0] !== '.') {
fs.stat(path, function (error, stats) {
if (error || (!stats.isFile() && !stats.isDirectory())) {
gw.error(404,error);
fs.stat(parent, function (error2, stats) {
if (!error2 && stats.isDirectory()) {
fs.readdir(parent, function (error, files){
if (error) {
gw.error(404, error);
} else {
var template = false;
for (var i in files) {
var check = new RegExp('^'+nameonly+checkExt.source, 'i');
if (check.test(files[i])) {
template = files[i];
break;
}
}
if (template) {
var templateExt = template.replace(/^.*\./,'');
if (templateExt === 'html' || templateExt === 'htm') {
gw.file(paths.join(parent, template));
} else {
gw.render(paths.join(parent, template));
}
} else {
gw.error(404);
}
}
});

} else {
gw.error(404,error);
}
});

} else if (stats.isDirectory()) {
fs.readdir(path, function (error, files){
if (error) {
Expand Down Expand Up @@ -57,7 +92,8 @@ var middleware = module.exports = new Middleware({
});
} else if (stats.isFile()) {
if (templated.getEngines().indexOf(ext) >= 0) {
gw.render(path);
//gw.render(path);
gw.redirect(gw.path.replace(/\.[^\.]*$/,''));
} else {
stats.path = path;
gw.file(stats);
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": "pillars",
"description": "Pillars.js web development framework for Node.js",
"version": "0.4.2",
"version": "0.5.0",
"author": {
"name": "Javier Gallego Martín",
"email": "bifuer@gmail.com"
Expand Down Expand Up @@ -65,7 +65,7 @@
"unit.js": "*"
},
"engines" : {
"node" : ">=0.12.0"
"node" : ">=6.9.5"
},
"main": "./index.js",
"scripts":{
Expand Down

0 comments on commit 39129e4

Please sign in to comment.