|
@ -2,11 +2,8 @@ |
|
|
const path = require('path') |
|
|
const path = require('path') |
|
|
const http = require('http') |
|
|
const http = require('http') |
|
|
const url = require('url') |
|
|
const url = require('url') |
|
|
const Koa = require('koa') |
|
|
|
|
|
const convert = require('koa-convert') |
|
|
|
|
|
const cors = require('kcors') |
|
|
|
|
|
const _ = require('koa-route') |
|
|
|
|
|
const pathToRegexp = require('path-to-regexp') |
|
|
|
|
|
|
|
|
const arrayify = require('array-back') |
|
|
|
|
|
let debug |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @module local-web-server |
|
|
* @module local-web-server |
|
@ -43,15 +40,30 @@ function localWebServer (options) { |
|
|
verbose: false |
|
|
verbose: false |
|
|
}, options) |
|
|
}, options) |
|
|
|
|
|
|
|
|
|
|
|
if (options.verbose) { |
|
|
|
|
|
process.env.DEBUG = '*' |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const Koa = require('koa') |
|
|
|
|
|
const convert = require('koa-convert') |
|
|
|
|
|
const cors = require('kcors') |
|
|
|
|
|
const _ = require('koa-route') |
|
|
|
|
|
const pathToRegexp = require('path-to-regexp') |
|
|
|
|
|
debug = require('debug')('local-web-server') |
|
|
|
|
|
|
|
|
const log = options.log |
|
|
const log = options.log |
|
|
log.options = log.options || {} |
|
|
log.options = log.options || {} |
|
|
|
|
|
|
|
|
const app = new Koa() |
|
|
const app = new Koa() |
|
|
const _use = app.use |
|
|
const _use = app.use |
|
|
app.use = x => _use.call(app, convert(x)) |
|
|
app.use = x => _use.call(app, convert(x)) |
|
|
|
|
|
|
|
|
function verbose (category, message) { |
|
|
function verbose (category, message) { |
|
|
if (options.verbose) { |
|
|
if (options.verbose) { |
|
|
process.nextTick(() => app.emit('verbose', category, message)) |
|
|
|
|
|
|
|
|
debug(category, message) |
|
|
|
|
|
// process.nextTick(() => {
|
|
|
|
|
|
// app.emit('verbose', category, message)
|
|
|
|
|
|
// })
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
app._verbose = verbose |
|
|
app._verbose = verbose |
|
@ -89,7 +101,7 @@ function localWebServer (options) { |
|
|
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') |
|
|
verbose('etag caching', 'enabled') |
|
|
|
|
|
|
|
|
// verbose('etag caching', 'enabled')
|
|
|
app.use(conditional()) |
|
|
app.use(conditional()) |
|
|
app.use(etag()) |
|
|
app.use(etag()) |
|
|
} |
|
|
} |
|
@ -131,17 +143,20 @@ function localWebServer (options) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Mock Responses */ |
|
|
|
|
|
app.use(mockResponses({ root: options.static.root, verbose: verbose })) |
|
|
|
|
|
|
|
|
/* serve static files */ |
|
|
/* serve static files */ |
|
|
if (options.static.root) { |
|
|
if (options.static.root) { |
|
|
const serve = require('koa-static') |
|
|
const serve = require('koa-static') |
|
|
verbose('static', `root: ${options.static.root} options: ${JSON.stringify(options.static.options)}` ) |
|
|
|
|
|
|
|
|
// verbose('static', 'enabled')
|
|
|
app.use(serve(options.static.root, options.static.options)) |
|
|
app.use(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') |
|
|
verbose('serve-index', `root: ${options.serveIndex.path} options: ${JSON.stringify(options.serveIndex.options)}` ) |
|
|
|
|
|
|
|
|
// verbose('serve-index', 'enabled')
|
|
|
app.use(serveIndex(options.serveIndex.path, options.serveIndex.options)) |
|
|
app.use(serveIndex(options.serveIndex.path, options.serveIndex.options)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -210,6 +225,22 @@ function blacklist (forbid) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function mockResponses (options) { |
|
|
|
|
|
options = options || { root: process.cwd() } |
|
|
|
|
|
return function mockResponses (ctx, next) { |
|
|
|
|
|
if (/\.mock.js$/.test(ctx.path)) { |
|
|
|
|
|
const mocks = arrayify(require(path.join(options.root, ctx.path))) |
|
|
|
|
|
const mock = mocks.find(mock => { |
|
|
|
|
|
return !mock.request || mock.request.method === ctx.method |
|
|
|
|
|
}) |
|
|
|
|
|
Object.assign(ctx.response, mock.response) |
|
|
|
|
|
options.verbose('mock response', JSON.stringify(mock.response), JSON.stringify(ctx.response)) |
|
|
|
|
|
} else { |
|
|
|
|
|
return next() |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
process.on('unhandledRejection', (reason, p) => { |
|
|
process.on('unhandledRejection', (reason, p) => { |
|
|
throw reason |
|
|
throw reason |
|
|
}) |
|
|
}) |