ProxyManager.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* eslint-disable
  2. max-len,
  3. */
  4. // TODO: This file was created by bulk-decaffeinate.
  5. // Fix any style issues and re-enable lint.
  6. /*
  7. * decaffeinate suggestions:
  8. * DS102: Remove unnecessary code created because of implicit returns
  9. * DS205: Consider reworking code to avoid use of IIFEs
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let ProxyManager
  14. const settings = require('@overleaf/settings')
  15. const logger = require('logger-sharelatex')
  16. const request = require('request')
  17. const URL = require('url')
  18. module.exports = ProxyManager = {
  19. apply(publicApiRouter) {
  20. return (() => {
  21. const result = []
  22. for (var proxyUrl in settings.proxyUrls) {
  23. const target = settings.proxyUrls[proxyUrl]
  24. result.push(
  25. (function (target) {
  26. const method =
  27. (target.options != null ? target.options.method : undefined) ||
  28. 'get'
  29. return publicApiRouter[method](
  30. proxyUrl,
  31. ProxyManager.createProxy(target)
  32. )
  33. })(target)
  34. )
  35. }
  36. return result
  37. })()
  38. },
  39. createProxy(target) {
  40. return function (req, res, next) {
  41. const targetUrl = makeTargetUrl(target, req)
  42. logger.log({ targetUrl, reqUrl: req.url }, 'proxying url')
  43. const options = { url: targetUrl }
  44. if (req.headers != null ? req.headers.cookie : undefined) {
  45. options.headers = { Cookie: req.headers.cookie }
  46. }
  47. if ((target != null ? target.options : undefined) != null) {
  48. Object.assign(options, target.options)
  49. }
  50. if (['post', 'put'].includes(options.method)) {
  51. options.form = req.body
  52. }
  53. const upstream = request(options)
  54. upstream.on('error', error =>
  55. logger.error({ err: error }, 'error in ProxyManager')
  56. )
  57. // TODO: better handling of status code
  58. // see https://github.com/overleaf/write_latex/wiki/Streams-and-pipes-in-Node.js
  59. return upstream.pipe(res)
  60. }
  61. },
  62. }
  63. // make a URL from a proxy target.
  64. // if the query is specified, set/replace the target's query with the given query
  65. var makeTargetUrl = function (target, req) {
  66. const targetUrl = URL.parse(parseSettingUrl(target, req))
  67. if (req.query != null && Object.keys(req.query).length > 0) {
  68. targetUrl.query = req.query
  69. targetUrl.search = null // clear `search` as it takes precedence over `query`
  70. }
  71. return targetUrl.format()
  72. }
  73. var parseSettingUrl = function (target, { params }) {
  74. let path
  75. if (typeof target === 'string') {
  76. return target
  77. }
  78. if (typeof target.path === 'function') {
  79. path = target.path(params)
  80. } else {
  81. ;({ path } = target)
  82. }
  83. return `${target.baseUrl}${path || ''}`
  84. }