Comparing version 0.4.0 to 0.5.0
@@ -0,0 +0,0 @@ 'use strict'; |
'use strict'; | ||
module.exports = require('./lib/Apx') |
105
lib/Apx.js
@@ -7,2 +7,3 @@ 'use strict'; | ||
, events = require('events') | ||
, util = require('util') | ||
, emitter | ||
@@ -167,2 +168,3 @@ | ||
function(initializer,next){ | ||
self.sysLog.info('Starting initializer: ' + initializer.name) | ||
self.initializers[initializer.name] = initializer.start(self,next) | ||
@@ -185,2 +187,3 @@ }, | ||
function(translator,next){ | ||
self.sysLog.info('Starting translator: ' + translator.name) | ||
self.translators[translator.name] = translator.start(self,next) | ||
@@ -239,2 +242,3 @@ }, | ||
function(translator,next){ | ||
self.sysLog.info('Stopping translator: ' + translator.name) | ||
self.translators[translator.name] = translator.stop(self,next) | ||
@@ -256,2 +260,3 @@ }, | ||
function(initializer,next){ | ||
self.sysLog.info('Stopping initializer: ' + initializer.name) | ||
self.initializers[initializer.name] = initializer.stop(self,next) | ||
@@ -316,2 +321,3 @@ }, | ||
if('string' === typeof items) items = [items] | ||
if('object' === typeof items && !util.isArray(items)) items = [items] | ||
//do all of this async | ||
@@ -321,4 +327,20 @@ async.each( | ||
function(item,finish){ | ||
//try to load a package name natively | ||
if(-1 === item.indexOf('/') && -1 === item.indexOf('\\') && -1 === item.indexOf('.js')){ | ||
//objects should be passed directly | ||
if('object' === typeof item && !util.isArray(item) && 'string' !== typeof item){ | ||
self.sysLog.info('Loading object directly (' + item.name + ')') | ||
iterator(item,finish) | ||
/** | ||
* Try to load a package name natively | ||
* this will check if the package is a file or should be loaded using the | ||
* methods of the nodejs require() function see the documentation | ||
* here: http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders | ||
*/ | ||
} else if( | ||
0 !== item.indexOf('./') && | ||
0 !== item.indexOf('../') && | ||
0 !== item.indexOf('/') && | ||
(item.length - 3) !== item.indexOf('.js') && | ||
(item.length - 5) !== item.indexOf('.json') && | ||
(item.length - 5) !== item.indexOf('.node') | ||
){ | ||
self.sysLog.info('Loading package (' + item + ')') | ||
@@ -328,2 +350,3 @@ iterator(require(item),finish) | ||
} else { | ||
self.sysLog.info('Trying to glob path (' + item + ')') | ||
glob(item,{cwd: cwd},function(err,files){ | ||
@@ -365,28 +388,64 @@ if(err) throw err | ||
var res = new self.Response() | ||
var finish = function(err){ | ||
var runMiddleware = function(position,done){ | ||
//make sure position is usable even if its not passed | ||
if('function' === typeof position){ | ||
done = position | ||
position = 'pre' | ||
} | ||
if('pre' !== position && 'post' !== position){ | ||
position = 'pre' | ||
} | ||
//run middleware | ||
if(self.config.exists('middleware')){ | ||
self.sysLog.info('Running ' + position + ' middleware for action ' + action.name + ':' + method) | ||
self.loadItems( | ||
self.config.get('middleware'), | ||
function(middleware,next){ | ||
var method = position | ||
if('pre' === position && 'function' === typeof middleware.run){ | ||
method = 'run' | ||
} | ||
if('post' === position){ | ||
method = 'post' | ||
} | ||
//only run the middleware if a method is defined | ||
if('function' === typeof middleware[method]){ | ||
middleware[method](self,req,res,next,done) | ||
} | ||
}, | ||
done | ||
) | ||
} else { | ||
done(null) | ||
} | ||
} | ||
var runAction = function(next){ | ||
self.sysLog.info('Running action ' + action.name + ' : ' + method) | ||
emitter.emit('runActionBefore',action) | ||
action[method](self,req,res,function(err){ | ||
emitter.emit('runActionAfter',action,err,res) | ||
next(err,res) | ||
}) | ||
} | ||
//run the action and middleware before and after | ||
runMiddleware('pre',function(err){ | ||
if(err){ | ||
emitter.emit('error',err) | ||
} else { | ||
//run action | ||
self.sysLog.info('Running action ' + action.name + ' : ' + method) | ||
emitter.emit('runActionBefore',action) | ||
action[method](self,req,res,function(err){ | ||
emitter.emit('runActionAfter',action,err,res) | ||
fn(err,res) | ||
runAction(function(err,res){ | ||
if(err){ | ||
emitter.emit('error',err) | ||
} else { | ||
runMiddleware('post',function(err){ | ||
if(err){ | ||
emitter.emit('error',err) | ||
} else { | ||
fn(err,res) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
//run middleware | ||
if(self.config.get('middleware')){ | ||
self.sysLog.info('Running middleware for action ' + action.name + ':' + method) | ||
self.loadItems( | ||
self.config.get('middleware'), | ||
function(middleware,next){ | ||
middleware.run(self,req,res,next,finish) | ||
}, | ||
finish | ||
) | ||
} else { | ||
finish(null) | ||
} | ||
}) | ||
} | ||
@@ -393,0 +452,0 @@ |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ The MIT License (MIT) |
{ | ||
"name": "apx", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"description": "A scalable, extensible, modular API Server", | ||
@@ -40,3 +40,3 @@ "homepage": "https://github.com/snailjs/apx", | ||
"glob": "~3.2.7", | ||
"object-manage": "~0.5.0", | ||
"object-manage": "~0.6.0", | ||
"async": "~0.2.9" | ||
@@ -43,0 +43,0 @@ }, |
@@ -549,2 +549,10 @@ [SnailJS](//github.com/snailjs/).[APX](//github.com/snailjs/apx/) | ||
### 0.5.0 | ||
* Implementation of APX authentication system [#3](https://github.com/snailjs/apx/issues/3) | ||
* APX now loads packages exactly like Node's `require()`. Except for the addition of parsing | ||
globs. [see this](http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders) | ||
* Middleware now supports `pre` and `post` actions with a default of `run` which will be fired before | ||
the action. | ||
### 0.4.0 | ||
@@ -551,0 +559,0 @@ * Usage no longer involves `new Apx` the returned object is now an event emitter |
@@ -53,2 +53,3 @@ 'use strict'; | ||
var action = { | ||
name: 'foo', | ||
run: function(apx,req,res,next){ | ||
@@ -66,2 +67,26 @@ expect(req.get('mydata')).to.equal('val1') | ||
}) | ||
it('should run middleware before and after',function(done){ | ||
var action = { | ||
name: 'foo', | ||
run: function(apx,req,res,next){ | ||
expect(req.get('foo')).to.equal('yes') | ||
next() | ||
} | ||
} | ||
var middleware = { | ||
pre: function(apx,req,res,next){ | ||
req.set('foo','yes') | ||
next() | ||
}, | ||
post: function(apx,req,res,next){ | ||
res.set('foo','no') | ||
next() | ||
} | ||
} | ||
instance.config.set('middleware',middleware) | ||
instance.runAction(action,{},function(err,res){ | ||
expect(res.get('foo')).to.equal('no') | ||
done() | ||
}) | ||
}) | ||
it('should run a task',function(done){ | ||
@@ -108,3 +133,66 @@ var task = { | ||
}) | ||
describe('Load Items',function(){ | ||
var instance | ||
before(function(done){ | ||
apx.once('ready',function(apx){ | ||
instance = apx | ||
done() | ||
}) | ||
apx.start({ | ||
sysLogLevel: 2, | ||
testing: true, | ||
cwd: __dirname | ||
}) | ||
}) | ||
after(function(done){ | ||
apx.once('dead',function(){ | ||
done() | ||
}) | ||
apx.stop() | ||
}) | ||
it('should allow passing of plugins directly as objects',function(done){ | ||
instance.loadItems({name: 'foo'},function(item,next){ | ||
expect(item.name).to.equal('foo') | ||
next() | ||
},done) | ||
}) | ||
it('should load packages when no relative path is given',function(){ | ||
var load = function(){ | ||
instance.loadItems('foo',function(item,next){ | ||
next() | ||
}) | ||
} | ||
expect(load).to.throw('Cannot find module \'foo\'') | ||
}) | ||
it('should load a glob when path ends with .js',function(done){ | ||
instance.loadItems('foo.js',function(item,next){ | ||
expect(item.name).to.equal('foo') | ||
next() | ||
},done) | ||
}) | ||
it('should load a glob when path ends with .json',function(done){ | ||
instance.loadItems('foo.json',function(item,next){ | ||
expect(item.name).to.equal('foo') | ||
next() | ||
},done) | ||
}) | ||
it('should load a glob when path ends with .node',function(done){ | ||
instance.loadItems('foo.node',function(item,next){ | ||
expect(item.name).to.equal('foo') | ||
next() | ||
},done) | ||
}) | ||
it('should load a glob when path starts with ./',function(done){ | ||
instance.loadItems('./foo',function(item,next){ | ||
expect(item.name).to.equal('foo') | ||
next() | ||
},done) | ||
}) | ||
it('should load a glob when path starts with ../',function(done){ | ||
instance.loadItems('../foo',function(item,next){ | ||
expect(item.name).to.equal('foo') | ||
next() | ||
},done) | ||
}) | ||
}) | ||
}) |
@@ -0,0 +0,0 @@ 'use strict'; |
@@ -0,0 +0,0 @@ 'use strict'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
82767
940
603
+ Addedobject-manage@0.6.0(transitive)
+ Addedredis@0.10.3(transitive)
+ Addeduuid@1.4.2(transitive)
- Removedobject-manage@0.5.1(transitive)
Updatedobject-manage@~0.6.0