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.

203 lines
6.2 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
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 url = require('url')
  4. const arrayify = require('array-back')
  5. /**
  6. * @module local-web-server
  7. */
  8. module.exports = localWebServer
  9. /**
  10. * Returns a Koa application you can launch or mix into an existing app.
  11. *
  12. * @param [options] {object} - options
  13. * @param [options.static] {object} - koa-static config
  14. * @param [options.static.root=.] {string} - root directory
  15. * @param [options.static.options] {string} - [options](https://github.com/koajs/static#options)
  16. * @param [options.serveIndex] {object} - koa-serve-index config
  17. * @param [options.serveIndex.path=.] {string} - root directory
  18. * @param [options.serveIndex.options] {string} - [options](https://github.com/expressjs/serve-index#options)
  19. * @param [options.forbid] {string[]} - A list of forbidden routes, each route being an [express route-path](http://expressjs.com/guide/routing.html#route-paths).
  20. * @param [options.spa] {string} - specify an SPA file to catch requests for everything but static assets.
  21. * @param [options.log] {object} - [morgan](https://github.com/expressjs/morgan) config
  22. * @param [options.log.format] {string} - [log format](https://github.com/expressjs/morgan#predefined-formats)
  23. * @param [options.log.options] {object} - [options](https://github.com/expressjs/morgan#options)
  24. * @param [options.compress] {boolean} - Serve gzip-compressed resources, where applicable
  25. * @param [options.mime] {object} - A list of mime-type overrides, passed directly to [mime.define()](https://github.com/broofa/node-mime#mimedefine)
  26. * @param [options.rewrite] {module:local-web-server~rewriteRule[]} - One or more rewrite rules
  27. * @param [options.verbose] {boolean} - Print detailed output, useful for debugging
  28. *
  29. * @alias module:local-web-server
  30. * @return {external:KoaApplication}
  31. * @example
  32. * const localWebServer = require('local-web-server')
  33. * localWebServer().listen(8000)
  34. */
  35. function localWebServer (options) {
  36. options = Object.assign({
  37. static: {},
  38. serveIndex: {},
  39. spa: null,
  40. log: {},
  41. compress: false,
  42. mime: {},
  43. forbid: [],
  44. rewrite: [],
  45. verbose: false
  46. }, options)
  47. if (options.verbose) {
  48. process.env.DEBUG = '*'
  49. }
  50. const log = options.log
  51. log.options = log.options || {}
  52. if (options.verbose && !log.format) {
  53. log.format = 'none'
  54. }
  55. options.rewrite = arrayify(options.rewrite)
  56. options.forbid = arrayify(options.forbid)
  57. const debug = require('debug')('local-web-server')
  58. const Koa = require('koa')
  59. const convert = require('koa-convert')
  60. const cors = require('kcors')
  61. const _ = require('koa-route')
  62. const json = require('koa-json')
  63. const bodyParser = require('koa-bodyparser')
  64. const mw = require('./middleware')
  65. const app = new Koa()
  66. const _use = app.use
  67. app.use = x => _use.call(app, convert(x))
  68. /* CORS: allow from any origin */
  69. app.use(cors())
  70. /* pretty print JSON */
  71. app.use(json())
  72. /* request body parser */
  73. app.use(bodyParser())
  74. /* rewrite rules */
  75. if (options.rewrite && options.rewrite.length) {
  76. options.rewrite.forEach(route => {
  77. if (route.to) {
  78. if (url.parse(route.to).host) {
  79. debug('proxy rewrite', `${route.from} -> ${route.to}`)
  80. app.use(_.all(route.from, mw.proxyRequest(route, app)))
  81. } else {
  82. const rewrite = require('koa-rewrite')
  83. const rmw = rewrite(route.from, route.to)
  84. rmw._name = 'rewrite'
  85. app.use(rmw)
  86. }
  87. }
  88. })
  89. }
  90. /* path blacklist */
  91. if (options.forbid.length) {
  92. debug('forbid', options.forbid.join(', '))
  93. app.use(mw.blacklist(options.forbid))
  94. }
  95. /* Cache */
  96. if (!options['no-cache']) {
  97. const conditional = require('koa-conditional-get')
  98. const etag = require('koa-etag')
  99. app.use(conditional())
  100. app.use(etag())
  101. }
  102. /* mime-type overrides */
  103. if (options.mime) {
  104. debug('mime override', JSON.stringify(options.mime))
  105. app.use(mw.mime(options.mime))
  106. }
  107. /* compress response */
  108. if (options.compress) {
  109. const compress = require('koa-compress')
  110. debug('compression', 'enabled')
  111. app.use(compress())
  112. }
  113. /* Logging */
  114. if (log.format !== 'none') {
  115. const morgan = require('koa-morgan')
  116. if (!log.format) {
  117. const streamLogStats = require('stream-log-stats')
  118. log.options.stream = streamLogStats({ refreshRate: 500 })
  119. app.use(morgan.middleware('common', log.options))
  120. } else if (log.format === 'logstalgia') {
  121. morgan.token('date', logstalgiaDate)
  122. app.use(morgan.middleware('combined', log.options))
  123. } else {
  124. app.use(morgan.middleware(log.format, log.options))
  125. }
  126. }
  127. /* Mock Responses */
  128. app.use(mw.mockResponses({ root: options.static.root }))
  129. /* serve static files */
  130. if (options.static.root) {
  131. const serve = require('koa-static')
  132. app.use(serve(options.static.root, options.static.options))
  133. }
  134. /* serve directory index */
  135. if (options.serveIndex.path) {
  136. const serveIndex = require('koa-serve-index')
  137. app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
  138. }
  139. /* for any URL not matched by static (e.g. `/search`), serve the SPA */
  140. if (options.spa) {
  141. const send = require('koa-send')
  142. debug('SPA', options.spa)
  143. app.use(_.all('*', function * () {
  144. yield send(this, options.spa, { root: path.resolve(options.static.root) || process.cwd() })
  145. }))
  146. }
  147. return app
  148. }
  149. function logstalgiaDate () {
  150. var d = new Date()
  151. return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
  152. }
  153. process.on('unhandledRejection', (reason, p) => {
  154. throw reason
  155. })
  156. /**
  157. * The `from` and `to` routes are specified using [express route-paths](http://expressjs.com/guide/routing.html#route-paths)
  158. *
  159. * @example
  160. * ```json
  161. * {
  162. * "rewrite": [
  163. * { "from": "/css/*", "to": "/build/styles/$1" },
  164. * { "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
  165. * { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
  166. * ]
  167. * }
  168. * ```
  169. *
  170. * @typedef rewriteRule
  171. * @property from {string} - request route
  172. * @property to {string} - target route
  173. */
  174. /**
  175. * @external KoaApplication
  176. * @see https://github.com/koajs/koa/blob/master/docs/api/index.md#application
  177. */