Browse Source

refactor

master
Lloyd Brookes 9 years ago
parent
commit
bd182e4ffd
  1. 187
      bin/cli.js
  2. 27
      extend/cache-control.js
  3. 8
      lib/cli-data.js
  4. 327
      lib/local-web-server.js
  5. 211
      lib/middleware-stack.js
  6. 3
      package.json

187
bin/cli.js

@ -1,171 +1,20 @@
#!/usr/bin/env node
'use strict'
const cli = require('../lib/cli-data')
const commandLineUsage = require('command-line-usage')
const ansi = require('ansi-escape-sequences')
const path = require('path')
const arrayify = require('array-back')
const t = require('typical')
function ws () {
const usage = commandLineUsage(cli.usageData)
let options
try {
options = collectOptions()
} catch (err) {
stop([ `[red]{Error}: ${err.message}`, usage ], 1)
return
}
if (options.misc.help) {
stop(usage, 0)
} else if (options.misc.config) {
stop(JSON.stringify(options.server, null, ' '), 0)
} else {
const localWebServer = require('../')
const Koa = require('koa')
const valid = validateOptions(options, usage)
if (!valid) return
const app = new Koa()
app.on('error', err => {
if (options.server['log-format']) {
console.error(ansi.format(err.message, 'red'))
}
})
const ws = localWebServer({
static: {
root: options.server.directory,
options: {
hidden: true
}
},
serveIndex: {
path: options.server.directory,
options: {
icons: true,
hidden: true
}
},
log: {
format: options.server['log-format']
},
compress: options.server.compress,
mime: options.server.mime,
forbid: options.server.forbid,
spa: options.server.spa,
'no-cache': options.server['no-cache'],
rewrite: options.server.rewrite,
verbose: options.server.verbose,
mocks: options.server.mocks
})
app.use(ws)
if (options.server.https) {
options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
}
if (options.server.key && options.server.cert) {
const https = require('https')
const fs = require('fs')
const serverOptions = {
key: fs.readFileSync(options.server.key),
cert: fs.readFileSync(options.server.cert)
}
const server = https.createServer(serverOptions, app.callback())
server.listen(options.server.port, onServerUp.bind(null, options, true))
} else {
app.listen(options.server.port, onServerUp.bind(null, options))
}
}
}
function stop (msgs, exitCode) {
arrayify(msgs).forEach(msg => console.error(ansi.format(msg)))
process.exitCode = exitCode
}
function onServerUp (options, isHttps) {
const ipList = getIPList(isHttps)
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.server.port}}`)
.join(', ')
console.error(ansi.format(
path.resolve(options.server.directory) === process.cwd()
? `serving at ${ipList}`
: `serving [underline]{${options.server.directory}} at ${ipList}`
))
}
function getIPList (isHttps) {
const flatten = require('reduce-flatten')
const os = require('os')
let ipList = Object.keys(os.networkInterfaces())
.map(key => os.networkInterfaces()[key])
.reduce(flatten, [])
.filter(iface => iface.family === 'IPv4')
ipList.unshift({ address: os.hostname() })
return ipList
}
/**
* Return default, stored and command-line options combined
*/
function collectOptions () {
const commandLineArgs = require('command-line-args')
const loadConfig = require('config-master')
const stored = loadConfig('local-web-server')
/* parse command line args */
let options = commandLineArgs(cli.definitions)
const builtIn = {
port: 8000,
directory: process.cwd(),
forbid: [],
rewrite: []
}
if (options.server.rewrite) {
options.server.rewrite = parseRewriteRules(options.server.rewrite)
}
/* override built-in defaults with stored config and then command line args */
options.server = Object.assign(builtIn, stored, options.server)
return options
}
function parseRewriteRules (rules) {
return rules && rules.map(rule => {
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
return {
from: matches[1],
to: matches[2]
}
})
}
function validateOptions (options, usage) {
let valid = true
function invalid (msg) {
return `[red underline]{Invalid:} [bold]{${msg}}`
}
if (!t.isNumber(options.server.port)) {
stop([ invalid(`--port must be numeric`), usage ], 1)
valid = false
}
return valid
}
ws()
const Cli = require('../')
const ws = new Cli()
ws.middleware.addCors()
ws.middleware.addJson()
ws.middleware.addRewrite()
ws.middleware.addBodyParser()
ws.middleware.addBlacklist()
ws.middleware.addCache()
ws.middleware.addMimeType()
ws.middleware.addCompression()
ws.middleware.addLogging()
ws.middleware.addMockResponses()
ws.middleware.addSpa()
ws.middleware.addStatic()
ws.middleware.addIndex()
ws.listen()

27
extend/cache-control.js

@ -1,17 +1,24 @@
'use strict'
const Koa = require('koa')
const localWebServer = require('../')
const Cli = require('../cli')
const cacheControl = require('koa-cache-control')
const convert = require('koa-convert')
const cliData = require('../lib/cli-data')
const app = new Koa()
const ws = localWebServer({
cliData.push({ name: 'black' })
const ws = new Cli({
'no-cache': true,
log: { format: 'dev' }
})
app.use(convert(cacheControl({
maxAge: 15
})))
app.use(ws)
app.listen(8000)
ws.middleware.splice(
ws.middleware.findIndex(m => m.name === 'mime-type'),
1,
{
name: 'cache-control',
create: convert(cacheControl({
maxAge: 15
}))
}
)
ws.listen()

8
lib/cli-data.js

@ -1,4 +1,4 @@
exports.definitions = [
exports.optionDefinitions = [
{
name: 'port', alias: 'p', type: Number, defaultOption: true,
description: 'Web server port.', group: 'server'
@ -57,7 +57,7 @@ exports.definitions = [
}
]
exports.usageData = [
exports.usage = [
{
header: 'local-web-server',
content: 'A simple web-server for productive front-end development.'
@ -72,12 +72,12 @@ exports.usageData = [
},
{
header: 'Server options',
optionList: exports.definitions,
optionList: exports.optionDefinitions,
group: 'server'
},
{
header: 'Misc options',
optionList: exports.definitions,
optionList: exports.optionDefinitions,
group: 'misc'
},
{

327
lib/local-web-server.js

@ -1,229 +1,158 @@
#!/usr/bin/env node
'use strict'
const ansi = require('ansi-escape-sequences')
const path = require('path')
const url = require('url')
const arrayify = require('array-back')
const t = require('typical')
const Tool = require('command-line-tool')
const tool = new Tool()
/**
* @module local-web-server
*/
module.exports = localWebServer
class Cli {
constructor () {
this.options = null
this.app = null
this.middleware = null
/**
* Returns a Koa application you can launch or mix into an existing app.
*
* @param [options] {object} - options
* @param [options.static] {object} - koa-static config
* @param [options.static.root=.] {string} - root directory
* @param [options.static.options] {string} - [options](https://github.com/koajs/static#options)
* @param [options.serveIndex] {object} - koa-serve-index config
* @param [options.serveIndex.path=.] {string} - root directory
* @param [options.serveIndex.options] {string} - [options](https://github.com/expressjs/serve-index#options)
* @param [options.forbid] {string[]} - A list of forbidden routes, each route being an [express route-path](http://expressjs.com/guide/routing.html#route-paths).
* @param [options.spa] {string} - specify an SPA file to catch requests for everything but static assets.
* @param [options.log] {object} - [morgan](https://github.com/expressjs/morgan) config
* @param [options.log.format] {string} - [log format](https://github.com/expressjs/morgan#predefined-formats)
* @param [options.log.options] {object} - [options](https://github.com/expressjs/morgan#options)
* @param [options.compress] {boolean} - Serve gzip-compressed resources, where applicable
* @param [options.mime] {object} - A list of mime-type overrides, passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine)
* @param [options.rewrite] {module:local-web-server~rewriteRule[]} - One or more rewrite rules
* @param [options.verbose] {boolean} - Print detailed output, useful for debugging
*
* @alias module:local-web-server
* @return {external:KoaApplication}
* @example
* const localWebServer = require('local-web-server')
* localWebServer().listen(8000)
*/
function localWebServer (options) {
options = Object.assign({
static: {},
serveIndex: {
options: {
icons: true,
hidden: true
}
},
cacheControl: {},
spa: null,
log: {},
compress: false,
mime: {},
forbid: [],
rewrite: [],
verbose: false,
mocks: []
}, options)
let options = collectOptions()
this.options = options
if (options.verbose) {
process.env.DEBUG = '*'
if (options.misc.config) {
tool.stop(JSON.stringify(options.server, null, ' '), 0)
} else {
const Koa = require('koa')
const app = new Koa()
this.app = app
const MiddlewareStack = require('./middleware-stack')
this.middleware = new MiddlewareStack({
static: {
root: options.server.directory,
options: {
hidden: true
}
},
serveIndex: {
path: options.server.directory,
options: {
icons: true,
hidden: true
}
},
log: {
format: options.server['log-format']
},
compress: options.server.compress,
mime: options.server.mime,
forbid: options.server.forbid,
spa: options.server.spa,
'no-cache': options.server['no-cache'],
rewrite: options.server.rewrite,
verbose: options.server.verbose,
mocks: options.server.mocks
})
app.on('error', err => {
if (options.server['log-format']) {
console.error(ansi.format(err.message, 'red'))
}
})
}
}
const log = options.log
log.options = log.options || {}
listen () {
this.app.use(this.middleware.getMiddleware())
const options = this.options
if (options.server.https) {
options.server.key = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.key')
options.server.cert = path.resolve(__dirname, '..', 'ssl', '127.0.0.1.crt')
}
if (options.verbose && !log.format) {
log.format = 'none'
}
if (options.server.key && options.server.cert) {
const https = require('https')
const fs = require('fs')
if (!options.static.root) options.static.root = process.cwd()
if (!options.serveIndex.path) options.serveIndex.path = process.cwd()
options.rewrite = arrayify(options.rewrite)
options.forbid = arrayify(options.forbid)
options.mocks = arrayify(options.mocks)
const debug = require('debug')('local-web-server')
const convert = require('koa-convert')
const cors = require('kcors')
const _ = require('koa-route')
const json = require('koa-json')
const bodyParser = require('koa-bodyparser')
const mw = require('./middleware')
let middlewareStack = []
/* CORS: allow from any origin */
middlewareStack.push(cors())
/* pretty print JSON */
middlewareStack.push(json())
/* rewrite rules */
if (options.rewrite && options.rewrite.length) {
options.rewrite.forEach(route => {
if (route.to) {
/* `to` address is remote if the url specifies a host */
if (url.parse(route.to).host) {
debug('proxy rewrite', `${route.from} -> ${route.to}`)
middlewareStack.push(_.all(route.from, mw.proxyRequest(route)))
} else {
const rewrite = require('koa-rewrite')
const rmw = rewrite(route.from, route.to)
rmw._name = 'rewrite'
middlewareStack.push(rmw)
}
const serverOptions = {
key: fs.readFileSync(options.server.key),
cert: fs.readFileSync(options.server.cert)
}
})
}
/* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */
middlewareStack.push(bodyParser())
/* path blacklist */
if (options.forbid.length) {
debug('forbid', options.forbid.join(', '))
middlewareStack.push(mw.blacklist(options.forbid))
const server = https.createServer(serverOptions, this.app.callback())
server.listen(options.server.port, onServerUp.bind(null, options, true))
} else {
this.app.listen(options.server.port, onServerUp.bind(null, options))
}
}
}
/* cache */
if (!options['no-cache']) {
const conditional = require('koa-conditional-get')
const etag = require('koa-etag')
middlewareStack.push(conditional())
middlewareStack.push(etag())
}
function onServerUp (options, isHttps) {
const ipList = getIPList(isHttps)
.map(iface => `[underline]{${isHttps ? 'https' : 'http'}://${iface.address}:${options.server.port}}`)
.join(', ')
/* mime-type overrides */
if (options.mime) {
debug('mime override', JSON.stringify(options.mime))
middlewareStack.push(mw.mime(options.mime))
}
console.error(ansi.format(
path.resolve(options.server.directory) === process.cwd()
? `serving at ${ipList}`
: `serving [underline]{${options.server.directory}} at ${ipList}`
))
}
/* compress response */
if (options.compress) {
const compress = require('koa-compress')
debug('compression', 'enabled')
middlewareStack.push(compress())
}
function getIPList (isHttps) {
const flatten = require('reduce-flatten')
const os = require('os')
/* Logging */
if (log.format !== 'none') {
const morgan = require('koa-morgan')
if (!log.format) {
const streamLogStats = require('stream-log-stats')
log.options.stream = streamLogStats({ refreshRate: 500 })
middlewareStack.push(morgan('common', log.options))
} else if (log.format === 'logstalgia') {
morgan.token('date', logstalgiaDate)
middlewareStack.push(morgan('combined', log.options))
} else {
middlewareStack.push(morgan(log.format, log.options))
}
}
let ipList = Object.keys(os.networkInterfaces())
.map(key => os.networkInterfaces()[key])
.reduce(flatten, [])
.filter(iface => iface.family === 'IPv4')
ipList.unshift({ address: os.hostname() })
return ipList
}
/* Mock Responses */
options.mocks.forEach(mock => {
if (mock.module) {
mock.responses = require(path.resolve(path.join(options.static.root, mock.module)))
}
/**
* Return default, stored and command-line options combined
*/
function collectOptions () {
const loadConfig = require('config-master')
const stored = loadConfig('local-web-server')
const cli = require('../lib/cli-data')
if (mock.responses) {
middlewareStack.push(mw.mockResponses(mock.route, mock.responses))
} else if (mock.response) {
mock.target = {
request: mock.request,
response: mock.response
}
middlewareStack.push(mw.mockResponses(mock.route, mock.target))
}
})
/* parse command line args */
let options = tool.getOptions(cli.optionDefinitions, cli.usage)
/* for any URL not matched by static (e.g. `/search`), serve the SPA */
if (options.spa) {
const historyApiFallback = require('koa-connect-history-api-fallback')
debug('SPA', options.spa)
middlewareStack.push(historyApiFallback({
index: options.spa,
verbose: options.verbose
}))
const builtIn = {
port: 8000,
directory: process.cwd(),
forbid: [],
rewrite: []
}
/* serve static files */
if (options.static.root) {
const serve = require('koa-static')
middlewareStack.push(serve(options.static.root, options.static.options))
if (options.server.rewrite) {
options.server.rewrite = parseRewriteRules(options.server.rewrite)
}
/* serve directory index */
if (options.serveIndex.path) {
const serveIndex = require('koa-serve-index')
middlewareStack.push(serveIndex(options.serveIndex.path, options.serveIndex.options))
}
/* override built-in defaults with stored config and then command line args */
options.server = Object.assign(builtIn, stored, options.server)
const compose = require('koa-compose')
middlewareStack = middlewareStack.map(convert)
return compose(middlewareStack)
validateOptions(options)
return options
}
function logstalgiaDate () {
var d = new Date()
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
function parseRewriteRules (rules) {
return rules && rules.map(rule => {
const matches = rule.match(/(\S*)\s*->\s*(\S*)/)
return {
from: matches[1],
to: matches[2]
}
})
}
process.on('unhandledRejection', (reason, p) => {
throw reason
})
/**
* The `from` and `to` routes are specified using [express route-paths](http://expressjs.com/guide/routing.html#route-paths)
*
* @example
* ```json
* {
* "rewrite": [
* { "from": "/css/*", "to": "/build/styles/$1" },
* { "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
* { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
* ]
* }
* ```
*
* @typedef rewriteRule
* @property from {string} - request route
* @property to {string} - target route
*/
function validateOptions (options) {
if (!t.isNumber(options.server.port)) {
tool.printError('--port must be numeric')
console.error(tool.usage)
tool.halt()
}
}
/**
* @external KoaApplication
* @see https://github.com/koajs/koa/blob/master/docs/api/index.md#application
*/
module.exports = Cli

211
lib/middleware-stack.js

@ -0,0 +1,211 @@
'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')
class MiddlewareStack extends Array {
constructor (options) {
super()
options = Object.assign({
static: {},
serveIndex: {
options: {
icons: true,
hidden: true
}
},
cacheControl: {},
spa: null,
log: {},
compress: false,
mime: {},
forbid: [],
rewrite: [],
verbose: false,
mocks: []
}, options)
if (options.verbose) {
process.env.DEBUG = '*'
}
const log = options.log
log.options = log.options || {}
if (options.verbose && !log.format) {
log.format = 'none'
}
this.log = log
if (!options.static.root) options.static.root = process.cwd()
if (!options.serveIndex.path) options.serveIndex.path = process.cwd()
options.rewrite = arrayify(options.rewrite)
options.forbid = arrayify(options.forbid)
options.mocks = arrayify(options.mocks)
this.options = options
}
/**
* allow from any origin
*/
addCors () {
this.push(require('kcors')())
return this
}
/* pretty print JSON */
addJson () {
this.push(require('koa-json')())
return this
}
/* rewrite rules */
addRewrite () {
const _ = require('koa-route')
const options = this.options.rewrite
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) {
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')())
}
/* path blacklist */
addBlacklist () {
const options = this.options.forbid
if (options.length) {
debug('forbid', options.join(', '))
this.push(mw.blacklist(options))
}
}
/* cache */
addCache () {
if (!this.options['no-cache']) {
this.push(require('koa-conditional-get')())
this.push(require('koa-etag')())
}
}
/* mime-type overrides */
addMimeType () {
const options = this.options.mime
if (options) {
debug('mime override', JSON.stringify(options))
this.push(mw.mime(options))
}
}
/* compress response */
addCompression () {
if (this.options.compress) {
const compress = require('koa-compress')
debug('compression', 'enabled')
this.push(compress())
}
}
/* Logging */
addLogging () {
const log = this.log
if (log.format !== 'none') {
const morgan = require('koa-morgan')
if (!log.format) {
const streamLogStats = require('stream-log-stats')
log.options.stream = streamLogStats({ refreshRate: 500 })
this.push(morgan('common', log.options))
} else if (log.format === 'logstalgia') {
morgan.token('date', () => {
var d = new Date()
return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
})
this.push(morgan('combined', log.options))
} else {
this.push(morgan(log.format, log.options))
}
}
}
/* Mock Responses */
addMockResponses () {
const options = this.options.mocks
options.forEach(mock => {
if (mock.module) {
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))
}
})
}
/* for any URL not matched by static (e.g. `/search`), serve the SPA */
addSpa () {
if (this.options.spa) {
const historyApiFallback = require('koa-connect-history-api-fallback')
debug('SPA', this.options.spa)
this.push(historyApiFallback({
index: this.options.spa,
verbose: this.options.verbose
}))
}
}
/* serve static files */
addStatic () {
const options = this.options.static
if (options.root) {
const serve = require('koa-static')
this.push(serve(options.root, options.options))
}
return this
}
/* serve directory index */
addIndex () {
const options = this.options.serveIndex
if (options.path) {
const serveIndex = require('koa-serve-index')
this.push(serveIndex(options.path, options.options))
}
return this
}
getMiddleware (options) {
const compose = require('koa-compose')
const convert = require('koa-convert')
const middlewareStack = this.map(convert)
return compose(middlewareStack)
}
}
module.exports = MiddlewareStack

3
package.json

@ -31,8 +31,7 @@
"dependencies": {
"ansi-escape-sequences": "^2.2.2",
"array-back": "^1.0.3",
"command-line-args": "^3.0.0",
"command-line-usage": "^3.0.1",
"command-line-tool": "75lb/command-line-tool",
"config-master": "^2.0.2",
"debug": "^2.2.0",
"http-proxy": "^1.13.3",

Loading…
Cancel
Save