UrlHelper.js 872 B

1234567891011121314151617181920212223242526272829303132
  1. const Settings = require('settings-sharelatex')
  2. const { URL } = require('url')
  3. function getSafeRedirectPath(value) {
  4. const baseURL = Settings.siteUrl // base URL is required to construct URL from path
  5. const url = new URL(value, baseURL)
  6. let safePath = `${url.pathname}${url.search}${url.hash}`.replace(/^\/+/, '/')
  7. if (safePath === '/') {
  8. safePath = undefined
  9. }
  10. return safePath
  11. }
  12. const UrlHelper = {
  13. getSafeRedirectPath,
  14. wrapUrlWithProxy(url) {
  15. // TODO: Consider what to do for Community and Enterprise edition?
  16. if (!Settings.apis.linkedUrlProxy.url) {
  17. throw new Error('no linked url proxy configured')
  18. }
  19. return `${Settings.apis.linkedUrlProxy.url}?url=${encodeURIComponent(url)}`
  20. },
  21. prependHttpIfNeeded(url) {
  22. if (!url.match('://')) {
  23. url = `http://${url}`
  24. }
  25. return url
  26. }
  27. }
  28. module.exports = UrlHelper