Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

does anyone really user after filters?

  • Loading branch information...
commit d49943a31973443c867e9baedcaf50b1e8cf3e34 1 parent a35cfea
Kellen Presley authored
43 examples/filters/app.js
@@ -35,8 +35,8 @@ Picard.routeSet('selective', function(){
35 35 })
36 36
37 37 this.get('/snappy_path', function(env){
38   - env.onScreen('Hello from the snappy path!') // After an async filter you'll need
39   - }) // to call onScreen in your route handler
  38 + env.onScreen('Hello from the snappy path!')
  39 + })
40 40
41 41 this.get('/login', function(){
42 42 return '<a href="/snappy_path?session_id=' + new Date().getTime() + '">fake login</a>'
@@ -44,4 +44,43 @@ Picard.routeSet('selective', function(){
44 44
45 45 })
46 46
  47 +Picard.routeSet('afters', function(){
  48 +
  49 + this.after(function(env){ // matches /.*/
  50 + // do stuff
  51 + return true // pass to the next after filter
  52 + })
  53 +
  54 + this.after('fake', function(env){ // only matches routes with fake
  55 + // do other stuff
  56 + // return or not, doesn't matter at this point
  57 + })
  58 +
  59 + this.get('/fake_path', function(env){
  60 + return 'Hello from the fake path!'
  61 + })
  62 +
  63 + ////
  64 +
  65 + this.after('foo', function(env){
  66 + env.foo = 'bar!'
  67 + env.filterDone()
  68 + })
  69 +
  70 + this.after('foo', function(env){
  71 + process.nextTick(function(){
  72 + env.filterDone() // next after filter
  73 + })
  74 + })
  75 +
  76 + this.after(function(env){
  77 + require('sys').puts(env.foo) // "bar!"
  78 + })
  79 +
  80 + this.get('/foo_path', function(env){
  81 + return 'Hello from the foo path!'
  82 + })
  83 +
  84 +})
  85 +
47 86 Picard.start()
12 lib/picard/filters.js
@@ -10,6 +10,18 @@ var Filter = {
10 10 matcher: new RegExp(matcher), handler: cb
11 11 })
12 12 return this
  13 + },
  14 + after: function(matcher, cb){
  15 + if ( toString.call(matcher) == '[object Function]' ){
  16 + cb = matcher
  17 + matcher = '.*'
  18 + }
  19 + if ( !('afters' in this) ) this.afters = []
  20 +
  21 + this.afters.push({
  22 + matcher: new RegExp(matcher), handler: cb
  23 + })
  24 + return this
13 25 }
14 26 }
15 27
17 lib/picard/request.js
@@ -192,6 +192,23 @@ var RequestExtensions = {
192 192 } catch(ex) {
193 193 req.handleException(ex)
194 194 }
  195 + } else if (req.afterFilters && req.afterFilters.length > 0 ) {
  196 +
  197 + filter = req.afterFilters.shift()
  198 +
  199 + try {
  200 + res = filter.handler(req) // call the filter
  201 +
  202 + if ( res === false ){
  203 + return // stop execution
  204 + } else if ( res != undefined ) { // not false or undefined, so keep going
  205 + req.filterDone()
  206 + }
  207 +
  208 + } catch (ex) {
  209 + req.log('error encountered in after filter: ')
  210 + req.log(ex)
  211 + }
195 212
196 213 } else { // we're done filtering
197 214 req.afterMainHandler(req.route.handler(req))
47 lib/picard/routing.js
@@ -21,50 +21,53 @@ var Routes = {
21 21 return routingAPI
22 22 },
23 23
24   - executeCallback: function(request, cb){
25   - var routesByType = Routes.byRestType(request),
  24 + executeCallback: function(req, cb){
  25 + var routesByType = Routes.byRestType(req),
26 26 route, matches, path
27 27
28 28 for(var i=0, l = routesByType.length; i < l; i++){
29 29 route = routesByType[i]
30   - path = request.parsedUrl().pathname
  30 + path = req.parsedUrl().pathname
31 31 matches = path.match(route.path)
32 32
33 33 if( matches ){ // incoming request matches route
34   - if ( request.route ){
35   - if ( request.route == route )
36   - request.route = null // match the next route
  34 + if ( req.route ){
  35 + if ( req.route == route )
  36 + req.route = null // match the next route
37 37 continue
38 38 }
39 39
40   - request.extractRouteParams(route, matches)
  40 + req.extractRouteParams(route, matches)
41 41
42 42 try {
43   - request.route = route
  43 + req.route = route
44 44
45 45 if ( route.routeSet ){
46 46
47 47 if ( route.routeSet.befores ){
48   - route.routeSet.setMatchingBeforeFiltersOn(request, path)
49   - request.afterMainHandler = cb
50   - request.filterDone()
  48 + req.afterMainHandler = cb
  49 + route.routeSet.setMatchingBeforeFiltersOn(req, path)
51 50 } else {
52   - cb(route.handler(request)) // default
  51 +
  52 + if ( route.routeSet.afters )
  53 + route.routeSet.setMatchingAfterFiltersOn(req, path)
  54 +
  55 + cb(route.handler(req))
53 56 }
54 57
55 58 } else {
56   - cb(route.handler(request)) // call programmer defined action
  59 + cb(route.handler(req)) // call programmer defined action
57 60 }
58 61 } catch(ex) {
59   - request.handleException(ex)
  62 + req.handleException(ex)
60 63 }
61 64 }
62 65 }
63   - if ( !request.route )
  66 + if ( !req.route )
64 67 cb('static')
65 68 },
66   - byRestType: function(request){
67   - switch( (request._method || request.method).toUpperCase() ) {
  69 + byRestType: function(req){
  70 + switch( (req._method || req.method).toUpperCase() ) {
68 71 case 'GET' : return Routes.getRoutes
69 72 case 'POST' : return Routes.postRoutes
70 73 case 'PUT' : return Routes.putRoutes
@@ -197,6 +200,16 @@ RouteSet.prototype = {
197 200 req.beforeFilters.push(this.befores[i])
198 201 }
199 202 }
  203 + req.filterDone()
  204 + },
  205 + setMatchingAfterFiltersOn: function(req, path){
  206 + req.afterFilters = []
  207 + for(var i=0, len = this.afters.length; i<len; i++){
  208 + if ( this.afters[i].matcher.test(path) ){
  209 + req.afterFilters.push(this.afters[i])
  210 + }
  211 + }
  212 + req.filterDone()
200 213 }
201 214 }
202 215
22 spec/filters/filters_spec.js
@@ -32,3 +32,25 @@ describe('advanced before filters', function(){
32 32 })
33 33
34 34 })
  35 +
  36 +describe('after filters', function(){
  37 +
  38 + it('should allow after filters', function(){
  39 + testReq('GET', '/fake_path', function(status, _, body){
  40 + expect(status).toEqual(200)
  41 + expect(body).toMatch('fake path')
  42 + asyncSpecDone()
  43 + })
  44 + asyncSpecWait()
  45 + })
  46 +
  47 + it('should honor onScreen calls from the before filters', function(){
  48 + testReq('GET', '/foo_path', function(status, _, body){
  49 + expect(status).toEqual(200)
  50 + expect(body).toMatch('foo path')
  51 + asyncSpecDone()
  52 + })
  53 + asyncSpecWait()
  54 + })
  55 +
  56 +})

0 comments on commit d49943a

Please sign in to comment.
Something went wrong with that request. Please try again.