|
@ -66,7 +66,6 @@ function localWebServer (options) { |
|
|
options.mocks = arrayify(options.mocks) |
|
|
options.mocks = arrayify(options.mocks) |
|
|
|
|
|
|
|
|
const debug = require('debug')('local-web-server') |
|
|
const debug = require('debug')('local-web-server') |
|
|
const Koa = require('koa') |
|
|
|
|
|
const convert = require('koa-convert') |
|
|
const convert = require('koa-convert') |
|
|
const cors = require('kcors') |
|
|
const cors = require('kcors') |
|
|
const _ = require('koa-route') |
|
|
const _ = require('koa-route') |
|
@ -74,19 +73,13 @@ function localWebServer (options) { |
|
|
const bodyParser = require('koa-bodyparser') |
|
|
const bodyParser = require('koa-bodyparser') |
|
|
const mw = require('./middleware') |
|
|
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 */ |
|
|
/* CORS: allow from any origin */ |
|
|
// app.use(cors())
|
|
|
|
|
|
middlewares.push(cors()) |
|
|
|
|
|
|
|
|
middlewareStack.push(cors()) |
|
|
|
|
|
|
|
|
/* pretty print JSON */ |
|
|
/* pretty print JSON */ |
|
|
// app.use(json())
|
|
|
|
|
|
middlewares.push(json()) |
|
|
|
|
|
|
|
|
middlewareStack.push(json()) |
|
|
|
|
|
|
|
|
/* rewrite rules */ |
|
|
/* rewrite rules */ |
|
|
if (options.rewrite && options.rewrite.length) { |
|
|
if (options.rewrite && options.rewrite.length) { |
|
@ -95,53 +88,45 @@ function localWebServer (options) { |
|
|
/* `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) { |
|
|
debug('proxy rewrite', `${route.from} -> ${route.to}`) |
|
|
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 { |
|
|
} else { |
|
|
const rewrite = require('koa-rewrite') |
|
|
const rewrite = require('koa-rewrite') |
|
|
const rmw = rewrite(route.from, route.to) |
|
|
const rmw = rewrite(route.from, route.to) |
|
|
rmw._name = 'rewrite' |
|
|
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. */ |
|
|
/* 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 */ |
|
|
/* path blacklist */ |
|
|
if (options.forbid.length) { |
|
|
if (options.forbid.length) { |
|
|
debug('forbid', options.forbid.join(', ')) |
|
|
debug('forbid', options.forbid.join(', ')) |
|
|
// app.use(mw.blacklist(options.forbid))
|
|
|
|
|
|
middlewares.push(mw.blacklist(options.forbid)) |
|
|
|
|
|
|
|
|
middlewareStack.push(mw.blacklist(options.forbid)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* cache */ |
|
|
/* cache */ |
|
|
if (!options['no-cache']) { |
|
|
if (!options['no-cache']) { |
|
|
const conditional = require('koa-conditional-get') |
|
|
const conditional = require('koa-conditional-get') |
|
|
const etag = require('koa-etag') |
|
|
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 */ |
|
|
/* mime-type overrides */ |
|
|
if (options.mime) { |
|
|
if (options.mime) { |
|
|
debug('mime override', JSON.stringify(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 */ |
|
|
/* compress response */ |
|
|
if (options.compress) { |
|
|
if (options.compress) { |
|
|
const compress = require('koa-compress') |
|
|
const compress = require('koa-compress') |
|
|
debug('compression', 'enabled') |
|
|
debug('compression', 'enabled') |
|
|
// app.use(compress())
|
|
|
|
|
|
middlewares.push(compress()) |
|
|
|
|
|
|
|
|
middlewareStack.push(compress()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* Logging */ |
|
|
/* Logging */ |
|
@ -151,15 +136,12 @@ function localWebServer (options) { |
|
|
if (!log.format) { |
|
|
if (!log.format) { |
|
|
const streamLogStats = require('stream-log-stats') |
|
|
const streamLogStats = require('stream-log-stats') |
|
|
log.options.stream = streamLogStats({ refreshRate: 500 }) |
|
|
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') { |
|
|
} else if (log.format === 'logstalgia') { |
|
|
morgan.token('date', logstalgiaDate) |
|
|
morgan.token('date', logstalgiaDate) |
|
|
// app.use(morgan('combined', log.options))
|
|
|
|
|
|
middlewares.push(morgan('combined', log.options)) |
|
|
|
|
|
|
|
|
middlewareStack.push(morgan('combined', log.options)) |
|
|
} else { |
|
|
} 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) { |
|
|
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) { |
|
|
} else if (mock.response) { |
|
|
mock.target = { |
|
|
mock.target = { |
|
|
request: mock.request, |
|
|
request: mock.request, |
|
|
response: mock.response |
|
|
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) { |
|
|
if (options.spa) { |
|
|
const historyApiFallback = require('koa-connect-history-api-fallback') |
|
|
const historyApiFallback = require('koa-connect-history-api-fallback') |
|
|
debug('SPA', options.spa) |
|
|
debug('SPA', options.spa) |
|
|
// app.use(historyApiFallback({
|
|
|
|
|
|
// index: options.spa,
|
|
|
|
|
|
// verbose: options.verbose
|
|
|
|
|
|
// }))
|
|
|
|
|
|
middlewares.push(historyApiFallback({ |
|
|
|
|
|
|
|
|
middlewareStack.push(historyApiFallback({ |
|
|
index: options.spa, |
|
|
index: options.spa, |
|
|
verbose: options.verbose |
|
|
verbose: options.verbose |
|
|
})) |
|
|
})) |
|
@ -199,20 +175,18 @@ function localWebServer (options) { |
|
|
/* serve static files */ |
|
|
/* serve static files */ |
|
|
if (options.static.root) { |
|
|
if (options.static.root) { |
|
|
const serve = require('koa-static') |
|
|
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 */ |
|
|
/* serve directory index */ |
|
|
if (options.serveIndex.path) { |
|
|
if (options.serveIndex.path) { |
|
|
const serveIndex = require('koa-serve-index') |
|
|
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') |
|
|
const compose = require('koa-compose') |
|
|
middlewares = middlewares.map(convert) |
|
|
|
|
|
return compose(middlewares) |
|
|
|
|
|
|
|
|
middlewareStack = middlewareStack.map(convert) |
|
|
|
|
|
return compose(middlewareStack) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function logstalgiaDate () { |
|
|
function logstalgiaDate () { |
|
|