regex.worker.js 990 B

1234567891011121314151617181920212223242526272829303132
  1. function generateREForProtocol(protocol) {
  2. return [
  3. new RegExp(
  4. `${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])$`
  5. ),
  6. new RegExp(
  7. `${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/])$`
  8. ),
  9. ]
  10. }
  11. const ws = generateREForProtocol("^(wss?:\\/\\/)?")
  12. const sse = generateREForProtocol("^(https?:\\/\\/)?")
  13. const socketio = generateREForProtocol("^((wss?:\\/\\/)|(https?:\\/\\/))?")
  14. const regex = { ws, sse, socketio }
  15. // type = ws/sse/socketio
  16. async function validator(type, url) {
  17. console.time(`validator ${url}`)
  18. const [res1, res2] = await Promise.all([
  19. regex[type][0].test(url),
  20. regex[type][1].test(url),
  21. ])
  22. console.timeEnd(`validator ${url}`)
  23. return res1 || res2
  24. }
  25. onmessage = async ({ data }) => {
  26. const { type, url } = data
  27. const result = await validator(type, url)
  28. postMessage({ type, url, result })
  29. }