-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcontroller.js
More file actions
416 lines (362 loc) · 14.1 KB
/
controller.js
File metadata and controls
416 lines (362 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/* jshint node: true */
'use strict';
var Class = require( 'uberclass' )
, path = require( 'path' )
, util = require( 'util' )
, i = require( 'i' )()
, debug = require( 'debug' )( 'clever-controller' )
, NoActionException = require('./exceptions/NoAction')
, routedControllers = [];
/**
* Clever Controller - lightning-fast flexible controller prototype
*
* @define {CleverController} Clever Controller Class
* @type {Class}
*/
var Controller = Class.extend(
/* @Static */
{
/**
* Defines any (string) route or (array) routes to be used in conjuction with autoRouting
*
* Note:
* You do not need to provide a value for this, if autoRouting is enabled,
* and you haven't defined a route one will be assigned based on the filename of your Controller.
*
* @examples
* route: false
* route: '[POST] /example/:id'
* route: [
* '[POST] /example/?',
* '/example/:id/?',
* '/example/:id/:action/?',
* '/examples/?',
* '/examples/:action/?'
* ]
*
* @default false
* @type {Boolean|String|Array}
*/
route: false,
/**
* Turns autoRouting on when not set to false, and when set to an array provides an
* easy way to define middleware for the controller
*
* @examples
* autoRouting: false
* autoRouting: [
* function( req, res, next ) {
* // define middleware here
* },
* PermissionController.requiresPermission({
* all: 'Permission.*'
* }),
* 'controllerFunction' // Where the controller has a function with this name
* ]
*
* @default true
* @type {Boolean|Array}
*/
autoRouting: true,
/**
* Turns action based routing on or off
*
* @default true
* @type {Boolean}
*/
actionRouting: true,
/**
* Turns restful method based routing on or off
*
* @default true
* @type {Boolean}
*/
restfulRouting: true,
/**
* Regex used to determine what constitutes a valid
*
* @default true
* @type {Boolean}
*/
idRegex: /(^[0-9]+$|^[0-9a-fA-F]{24}$)/,
/**
* Use this function to attach your controller's to routes (either express or restify are supported)
* @return {Function} returns constructor function
*/
attach: function() {
return this.callback( 'newInstance' );
},
/**
* Class (Static) constructor
*
* @constructor
* @return {undefined}
*/
setup: function() {
if ( this.autoRouting !== false && this.route !== false && routedControllers.indexOf( this.route ) === -1 ) {
routedControllers.push( this.route );
if ( typeof this.app !== 'undefined' ) {
this.autoRoute( this.app );
} else {
try {
var injector = require( 'clever-injector' );
injector.inject( this.callback( function( app ) {
this.autoRoute( app );
}));
} catch( e ) {
debug( 'Unable to autoRoute, Controller.app is not defined and clever-injector attempt failed with: ' + e + ( e.stack || ' Without a StackTrace') );
}
}
}
},
/**
* Attaches controllers routes to the app if autoRouting is enabled and routes have been defined
*
* @param {Object} app Either express.app or restify
* @return {undefined}
*/
autoRoute: function( app ) {
var middleware = []
, routes = this.route instanceof Array ? this.route : this.route.split( '|' );
debug( 'Autorouting for route ' + routes.join( ', ' ) );
// Check for middleware that we need to put before the actual attach()
if ( this.autoRouting instanceof Array ) {
debug( 'Found middleware for ' + routes.join( ', ' ) + ' - ' + util.inspect( this.autoRouting ).replace( /\n/ig, ' ' ) );
this.autoRouting.forEach( this.callback( function( mw ) {
middleware.push( typeof mw === 'string' ? this.callback( mw ) : mw );
}));
}
// Add our attach() function to handle requests
middleware.push( this.attach() );
// Bind the actual routes
routes.forEach(function( route ) {
var methods = [ 'GET', 'POST', 'PUT', 'DELETE' ];
debug( 'Attaching route ' + route );
if ( /(^[^\/]+)\ ?(\/.*)/ig.test( route ) ) {
methods = RegExp.$1;
route = RegExp.$2;
methods = methods.match( /\[([^\[\]]+)\]/ig );
if ( methods.length ) {
methods = methods[ 0 ].replace( /(\[|\])/ig, '' );
methods = methods.split( ',' );
}
}
methods.forEach( function( method ) {
app[ method.toLowerCase() ].apply( app, [ route ].concat( middleware ) );
});
});
},
/**
* Use this function to create a new controller that extends from Controller
* @return {Controller} the newly created controller class
*/
extend: function() {
var extendingArgs = [].slice.call( arguments )
, autoRouting = ( extendingArgs.length === 2 ) ? extendingArgs[ 0 ].autoRouting !== false : this.autoRouting
, definedRoute = ( extendingArgs.length === 2 ) ? extendingArgs[ 0 ].route !== undefined : this.route;
// Figure out if we are autoRouting and do not have a defined route already
if ( autoRouting && !definedRoute ) {
var stack = new Error().stack.split( '\n' )
, extendingFilePath = false
, extendingFileName = false
, route = null;
stack = stack.splice( 1, stack.length - 1 );
// Walk backwards over the stack to find the filename where this is defined
while( stack.length > 0 && extendingFilePath === false ) {
var file = stack.shift();
if ( !/clever-controller/ig.test( file ) && !/uberclass/ig.test( file ) ) {
if ( /.*\(([^\)]+)\:.*\:.*\)/ig.test( file ) ) {
extendingFilePath = RegExp.$1;
extendingFileName = path.basename( extendingFilePath );
}
}
}
// Determine the route names if we have found a file
if ( [ '', 'controller.js' ].indexOf( extendingFileName.toLowerCase() ) === -1 ) {
var singular = i.singularize( extendingFileName.replace( /(controller)?.(js|es6)/ig, '' ).toLowerCase() )
, plural = i.pluralize( singular );
route = [];
route.push( '[POST] /' + singular + '/?' )
route.push( '/' + singular + '/:id/?' );
route.push( '/' + singular + '/:id/:action/?' );
route.push( '/' + plural + '/?' );
route.push( '/' + plural + '/:action/?' );
route = route.join( '|' );
if ( extendingArgs.length === 2 ) {
extendingArgs[ 0 ].route = route;
} else {
extendingArgs.unshift({ route: route });
}
}
}
// Call extend on the parent
return this._super.apply( this, extendingArgs );
}
},
/* @Prototype */
{
/**
* The Request Object
* @type {Request}
*/
req: null,
/**
* The Response Object
* @type {Response}
*/
res: null,
/**
* The next function provided by connect, used to continue past this controller
* @type {Function}
*/
next: null,
/**
* Is set to the most recent action function that was called
* @type {String}
*/
action: null,
/**
* The name of the default Response handler function
*
* @default 'json'
* @type {String}
*/
resFunc: 'json',
/**
* This will wrap the performanceSafeSetup() function with a try/catch for safety,
* most of the actual construction is done inside of Controller.performanceSafeSetup()
*
* @constructor
* @param {Request} req the Request Object
* @param {Response} res the Response Object
* @param {Function} next connects next() function
* @return {Array} arguments that will be passed to init() to complete the constructor loop
*/
setup: function( req, res, next ) {
this.next = next;
this.req = req;
this.res = res;
try {
return this.performanceSafeSetup( req, res, next );
} catch( e ) {
return [ e ];
}
},
/**
* This is effectively what would be in setup() but instead lives here outside of the try/catch
* so google v8 can optimise the code within
*
* @param {Request} req the Request Object
* @param {Response} res the Response Object
* @param {Function} next connects next() function
* @return {Array} arguments that will be passed to init() to complete the constructor loop
*/
performanceSafeSetup: function( req, res, next ) {
var methodAction = req.method.toLowerCase() + 'Action'
, actionRouting = this.Class.actionRouting
, actionMethod = /\/([a-zA-z\.]+)(\/?|\?.*|\#.*)?$/ig.test( req.url ) ? RegExp.$1 + 'Action' : ( req.params.action !== undefined ? req.params.action : false )
, restfulRouting = this.Class.restfulRouting
, idRegex = this.Class.idRegex
, hasIdParam = req.params && req.params.id !== undefined ? true : false
, id = !!hasIdParam && idRegex.test( req.params.id ) ? req.params.id : false
, hasActionParam = req.params && req.params.action !== undefined ? true : false
, action = !!hasActionParam && !idRegex.test( req.params.action ) ? req.params.action + 'Action' : false;
debug( 'methodAction:' + methodAction );
debug( 'actionMethod:' + actionMethod );
debug( 'actionRouting:' + actionRouting );
debug( 'actionMethod:' + actionMethod );
debug( 'restfulRouting:' + restfulRouting );
debug( 'hasIdParam:' + hasIdParam );
debug( 'id:' + id );
debug( 'hasActionParam:' + hasActionParam );
debug( 'action:' + action );
if ( !!actionRouting && !!hasActionParam && action !== false && typeof this[ action ] === 'function' ) {
debug( 'actionRouting: mapped by url to ' + action );
return [ null, action, next ];
}
if ( actionMethod !== false && typeof this[ actionMethod ] === 'function' ) {
debug( 'actionRouting: mapped by param to ' + actionMethod );
return [ null, actionMethod, next ];
}
if ( !!restfulRouting ) {
if ( methodAction === 'getAction' && !id && typeof this.listAction === 'function' ) {
methodAction = 'listAction';
}
if ( typeof this[ methodAction ] === 'function' ) {
debug( 'restfulRouting mapped to ' + methodAction );
return [ null, methodAction, next ];
}
}
return [ new NoActionException(), null, next ];
},
/**
* The final function in the constructor routine, called eventually after setup() has finished
*
* @param {Error} error any errors encountered during the setup() portion of the constructor
* @param {String} method the name of the method to call on this controller
* @param {Function} next connects next() function
* @return {undefined}
*/
init: function( error, method, next ) {
if ( error && error instanceof NoActionException ) {
debug( 'No route mapping found, calling next()' );
next();
} else {
try {
if ( error ) {
throw error;
}
if ( method !== null ) {
this.action = method;
debug( 'calling ' + this.action );
var promise = this[ method ]( this.req, this.res );
if ( typeof promise === 'object' && typeof promise.then === 'function' && typeof this.proxy === 'function' ) {
promise.then( this.proxy( 'handleServiceMessage' ) ).catch( this.proxy( 'handleServiceMessage' ) );
}
} else {
this.next();
}
} catch( e ) {
this.handleException( e );
}
}
},
send: function( content, code, type ) {
if ( !this.responseSent && !this.res.complete ) {
this.responseSent = true;
var toCall = type || this.resFunc;
if ( code ) {
if ( undefined !== this.res.status ) {
this.res.status( code )[ toCall ]( content );
} else {
this.res[ toCall ]( code, content );
}
} else {
this.res[ toCall ]( content );
}
}
},
render: function( template, data ) {
this.res.render( template, data );
},
handleServiceMessage: function( exception ) {
return this.handleException( exception );
},
handleException: function( exception ) {
this.send({
message: 'Unhandled exception: ' + exception,
stack: exception.stack ? exception.stack.split( '\n' ) : undefined
},
exception.statusCode || 500 );
},
isGet: function() {
return this.req.method.toLowerCase() === 'get';
},
isPost: function() {
return this.req.method.toLowerCase() === 'post';
},
isPut: function() {
return this.req.method.toLowerCase() === 'put';
}
});
module.exports = Controller;