|
|
@ -8,17 +8,11 @@ const mw = require('./middleware') |
|
|
|
class MiddlewareStack extends Array { |
|
|
|
constructor (options) { |
|
|
|
super() |
|
|
|
this.options = options |
|
|
|
|
|
|
|
options = Object.assign({ |
|
|
|
static: {}, |
|
|
|
serveIndex: { |
|
|
|
options: { |
|
|
|
icons: true, |
|
|
|
hidden: true |
|
|
|
} |
|
|
|
}, |
|
|
|
cacheControl: {}, |
|
|
|
spa: null, |
|
|
|
log: {}, |
|
|
|
compress: false, |
|
|
|
mime: {}, |
|
|
|
forbid: [], |
|
|
@ -31,20 +25,14 @@ class MiddlewareStack extends Array { |
|
|
|
process.env.DEBUG = '*' |
|
|
|
} |
|
|
|
|
|
|
|
const log = options.log |
|
|
|
log.options = log.options || {} |
|
|
|
|
|
|
|
if (options.verbose && !log.format) { |
|
|
|
log.format = 'none' |
|
|
|
} |
|
|
|
this.log = log |
|
|
|
|
|
|
|
if (!options.static.root) options.static.root = process.cwd() |
|
|
|
if (!options.serveIndex.path) options.serveIndex.path = process.cwd() |
|
|
|
options.rewrite = arrayify(options.rewrite) |
|
|
|
options.forbid = arrayify(options.forbid) |
|
|
|
options.mocks = arrayify(options.mocks) |
|
|
|
this.options = options |
|
|
|
} |
|
|
|
|
|
|
|
add (middleware) { |
|
|
|
this.push(middleware) |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -63,14 +51,13 @@ class MiddlewareStack extends Array { |
|
|
|
|
|
|
|
/* rewrite rules */ |
|
|
|
addRewrite () { |
|
|
|
const _ = require('koa-route') |
|
|
|
|
|
|
|
const options = this.options.rewrite |
|
|
|
if (options.length) { |
|
|
|
options.forEach(route => { |
|
|
|
if (route.to) { |
|
|
|
/* `to` address is remote if the url specifies a host */ |
|
|
|
if (url.parse(route.to).host) { |
|
|
|
const _ = require('koa-route') |
|
|
|
debug('proxy rewrite', `${route.from} -> ${route.to}`) |
|
|
|
this.push(_.all(route.from, mw.proxyRequest(route))) |
|
|
|
} else { |
|
|
@ -89,6 +76,7 @@ class MiddlewareStack extends Array { |
|
|
|
See https://github.com/nodejitsu/node-http-proxy/issues/180. */
|
|
|
|
addBodyParser () { |
|
|
|
this.push(require('koa-bodyparser')()) |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* path blacklist */ |
|
|
@ -98,6 +86,7 @@ class MiddlewareStack extends Array { |
|
|
|
debug('forbid', options.join(', ')) |
|
|
|
this.push(mw.blacklist(options)) |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* cache */ |
|
|
@ -106,6 +95,7 @@ class MiddlewareStack extends Array { |
|
|
|
this.push(require('koa-conditional-get')()) |
|
|
|
this.push(require('koa-etag')()) |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* mime-type overrides */ |
|
|
@ -115,6 +105,7 @@ class MiddlewareStack extends Array { |
|
|
|
debug('mime override', JSON.stringify(options)) |
|
|
|
this.push(mw.mime(options)) |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* compress response */ |
|
|
@ -124,28 +115,36 @@ class MiddlewareStack extends Array { |
|
|
|
debug('compression', 'enabled') |
|
|
|
this.push(compress()) |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* Logging */ |
|
|
|
addLogging () { |
|
|
|
const log = this.log |
|
|
|
if (log.format !== 'none') { |
|
|
|
addLogging (format, options) { |
|
|
|
format = this.options.server['log-format'] || format |
|
|
|
options = options || {} |
|
|
|
|
|
|
|
if (this.options.verbose && !format) { |
|
|
|
format = 'none' |
|
|
|
} |
|
|
|
|
|
|
|
if (format !== 'none') { |
|
|
|
const morgan = require('koa-morgan') |
|
|
|
|
|
|
|
if (!log.format) { |
|
|
|
if (!format) { |
|
|
|
const streamLogStats = require('stream-log-stats') |
|
|
|
log.options.stream = streamLogStats({ refreshRate: 500 }) |
|
|
|
this.push(morgan('common', log.options)) |
|
|
|
} else if (log.format === 'logstalgia') { |
|
|
|
options.stream = streamLogStats({ refreshRate: 500 }) |
|
|
|
this.push(morgan('common', options)) |
|
|
|
} else if (format === 'logstalgia') { |
|
|
|
morgan.token('date', () => { |
|
|
|
var d = new Date() |
|
|
|
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '') |
|
|
|
}) |
|
|
|
this.push(morgan('combined', log.options)) |
|
|
|
this.push(morgan('combined', options)) |
|
|
|
} else { |
|
|
|
this.push(morgan(log.format, log.options)) |
|
|
|
this.push(morgan(format, options)) |
|
|
|
} |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* Mock Responses */ |
|
|
@ -166,6 +165,7 @@ class MiddlewareStack extends Array { |
|
|
|
this.push(mw.mockResponses(mock.route, mock.target)) |
|
|
|
} |
|
|
|
}) |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* for any URL not matched by static (e.g. `/search`), serve the SPA */ |
|
|
@ -178,24 +178,27 @@ class MiddlewareStack extends Array { |
|
|
|
verbose: this.options.verbose |
|
|
|
})) |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* serve static files */ |
|
|
|
addStatic () { |
|
|
|
const options = this.options.static |
|
|
|
if (options.root) { |
|
|
|
addStatic (root, options) { |
|
|
|
root = this.options.server.directory || root || process.cwd() |
|
|
|
options = Object.assign({ hidden: true }, options) |
|
|
|
if (root) { |
|
|
|
const serve = require('koa-static') |
|
|
|
this.push(serve(options.root, options.options)) |
|
|
|
this.push(serve(root, options)) |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|
|
|
|
|
/* serve directory index */ |
|
|
|
addIndex () { |
|
|
|
const options = this.options.serveIndex |
|
|
|
if (options.path) { |
|
|
|
addIndex (path, options) { |
|
|
|
path = this.options.server.directory || path || process.cwd() |
|
|
|
options = Object.assign({ icons: true, hidden: true }, options) |
|
|
|
if (path) { |
|
|
|
const serveIndex = require('koa-serve-index') |
|
|
|
this.push(serveIndex(options.path, options.options)) |
|
|
|
this.push(serveIndex(path, options)) |
|
|
|
} |
|
|
|
return this |
|
|
|
} |
|
|
|