valid.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. const [wsRegexIP, wsRegexHostname] = generateREForProtocol("^(wss?:\\/\\/)?")
  2. const [sseRegexIP, sseRegexHostname] =
  3. generateREForProtocol("^(https?:\\/\\/)?")
  4. const [socketioRegexIP, socketioRegexHostname] = generateREForProtocol(
  5. "^((wss?:\\/\\/)|(https?:\\/\\/))?"
  6. )
  7. function generateREForProtocol(protocol) {
  8. return [
  9. new RegExp(
  10. `${protocol}(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$`
  11. ),
  12. new RegExp(
  13. `${protocol}(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9/])$`
  14. ),
  15. ]
  16. }
  17. /**
  18. * valid url for ws/wss
  19. */
  20. export function wsValid(url) {
  21. return wsRegexIP.test(url) || wsRegexHostname.test(url)
  22. }
  23. /**
  24. * valid url for http/https
  25. */
  26. export function httpValid(url) {
  27. return sseRegexIP.test(url) || sseRegexHostname.test(url)
  28. }
  29. /**
  30. * valid url for ws/wss/http/https
  31. */
  32. export function socketioValid(url) {
  33. return socketioRegexIP.test(url) || socketioRegexHostname.test(url)
  34. }