@ -2,10 +2,11 @@
const arrayify = require ( 'array-back' )
const path = require ( 'path' )
const url = require ( 'url' )
const debug = require ( 'debug' ) ( 'local-web-server' )
const debug = require ( './ debug' )
const mw = require ( './middleware' )
const t = require ( 'typical' )
const compose = require ( 'koa-compose' )
const flatten = require ( 'reduce-flatten' )
class MiddlewareStack extends Array {
add ( middleware ) {
@ -36,9 +37,9 @@ class MiddlewareStack extends Array {
description : "A list of URL rewrite rules. For each rule, separate the 'from' and 'to' routes with '->'. Whitespace surrounded the routes is ignored. E.g. '/from -> /to'."
} ,
middleware : function ( cliOptions ) {
const options = arrayify ( cliOptions . middleware . rewrite || rewriteRules )
const options = arrayify ( cliOptions . rewrite || rewriteRules )
if ( options . length ) {
options . forEach ( route => {
return options . map ( route => {
if ( route . to ) {
/* `to` address is remote if the url specifies a host */
if ( url . parse ( route . to ) . host ) {
@ -75,7 +76,7 @@ class MiddlewareStack extends Array {
description : 'A list of forbidden routes.'
} ,
middleware : function ( cliOptions ) {
forbidList = arrayify ( cliOptions . middleware . forbid || forbidList )
forbidList = arrayify ( cliOptions . forbid || forbidList )
if ( forbidList . length ) {
const pathToRegexp = require ( 'path-to-regexp' )
debug ( 'forbid' , forbidList . join ( ', ' ) )
@ -101,7 +102,7 @@ class MiddlewareStack extends Array {
description : 'Disable etag-based caching - forces loading from disk each request.'
} ,
middleware : function ( cliOptions ) {
const noCache = cliOptions . middleware [ 'no-cache' ]
const noCache = cliOptions [ 'no-cache' ]
if ( ! noCache ) {
return [
require ( 'koa-conditional-get' ) ( ) ,
@ -117,7 +118,7 @@ class MiddlewareStack extends Array {
addMimeType ( mime ) {
this . push ( {
middleware : function ( cliOptions ) {
mime = cliOptions . middleware . mi me || mime
mime = cliOptions . mime || mime
if ( mime ) {
debug ( 'mime override' , JSON . stringify ( mime ) )
return mw . mime ( mime )
@ -135,8 +136,8 @@ class MiddlewareStack extends Array {
description : 'Serve gzip-compressed resources, where applicable.'
} ,
middleware : function ( cliOptions ) {
compress = t . isDefined ( cliOptions . middleware . compress )
? cliOptions . middleware . compress
compress = t . isDefined ( cliOptions . compress )
? cliOptions . compress
: compress
if ( compress ) {
debug ( 'compression' , 'enabled' )
@ -158,9 +159,9 @@ class MiddlewareStack extends Array {
description : "If a format is supplied an access log is written to stdout. If not, a dynamic statistics view is displayed. Use a preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> :url')."
} ,
middleware : function ( cliOptions ) {
format = cliOptions . middleware [ 'log-format' ] || format
format = cliOptions [ 'log-format' ] || format
if ( cliOptions . misc . verbose && ! format ) {
if ( cliOptions . verbose && ! format ) {
format = 'none'
}
@ -190,11 +191,11 @@ class MiddlewareStack extends Array {
addMockResponses ( mocks ) {
this . push ( {
middleware : function ( cliOptions ) {
mocks = arrayify ( cliOptions . middleware . m ocks || mocks )
mocks . forEach ( mock => {
mocks = arrayify ( cliOptions . mocks || mocks )
return mocks . map ( mock => {
if ( mock . module ) {
// TODO: ENSURE cliOptions.static.root is correct value
mock . responses = require ( path . resolve ( path . join ( cliOptions . static . root , mock . module ) ) )
const modulePath = path . resolve ( path . join ( cliOptions . directory , mock . module ) )
mock . responses = require ( modulePath )
}
if ( mock . responses ) {
@ -220,13 +221,13 @@ class MiddlewareStack extends Array {
description : 'Path to a Single Page App, e.g. app.html.'
} ,
middleware : function ( cliOptions ) {
spa = t . isDefined ( cliOptions . middleware . spa ) ? cliOptions . middleware . spa : spa
spa = t . isDefined ( cliOptions . spa ) ? cliOptions . spa : spa
if ( spa ) {
const historyApiFallback = require ( 'koa-connect-history-api-fallback' )
debug ( 'SPA' , spa )
return historyApiFallback ( {
index : spa ,
verbose : cliOptions . misc . verbose
verbose : cliOptions . verbose
} )
}
}
@ -242,12 +243,12 @@ class MiddlewareStack extends Array {
description : 'Root directory, defaults to the current directory.'
} ,
middleware : function ( cliOptions ) {
root = cliOptions . middleware . directory || root || process . cwd ( )
cliOptions . directory = cliOptions . directory || root || process . cwd ( )
options = Object . assign ( { hidden : true } , options )
// console.log(root, options, cliOptions)
if ( root ) {
if ( cliOptions . directory ) {
const serve = require ( 'koa-static' )
return serve ( root , options )
return serve ( cliOptions . directory , options )
}
}
} )
@ -258,7 +259,7 @@ class MiddlewareStack extends Array {
addIndex ( path , options ) {
this . push ( {
middleware : function ( cliOptions ) {
path = cliOptions . middleware . directory || path || process . cwd ( )
path = cliOptions . directory || path || process . cwd ( )
options = Object . assign ( { icons : true , hidden : true } , options )
if ( path ) {
const serveIndex = require ( 'koa-serve-index' )
@ -270,7 +271,6 @@ class MiddlewareStack extends Array {
}
getOptionDefinitions ( ) {
const flatten = require ( 'reduce-flatten' )
return this
. filter ( mw => mw . optionDefinitions )
. map ( mw => mw . optionDefinitions )
@ -283,8 +283,13 @@ class MiddlewareStack extends Array {
compose ( options ) {
const convert = require ( 'koa-convert' )
const middlewareStack = this
. filter ( mw => mw )
. map ( mw => compose ( arrayify ( mw . middleware ( options ) ) . map ( convert ) ) )
. filter ( mw => mw . middleware )
. map ( mw => mw . middleware )
. map ( middleware => middleware ( options ) )
. filter ( middleware => middleware )
. reduce ( flatten , [ ] )
. map ( convert )
// console.error(require('util').inspect(middlewareStack, { depth: 3, colors: true }))
return compose ( middlewareStack )
}
}