Refactor Realtime URL validation functions
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
const wsRegex = generateREForProtocol("^(wss?:\\/\\/)?")
|
||||
const sseRegex = generateREForProtocol("^(https?:\\/\\/)?")
|
||||
const socketioRegex = generateREForProtocol("^((wss?:\\/\\/)|(https?:\\/\\/))?")
|
||||
|
||||
function generateREForProtocol(protocol) {
|
||||
return new RegExp(
|
||||
`${protocol}[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&\\/\\/=]*)`
|
||||
@@ -8,22 +12,19 @@ function generateREForProtocol(protocol) {
|
||||
* valid url for ws/wss
|
||||
*/
|
||||
export function wsValid(url) {
|
||||
const protocol = "^(wss?:\\/\\/)?"
|
||||
return generateREForProtocol(protocol).test(url)
|
||||
return wsRegex.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* valid url for http/https
|
||||
*/
|
||||
export function sseValid(url) {
|
||||
const protocol = "^(https?:\\/\\/)?"
|
||||
return generateREForProtocol(protocol).test(url)
|
||||
return sseRegex.test(url)
|
||||
}
|
||||
|
||||
/**
|
||||
* valid url for ws/wss/http/https
|
||||
*/
|
||||
export function socketioValid(url) {
|
||||
const protocol = "^((wss?:\\/\\/)|(https?:\\/\\/))?"
|
||||
return generateREForProtocol(protocol).test(url)
|
||||
return socketioRegex.test(url)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user