Browse Source

refactor

master
Lloyd Brookes 9 years ago
parent
commit
9c3db53d27
  1. 2
      README.md
  2. 5
      bin/cli.js
  3. 17
      extend/cache-control.js
  4. 10
      extend/index.html
  5. 7
      extend/live-reload.js
  6. 53
      lib/cli-data.js
  7. 65
      lib/local-web-server.js
  8. 172
      lib/middleware-stack.js
  9. 12
      lib/middleware.js

2
README.md

@ -8,7 +8,7 @@
***Requires node v4.0.0 or higher. Install the [previous release](https://github.com/75lb/local-web-server/tree/prev) for older node support.*** ***Requires node v4.0.0 or higher. Install the [previous release](https://github.com/75lb/local-web-server/tree/prev) for older node support.***
# local-web-server # local-web-server
A simple web-server for productive front-end development. Typical use cases:
A simple, extensible web-server for productive front-end development. Typical use cases:
* Front-end Development * Front-end Development
* Static or Single Page App development * Static or Single Page App development

5
bin/cli.js

@ -3,8 +3,7 @@
const LocalWebServer = require('../') const LocalWebServer = require('../')
const ws = new LocalWebServer() const ws = new LocalWebServer()
ws.middleware
.addCors()
ws.addCors()
.addJson() .addJson()
.addRewrite() .addRewrite()
.addBodyParser() .addBodyParser()
@ -17,4 +16,4 @@ ws.middleware
.addSpa() .addSpa()
.addStatic() .addStatic()
.addIndex() .addIndex()
ws.listen()
.start()

17
extend/cache-control.js

@ -1,16 +1,17 @@
'use strict' 'use strict'
const LocalWebServer = require('../') const LocalWebServer = require('../')
const cacheControl = require('koa-cache-control') const cacheControl = require('koa-cache-control')
const cliData = require('../lib/cli-data')
cliData.optionDefinitions.push({ name: 'maxage', group: 'misc' })
const optionDefinitions = { name: 'maxage', type: Number, defaultValue: 1000 }
const ws = new LocalWebServer() const ws = new LocalWebServer()
ws.middleware
.addLogging('dev')
.add(cacheControl({
maxAge: 15
}))
ws.addLogging('dev')
.add({
optionDefinitions: optionDefinitions,
middleware: function (options) {
return cacheControl({ maxAge: options.middleware.maxage })
}
})
.addStatic() .addStatic()
.addIndex() .addIndex()
ws.listen()
.start()

10
extend/index.html

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>live-reload demo</title>
</head>
<body>
<h1>Live reloaded attached</h1>
</body>
</html>

7
extend/live-reload.js

@ -3,8 +3,7 @@ const Cli = require('../')
const liveReload = require('koa-livereload') const liveReload = require('koa-livereload')
const ws = new Cli() const ws = new Cli()
ws.middleware
.addLogging('dev')
.add(liveReload())
ws.addLogging('dev')
.add({ middleware: liveReload })
.addStatic() .addStatic()
ws.listen(8000)
.start()

53
lib/cli-data.js

@ -4,34 +4,6 @@ exports.optionDefinitions = [
description: 'Web server port.', group: 'server' description: 'Web server port.', group: 'server'
}, },
{ {
name: 'directory', alias: 'd', type: String, typeLabel: '[underline]{path}',
description: 'Root directory, defaults to the current directory.', group: 'server'
},
{
name: 'log-format', alias: 'f', type: String,
description: "If a format is supplied an access log is written to stdout. If not, a dynamic statistics view is displayed. Use a preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> :url').", group: 'server'
},
{
name: 'rewrite', alias: 'r', type: String, multiple: true, typeLabel: '[underline]{expression} ...',
description: "A list of URL rewrite rules. For each rule, separate the 'from' and 'to' routes with '->'. Whitespace surrounded the routes is ignored. E.g. '/from -> /to'.", group: 'server'
},
{
name: 'spa', alias: 's', type: String, typeLabel: '[underline]{file}',
description: 'Path to a Single Page App, e.g. app.html.', group: 'server'
},
{
name: 'compress', alias: 'c', type: Boolean,
description: 'Serve gzip-compressed resources, where applicable.', group: 'server'
},
{
name: 'forbid', alias: 'b', type: String, multiple: true, typeLabel: '[underline]{path} ...',
description: 'A list of forbidden routes.', group: 'server'
},
{
name: 'no-cache', alias: 'n', type: Boolean,
description: 'Disable etag-based caching - forces loading from disk each request.', group: 'server'
},
{
name: 'key', type: String, typeLabel: '[underline]{file}', group: 'server', name: 'key', type: String, typeLabel: '[underline]{file}', group: 'server',
description: 'SSL key. Supply along with --cert to launch a https server.' description: 'SSL key. Supply along with --cert to launch a https server.'
}, },
@ -44,20 +16,21 @@ exports.optionDefinitions = [
description: 'Enable HTTPS using a built-in key and cert, registered to the domain 127.0.0.1.' description: 'Enable HTTPS using a built-in key and cert, registered to the domain 127.0.0.1.'
}, },
{ {
name: 'verbose', type: Boolean,
description: 'Verbose output, useful for debugging.', group: 'server'
},
{
name: 'help', alias: 'h', type: Boolean, name: 'help', alias: 'h', type: Boolean,
description: 'Print these usage instructions.', group: 'misc' description: 'Print these usage instructions.', group: 'misc'
}, },
{ {
name: 'config', type: Boolean, name: 'config', type: Boolean,
description: 'Print the stored config.', group: 'misc' description: 'Print the stored config.', group: 'misc'
},
{
name: 'verbose', type: Boolean,
description: 'Verbose output, useful for debugging.', group: 'misc'
} }
] ]
exports.usage = [
function usage (middlewareDefinitions) {
return [
{ {
header: 'local-web-server', header: 'local-web-server',
content: 'A simple web-server for productive front-end development.' content: 'A simple web-server for productive front-end development.'
@ -71,16 +44,24 @@ exports.usage = [
] ]
}, },
{ {
header: 'Server options',
header: 'Server',
optionList: exports.optionDefinitions, optionList: exports.optionDefinitions,
group: 'server' group: 'server'
}, },
{ {
header: 'Misc options',
header: 'Middleware',
optionList: middlewareDefinitions,
group: 'middleware'
},
{
header: 'Misc',
optionList: exports.optionDefinitions, optionList: exports.optionDefinitions,
group: 'misc' group: 'misc'
}, },
{ {
content: 'Project home: [underline]{https://github.com/75lb/local-web-server}' content: 'Project home: [underline]{https://github.com/75lb/local-web-server}'
} }
]
]
}
exports.usage = usage

65
lib/local-web-server.js

@ -5,38 +5,34 @@ const path = require('path')
const arrayify = require('array-back') const arrayify = require('array-back')
const t = require('typical') const t = require('typical')
const Tool = require('command-line-tool') const Tool = require('command-line-tool')
const MiddlewareStack = require('./middleware-stack')
const tool = new Tool() const tool = new Tool()
class Cli {
constructor () {
this.options = collectOptions()
this.app = null
this.middleware = null
class Cli extends MiddlewareStack {
start () {
const options = collectOptions(this.getOptionDefinitions())
this.options = options
const options = this.options
if (options.misc.verbose) {
process.env.DEBUG = '*'
}
if (options.misc.config) { if (options.misc.config) {
tool.stop(JSON.stringify(options.server, null, ' '), 0)
tool.stop(JSON.stringify(options, null, ' '), 0)
} else { } else {
const Koa = require('koa') const Koa = require('koa')
const app = new Koa() const app = new Koa()
app.on('error', err => { app.on('error', err => {
if (options.server['log-format']) {
if (options.middleware['log-format']) {
console.error(ansi.format(err.message, 'red')) console.error(ansi.format(err.message, 'red'))
} }
}) })
this.app = app
const MiddlewareStack = require('./middleware-stack')
this.middleware = new MiddlewareStack(options)
}
}
app.use(this.compose(options))
listen () {
this.app.use(this.middleware.compose())
const options = this.options
const key = this.options.server.key
const cert = this.options.server.cert
let key = options.server.key
let cert = options.server.cert
if (options.server.https) { if (options.server.https) {
key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key') key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt') cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
@ -51,10 +47,11 @@ class Cli {
cert: fs.readFileSync(cert) cert: fs.readFileSync(cert)
} }
const server = https.createServer(serverOptions, this.app.callback())
const server = https.createServer(serverOptions, app.callback())
server.listen(options.server.port, onServerUp.bind(null, options, true)) server.listen(options.server.port, onServerUp.bind(null, options, true))
} else { } else {
this.app.listen(options.server.port, onServerUp.bind(null, options))
app.listen(options.server.port, onServerUp.bind(null, options))
}
} }
} }
} }
@ -65,9 +62,9 @@ function onServerUp (options, isHttps) {
.join(', ') .join(', ')
console.error(ansi.format( console.error(ansi.format(
path.resolve(options.server.directory) === process.cwd()
path.resolve(options.middleware.directory) === process.cwd()
? `serving at ${ipList}` ? `serving at ${ipList}`
: `serving [underline]{${options.server.directory}} at ${ipList}`
: `serving [underline]{${options.middleware.directory}} at ${ipList}`
)) ))
} }
@ -86,37 +83,39 @@ function getIPList (isHttps) {
/** /**
* Return default, stored and command-line options combined * Return default, stored and command-line options combined
*/ */
function collectOptions () {
function collectOptions (mwOptionDefinitions) {
const loadConfig = require('config-master') const loadConfig = require('config-master')
const stored = loadConfig('local-web-server') const stored = loadConfig('local-web-server')
const cli = require('../lib/cli-data') const cli = require('../lib/cli-data')
/* parse command line args */ /* parse command line args */
let options = tool.getOptions(cli.optionDefinitions, cli.usage)
const definitions = cli.optionDefinitions.concat(arrayify(mwOptionDefinitions))
let options = tool.getOptions(definitions, cli.usage(definitions))
const builtIn = {
port: 8000,
directory: process.cwd()
}
/* override built-in defaults with stored config and then command line args */
options.server = Object.assign({ port: 8000 }, stored.server, options.server)
options.middleware = Object.assign({ directory: process.cwd() }, stored.middleware || {}, options.middleware)
if (options.server.rewrite) {
options.server.rewrite = parseRewriteRules(options.server.rewrite)
if (options.middleware.rewrite) {
options.middleware.rewrite = parseRewriteRules(options.middleware.rewrite)
} }
/* override built-in defaults with stored config and then command line args */
options.server = Object.assign(builtIn, stored, options.server)
validateOptions(options) validateOptions(options)
// console.log(options)
return options return options
} }
function parseRewriteRules (rules) { function parseRewriteRules (rules) {
return rules && rules.map(rule => { return rules && rules.map(rule => {
if (t.isString(rule)) {
const matches = rule.match(/(\S*)\s*->\s*(\S*)/) const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
return { return {
from: matches[1], from: matches[1],
to: matches[2] to: matches[2]
} }
} else {
return rule
}
}) })
} }

172
lib/middleware-stack.js

@ -5,17 +5,9 @@ const url = require('url')
const debug = require('debug')('local-web-server') const debug = require('debug')('local-web-server')
const mw = require('./middleware') const mw = require('./middleware')
const t = require('typical') const t = require('typical')
const compose = require('koa-compose')
class MiddlewareStack extends Array { class MiddlewareStack extends Array {
constructor (options) {
super()
this.options = options
if (options.verbose) {
process.env.DEBUG = '*'
}
}
add (middleware) { add (middleware) {
this.push(middleware) this.push(middleware)
return this return this
@ -25,19 +17,26 @@ class MiddlewareStack extends Array {
* allow from any origin * allow from any origin
*/ */
addCors () { addCors () {
this.push(require('kcors')())
this.push({ middleware: require('kcors') })
return this return this
} }
/* pretty print JSON */ /* pretty print JSON */
addJson () { addJson () {
this.push(require('koa-json')())
this.push({ middleware: require('koa-json') })
return this return this
} }
/* rewrite rules */ /* rewrite rules */
addRewrite (rewriteRules) { addRewrite (rewriteRules) {
const options = arrayify(this.options.server.rewrite || rewriteRules)
this.push({
optionDefinitions: {
name: 'rewrite', alias: 'r', type: String, multiple: true,
typeLabel: '[underline]{expression} ...',
description: "A list of URL rewrite rules. For each rule, separate the 'from' and 'to' routes with '->'. Whitespace surrounded the routes is ignored. E.g. '/from -> /to'."
},
middleware: function (cliOptions) {
const options = arrayify(cliOptions.middleware.rewrite || rewriteRules)
if (options.length) { if (options.length) {
options.forEach(route => { options.forEach(route => {
if (route.to) { if (route.to) {
@ -45,81 +44,123 @@ class MiddlewareStack extends Array {
if (url.parse(route.to).host) { if (url.parse(route.to).host) {
const _ = require('koa-route') 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)))
return _.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'
this.push(rmw)
return rmw
} }
} }
}) })
} }
}
})
return this return this
} }
/* must come after rewrite. /* must come after rewrite.
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({ middleware: require('koa-bodyparser') })
return this return this
} }
/* path blacklist */ /* path blacklist */
addBlacklist (forbidList) { addBlacklist (forbidList) {
forbidList = arrayify(this.options.server.forbid || forbidList)
this.push({
optionDefinitions: {
name: 'forbid', alias: 'b', type: String,
multiple: true, typeLabel: '[underline]{path} ...',
description: 'A list of forbidden routes.'
},
middleware: function (cliOptions) {
forbidList = arrayify(cliOptions.middleware.forbid || forbidList)
if (forbidList.length) { if (forbidList.length) {
const pathToRegexp = require('path-to-regexp') const pathToRegexp = require('path-to-regexp')
debug('forbid', forbidList.join(', ')) debug('forbid', forbidList.join(', '))
this.push(function blacklist (ctx, next) {
return function blacklist (ctx, next) {
if (forbidList.some(expression => pathToRegexp(expression).test(ctx.path))) { if (forbidList.some(expression => pathToRegexp(expression).test(ctx.path))) {
const http = require('http')
ctx.throw(403, http.STATUS_CODES[403]) ctx.throw(403, http.STATUS_CODES[403])
} else { } else {
return next() return next()
} }
})
} }
}
}
})
return this return this
} }
/* cache */ /* cache */
addCache () { addCache () {
const noCache = this.options.server['no-cache']
this.push({
optionDefinitions: {
name: 'no-cache', alias: 'n', type: Boolean,
description: 'Disable etag-based caching - forces loading from disk each request.'
},
middleware: function (cliOptions) {
const noCache = cliOptions.middleware['no-cache']
if (!noCache) { if (!noCache) {
this.push(require('koa-conditional-get')())
this.push(require('koa-etag')())
return [
require('koa-conditional-get')(),
require('koa-etag')()
]
} }
}
})
return this return this
} }
/* mime-type overrides */ /* mime-type overrides */
addMimeType (mime) { addMimeType (mime) {
mime = this.options.server.mime || mime
this.push({
middleware: function (cliOptions) {
mime = cliOptions.middleware.mime || mime
if (mime) { if (mime) {
debug('mime override', JSON.stringify(mime)) debug('mime override', JSON.stringify(mime))
this.push(mw.mime(mime))
return mw.mime(mime)
} }
}
})
return this return this
} }
/* compress response */ /* compress response */
addCompression (compress) { addCompression (compress) {
compress = t.isDefined(this.options.server.compress)
? this.options.server.compress
this.push({
optionDefinitions: {
name: 'compress', alias: 'c', type: Boolean,
description: 'Serve gzip-compressed resources, where applicable.'
},
middleware: function (cliOptions) {
compress = t.isDefined(cliOptions.middleware.compress)
? cliOptions.middleware.compress
: compress : compress
if (compress) { if (compress) {
debug('compression', 'enabled') debug('compression', 'enabled')
this.push(require('koa-compress')())
return require('koa-compress')()
}
} }
})
return this return this
} }
/* Logging */ /* Logging */
addLogging (format, options) { addLogging (format, options) {
format = this.options.server['log-format'] || format
options = options || {} options = options || {}
this.push({
optionDefinitions: {
name: 'log-format',
alias: 'f',
type: String,
description: "If a format is supplied an access log is written to stdout. If not, a dynamic statistics view is displayed. Use a preset ('none', 'dev','combined', 'short', 'tiny' or 'logstalgia') or supply a custom format (e.g. ':method -> :url')."
},
middleware: function (cliOptions) {
format = cliOptions.middleware['log-format'] || format
if (this.options.verbose && !format) {
if (cliOptions.misc.verbose && !format) {
format = 'none' format = 'none'
} }
@ -129,37 +170,43 @@ class MiddlewareStack extends Array {
if (!format) { if (!format) {
const streamLogStats = require('stream-log-stats') const streamLogStats = require('stream-log-stats')
options.stream = streamLogStats({ refreshRate: 500 }) options.stream = streamLogStats({ refreshRate: 500 })
this.push(morgan('common', options))
return morgan('common', options)
} else if (format === 'logstalgia') { } 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', options))
return morgan('combined', options)
} else { } else {
this.push(morgan(format, options))
return morgan(format, options)
}
} }
} }
})
return this return this
} }
/* Mock Responses */ /* Mock Responses */
addMockResponses (mocks) { addMockResponses (mocks) {
mocks = arrayify(this.options.server.mocks || mocks)
this.push({
middleware: function (cliOptions) {
mocks = arrayify(cliOptions.middleware.mocks || mocks)
mocks.forEach(mock => { mocks.forEach(mock => {
if (mock.module) { if (mock.module) {
// TODO: ENSURE this.options.static.root is correct value
mock.responses = require(path.resolve(path.join(this.options.static.root, mock.module)))
// TODO: ENSURE cliOptions.static.root is correct value
mock.responses = require(path.resolve(path.join(cliOptions.static.root, mock.module)))
} }
if (mock.responses) { if (mock.responses) {
this.push(mw.mockResponses(mock.route, mock.responses))
return 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
} }
this.push(mw.mockResponses(mock.route, mock.target))
return mw.mockResponses(mock.route, mock.target)
}
})
} }
}) })
return this return this
@ -167,44 +214,77 @@ class MiddlewareStack extends Array {
/* 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 */
addSpa (spa) { addSpa (spa) {
spa = t.isDefined(this.options.server.spa) ? this.options.server.spa : spa
this.push({
optionDefinitions: {
name: 'spa', alias: 's', type: String, typeLabel: '[underline]{file}',
description: 'Path to a Single Page App, e.g. app.html.'
},
middleware: function (cliOptions) {
spa = t.isDefined(cliOptions.middleware.spa) ? cliOptions.middleware.spa : spa
if (spa) { if (spa) {
const historyApiFallback = require('koa-connect-history-api-fallback') const historyApiFallback = require('koa-connect-history-api-fallback')
debug('SPA', spa) debug('SPA', spa)
this.push(historyApiFallback({
return historyApiFallback({
index: spa, index: spa,
verbose: this.options.verbose
}))
verbose: cliOptions.misc.verbose
})
} }
}
})
return this return this
} }
/* serve static files */ /* serve static files */
addStatic (root, options) { addStatic (root, options) {
root = this.options.server.directory || root || process.cwd()
this.push({
optionDefinitions: {
name: 'directory', alias: 'd', type: String, typeLabel: '[underline]{path}',
description: 'Root directory, defaults to the current directory.'
},
middleware: function (cliOptions) {
root = cliOptions.middleware.directory || root || process.cwd()
options = Object.assign({ hidden: true }, options) options = Object.assign({ hidden: true }, options)
// console.log(root, options, cliOptions)
if (root) { if (root) {
const serve = require('koa-static') const serve = require('koa-static')
this.push(serve(root, options))
return serve(root, options)
} }
}
})
return this return this
} }
/* serve directory index */ /* serve directory index */
addIndex (path, options) { addIndex (path, options) {
path = this.options.server.directory || path || process.cwd()
this.push({
middleware: function (cliOptions) {
path = cliOptions.middleware.directory || path || process.cwd()
options = Object.assign({ icons: true, hidden: true }, options) options = Object.assign({ icons: true, hidden: true }, options)
if (path) { if (path) {
const serveIndex = require('koa-serve-index') const serveIndex = require('koa-serve-index')
this.push(serveIndex(path, options))
return serveIndex(path, options)
} }
}
})
return this return this
} }
getOptionDefinitions () {
const flatten = require('reduce-flatten')
return this
.filter(mw => mw.optionDefinitions)
.map(mw => mw.optionDefinitions)
.reduce(flatten, [])
.map(def => {
def.group = 'middleware'
return def
})
}
compose (options) { compose (options) {
const compose = require('koa-compose')
const convert = require('koa-convert') const convert = require('koa-convert')
const middlewareStack = this.map(convert)
const middlewareStack = this
.filter(mw => mw)
.map(mw => compose(arrayify(mw.middleware(options)).map(convert)))
return compose(middlewareStack) return compose(middlewareStack)
} }
} }

12
lib/middleware.js

@ -1,6 +1,5 @@
'use strict' 'use strict'
const path = require('path') const path = require('path')
const http = require('http')
const url = require('url') const url = require('url')
const arrayify = require('array-back') const arrayify = require('array-back')
const t = require('typical') const t = require('typical')
@ -11,7 +10,6 @@ const debug = require('debug')('local-web-server')
* @module middleware * @module middleware
*/ */
exports.proxyRequest = proxyRequest exports.proxyRequest = proxyRequest
exports.blacklist = blacklist
exports.mockResponses = mockResponses exports.mockResponses = mockResponses
exports.mime = mime exports.mime = mime
@ -49,16 +47,6 @@ function proxyRequest (route) {
} }
} }
function blacklist (forbid) {
return function blacklist (ctx, next) {
if (forbid.some(expression => pathToRegexp(expression).test(ctx.path))) {
ctx.throw(403, http.STATUS_CODES[403])
} else {
return next()
}
}
}
function mime (mimeTypes) { function mime (mimeTypes) {
return function mime (ctx, next) { return function mime (ctx, next) {
return next().then(() => { return next().then(() => {

Loading…
Cancel
Save