|
|
@ -66,7 +66,6 @@ function localWebServer (options) { |
|
|
|
options.mocks = arrayify(options.mocks) |
|
|
|
|
|
|
|
const debug = require('debug')('local-web-server') |
|
|
|
const Koa = require('koa') |
|
|
|
const convert = require('koa-convert') |
|
|
|
const cors = require('kcors') |
|
|
|
const _ = require('koa-route') |
|
|
@ -74,19 +73,13 @@ function localWebServer (options) { |
|
|
|
const bodyParser = require('koa-bodyparser') |
|
|
|
const mw = require('./middleware') |
|
|
|
|
|
|
|
let middlewares = [] |
|
|
|
|
|
|
|
// const app = new Koa()
|
|
|
|
// const _use = app.use
|
|
|
|
// app.use = x => _use.call(app, convert(x))
|
|
|
|
let middlewareStack = [] |
|
|
|
|
|
|
|
/* CORS: allow from any origin */ |
|
|
|
// app.use(cors())
|
|
|
|
middlewares.push(cors()) |
|
|
|
middlewareStack.push(cors()) |
|
|
|
|
|
|
|
/* pretty print JSON */ |
|
|
|
// app.use(json())
|
|
|
|
middlewares.push(json()) |
|
|
|
middlewareStack.push(json()) |
|
|
|
|
|
|
|
/* rewrite rules */ |
|
|
|
if (options.rewrite && options.rewrite.length) { |
|
|
@ -95,53 +88,45 @@ function localWebServer (options) { |
|
|
|
/* `to` address is remote if the url specifies a host */ |
|
|
|
if (url.parse(route.to).host) { |
|
|
|
debug('proxy rewrite', `${route.from} -> ${route.to}`) |
|
|
|
// app.use(_.all(route.from, mw.proxyRequest(route)))
|
|
|
|
middlewares.push(_.all(route.from, mw.proxyRequest(route))) |
|
|
|
middlewareStack.push(_.all(route.from, mw.proxyRequest(route))) |
|
|
|
} else { |
|
|
|
const rewrite = require('koa-rewrite') |
|
|
|
const rmw = rewrite(route.from, route.to) |
|
|
|
rmw._name = 'rewrite' |
|
|
|
// app.use(rmw)
|
|
|
|
middlewares.push(rmw) |
|
|
|
middlewareStack.push(rmw) |
|
|
|
} |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
/* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */ |
|
|
|
// app.use(bodyParser())
|
|
|
|
middlewares.push(bodyParser()) |
|
|
|
middlewareStack.push(bodyParser()) |
|
|
|
|
|
|
|
/* path blacklist */ |
|
|
|
if (options.forbid.length) { |
|
|
|
debug('forbid', options.forbid.join(', ')) |
|
|
|
// app.use(mw.blacklist(options.forbid))
|
|
|
|
middlewares.push(mw.blacklist(options.forbid)) |
|
|
|
middlewareStack.push(mw.blacklist(options.forbid)) |
|
|
|
} |
|
|
|
|
|
|
|
/* cache */ |
|
|
|
if (!options['no-cache']) { |
|
|
|
const conditional = require('koa-conditional-get') |
|
|
|
const etag = require('koa-etag') |
|
|
|
// app.use(conditional())
|
|
|
|
// app.use(etag())
|
|
|
|
middlewares.push(conditional()) |
|
|
|
middlewares.push(etag()) |
|
|
|
middlewareStack.push(conditional()) |
|
|
|
middlewareStack.push(etag()) |
|
|
|
} |
|
|
|
|
|
|
|
/* mime-type overrides */ |
|
|
|
if (options.mime) { |
|
|
|
debug('mime override', JSON.stringify(options.mime)) |
|
|
|
// app.use(mw.mime(options.mime))
|
|
|
|
middlewares.push(mw.mime(options.mime)) |
|
|
|
middlewareStack.push(mw.mime(options.mime)) |
|
|
|
} |
|
|
|
|
|
|
|
/* compress response */ |
|
|
|
if (options.compress) { |
|
|
|
const compress = require('koa-compress') |
|
|
|
debug('compression', 'enabled') |
|
|
|
// app.use(compress())
|
|
|
|
middlewares.push(compress()) |
|
|
|
middlewareStack.push(compress()) |
|
|
|
} |
|
|
|
|
|
|
|
/* Logging */ |
|
|
@ -151,15 +136,12 @@ function localWebServer (options) { |
|
|
|
if (!log.format) { |
|
|
|
const streamLogStats = require('stream-log-stats') |
|
|
|
log.options.stream = streamLogStats({ refreshRate: 500 }) |
|
|
|
// app.use(morgan('common', log.options))
|
|
|
|
middlewares.push(morgan('common', log.options)) |
|
|
|
middlewareStack.push(morgan('common', log.options)) |
|
|
|
} else if (log.format === 'logstalgia') { |
|
|
|
morgan.token('date', logstalgiaDate) |
|
|
|
// app.use(morgan('combined', log.options))
|
|
|
|
middlewares.push(morgan('combined', log.options)) |
|
|
|
middlewareStack.push(morgan('combined', log.options)) |
|
|
|
} else { |
|
|
|
// app.use(morgan(log.format, log.options))
|
|
|
|
middlewares.push(morgan(log.format, log.options)) |
|
|
|
middlewareStack.push(morgan(log.format, log.options)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -170,15 +152,13 @@ function localWebServer (options) { |
|
|
|
} |
|
|
|
|
|
|
|
if (mock.responses) { |
|
|
|
// app.use(mw.mockResponses(mock.route, mock.responses))
|
|
|
|
middlewares.push(mw.mockResponses(mock.route, mock.responses)) |
|
|
|
middlewareStack.push(mw.mockResponses(mock.route, mock.responses)) |
|
|
|
} else if (mock.response) { |
|
|
|
mock.target = { |
|
|
|
request: mock.request, |
|
|
|
response: mock.response |
|
|
|
} |
|
|
|
// app.use(mw.mockResponses(mock.route, mock.target))
|
|
|
|
middlewares.push(mw.mockResponses(mock.route, mock.target)) |
|
|
|
middlewareStack.push(mw.mockResponses(mock.route, mock.target)) |
|
|
|
} |
|
|
|
}) |
|
|
|
|
|
|
@ -186,11 +166,7 @@ function localWebServer (options) { |
|
|
|
if (options.spa) { |
|
|
|
const historyApiFallback = require('koa-connect-history-api-fallback') |
|
|
|
debug('SPA', options.spa) |
|
|
|
// app.use(historyApiFallback({
|
|
|
|
// index: options.spa,
|
|
|
|
// verbose: options.verbose
|
|
|
|
// }))
|
|
|
|
middlewares.push(historyApiFallback({ |
|
|
|
middlewareStack.push(historyApiFallback({ |
|
|
|
index: options.spa, |
|
|
|
verbose: options.verbose |
|
|
|
})) |
|
|
@ -199,20 +175,18 @@ function localWebServer (options) { |
|
|
|
/* serve static files */ |
|
|
|
if (options.static.root) { |
|
|
|
const serve = require('koa-static') |
|
|
|
// app.use(serve(options.static.root, options.static.options))
|
|
|
|
middlewares.push(serve(options.static.root, options.static.options)) |
|
|
|
middlewareStack.push(serve(options.static.root, options.static.options)) |
|
|
|
} |
|
|
|
|
|
|
|
/* serve directory index */ |
|
|
|
if (options.serveIndex.path) { |
|
|
|
const serveIndex = require('koa-serve-index') |
|
|
|
// app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
|
|
|
|
middlewares.push(serveIndex(options.serveIndex.path, options.serveIndex.options)) |
|
|
|
middlewareStack.push(serveIndex(options.serveIndex.path, options.serveIndex.options)) |
|
|
|
} |
|
|
|
|
|
|
|
const compose = require('koa-compose') |
|
|
|
middlewares = middlewares.map(convert) |
|
|
|
return compose(middlewares) |
|
|
|
middlewareStack = middlewareStack.map(convert) |
|
|
|
return compose(middlewareStack) |
|
|
|
} |
|
|
|
|
|
|
|
function logstalgiaDate () { |
|
|
|