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.

110 lines
3.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
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 arrayify = require('array-back')
  6. const t = require('typical')
  7. const pathToRegexp = require('path-to-regexp')
  8. const debug = require('debug')('local-web-server')
  9. /**
  10. * @module middleware
  11. */
  12. exports.proxyRequest = proxyRequest
  13. exports.blacklist = blacklist
  14. exports.mockResponses = mockResponses
  15. exports.mime = mime
  16. function proxyRequest (route, app) {
  17. const httpProxy = require('http-proxy')
  18. const proxy = httpProxy.createProxyServer({
  19. changeOrigin: true
  20. })
  21. return function proxyMiddleware () {
  22. const next = arguments[arguments.length - 1]
  23. const keys = []
  24. route.re = pathToRegexp(route.from, keys)
  25. route.new = this.url.replace(route.re, route.to)
  26. keys.forEach((key, index) => {
  27. const re = RegExp(`:${key.name}`, 'g')
  28. route.new = route.new
  29. .replace(re, arguments[index + 1] || '')
  30. })
  31. this.response = false
  32. debug('proxy request', `from: ${this.path}, to: ${url.parse(route.new).href}`)
  33. proxy.once('error', err => {
  34. this.throw(500, `[PROXY] ${err.message}: ${route.new}`)
  35. })
  36. proxy.once('proxyReq', function (proxyReq) {
  37. proxyReq.path = url.parse(route.new).path
  38. })
  39. proxy.web(this.req, this.res, { target: route.new })
  40. }
  41. }
  42. function blacklist (forbid) {
  43. return function blacklist (ctx, next) {
  44. if (forbid.some(expression => pathToRegexp(expression).test(ctx.path))) {
  45. ctx.throw(403, http.STATUS_CODES[403])
  46. } else {
  47. return next()
  48. }
  49. }
  50. }
  51. function mime (mimeTypes) {
  52. return function mime (ctx, next) {
  53. return next().then(() => {
  54. const reqPathExtension = path.extname(ctx.path).slice(1)
  55. Object.keys(mimeTypes).forEach(mimeType => {
  56. const extsToOverride = mimeTypes[mimeType]
  57. if (extsToOverride.indexOf(reqPathExtension) > -1) ctx.type = mimeType
  58. })
  59. })
  60. }
  61. }
  62. function mockResponses (route, targets) {
  63. targets = arrayify(targets)
  64. debug('mock route: %s, targets: %s', route, targets.length)
  65. const pathRe = pathToRegexp(route)
  66. return function mockResponse (ctx, next) {
  67. if (pathRe.test(ctx.path)) {
  68. const testValue = require('test-value')
  69. /* find a mock with compatible method and accepts */
  70. let target = targets.find(target => {
  71. return testValue(target, {
  72. request: {
  73. method: [ ctx.method, undefined ],
  74. accepts: type => ctx.accepts(type)
  75. }
  76. })
  77. })
  78. /* else take the first target without a request (no request means 'all requests') */
  79. if (!target) {
  80. target = targets.find(target => !target.request)
  81. }
  82. if (target) {
  83. if (t.isFunction(target.response)) {
  84. const pathMatches = ctx.path.match(pathRe).slice(1)
  85. return target.response.apply(null, [ctx].concat(pathMatches))
  86. } else if (t.isPlainObject(target.response)) {
  87. Object.assign(ctx.response, target.response)
  88. } else {
  89. throw new Error(`Invalid response: ${JSON.stringify(target.response)}`)
  90. }
  91. }
  92. } else {
  93. return next()
  94. }
  95. }
  96. }