@ -2,10 +2,11 @@
const arrayify = require ( 'array-back' )
const arrayify = require ( 'array-back' )
const path = require ( 'path' )
const path = require ( 'path' )
const url = require ( 'url' )
const url = require ( 'url' )
const debug = require ( 'debug' ) ( 'local-web-server' )
const debug = require ( './ debug' )
const mw = require ( './middleware' )
const mw = require ( './middleware' )
const t = require ( 'typical' )
const t = require ( 'typical' )
const compose = require ( 'koa-compose' )
const compose = require ( 'koa-compose' )
const flatten = require ( 'reduce-flatten' )
class MiddlewareStack extends Array {
class MiddlewareStack extends Array {
add ( middleware ) {
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'."
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 ) {
middleware : function ( cliOptions ) {
const options = arrayify ( cliOptions . middleware . rewrite || rewriteRules )
const options = arrayify ( cliOptions . rewrite || rewriteRules )
if ( options . length ) {
if ( options . length ) {
options . forEach ( route => {
return options . map ( route => {
if ( route . to ) {
if ( route . to ) {
/* `to` address is remote if the url specifies a host */
/* `to` address is remote if the url specifies a host */
if ( url . parse ( route . to ) . host ) {
if ( url . parse ( route . to ) . host ) {
@ -75,7 +76,7 @@ class MiddlewareStack extends Array {
description : 'A list of forbidden routes.'
description : 'A list of forbidden routes.'
} ,
} ,
middleware : function ( cliOptions ) {
middleware : function ( cliOptions ) {
forbidList = arrayify ( cliOptions . middleware . forbid || forbidList )
forbidList = arrayify ( cliOptions . forbid || forbidList )
if ( forbidList . length ) {
if ( forbidList . length ) {
const pathToRegexp = require ( 'path-to-regexp' )
const pathToRegexp = require ( 'path-to-regexp' )
debug ( 'forbid' , forbidList . join ( ', ' ) )
debug ( 'forbid' , forbidList . join ( ', ' ) )
@ -101,7 +102,7 @@ class MiddlewareStack extends Array {
description : 'Disable etag-based caching - forces loading from disk each request.'
description : 'Disable etag-based caching - forces loading from disk each request.'
} ,
} ,
middleware : function ( cliOptions ) {
middleware : function ( cliOptions ) {
const noCache = cliOptions . middleware [ 'no-cache' ]
const noCache = cliOptions [ 'no-cache' ]
if ( ! noCache ) {
if ( ! noCache ) {
return [
return [
require ( 'koa-conditional-get' ) ( ) ,
require ( 'koa-conditional-get' ) ( ) ,
@ -117,7 +118,7 @@ class MiddlewareStack extends Array {
addMimeType ( mime ) {
addMimeType ( mime ) {
this . push ( {
this . push ( {
middleware : function ( cliOptions ) {
middleware : function ( cliOptions ) {
mime = cliOptions . middleware . mi me || mime
mime = cliOptions . mime || mime
if ( mime ) {
if ( mime ) {
debug ( 'mime override' , JSON . stringify ( mime ) )
debug ( 'mime override' , JSON . stringify ( mime ) )
return mw . mime ( mime )
return mw . mime ( mime )
@ -135,8 +136,8 @@ class MiddlewareStack extends Array {
description : 'Serve gzip-compressed resources, where applicable.'
description : 'Serve gzip-compressed resources, where applicable.'
} ,
} ,
middleware : function ( cliOptions ) {
middleware : function ( cliOptions ) {
compress = t . isDefined ( cliOptions . middleware . compress )
? cliOptions . middleware . compress
compress = t . isDefined ( cliOptions . compress )
? cliOptions . compress
: compress
: compress
if ( compress ) {
if ( compress ) {
debug ( 'compression' , 'enabled' )
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')."
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 ) {
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'
format = 'none'
}
}
@ -190,11 +191,11 @@ class MiddlewareStack extends Array {
addMockResponses ( mocks ) {
addMockResponses ( mocks ) {
this . push ( {
this . push ( {
middleware : function ( cliOptions ) {
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 ) {
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 ) {
if ( mock . responses ) {
@ -220,13 +221,13 @@ class MiddlewareStack extends Array {
description : 'Path to a Single Page App, e.g. app.html.'
description : 'Path to a Single Page App, e.g. app.html.'
} ,
} ,
middleware : function ( cliOptions ) {
middleware : function ( cliOptions ) {
spa = t . isDefined ( cliOptions . middleware . spa ) ? cliOptions . middleware . spa : spa
spa = t . isDefined ( cliOptions . spa ) ? cliOptions . spa : spa
if ( spa ) {
if ( spa ) {
const historyApiFallback = require ( 'koa-connect-history-api-fallback' )
const historyApiFallback = require ( 'koa-connect-history-api-fallback' )
debug ( 'SPA' , spa )
debug ( 'SPA' , spa )
return historyApiFallback ( {
return historyApiFallback ( {
index : spa ,
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.'
description : 'Root directory, defaults to the current directory.'
} ,
} ,
middleware : function ( cliOptions ) {
middleware : function ( cliOptions ) {
root = cliOptions . middleware . directory || root || process . cwd ( )
cliOptions . directory = cliOptions . directory || root || process . cwd ( )
options = Object . assign ( { hidden : true } , options )
options = Object . assign ( { hidden : true } , options )
// console.log(root, options, cliOptions)
// console.log(root, options, cliOptions)
if ( root ) {
if ( cliOptions . directory ) {
const serve = require ( 'koa-static' )
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 ) {
addIndex ( path , options ) {
this . push ( {
this . push ( {
middleware : function ( cliOptions ) {
middleware : function ( cliOptions ) {
path = cliOptions . middleware . directory || path || process . cwd ( )
path = cliOptions . directory || path || process . cwd ( )
options = Object . assign ( { icons : true , hidden : true } , options )
options = Object . assign ( { icons : true , hidden : true } , options )
if ( path ) {
if ( path ) {
const serveIndex = require ( 'koa-serve-index' )
const serveIndex = require ( 'koa-serve-index' )
@ -270,7 +271,6 @@ class MiddlewareStack extends Array {
}
}
getOptionDefinitions ( ) {
getOptionDefinitions ( ) {
const flatten = require ( 'reduce-flatten' )
return this
return this
. filter ( mw => mw . optionDefinitions )
. filter ( mw => mw . optionDefinitions )
. map ( mw => mw . optionDefinitions )
. map ( mw => mw . optionDefinitions )
@ -283,8 +283,13 @@ class MiddlewareStack extends Array {
compose ( options ) {
compose ( options ) {
const convert = require ( 'koa-convert' )
const convert = require ( 'koa-convert' )
const middlewareStack = this
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 )
return compose ( middlewareStack )
}
}
}
}