Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Give 0.8.x the new preprocessing API. #14

Merged
merged 3 commits into from

1 participant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 63 additions and 20 deletions.
  1. +63 −20 Trailer.js
View
83 Trailer.js
@@ -123,37 +123,38 @@ data = trailer.data = {
}
};
-http.ServerResponse.prototype.error = function ( code ) {
+http.ServerResponse.prototype.error = function () {
var i, o;
i = this.request,
- o = this,
- o.status = http.STATUS_CODES[ code ] ? code : 500;
+ o = this;
- if ( i.conf.errors && i.conf.errors[ code ] ) {
- if ( Function.is( i.conf.errors[ code ] ) ) {
- i.conf.errors[ code ]( i, o )
+ o.extend.apply( this, arguments )
+
+ if ( i.conf.errors && i.conf.errors[ i.status ] ) {
+ if ( Function.is( i.conf.errors[ i.status ] ) ) {
+ i.conf.errors[ i.status ]( i, o )
} else {
- path.exists( i.conf.errors[ code ], function ( exists ) {
+ path.exists( i.conf.errors[ i.status ], function ( exists ) {
if ( exists ) {
- fs.readFile( i.conf.errors[ code ], function ( error, content ) {
+ fs.readFile( i.conf.errors[ i.status ], function ( error, content ) {
o.finish( content )
})
} else {
- path.exists( path.join( i.conf.root, i.conf.errors[ code ] ), function ( exists ) {
+ path.exists( path.join( i.conf.root, i.conf.errors[ i.status ] ), function ( exists ) {
if ( exists ) {
- fs.readFile( path.join( i.conf.root, i.conf.errors[ code ] ), function ( error, content ) {
+ fs.readFile( path.join( i.conf.root, i.conf.errors[ i.status ] ), function ( error, content ) {
o.finish( content )
})
} else {
- o.finish( i.conf.errors[ code ] )
+ o.finish( i.conf.errors[ i.status ] )
}
})
}
})
}
} else {
- o.finish( code + ": " + http.STATUS_CODES[ code ] )
+ o.finish( i.status + ": " + http.STATUS_CODES[ i.status ] )
}
return this
@@ -244,6 +245,8 @@ server.on( 'request', function ( i, o ) {
})
trailer.GET = trailer.POST = trailer.HEAD = function ( i, o ) {
+
+ var other;
if ( i.conf.gzip && /gzip/.test( i.headers[ "accept-encoding" ] ) ) {
o.extend( { "Content-Encoding": "gzip" } )
@@ -253,14 +256,11 @@ trailer.GET = trailer.POST = trailer.HEAD = function ( i, o ) {
o.stream = o
}
- if ( i.conf.api && Function.is( i.conf.api[ i.up ] ) ) {
- i.conf.api[ i.up ]( i, o, i.extra )
- } else if ( i.conf.api && Function.is( i.conf.api[ i.extra ] ) ) {
- i.conf.api[ i.extra ]( i, o, i.up )
- } else if ( !i.depth && i.conf.redirect && i.conf.redirect[ i.url.pathname ] ) {
- o.extend( { "Location": i.conf.redirect[ i.url.pathname ] } )
- .error( 307 )
- } else {
+ other = i.handler.preprocessors.some( function ( preprocessor ) {
+ return preprocessor( i, o )
+ } )
+
+ if ( !other ) {
path.exists( i.path, function ( exists ) {
if ( exists ) {
o.status = 200
@@ -379,6 +379,49 @@ trailer.GET = trailer.POST = trailer.HEAD = function ( i, o ) {
}
}
+trailer.GET.preprocessors = []
+
+trailer.GET.preprocessors.push(
+ function ( i, o ) {
+ if ( i.conf.api ) {
+ if ( i.conf.api[ i.up ] ) {
+ return i.conf.api[ i.up ]( i, o, i.extra )
+ } else if ( i.conf.api[ i.extra ] ) {
+ return i.conf.api[ i.extra ]( i, o, i.up )
+ }
+ }
+ },
+
+ function ( i, o ) {
+ var prop, targets, target, pass;
+
+ if ( !i.depth && i.conf.redirect ) {
+ if ( i.conf.redirect[ i.url ] ) {
+ o.error( 302, {
+ "Location": i.conf.redirect[ i.url ]
+ } )
+
+ return true
+ } else {
+ targets = Object.keys( i.conf.redirect ).filter( function ( target ) {
+ return target >= 300 && target < 400
+ } )
+
+ for ( target = 0; target < targets.length && !pass; target++ ) {
+ if ( i.conf.redirect[ target ][ i.url ] ) {
+ pass = true
+ o.error( target, {
+ "Location": i.conf.redirect[ target ][ i.url ]
+ } )
+
+ return true
+ }
+ }
+ }
+ }
+ }
+)
+
Something went wrong with that request. Please try again.