You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.8 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. 'use strict'
  2. const path = require('path')
  3. const http = require('http')
  4. const url = require('url')
  5. const Koa = require('koa')
  6. const convert = require('koa-convert')
  7. const cors = require('kcors')
  8. const _ = require('koa-route')
  9. const pathToRegexp = require('path-to-regexp')
  10. /**
  11. * @module local-web-server
  12. */
  13. module.exports = localWebServer
  14. /**
  15. * Returns a Koa application you can launch or mix into an existing app.
  16. *
  17. * @param [options] {object} - options
  18. * @param [options.static] {object} - koa-static config
  19. * @param [options.static.root] {string} - root directory
  20. * @param [options.static.options] {string} - [options](https://github.com/koajs/static#options)
  21. * @param [options.serveIndex] {object} - koa-serve-index config
  22. * @param [options.serveIndex.path] {string} - root directory
  23. * @param [options.serveIndex.options] {string} - [options](https://github.com/expressjs/serve-index#options)
  24. * @param [options.forbid] {string[]} - A list of forbidden routes, each route being an [express route-path](http://expressjs.com/guide/routing.html#route-paths).
  25. * @param [options.spa] {string} - specify an SPA file to catch requests for everything but static assets.
  26. * @param [options.log] {object} - [morgan](https://github.com/expressjs/morgan) config
  27. * @param [options.log.format] {string} - [log format](https://github.com/expressjs/morgan#predefined-formats)
  28. * @param [options.log.options] {object} - [options](https://github.com/expressjs/morgan#options)
  29. * @param [options.compress] {boolean} - Serve gzip-compressed resources, where applicable
  30. * @param [options.mime] {object} - A list of mime-type overrides, passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine)
  31. * @param [options.rewrite] {module:local-web-server~rewriteRule[]} - One or more rewrite rules
  32. * @param [options.verbose] {boolean} - Print detailed output, useful for debugging
  33. *
  34. * @alias module:local-web-server
  35. * @return {external:KoaApplication}
  36. * @example
  37. * const localWebServer = require('local-web-server')
  38. * localWebServer().listen(8000)
  39. */
  40. function localWebServer (options) {
  41. options = Object.assign({
  42. static: {},
  43. serveIndex: {},
  44. spa: null,
  45. log: {},
  46. compress: false,
  47. mime: {},
  48. forbid: [],
  49. rewrite: [],
  50. verbose: false
  51. }, options)
  52. const log = options.log
  53. log.options = log.options || {}
  54. const app = new Koa()
  55. const _use = app.use
  56. app.use = x => _use.call(app, convert(x))
  57. function verbose (category, message) {
  58. if (options.verbose) {
  59. process.nextTick(() => app.emit('verbose', category, message))
  60. }
  61. }
  62. app._verbose = verbose
  63. if (options.verbose && !log.format) {
  64. log.format = 'none'
  65. }
  66. /* CORS: allow from any origin */
  67. app.use(cors())
  68. /* rewrite rules */
  69. if (options.rewrite && options.rewrite.length) {
  70. options.rewrite.forEach(route => {
  71. if (route.to) {
  72. if (url.parse(route.to).host) {
  73. verbose('proxy rewrite', `${route.from} -> ${route.to}`)
  74. app.use(_.all(route.from, proxyRequest(route, app)))
  75. } else {
  76. const rewrite = require('koa-rewrite')
  77. verbose('local rewrite', `${route.from} -> ${route.to}`)
  78. app.use(rewrite(route.from, route.to))
  79. }
  80. }
  81. })
  82. }
  83. /* path blacklist */
  84. if (options.forbid.length) {
  85. verbose('forbid', options.forbid.join(', '))
  86. app.use(blacklist(options.forbid))
  87. }
  88. /* Cache */
  89. if (!options['no-cache']) {
  90. const conditional = require('koa-conditional-get')
  91. const etag = require('koa-etag')
  92. verbose('etag caching', 'enabled')
  93. app.use(conditional())
  94. app.use(etag())
  95. }
  96. /* mime-type overrides */
  97. if (options.mime) {
  98. verbose('mime override', JSON.stringify(options.mime))
  99. app.use((ctx, next) => {
  100. return next().then(() => {
  101. const reqPathExtension = path.extname(ctx.path).slice(1)
  102. Object.keys(options.mime).forEach(mimeType => {
  103. const extsToOverride = options.mime[mimeType]
  104. if (extsToOverride.indexOf(reqPathExtension) > -1) ctx.type = mimeType
  105. })
  106. })
  107. })
  108. }
  109. /* compress response */
  110. if (options.compress) {
  111. const compress = require('koa-compress')
  112. verbose('compression', 'enabled')
  113. app.use(compress())
  114. }
  115. /* Logging */
  116. if (log.format !== 'none') {
  117. const morgan = require('koa-morgan')
  118. if (!log.format) {
  119. const streamLogStats = require('stream-log-stats')
  120. log.options.stream = streamLogStats({ refreshRate: 500 })
  121. app.use(morgan.middleware('common', log.options))
  122. } else if (log.format === 'logstalgia') {
  123. morgan.token('date', logstalgiaDate)
  124. app.use(morgan.middleware('combined', log.options))
  125. } else {
  126. app.use(morgan.middleware(log.format, log.options))
  127. }
  128. }
  129. /* serve static files */
  130. if (options.static.root) {
  131. const serve = require('koa-static')
  132. verbose('static', `root: ${options.static.root} options: ${JSON.stringify(options.static.options)}` )
  133. app.use(serve(options.static.root, options.static.options))
  134. }
  135. /* serve directory index */
  136. if (options.serveIndex.path) {
  137. const serveIndex = require('koa-serve-index')
  138. verbose('serve-index', `root: ${options.serveIndex.path} options: ${JSON.stringify(options.serveIndex.options)}` )
  139. app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
  140. }
  141. /* for any URL not matched by static (e.g. `/search`), serve the SPA */
  142. if (options.spa) {
  143. const send = require('koa-send')
  144. verbose('SPA', options.spa)
  145. app.use(_.all('*', function * () {
  146. yield send(this, options.spa, { root: path.resolve(options.static.root) || process.cwd() })
  147. }))
  148. }
  149. return app
  150. }
  151. function logstalgiaDate () {
  152. var d = new Date()
  153. return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
  154. }
  155. function proxyRequest (route, app) {
  156. const httpProxy = require('http-proxy')
  157. const proxy = httpProxy.createProxyServer({
  158. changeOrigin: true
  159. })
  160. return function * proxyMiddleware () {
  161. const next = arguments[arguments.length - 1]
  162. const keys = []
  163. route.re = pathToRegexp(route.from, keys)
  164. route.new = this.url.replace(route.re, route.to)
  165. keys.forEach((key, index) => {
  166. const re = RegExp(`:${key.name}`, 'g')
  167. route.new = route.new
  168. .replace(re, arguments[index] || '')
  169. })
  170. /* test no keys remain in the new path */
  171. keys.length = 0
  172. pathToRegexp(url.parse(route.new).path, keys)
  173. if (keys.length) {
  174. this.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
  175. return next()
  176. }
  177. this.response = false
  178. app._verbose('proxy request', `from: ${this.path}, to: ${url.parse(route.new).href}`)
  179. proxy.once('error', err => {
  180. this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
  181. })
  182. proxy.once('proxyReq', function (proxyReq) {
  183. proxyReq.path = url.parse(route.new).path
  184. })
  185. proxy.web(this.req, this.res, { target: route.new })
  186. }
  187. }
  188. function blacklist (forbid) {
  189. return function blacklist (ctx, next) {
  190. if (forbid.some(expression => pathToRegexp(expression).test(ctx.path))) {
  191. ctx.throw(403, http.STATUS_CODES[403])
  192. } else {
  193. return next()
  194. }
  195. }
  196. }
  197. process.on('unhandledRejection', (reason, p) => {
  198. throw reason
  199. })
  200. /**
  201. * The `from` and `to` routes are specified using [express route-paths](http://expressjs.com/guide/routing.html#route-paths)
  202. *
  203. * @example
  204. * ```json
  205. * {
  206. * "rewrite": [
  207. * { "from": "/css/*", "to": "/build/styles/$1" },
  208. * { "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
  209. * { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
  210. * ]
  211. * }
  212. * ```
  213. *
  214. * @typedef rewriteRule
  215. * @property from {string} - request route
  216. * @property to {string} - target route
  217. */
  218. /**
  219. * @external KoaApplication
  220. * @see https://github.com/koajs/koa/blob/master/docs/api/index.md#application
  221. */