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) {
|
function generateREForProtocol(protocol) {
|
||||||
return new RegExp(
|
return new RegExp(
|
||||||
`${protocol}[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&\\/\\/=]*)`
|
`${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
|
* valid url for ws/wss
|
||||||
*/
|
*/
|
||||||
export function wsValid(url) {
|
export function wsValid(url) {
|
||||||
const protocol = "^(wss?:\\/\\/)?"
|
return wsRegex.test(url)
|
||||||
return generateREForProtocol(protocol).test(url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* valid url for http/https
|
* valid url for http/https
|
||||||
*/
|
*/
|
||||||
export function sseValid(url) {
|
export function sseValid(url) {
|
||||||
const protocol = "^(https?:\\/\\/)?"
|
return sseRegex.test(url)
|
||||||
return generateREForProtocol(protocol).test(url)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* valid url for ws/wss/http/https
|
* valid url for ws/wss/http/https
|
||||||
*/
|
*/
|
||||||
export function socketioValid(url) {
|
export function socketioValid(url) {
|
||||||
const protocol = "^((wss?:\\/\\/)|(https?:\\/\\/))?"
|
return socketioRegex.test(url)
|
||||||
return generateREForProtocol(protocol).test(url)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user