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