2020-05-15 06:14:24 +00:00
|
|
|
const wsRegex = generateREForProtocol("^(wss?:\\/\\/)?")
|
|
|
|
|
const sseRegex = generateREForProtocol("^(https?:\\/\\/)?")
|
|
|
|
|
const socketioRegex = generateREForProtocol("^((wss?:\\/\\/)|(https?:\\/\\/))?")
|
|
|
|
|
|
2020-05-15 06:10:26 +00:00
|
|
|
function generateREForProtocol(protocol) {
|
2020-03-05 03:15:07 +00:00
|
|
|
return new RegExp(
|
2020-05-15 06:10:26 +00:00
|
|
|
`${protocol}[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_\\+.~#?&\\/\\/=]*)`
|
2020-03-05 03:15:07 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 06:40:44 +00:00
|
|
|
/**
|
|
|
|
|
* valid url for ws/wss
|
|
|
|
|
*/
|
|
|
|
|
export function wsValid(url) {
|
2020-05-15 06:14:24 +00:00
|
|
|
return wsRegex.test(url)
|
2020-03-04 06:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* valid url for http/https
|
|
|
|
|
*/
|
|
|
|
|
export function sseValid(url) {
|
2020-05-15 06:14:24 +00:00
|
|
|
return sseRegex.test(url)
|
2020-03-05 03:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* valid url for ws/wss/http/https
|
|
|
|
|
*/
|
|
|
|
|
export function socketioValid(url) {
|
2020-05-15 06:14:24 +00:00
|
|
|
return socketioRegex.test(url)
|
2020-03-04 06:40:44 +00:00
|
|
|
}
|