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.

215 lines
6.1 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
  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
  16. *
  17. * @param [options] {object} - options
  18. * @param [options.static] {object} - koajs/static config
  19. * @param [options.static.root] {string} - root directory
  20. * @param [options.static.options] {string} - 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
  24. * @param [options.forbid] {string[]} - a list of forbidden routes.
  25. *
  26. * @alias module:local-web-server
  27. * @example
  28. * const localWebServer = require('local-web-server')
  29. * localWebServer().listen(8000)
  30. */
  31. function localWebServer (options) {
  32. options = Object.assign({
  33. static: {},
  34. serveIndex: {},
  35. spa: null,
  36. log: {},
  37. compress: false,
  38. mime: {},
  39. forbid: [],
  40. rewrite: [],
  41. verbose: false
  42. }, options)
  43. const log = options.log
  44. log.options = log.options || {}
  45. const app = new Koa()
  46. const _use = app.use
  47. app.use = x => _use.call(app, convert(x))
  48. function verbose (category, message) {
  49. if (options.verbose) {
  50. process.nextTick(() => app.emit('verbose', category, message))
  51. }
  52. }
  53. app._verbose = verbose
  54. if (options.verbose && !log.format) {
  55. log.format = 'none'
  56. }
  57. /* CORS: allow from any origin */
  58. app.use(cors())
  59. /* rewrite rules */
  60. if (options.rewrite && options.rewrite.length) {
  61. options.rewrite.forEach(route => {
  62. if (route.to) {
  63. if (url.parse(route.to).host) {
  64. verbose('proxy rewrite', `${route.from} -> ${route.to}`)
  65. app.use(_.all(route.from, proxyRequest(route, app)))
  66. } else {
  67. const rewrite = require('koa-rewrite')
  68. verbose('local rewrite', `${route.from} -> ${route.to}`)
  69. app.use(rewrite(route.from, route.to))
  70. }
  71. }
  72. })
  73. }
  74. /* path blacklist */
  75. if (options.forbid.length) {
  76. verbose('forbid', options.forbid.join(', '))
  77. app.use(blacklist(options.forbid))
  78. }
  79. /* Cache */
  80. if (!options['no-cache']) {
  81. const conditional = require('koa-conditional-get')
  82. const etag = require('koa-etag')
  83. verbose('etag caching', 'enabled')
  84. app.use(conditional())
  85. app.use(etag())
  86. }
  87. /* mime-type overrides */
  88. if (options.mime) {
  89. verbose('mime override', JSON.stringify(options.mime))
  90. app.use((ctx, next) => {
  91. return next().then(() => {
  92. const reqPathExtension = path.extname(ctx.path).slice(1)
  93. Object.keys(options.mime).forEach(mimeType => {
  94. const extsToOverride = options.mime[mimeType]
  95. if (extsToOverride.indexOf(reqPathExtension) > -1) ctx.type = mimeType
  96. })
  97. })
  98. })
  99. }
  100. /* compress response */
  101. if (options.compress) {
  102. const compress = require('koa-compress')
  103. verbose('compression', 'enabled')
  104. app.use(compress())
  105. }
  106. /* Logging */
  107. if (log.format !== 'none') {
  108. const morgan = require('koa-morgan')
  109. if (!log.format) {
  110. const streamLogStats = require('stream-log-stats')
  111. log.options.stream = streamLogStats({ refreshRate: 500 })
  112. app.use(morgan.middleware('common', log.options))
  113. } else if (log.format === 'logstalgia') {
  114. morgan.token('date', logstalgiaDate)
  115. app.use(morgan.middleware('combined', log.options))
  116. } else {
  117. app.use(morgan.middleware(log.format, log.options))
  118. }
  119. }
  120. /* serve static files */
  121. if (options.static.root) {
  122. const serve = require('koa-static')
  123. verbose('static', `root: ${options.static.root} options: ${JSON.stringify(options.static.options)}` )
  124. app.use(serve(options.static.root, options.static.options))
  125. }
  126. /* serve directory index */
  127. if (options.serveIndex.path) {
  128. const serveIndex = require('koa-serve-index')
  129. verbose('serve-index', `root: ${options.serveIndex.path} options: ${JSON.stringify(options.serveIndex.options)}` )
  130. app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
  131. }
  132. /* for any URL not matched by static (e.g. `/search`), serve the SPA */
  133. if (options.spa) {
  134. const send = require('koa-send')
  135. verbose('SPA', options.spa)
  136. app.use(_.all('*', function * () {
  137. yield send(this, options.spa, { root: path.resolve(options.static.root) || process.cwd() })
  138. }))
  139. }
  140. return app
  141. }
  142. function logstalgiaDate () {
  143. var d = new Date()
  144. return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
  145. }
  146. function proxyRequest (route, app) {
  147. const httpProxy = require('http-proxy')
  148. const proxy = httpProxy.createProxyServer({
  149. changeOrigin: true
  150. })
  151. return function * proxyMiddleware () {
  152. const next = arguments[arguments.length - 1]
  153. const keys = []
  154. route.re = pathToRegexp(route.from, keys)
  155. route.new = this.path.replace(route.re, route.to)
  156. keys.forEach((key, index) => {
  157. const re = RegExp(`:${key.name}`, 'g')
  158. route.new = route.new
  159. .replace(re, arguments[index] || '')
  160. })
  161. /* test no keys remain in the new path */
  162. keys.length = 0
  163. pathToRegexp(route.new, keys)
  164. if (keys.length) {
  165. this.throw(500, `[PROXY] Invalid target URL: ${route.new}`)
  166. return next()
  167. }
  168. this.response = false
  169. app._verbose('proxy request', `from: ${this.path}, to: ${url.parse(route.new).href}`)
  170. proxy.once('error', err => {
  171. this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
  172. })
  173. proxy.once('proxyReq', function (proxyReq) {
  174. proxyReq.path = url.parse(route.new).path
  175. })
  176. proxy.web(this.req, this.res, { target: route.new })
  177. }
  178. }
  179. function blacklist (forbid) {
  180. return function blacklist (ctx, next) {
  181. if (forbid.some(expression => pathToRegexp(expression).test(ctx.path))) {
  182. ctx.throw(403, http.STATUS_CODES[403])
  183. } else {
  184. return next()
  185. }
  186. }
  187. }
  188. process.on('unhandledRejection', (reason, p) => {
  189. throw reason
  190. })