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.

40 lines
991 B

  1. const qs = require('querystring')
  2. const _ = require('lodash')
  3. const crypto = require('crypto')
  4. module.exports = {
  5. /**
  6. * Parse raw url path and make it safe
  7. */
  8. parsePath (rawPath) {
  9. let pathObj = {
  10. locale: 'en',
  11. path: 'home',
  12. private: false,
  13. privateNS: ''
  14. }
  15. // Clean Path
  16. rawPath = _.trim(qs.unescape(rawPath))
  17. if (_.startsWith(rawPath, '/')) { rawPath = rawPath.substring(1) }
  18. if (rawPath === '') { rawPath = 'home' }
  19. // Extract Info
  20. let pathParts = _.filter(_.split(rawPath, '/'), p => !_.isEmpty(p))
  21. if (pathParts[0].length === 1) {
  22. pathParts.shift()
  23. }
  24. if (pathParts[0].length === 2) {
  25. pathObj.locale = pathParts[0]
  26. pathParts.shift()
  27. }
  28. pathObj.path = _.join(pathParts, '/')
  29. return pathObj
  30. },
  31. /**
  32. * Generate unique hash from page
  33. */
  34. generateHash(opts) {
  35. return crypto.createHash('sha1').update(`${opts.locale}|${opts.path}|${opts.privateNS}`).digest('hex')
  36. }
  37. }