Files
hiring-test-one/lib/middleware-stack.js

213 lines
5.5 KiB
JavaScript
Raw Normal View History

2016-06-08 23:06:41 +01:00
'use strict'
const arrayify = require('array-back')
const path = require('path')
const url = require('url')
const debug = require('debug')('local-web-server')
const mw = require('./middleware')
2016-06-15 21:02:52 +01:00
const t = require('typical')
2016-06-08 23:06:41 +01:00
class MiddlewareStack extends Array {
constructor (options) {
super()
2016-06-09 22:54:57 +01:00
this.options = options
2016-06-08 23:06:41 +01:00
if (options.verbose) {
process.env.DEBUG = '*'
}
2016-06-09 22:54:57 +01:00
}
add (middleware) {
this.push(middleware)
return this
2016-06-08 23:06:41 +01:00
}
/**
* allow from any origin
*/
addCors () {
this.push(require('kcors')())
return this
}
/* pretty print JSON */
addJson () {
this.push(require('koa-json')())
return this
}
/* rewrite rules */
2016-06-15 21:02:52 +01:00
addRewrite (rewriteRules) {
const options = arrayify(this.options.server.rewrite || rewriteRules)
2016-06-08 23:06:41 +01:00
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) {
2016-06-09 22:54:57 +01:00
const _ = require('koa-route')
2016-06-08 23:06:41 +01:00
debug('proxy rewrite', `${route.from} -> ${route.to}`)
this.push(_.all(route.from, mw.proxyRequest(route)))
} else {
const rewrite = require('koa-rewrite')
const rmw = rewrite(route.from, route.to)
rmw._name = 'rewrite'
this.push(rmw)
}
}
})
}
return this
}
/* must come after rewrite.
See https://github.com/nodejitsu/node-http-proxy/issues/180. */
addBodyParser () {
this.push(require('koa-bodyparser')())
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* path blacklist */
2016-06-15 21:02:52 +01:00
addBlacklist (forbidList) {
forbidList = arrayify(this.options.server.forbid || forbidList)
if (forbidList.length) {
const pathToRegexp = require('path-to-regexp')
debug('forbid', forbidList.join(', '))
this.push(function blacklist (ctx, next) {
if (forbidList.some(expression => pathToRegexp(expression).test(ctx.path))) {
ctx.throw(403, http.STATUS_CODES[403])
} else {
return next()
}
})
2016-06-08 23:06:41 +01:00
}
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* cache */
addCache () {
2016-06-15 21:02:52 +01:00
const noCache = this.options.server['no-cache']
if (!noCache) {
2016-06-08 23:06:41 +01:00
this.push(require('koa-conditional-get')())
this.push(require('koa-etag')())
}
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* mime-type overrides */
2016-06-15 21:02:52 +01:00
addMimeType (mime) {
mime = this.options.server.mime || mime
if (mime) {
debug('mime override', JSON.stringify(mime))
this.push(mw.mime(mime))
2016-06-08 23:06:41 +01:00
}
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* compress response */
2016-06-15 21:02:52 +01:00
addCompression (compress) {
compress = t.isDefined(this.options.server.compress)
? this.options.server.compress
: compress
if (compress) {
2016-06-08 23:06:41 +01:00
debug('compression', 'enabled')
2016-06-15 21:02:52 +01:00
this.push(require('koa-compress')())
2016-06-08 23:06:41 +01:00
}
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* Logging */
2016-06-09 22:54:57 +01:00
addLogging (format, options) {
format = this.options.server['log-format'] || format
options = options || {}
if (this.options.verbose && !format) {
format = 'none'
}
if (format !== 'none') {
2016-06-08 23:06:41 +01:00
const morgan = require('koa-morgan')
2016-06-09 22:54:57 +01:00
if (!format) {
2016-06-08 23:06:41 +01:00
const streamLogStats = require('stream-log-stats')
2016-06-09 22:54:57 +01:00
options.stream = streamLogStats({ refreshRate: 500 })
this.push(morgan('common', options))
} else if (format === 'logstalgia') {
2016-06-08 23:06:41 +01:00
morgan.token('date', () => {
var d = new Date()
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
})
2016-06-09 22:54:57 +01:00
this.push(morgan('combined', options))
2016-06-08 23:06:41 +01:00
} else {
2016-06-09 22:54:57 +01:00
this.push(morgan(format, options))
2016-06-08 23:06:41 +01:00
}
}
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* Mock Responses */
2016-06-15 21:02:52 +01:00
addMockResponses (mocks) {
mocks = arrayify(this.options.server.mocks || mocks)
mocks.forEach(mock => {
2016-06-08 23:06:41 +01:00
if (mock.module) {
2016-06-15 21:02:52 +01:00
// TODO: ENSURE this.options.static.root is correct value
2016-06-08 23:06:41 +01:00
mock.responses = require(path.resolve(path.join(this.options.static.root, mock.module)))
}
if (mock.responses) {
this.push(mw.mockResponses(mock.route, mock.responses))
} else if (mock.response) {
mock.target = {
request: mock.request,
response: mock.response
}
this.push(mw.mockResponses(mock.route, mock.target))
}
})
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* for any URL not matched by static (e.g. `/search`), serve the SPA */
2016-06-15 21:02:52 +01:00
addSpa (spa) {
spa = t.isDefined(this.options.server.spa) ? this.options.server.spa : spa
if (spa) {
2016-06-08 23:06:41 +01:00
const historyApiFallback = require('koa-connect-history-api-fallback')
2016-06-15 21:02:52 +01:00
debug('SPA', spa)
2016-06-08 23:06:41 +01:00
this.push(historyApiFallback({
2016-06-15 21:02:52 +01:00
index: spa,
2016-06-08 23:06:41 +01:00
verbose: this.options.verbose
}))
}
2016-06-09 22:54:57 +01:00
return this
2016-06-08 23:06:41 +01:00
}
/* serve static files */
2016-06-09 22:54:57 +01:00
addStatic (root, options) {
root = this.options.server.directory || root || process.cwd()
options = Object.assign({ hidden: true }, options)
if (root) {
2016-06-08 23:06:41 +01:00
const serve = require('koa-static')
2016-06-09 22:54:57 +01:00
this.push(serve(root, options))
2016-06-08 23:06:41 +01:00
}
return this
}
/* serve directory index */
2016-06-09 22:54:57 +01:00
addIndex (path, options) {
path = this.options.server.directory || path || process.cwd()
options = Object.assign({ icons: true, hidden: true }, options)
if (path) {
2016-06-08 23:06:41 +01:00
const serveIndex = require('koa-serve-index')
2016-06-09 22:54:57 +01:00
this.push(serveIndex(path, options))
2016-06-08 23:06:41 +01:00
}
return this
}
2016-06-15 21:02:52 +01:00
compose (options) {
2016-06-08 23:06:41 +01:00
const compose = require('koa-compose')
const convert = require('koa-convert')
const middlewareStack = this.map(convert)
return compose(middlewareStack)
}
}
module.exports = MiddlewareStack