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.

249 lines
7.9 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
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. mocks: []
  47. }, options)
  48. if (options.verbose) {
  49. process.env.DEBUG = '*'
  50. }
  51. const log = options.log
  52. log.options = log.options || {}
  53. if (options.verbose && !log.format) {
  54. log.format = 'none'
  55. }
  56. if (!options.static.root) options.static.root = process.cwd()
  57. if (!options.serveIndex.path) options.serveIndex.path = process.cwd()
  58. options.rewrite = arrayify(options.rewrite)
  59. options.forbid = arrayify(options.forbid)
  60. options.mocks = arrayify(options.mocks)
  61. const debug = require('debug')('local-web-server')
  62. const Koa = require('koa')
  63. const convert = require('koa-convert')
  64. const cors = require('kcors')
  65. const _ = require('koa-route')
  66. const json = require('koa-json')
  67. const bodyParser = require('koa-bodyparser')
  68. const mw = require('./middleware')
  69. let middlewares = []
  70. // const app = new Koa()
  71. // const _use = app.use
  72. // app.use = x => _use.call(app, convert(x))
  73. /* CORS: allow from any origin */
  74. // app.use(cors())
  75. middlewares.push(cors())
  76. /* pretty print JSON */
  77. // app.use(json())
  78. middlewares.push(json())
  79. /* rewrite rules */
  80. if (options.rewrite && options.rewrite.length) {
  81. options.rewrite.forEach(route => {
  82. if (route.to) {
  83. /* `to` address is remote if the url specifies a host */
  84. if (url.parse(route.to).host) {
  85. debug('proxy rewrite', `${route.from} -> ${route.to}`)
  86. // app.use(_.all(route.from, mw.proxyRequest(route)))
  87. middlewares.push(_.all(route.from, mw.proxyRequest(route)))
  88. } else {
  89. const rewrite = require('koa-rewrite')
  90. const rmw = rewrite(route.from, route.to)
  91. rmw._name = 'rewrite'
  92. // app.use(rmw)
  93. middlewares.push(rmw)
  94. }
  95. }
  96. })
  97. }
  98. /* must come after rewrite. See https://github.com/nodejitsu/node-http-proxy/issues/180. */
  99. // app.use(bodyParser())
  100. middlewares.push(bodyParser())
  101. /* path blacklist */
  102. if (options.forbid.length) {
  103. debug('forbid', options.forbid.join(', '))
  104. // app.use(mw.blacklist(options.forbid))
  105. middlewares.push(mw.blacklist(options.forbid))
  106. }
  107. /* cache */
  108. if (!options['no-cache']) {
  109. const conditional = require('koa-conditional-get')
  110. const etag = require('koa-etag')
  111. // app.use(conditional())
  112. // app.use(etag())
  113. middlewares.push(conditional())
  114. middlewares.push(etag())
  115. }
  116. /* mime-type overrides */
  117. if (options.mime) {
  118. debug('mime override', JSON.stringify(options.mime))
  119. // app.use(mw.mime(options.mime))
  120. middlewares.push(mw.mime(options.mime))
  121. }
  122. /* compress response */
  123. if (options.compress) {
  124. const compress = require('koa-compress')
  125. debug('compression', 'enabled')
  126. // app.use(compress())
  127. middlewares.push(compress())
  128. }
  129. /* Logging */
  130. if (log.format !== 'none') {
  131. const morgan = require('koa-morgan')
  132. if (!log.format) {
  133. const streamLogStats = require('stream-log-stats')
  134. log.options.stream = streamLogStats({ refreshRate: 500 })
  135. // app.use(morgan('common', log.options))
  136. middlewares.push(morgan('common', log.options))
  137. } else if (log.format === 'logstalgia') {
  138. morgan.token('date', logstalgiaDate)
  139. // app.use(morgan('combined', log.options))
  140. middlewares.push(morgan('combined', log.options))
  141. } else {
  142. // app.use(morgan(log.format, log.options))
  143. middlewares.push(morgan(log.format, log.options))
  144. }
  145. }
  146. /* Mock Responses */
  147. options.mocks.forEach(mock => {
  148. if (mock.module) {
  149. mock.responses = require(path.resolve(path.join(options.static.root, mock.module)))
  150. }
  151. if (mock.responses) {
  152. // app.use(mw.mockResponses(mock.route, mock.responses))
  153. middlewares.push(mw.mockResponses(mock.route, mock.responses))
  154. } else if (mock.response) {
  155. mock.target = {
  156. request: mock.request,
  157. response: mock.response
  158. }
  159. // app.use(mw.mockResponses(mock.route, mock.target))
  160. middlewares.push(mw.mockResponses(mock.route, mock.target))
  161. }
  162. })
  163. /* for any URL not matched by static (e.g. `/search`), serve the SPA */
  164. if (options.spa) {
  165. const historyApiFallback = require('koa-connect-history-api-fallback')
  166. debug('SPA', options.spa)
  167. // app.use(historyApiFallback({
  168. // index: options.spa,
  169. // verbose: options.verbose
  170. // }))
  171. middlewares.push(historyApiFallback({
  172. index: options.spa,
  173. verbose: options.verbose
  174. }))
  175. }
  176. /* serve static files */
  177. if (options.static.root) {
  178. const serve = require('koa-static')
  179. // app.use(serve(options.static.root, options.static.options))
  180. middlewares.push(serve(options.static.root, options.static.options))
  181. }
  182. /* serve directory index */
  183. if (options.serveIndex.path) {
  184. const serveIndex = require('koa-serve-index')
  185. // app.use(serveIndex(options.serveIndex.path, options.serveIndex.options))
  186. middlewares.push(serveIndex(options.serveIndex.path, options.serveIndex.options))
  187. }
  188. const compose = require('koa-compose')
  189. middlewares = middlewares.map(convert)
  190. return compose(middlewares)
  191. }
  192. function logstalgiaDate () {
  193. var d = new Date()
  194. return (`${d.getDate()}/${d.getUTCMonth()}/${d.getFullYear()}:${d.toTimeString()}`).replace('GMT', '').replace(' (BST)', '')
  195. }
  196. process.on('unhandledRejection', (reason, p) => {
  197. throw reason
  198. })
  199. /**
  200. * The `from` and `to` routes are specified using [express route-paths](http://expressjs.com/guide/routing.html#route-paths)
  201. *
  202. * @example
  203. * ```json
  204. * {
  205. * "rewrite": [
  206. * { "from": "/css/*", "to": "/build/styles/$1" },
  207. * { "from": "/npm/*", "to": "http://registry.npmjs.org/$1" },
  208. * { "from": "/:user/repos/:name", "to": "https://api.github.com/repos/:user/:name" }
  209. * ]
  210. * }
  211. * ```
  212. *
  213. * @typedef rewriteRule
  214. * @property from {string} - request route
  215. * @property to {string} - target route
  216. */
  217. /**
  218. * @external KoaApplication
  219. * @see https://github.com/koajs/koa/blob/master/docs/api/index.md#application
  220. */