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.

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