Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Indent with 2 spaces instead of 4

It's a preference I picked up while I was gone
  • Loading branch information...
commit 2064861f473415fc7612ea22c10ae010d6ade5fd 1 parent 15392f2
@partheseas authored
Showing with 231 additions and 235 deletions.
  1. +231 −235 trailer.js
View
466 trailer.js
@@ -53,19 +53,19 @@ tl.MimeDictionary.prototype.define = function ( type, extensions ) {
var prop;
if ( String.is( type ) ) {
- if ( String.is( extensions ) ) {
- extensions = [ extensions ]
- }
-
- extensions.forEach( function ( extension ) {
- this.dictionary.extensions[ extension ] = type
- }, this )
-
- this.dictionary.mimes[ type ] = extensions[ 0 ]
+ if ( String.is( extensions ) ) {
+ extensions = [ extensions ]
+ }
+
+ extensions.forEach( function ( extension ) {
+ this.dictionary.extensions[ extension ] = type
+ }, this )
+
+ !this.dictionary.mimes[type]&&( this.dictionary.mimes[ type ] = extensions[ 0 ] )
} else {
- for ( prop in type ) {
- this.define( prop, type[ prop ] )
- }
+ for ( prop in type ) {
+ this.define( prop, type[ prop ] )
+ }
}
}
@@ -105,63 +105,63 @@ tl.MimeDictionary.prototype.fromApacheFile = function ( path, encoding ) {
tl.hitch = function ( conf ) {
- var prop, indexes;
+ var prop, indexes;
- if ( !conf ) {
- tl.warning( "hitch", "No configuration given!" )
- return false
- }
-
- if ( conf.headers ) for ( prop in basics.headers ) if ( basics.headers.hasOwnProperty( prop ) && !conf.headers.hasOwnProperty( prop ) ) {
- conf.headers[ prop ] = basics.headers[ prop ]
- }
-
- if ( conf.mimes !== false ) {
- !conf.mimes&&( conf.mimes = {} )
-
- for ( prop in basics.mimes ) if ( basics.mimes.hasOwnProperty( prop ) && !conf.mimes.hasOwnProperty( prop ) ) {
- conf.mimes[ prop ] = basics.mimes[ prop ]
- }
- }
-
- if ( Array.is( conf.indexes ) ) {
- indexes = {}
- conf.indexes.forEach( function ( index ) {
- indexes[ index ] = 0
- } )
- conf.indexes = indexes
- } else if ( !conf.indexes ) {
- conf.indexes = {}
- }
-
- if ( conf.host ) {
- configurations[ conf.host ] = conf
+ if ( !conf ) {
+ tl.warning( "hitch", "No configuration given!" )
+ return false
+ }
+
+ if ( conf.headers ) for ( prop in basics.headers ) if ( basics.headers.hasOwnProperty( prop ) && !conf.headers.hasOwnProperty( prop ) ) {
+ conf.headers[ prop ] = basics.headers[ prop ]
+ }
+
+ if ( conf.mimes !== false ) {
+ !conf.mimes&&( conf.mimes = {} )
+
+ for ( prop in basics.mimes ) if ( basics.mimes.hasOwnProperty( prop ) && !conf.mimes.hasOwnProperty( prop ) ) {
+ conf.mimes[ prop ] = basics.mimes[ prop ]
}
-
- if ( conf.hosts ) conf.hosts.forEach( function ( host ) {
- configurations[ host ] = conf
+ }
+
+ if ( Array.is( conf.indexes ) ) {
+ indexes = {}
+ conf.indexes.forEach( function ( index ) {
+ indexes[ index ] = 0
} )
-
- !configurations.fallback&&( configurations.fallback = conf )
- !server._handle&&( server.listen( Number( conf.port ) || 80 ) )
-
- conf._emitter = new tl.EventEmitter()
-
- // After the given arguments have been assorted, emit that Trailer has received a configuration.
- tl.emit( "hitch", conf, conf._emitter )
-
- return conf._emitter
+ conf.indexes = indexes
+ } else if ( !conf.indexes ) {
+ conf.indexes = {}
+ }
+
+ if ( conf.host ) {
+ configurations[ conf.host ] = conf
+ }
+
+ if ( conf.hosts ) conf.hosts.forEach( function ( host ) {
+ configurations[ host ] = conf
+ } )
+
+ !configurations.fallback&&( configurations.fallback = conf )
+ !server._handle&&( server.listen( Number( conf.port ) || 80 ) )
+
+ conf._emitter = new tl.EventEmitter()
+
+ // After the given arguments have been assorted, emit that Trailer has received a configuration.
+ tl.emit( "hitch", conf, conf._emitter )
+
+ return conf._emitter
}
server = tl.server = http.createServer(),
basics = tl.basics = {
- "headers": {
- "Server": "Trailer/" + tl.version + " Node/" + process.versions.node
- },
-
- "mimes": {
- "*": "text/plain"
- }
+ "headers": {
+ "Server": "Trailer/" + tl.version + " Node/" + process.versions.node
+ },
+
+ "mimes": {
+ "*": "text/plain"
+ }
},
configurations = tl.configurations = {},
data = tl.data = {
@@ -303,202 +303,198 @@ server.on( "request", function ( i, o ) {
})
tl.GET = tl.POST = tl.HEAD = function ( i, o ) {
-
- var
- other = i.handler.preprocessors.some( function ( preprocessor ) {
- return preprocessor( i, o )
- } );
-
- if ( !other ) {
- fs.exists( i.path, function ( exists ) {
- if ( exists ) {
- o.status = 200
- fs.stat( i.path, function ( error, stats ) {
- if ( i.cacheDate && ( i.cacheDate.getTime() === stats.mtime.getTime() ) ) {
- o.status = 304
- }
-
- // The client only wants headers, or has a cached version.
- if ( i.method === "HEAD" || o.status === 304 ) {
- o.body = null
- }
+ var
+ other = i.handler.preprocessors.some( function ( preprocessor ) {
+ return preprocessor( i, o )
+ } );
+
+ if ( !other ) {
+ fs.exists( i.path, function ( exists ) {
+ if ( exists ) {
+ o.status = 200
+ fs.stat( i.path, function ( error, stats ) {
+ if ( i.cacheDate && ( i.cacheDate.getTime() === stats.mtime.getTime() ) ) {
+ o.status = 304
+ }
+
+ // The client only wants headers, or has a cached version.
+ if ( i.method === "HEAD" || o.status === 304 ) {
+ o.body = null
+ }
+
+ if ( stats.isFile() ) {
+ i.conf.mimes&&!o.headers["Content-Type"]&&( o.headers[ "Content-Type" ] = i.conf.mimes[ path.extname( i.path ) ] || i.conf.mimes[ "*" ] )
+ !o.headers["Content-Length"]&&( o.headers[ "Content-Length" ] = stats.size )
+ !o.headers["Last-Modified"]&&( o.headers[ "Last-Modified" ] = stats.mtime.toUTCString() )
- if ( stats.isFile() ) {
- i.conf.mimes&&!o.headers["Content-Type"]&&( o.headers[ "Content-Type" ] = i.conf.mimes[ path.extname( i.path ) ] || i.conf.mimes[ "*" ] )
- !o.headers["Content-Length"]&&( o.headers[ "Content-Length" ] = stats.size )
- !o.headers["Last-Modified"]&&( o.headers[ "Last-Modified" ] = stats.mtime.toUTCString() )
-
- if ( o.body != null ) {
- fs.readFile( i.path, function ( error, content ) {
- if ( error ) {
- if ( error.message.match( /EACCESS/ ) ) {
- o.error( 403 )
- } else {
- o.error( 500 )
- }
- } else {
- o.body = content;
- o.emitter.emit( "finalized", i, o )
- }
- })
+ if ( o.body != null ) {
+ fs.readFile( i.path, function ( error, content ) {
+ if ( error ) {
+ if ( error.message.match( /EACCESS/ ) ) {
+ o.error( 403 )
} else {
- o.emitter.emit( "finalized", i, o )
+ o.error( 500 )
}
- } else if ( stats.isDirectory() ) {
- var check,
- indexes = Object.keys( i.conf.indexes );
+ } else {
+ o.body = content;
+ o.emitter.emit( "finalized", i, o )
+ }
+ })
+ } else {
+ o.emitter.emit( "finalized", i, o )
+ }
+ } else if ( stats.isDirectory() ) {
+ var check,
+ indexes = Object.keys( i.conf.indexes );
+
+ ( check = function check( index ) {
+ var file;
+
+ file = indexes[ index ]
+ if ( index < indexes.length ) {
+ if ( i.depth <= i.conf.indexes[ file ] ) {
+ fs.exists( path.resolve( i.path, file ), function ( exists ) {
+ if ( exists ) {
+ i.path = path.resolve( i.path, file )
+ i.handler( i, o )
+ } else {
+ check( index + 1 )
+ }
+ } )
+ } else {
+ check( index + 1 )
+ }
+ } else {
+ if ( i.depth === 1 && i.extra === "directory.html" ) {
+ fs.readdir( i.path, function ( error, files ) {
+ if ( error ) {
+ if ( error.message.match( /EACCESS/ ) ) {
+ o.error( 403 )
+ } else {
+ o.error( 500 )
+ }
+ } else {
+ files.forEach( function ( name ) {
+ name.charAt(0)!=="."&&( o.body += "- [" + name + "](" + path.join( i.up, name ) + ")\n" )
+ })
+
+ o.extend( { "Content-Type": "text/html" },
+ markdown.toHTML( o.body ) )
- ( check = function check( index ) {
- var file;
-
- file = indexes[ index ]
- if ( index < indexes.length ) {
- if ( i.depth <= i.conf.indexes[ file ] ) {
- fs.exists( path.resolve( i.path, file ), function ( exists ) {
- if ( exists ) {
- i.path = path.resolve( i.path, file )
- i.handler( i, o )
- } else {
- check( index + 1 )
- }
- } )
- } else {
- check( index + 1 )
- }
+ o.emitter.emit( "finalized", i, o )
+ }
+ })
+ } else if ( i.depth === 1 && i.extra === "directory.json" ) {
+ fs.readdir( i.path, function ( error, files ) {
+ if ( error ) {
+ if ( error.message.match( /EACCESS/ ) ) {
+ o.error( 403 )
} else {
- if ( i.depth === 1 && i.extra === "directory.html" ) {
- fs.readdir( i.path, function ( error, files ) {
- if ( error ) {
- if ( error.message.match( /EACCESS/ ) ) {
- o.error( 403 )
- } else {
- o.error( 500 )
- }
- } else {
- files.forEach( function ( name ) {
- name.charAt(0)!=="."&&( o.body += "- [" + name + "](" + path.join( i.up, name ) + ")\n" )
- })
-
- o.extend( { "Content-Type": "text/html" },
- markdown.toHTML( o.body ) )
-
- o.emitter.emit( "finalized", i, o )
- }
- })
- } else if ( i.depth === 1 && i.extra === "directory.json" ) {
- fs.readdir( i.path, function ( error, files ) {
- if ( error ) {
- if ( error.message.match( /EACCESS/ ) ) {
- o.error( 403 )
- } else {
- o.error( 500 )
- }
- } else {
- o.extend( { "Content-Type": "application/json" },
- JSON.stringify( files.filter( function ( name ) {
- return name.charAt( 0 ) !== "."
- } ) )
- )
-
- o.emitter.emit( "finalized", i, o )
- }
- })
- } else {
- o.error( 404 )
- }
+ o.error( 500 )
}
- } )( 0 )
+ } else {
+ o.extend( { "Content-Type": "application/json" },
+ JSON.stringify( files.filter( function ( name ) {
+ return name.charAt( 0 ) !== "."
+ } ) )
+ )
+
+ o.emitter.emit( "finalized", i, o )
+ }
+ })
+ } else {
+ o.error( 404 )
+ }
}
- })
- } else {
- if ( path.relative( '/', i.up ) ) {
- i.up = path.join( i.up, ".." )
- i.extra = path.relative( i.up, i.url.pathname )
-
- i.path = path.join( i.conf.root, i.up )
-
- i.depth++
-
- i.handler( i, o )
- } else {
- o.error( 404 )
+ } )( 0 )
}
- }
- })
- }
+ })
+ } else {
+ if ( path.relative( '/', i.up ) ) {
+ i.up = path.join( i.up, ".." )
+ i.extra = path.relative( i.up, i.url.pathname )
+ i.depth++
+
+ i.path = path.join( i.conf.root, i.up )
+ i.handler( i, o )
+ } else {
+ o.error( 404 )
+ }
+ }
+ })
+ }
}
tl.GET.preprocessors = []
+
+
var
suffix = /(\/.+)\/$/;
-
tl.GET.preprocessors.push(
- function clean( i, o ) {
- var r;
- if ( r = suffix.exec( i.url.pathname ) ) {
- i.url.pathname = r[ 1 ]
-
- o.finish( 302, {
- "Location": url.format( i.url )
- } )
- return true
- }
- },
-
- function api( i, o ) {
- if ( i.conf.api ) {
- if ( i.conf.api[ i.up ] ) {
- try {
- return i.conf.api[ i.up ]( i, o, i.extra )
- } catch ( error ) {
- o.error( 500 )
- return true
- }
- } else if ( i.conf.api[ i.extra ] ) {
- try {
- return i.conf.api[ i.extra ]( i, o, i.up )
- } catch ( error ) {
- o.error( 500 )
- return true
- }
- }
+ function clean( i, o ) {
+ var r;
+ if ( r = suffix.exec( i.url.pathname ) ) {
+ i.url.pathname = r[ 1 ]
+
+ o.finish( 302, {
+ "Location": url.format( i.url )
+ } )
+ return true
+ }
+ },
+
+ function api( i, o ) {
if ( i.conf.api ) {
+ if ( i.conf.api[ i.up ] ) {
+ try {
+ return i.conf.api[ i.up ]( i, o, i.extra )
+ } catch ( error ) {
+ o.error( 500 )
tl.error( i.up, i.up + " failed for " + i.url.pathname + "\n\""
+ error.stack + "\"" )
+ return true
}
- },
-
- function redirect( 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
- }
- }
- }
+ } else if ( i.conf.api[ i.extra ] ) {
+ try {
+ return i.conf.api[ i.extra ]( i, o, i.up )
+ } catch ( error ) {
+ o.error( 500 )
tl.error( i.extra, i.extra + " failed for " + i.url.pathname + "\n\""
+ error.stack + "\"" )
+ return true
}
+ }
+ }
+ },
+
+ function redirect( 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
+ }
+ }
+ }
}
},
Please sign in to comment.
Something went wrong with that request. Please try again.