autohost
Advanced tools
Comparing version 0.0.3 to 0.0.4
{ | ||
"name": "autohost", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Resource-driven http server", | ||
@@ -5,0 +5,0 @@ "main": "src/autohost.js", |
@@ -21,4 +21,6 @@ // Forked from Anvil's http host and other modules | ||
this.clients.lookup = {}; | ||
this.compilers = {}; | ||
this.middleware = {}; | ||
this.config = config || {}; | ||
this.topics = {}; | ||
this.config = config || {}; | ||
_.bindAll( this ); | ||
@@ -72,2 +74,16 @@ }; | ||
Host.prototype.addCompiler = function( extension, callback ) { | ||
this.compilers[ extension ] = callback; | ||
if( this.app ) { | ||
this.app.engine( extension, callback ); | ||
} | ||
}; | ||
Host.prototype.addMiddleware = function( filter, middleware ) { | ||
this.middleware[ filter ] = middleware; | ||
if( this.app ) { | ||
this.app.use( filter, middleware ); | ||
} | ||
}; | ||
Host.prototype.buildPath = function( pathSpec ) { | ||
@@ -160,3 +176,7 @@ var hasLocalPrefix; | ||
Host.prototype.init = function(config) { | ||
var self = this; | ||
var self = this, | ||
cwd = process.cwd(), | ||
public = path.resolve( cwd, ( this.config.static || './public' ) ), | ||
resources = path.resolve( cwd, (this.config.resources || "./resource" ) ); | ||
if( config ) { | ||
@@ -167,3 +187,4 @@ this.config = config; | ||
this.app.use( express.bodyParser() ); | ||
this.app.use( '/', function( req, res, next ) { | ||
this.app.use( this.app.router ); | ||
this.app.use( function( req, res, next ) { | ||
res.header( 'Access-Control-Allow-Origin', '*' ); | ||
@@ -173,10 +194,9 @@ res.header( 'Access-Control-Allow-Headers', 'X-Requested-With' ); | ||
} ); | ||
_.each( this.middleware, function( op, path ) { | ||
self.app.use( path, op ); | ||
} ); | ||
this.app.use( this.app.router ); | ||
var cwd = process.cwd(), | ||
public = path.resolve( cwd, ( this.config.static || './public' ) ), | ||
resources = path.resolve( cwd, (this.config.resources || "./resources" ) ); | ||
this.registerPath( '/', public ); | ||
this.loadResources( resources ); | ||
this.server = http.createServer( this.app ).listen( this.config.port || 8800 ); | ||
@@ -188,11 +208,9 @@ | ||
} | ||
this.loadResources( resources ); | ||
}; | ||
Host.prototype.loadModule = function( path ) { | ||
Host.prototype.loadModule = function( resourcePath ) { | ||
try { | ||
delete require.cache[ path ]; | ||
var mod = require( path )(); | ||
this.processResource( 'api', mod ); | ||
delete require.cache[ resourcePath ]; | ||
var mod = require( resourcePath )(); | ||
this.processResource( 'api', mod, path.dirname( resourcePath ) ); | ||
} catch (err) { | ||
@@ -219,11 +237,20 @@ console.log( 'Error loading resource module:', err ); | ||
Host.prototype.sendToClient = function( id, message, data ) { | ||
Host.prototype.processResource = function( prefix, resource, basePath ) { | ||
var self = this, | ||
socket = this.clients.lookup[ id ]; | ||
if( socket ) { | ||
socket.publish( message, data ); | ||
return true; | ||
} else { | ||
return false; | ||
name = resource.name, | ||
resourcePath = self.buildUrl( prefix, name ); | ||
if( resource.resources && resource.resources != '' ) { | ||
var directory = this.buildPath( [ basePath, resource.resources ] ); | ||
this.registerPath( name, directory ); | ||
} | ||
_.each( resource.actions, function( action ) { | ||
var handle = action.handle, | ||
topic = name + ( ( ( action.topic || '' ) == '' ) ? '' : '.' + action.topic ), | ||
url = self.buildUrl( prefix, name, ( action.path || '' ) ), | ||
verb = action.verb; | ||
console.log(verb, url); | ||
self.handleRoute( url, verb, resource, handle ); | ||
self.handleTopic( topic, resource, handle ); | ||
} ); | ||
}; | ||
@@ -272,18 +299,11 @@ | ||
Host.prototype.processResource = function( prefix, resource ) { | ||
Host.prototype.sendToClient = function( id, message, data ) { | ||
var self = this, | ||
name = resource.name, | ||
resourcePath = self.buildUrl( prefix, name ); | ||
if( resource.resourcePath ) { | ||
this.registerPath( resourcePath, resource.resourcePath ); | ||
socket = this.clients.lookup[ id ]; | ||
if( socket ) { | ||
socket.publish( message, data ); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
_.each( resource.actions, function( action ) { | ||
var handle = action.handle, | ||
topic = name + ( ( ( action.topic || '' ) == '' ) ? '' : '.' + action.topic ), | ||
url = self.buildUrl( prefix, name, ( action.path || '' ) ), | ||
verb = action.verb; | ||
self.handleRoute( url, verb, resource, handle ); | ||
self.handleTopic( topic, resource, handle ); | ||
} ); | ||
}; | ||
@@ -290,0 +310,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
11733
280
3