Comparing version 0.1.2 to 0.1.3
151
lib/index.js
@@ -135,11 +135,11 @@ | ||
// load rest classes | ||
, loadRestInterface: function( callback ){ | ||
fs.exists( this.path, function( exists ){ | ||
, loadDir: function( path, subtree, callback ){ | ||
fs.exists( path, function( exists ){ | ||
if ( !exists ) { | ||
log.warn( "REST directory [" + this.path + "] does not exist!" ); | ||
log.warn( "REST directory [" + path + "] does not exist!" ); | ||
callback(); | ||
} | ||
else { | ||
fs.readdir( this.path, function( err, files ){ | ||
fs.readdir( path, function( err, files ){ | ||
if ( err ) { | ||
@@ -153,16 +153,28 @@ log.error( err ); | ||
files.forEach( function( file ){ | ||
if ( /\.js$/gi.test( file ) ){ | ||
waiter.add( function( cb ){ | ||
try { | ||
// load base class for resource | ||
this.controllers[ file.replace( /\.js$/, "" ).toLowerCase() ] = new ( require( this.path + file ) )( this.controllerOptions ); | ||
} catch ( err ){ | ||
log.warn( "failed to load rest resource [" + file.replace( /\.js$/, "" ).toLowerCase() + "]" ); | ||
log.trace( err ); | ||
files.forEach( function( file ){ | ||
waiter.add( function( cb ){ | ||
fs.stat( path + file, function( err, stats ){ | ||
if ( err ) { | ||
log.warn( "failed to stat [" + path + "]!" ); | ||
cb(); | ||
} | ||
cb(); | ||
else { | ||
if ( stats.isDirectory() ){ | ||
subtree[ file ] = {}; | ||
this.loadDir( path + file, subtree[ file ], cb ); | ||
} | ||
else { | ||
if ( /\.js$/gi.test( file ) ){ | ||
try { | ||
// load base class for resource | ||
subtree[ file.replace( /\.js$/, "" ).toLowerCase() ] = new ( require( path + file ) )( this.controllerOptions ); | ||
} catch ( err ){ | ||
log.warn( "failed to load rest resource [" + file.replace( /\.js$/, "" ).toLowerCase() + "]" ); | ||
log.trace( err ); | ||
} | ||
} | ||
} | ||
} | ||
}.bind( this ) ); | ||
} | ||
}.bind( this ) ); | ||
}.bind( this ) ); | ||
@@ -179,2 +191,10 @@ | ||
// load rest classes | ||
, loadRestInterface: function( callback ){ | ||
this.loadDir( this.path, this.controllers, callback ); | ||
} | ||
, addRenderer: function( contentType, renderer, isDefault ){ | ||
@@ -207,38 +227,66 @@ if ( isDefault ) this.defaultRenderer = renderer; | ||
, findController: function( parts, query, tree ){ | ||
if ( parts.length === 0 ) return null; | ||
else { | ||
if ( tree.hasOwnProperty( parts[ 0 ] ) ){ | ||
// controller exists | ||
if ( parts.length === 1 ){ | ||
// entity without id | ||
return tree[ parts[ 0 ] ]; | ||
} | ||
else if ( parts.length === 2 ){ | ||
// entity with id | ||
if ( tree.hasOwnProperty( parts[ 0 ] + "-resource" ) ){ | ||
query.id = parts[ 1 ]; | ||
return tree[ parts[ 0 ] + "-resource" ]; | ||
} | ||
else return null; | ||
} | ||
else { | ||
// subentity | ||
query[ parts[ 0 ] ] = parts[ 1 ]; | ||
this.findController( parts.slice( 2 ), query, tree[ parts[ 0 ] ] ); | ||
} | ||
} | ||
} | ||
} | ||
, request: function( request, response, next ){ | ||
var call = /^\/([^\/]+)(\/?)(.*)$/gi.exec( request.pathname ), controller; | ||
, request: function( request, response, next ){ | ||
var parts = request.pathname.split( "/" ).filter( function( p ){ return p.length > 0; } ) | ||
, query = request.query | ||
, controller = this.findController( parts, query, this.controllers ); | ||
if ( !controller && this.controllers[ "root" ] ) controller = this.controllers[ "root" ]; | ||
// rest call | ||
if ( call && call.length === 4 ){ | ||
if ( controller ){ | ||
// get renderer | ||
this.contentTypeNegation( request, response, function( renderer, isDefaultRenderer ){ | ||
// supported method | ||
if ( this.methods.indexOf( request.method ) === -1 ) response.render( null, {}, 501 ); | ||
else { | ||
// attach render hook | ||
response.render = function( data, headers, statusCode ){ | ||
if ( !isDefaultRenderer ) { | ||
if ( !statusCode ) statusCode = 200; | ||
if ( !data ) data = {}; | ||
// get renderer | ||
this.contentTypeNegation( request, response, function( renderer, isDefaultRenderer ){ | ||
renderer.render( data, request, response, function( renderedData ){ | ||
response.sendUncompressed( renderedData, headers, statusCode ); | ||
} ); | ||
} | ||
else response.send( "", {}, 406 ); | ||
}.bind( this ); | ||
// attach render hook | ||
response.render = function( data, headers, statusCode ){ | ||
if ( !isDefaultRenderer ) { | ||
if ( !statusCode ) statusCode = 200; | ||
if ( !data ) data = {}; | ||
renderer.render( data, request, response, function( renderedData ){ | ||
response.sendUncompressed( renderedData, headers, statusCode ); | ||
} ); | ||
} | ||
else response.send( "", {}, 406 ); | ||
}.bind( this ); | ||
// supported method? | ||
if ( this.methods.indexOf( request.method ) === -1 ) response.render( null, {}, 501 ); | ||
else { | ||
if ( call[ 2 ] === "" && this.controllers.hasOwnProperty( call[ 1 ].toLowerCase() ) ) controller = this.controllers[ call[ 1 ].toLowerCase() ]; | ||
else if ( this.controllers.hasOwnProperty( call[ 1 ].toLowerCase() + "-resource" ) ) controller = this.controllers[ call[ 1 ].toLowerCase() + "-resource" ]; | ||
else return next(); | ||
// method supported by controller? | ||
if ( controller[ request.method ] ){ | ||
if ( typeof controller[ request.method ] === "function" ){ | ||
@@ -249,21 +297,14 @@ // invoke the controller | ||
else { | ||
if ( request.method === "options" ){ | ||
var allow = [ "options" ].concat( this.methods ).filter( function( method ){ | ||
return !!controller[ method ]; | ||
} ); | ||
response.render( null, { Allow: allow } ); | ||
} | ||
else response.render( null, {}, 501 ); | ||
var allow = [ "options" ].concat( this.methods ).filter( function( method ){ | ||
return typeof controller[ method ] === "function"; | ||
} ).join( ", " ).toUpperCase(); | ||
response.render( null, { Allow: allow }, request.method === "options" ? 200 : 405 ); | ||
} | ||
} | ||
}.bind( this ) ); | ||
}.bind( this ) ); | ||
} | ||
} | ||
// root ( list resources ) | ||
else if ( request.pathname === "/" && this.controllers.root && this.controllers.root.get ){ | ||
this.controllers.root.get( request, response, next ); | ||
} | ||
else next(); | ||
} | ||
} ); |
{ | ||
"name": "em-rest" | ||
, "description": "rest service middleware for ee-webservice" | ||
, "version": "0.1.2" | ||
, "version": "0.1.3" | ||
, "homepage": "https://github.com/eventEmitter/em-rest" | ||
@@ -6,0 +6,0 @@ , "author": "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
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
11213
271